Show root node in seq of Trie

Before, if you did a lookup in a trie and then did a seq of the result,
you would get all of the children of the node that you looked up, but
you wouldn't get the value of the node itself.

This was nice when I was only getting children of the root node
because the root node never had a value and I didn't want it.

But I think it makes more since to always include the root node.
If it's not wanted, filter it out at the application level.
main
Eric Ihli 4 years ago
parent 77475e0c13
commit fd191332cd

@ -50,6 +50,7 @@ But there's a couple cool Trie-specific functions.
#+begin_src clojure :results none :session usage-example
(trie/lookup loosely-packed-trie "do")
;; => {[\g] :dog, [\t] :dot}
(seq (trie/lookup loosely-packed-trie "do"))
#+end_src
`children` returns the direct children of a node.
@ -95,12 +96,12 @@ Here's a similar example to that above, but with values that we can tightly pack
v
(if (zero? v) nil v)))
(def loosely-packed-trie
(def tight-ready-loosely-packed-trie
(trie/make-trie '(1 2 3) 123 '(1 2 1) 121 '(1 2 2) 122 '(1 3 1) 131))
(def tightly-packed-trie
(tpt/tightly-packed-trie
loosely-packed-trie
tight-ready-loosely-packed-trie
encode-fn
decode-fn))

@ -236,7 +236,7 @@
)
(defn value-encode-fn [v]
(if (= v ::tpt/root)
(if (and (number? v) (zero? v))
(encoding/encode 0)
(byte-array
(concat (encoding/encode (:id v))
@ -245,7 +245,7 @@
(defn value-decode-fn [byte-buffer]
(let [id (encoding/decode byte-buffer)]
(if (zero? id)
{:id :root}
{:id id}
{:id id
:count (encoding/decode byte-buffer)})))

@ -38,23 +38,37 @@
(.limit ~byte-buffer original-limit#)
(.position ~byte-buffer original-position#)))))
(defn trie->depth-first-post-order-traversable-zipperable-vector
([path node decode-value-fn]
(vec
(map
(fn [child]
[(trie->depth-first-post-order-traversable-zipperable-vector
(defn -trie->depth-first-post-order-traversable-zipperable-vector
[path node decode-value-fn]
(vec
(map
(fn [child]
[(-trie->depth-first-post-order-traversable-zipperable-vector
(conj path (.key child))
child
decode-value-fn)
(wrap-byte-buffer
(.byte-buffer child)
(.limit (.byte-buffer child) (.limit child))
(.position (.byte-buffer child) (.address child))
(clojure.lang.MapEntry.
(conj path (.key child))
child
decode-value-fn)
(wrap-byte-buffer
(.byte-buffer child)
(.limit (.byte-buffer child) (.limit child))
(.position (.byte-buffer child) (.address child))
(clojure.lang.MapEntry.
(conj path (.key child))
(decode-value-fn (.byte-buffer child))))])
(trie/children node)))))
(decode-value-fn (.byte-buffer child))))])
(trie/children node))))
(defn trie->depth-first-post-order-traversable-zipperable-vector
[path node decode-value-fn]
(let [byte-buffer (.byte-buffer node)
val (wrap-byte-buffer
byte-buffer
(.limit byte-buffer (.limit node))
(.position byte-buffer (.address node))
(decode-value-fn byte-buffer))]
[(-trie->depth-first-post-order-traversable-zipperable-vector
path
node
decode-value-fn)
(clojure.lang.MapEntry. path val)]))
(defn rewind-to-key [bb stop]
(loop []

@ -1,15 +1,24 @@
(ns com.owoga.trie
(:require [clojure.zip :as zip]))
(defn trie->depth-first-post-order-traversable-zipperable-vector
(defn -trie->depth-first-post-order-traversable-zipperable-vector
([path node]
(vec
(map
(fn [[k v]]
[(trie->depth-first-post-order-traversable-zipperable-vector (conj path k) v)
[(-trie->depth-first-post-order-traversable-zipperable-vector (conj path k) v)
(clojure.lang.MapEntry. (conj path k) (.value v))])
(.children- node)))))
(defn trie->depth-first-post-order-traversable-zipperable-vector
[path node]
(if (.value node)
[(-trie->depth-first-post-order-traversable-zipperable-vector
path node)
(clojure.lang.MapEntry. path (.value node))]
(-trie->depth-first-post-order-traversable-zipperable-vector
path node)))
(defn depth-first-post-order-traversable-zipperable-vector->trie
[cls [children [key node]]]
(sorted-map

@ -18,15 +18,6 @@
nil
v)))
(comment
(let [t (->> '([1 3] 13 [1] 1 [1 2] 12)
(apply trie/make-trie)
(#(tpt/tightly-packed-trie % value-encode-fn value-decode-fn)))
bb (.byte-buffer t)]
(trie/lookup t [1 2]))
)
(deftest tightly-packed-trie-tests
(let [empty-trie (-> (trie/make-trie)
(#(tpt/tightly-packed-trie % value-encode-fn value-decode-fn)))
@ -70,7 +61,8 @@
[[1 2] nil]
[[1 3 1] 131]
[[1 3] nil]
[[1] nil])
[[1] nil]
[[] nil])
(seq initialized-trie))))))
(comment

Loading…
Cancel
Save