Skip to content

Commit

Permalink
improved next-ptr function to use simple filters instead of recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosnec committed Apr 29, 2014
1 parent dbd0d93 commit 414f645
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/b_plus_tree/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@
[b-plus-tree io nodes util]
[b-plus-tree.util :refer [dbg verbose]]))

(comment
(defn old-next-ptr
"Returns the next pointer when searching the tree for key in node.
Being replaced."
([key node raf]
(let [key-ptrs (:key-ptrs node)]
(if-let [ptr (loop [[k ptr] (first key-ptrs)
key-ptrs (next key-ptrs)]
(let [c (compare key k)]
(cond
(pos? c) (when (seq key-ptrs)
(recur (first key-ptrs)
(next key-ptrs)))
(zero? c) (-> key-ptrs first second)
; must be negative
:default ptr)))]
ptr
(:last node))))))

(defn next-ptr
"Returns the next pointer when searching the tree for key in node."
([key node raf]
(let [key-ptrs (:key-ptrs node)]
(if-let [ptr (loop [[k ptr] (first key-ptrs)
key-ptrs (next key-ptrs)]
(let [c (compare key k)]
(cond
(pos? c) (when (seq key-ptrs)
(recur (first key-ptrs)
(next key-ptrs)))
(zero? c) (-> key-ptrs first second)
; must be negative
:default ptr)))]
ptr
(:last node)))))
(if-let [[k ptr] (->> node
:key-ptrs
(filter (fn [[k ptr]] (pos? (compare k key))))
first)]
ptr
(:last node))))

(defn next-node
"Returns the next node when searching the tree for key in node."
Expand Down

0 comments on commit 414f645

Please sign in to comment.