Created
November 29, 2012 21:16
-
-
Save lfranchi/4171974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn item<? | |
[{patha :path} {pathb :path}] | |
(loop [patha patha | |
pathb pathb] | |
(let [{l :branch l-disamb :disamb} (first patha) | |
{r :branch r-disamb :disamb} (first pathb)] | |
(cond | |
(nil? l) (= r 1) | |
(nil? r) (= l 0) | |
(= l r) (if (and (= 1 (count patha)) (= 1 (count pathb))) | |
(disamb<? l-disamb r-disamb) ;; Finishes with two mini-siblings, order by disambiguator | |
(recur (subvec patha 1) (subvec pathb 1))) ;; Normal case | |
:else (< l r))))) | |
(defn ancestor? | |
"Returns if the first path is an ancestor to the second" | |
[patha pathb] | |
(loop [patha patha ;; Otherwise, determine if it's an ancestor | |
pathb pathb] | |
(let [{l :branch l-disamb :disamb} (first patha) | |
{r :branch r-disamb :disamb} (first pathb)] | |
(cond | |
(nil? l) true | |
(= l r) (recur (subvec patha 1) (subvec pathb 1))) ;; Normal case, equal so continue down the tree | |
:else false))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment