Skip to content

Instantly share code, notes, and snippets.

@lxmonk
Created September 3, 2012 06:17
Show Gist options
  • Save lxmonk/3607227 to your computer and use it in GitHub Desktop.
Save lxmonk/3607227 to your computer and use it in GitHub Desktop.
(declaim (optimize (speed 3) (debug 0) (space 0) (safety 0)))
(defun build-btree (item depth)
(declare (fixnum item depth))
(if (zerop depth) (list item)
(let ((item2 (+ item item))
(depth-1 (1- depth)))
(declare (fixnum item2 depth-1))
(cons item
(cons (build-btree (the fixnum (1- item2)) depth-1)
(build-btree item2 depth-1))))))
(defun check-node (node)
(declare (values fixnum))
(let ((data (car node))
(kids (cdr node)))
(declare (fixnum data))
(if kids
(- (+ data (check-node (car kids)))
(check-node (cdr kids)))
data)))
(defun loop-depths (max-depth &key (min-depth 4))
(declare (type fixnum max-depth min-depth))
(loop for d of-type fixnum from min-depth by 2 upto max-depth do
(loop with iterations of-type fixnum = (ash 1 (+ max-depth min-depth (- d)))
for i of-type fixnum from 1 upto iterations
sum (+ (the fixnum (check-node (build-btree i d)))
(the fixnum (check-node (build-btree (- i) d))))
into result of-type fixnum
finally
(format t "~D trees of depth ~D check: ~D~%"
(the fixnum (+ iterations iterations )) d result))))
(defun main (&optional (n (parse-integer
(or (car (last #+sbcl sb-ext:*posix-argv*
#+cmu extensions:*command-line-strings*
#+gcl si::*command-args*))
"1"))))
(declare (type (integer 0 255) n))
(format t "stretch tree of depth ~D check: ~D~%" (1+ n) (check-node (build-btree 0 (1+ n))))
(let ((*print-pretty* nil) (long-lived-tree (build-btree 0 n)))
(purify)
(loop-depths n)
(format t "long lived tree of depth ~D check: ~D~%" n (check-node long-lived-tree))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment