如何将树形图用于二维密钥?

时间:2015-10-02 09:02:01

标签: data-structures clojure clojure-core

我想映射大量的元组。我的地图看起来像:

{[1 2] :thing}

除了可能有几百万。我有一种感觉,树图可能是一个好东西,所以我试图让它工作。我似乎无法正确获得比较功能。

(defn compare 
  [[x y] [xx yy]]
  (cond
   (and (= x xx) (= y yy)) 0
   (and (<= x xx) (<= y yy)) -1
   (and (<= x xx) (> y yy)) -1
   (and (> x xx) (<= y yy)) 1
   (and (> x xx) (> y yy)) 1))

一些微不足道的输入似乎有效

user=> (compare [1 1] [1 1])
0
user=> (compare [1 1] [2 2])
-1
user=> (compare [1 2] [2 1])
-1
user=> (compare [2 1] [1 2])
1

但是如果我创建涵盖所有组合的输入,那么地图应该认为它们都是不同的。

(def inputs
    "All tuples of [0-4, 5-10]."
    (clojure.math.combinatorics/cartesian-product
      (range 0 4) 
      (range 5 10)))

(def input-pairs
     "All possible pairs of tuples"
     (clojure.math.combinatorics/cartesian-product inputs inputs))

如果我测试比较函数,当两个向量在结构上相同时,它仅返回零

user=> (doseq [[a b] input-pairs]
  #_=>   (when (zero? (compare a b)) (prn a b)))
(0 5) (0 5)
(0 6) (0 6)
(0 7) (0 7)
(0 8) (0 8)
(0 9) (0 9)
(1 5) (1 5)
etc

所以我认为我的比较功能是正确的。但是,在树形图中使用它会产生一些奇怪的结果:

(def inputs-kvs
    "Inputs in the format that the hash-map and sorted-map constructor understand"
    (mapcat #(vector % (apply str %))
            (clojure.math.combinatorics/cartesian-product
              (range 0 4) 
              (range 5 10))))

将这些放在散列图中会给出正确的答案

(count (apply assoc (hash-map) inputs-kvs))
=> 20

但是使用给定的比较将它们放在树形图中:

(def structure (sorted-map-by compare))
(count (apply assoc structure inputs-kvs))
=> 4

(apply assoc structure inputs-kvs)
=> {(0 5) "25", (1 6) "36", (2 7) "37", (3 5) "39"}

“25”已存储在(0 5)广告位中。但比较函数并未说明(0 5)(2 5)是相同的:

=> (compare [0 5] [2 5])
-1

我做错了什么?我可以做这个吗?将二维空间投影到一维空间上是否有意义?

(关于你可能有的问题,是的,我尝试过二维结构,例如(sorted-map 1 (sorted-map 2 :value)),但我正在努力寻找性能更佳的替代方案)

2 个答案:

答案 0 :(得分:2)

Clojure已经附带了它自己的compare

user=> (doc compare)
-------------------------
clojure.core/compare
([x y])
  Comparator. Returns a negative number, zero, or a positive number
  when x is logically 'less than', 'equal to', or 'greater than'
  y. Same as Java x.compareTo(y) except it also works for nil, and
  compares numbers and collections in a type-independent manner. x
  must implement Comparable

其行为与OP自身的功能相同,但最有可能更有效:

user=> (compare [1 1] [1 1])
0
user=> (compare [1 1] [2 2])
-1
user=> (compare [2 1] [1 2])
1

行为记录在Section about Vectors (IPersistentVector) in the Data Structures docs

  

首先按照长度比较矢量,然后按顺序比较每个元素。

因此,您可以使用核心中的sorted-map-by compare,或者因为您的数据结构只是sorted-map的默认值:

user=> (def m (into {} (let [r #(- (rand-int 10) (rand-int 10))] (for [a (range -1 2) b (range -1 2)] [[(r) (r)] (str a b)]))))
#'user/m
user=> (>pprint m)
{[-7 -4] "10",
 [-3 5] "01",
 [-5 -7] "00",
 [5 2] "11",
 [-3 1] "-10",
 [7 -4] "-11",
 [0 -6] "0-1",
 [3 1] "-1-1",
 [-8 -1] "1-1"}
nil
user=> (>pprint (into (sorted-map-by compare) m))
{[-8 -1] "1-1",
 [-7 -4] "10",
 [-5 -7] "00",
 [-3 1] "-10",
 [-3 5] "01",
 [0 -6] "0-1",
 [3 1] "-1-1",
 [5 2] "11",
 [7 -4] "-11"}
nil
user=> (>pprint (into (sorted-map) m))
{[-8 -1] "1-1",
 [-7 -4] "10",
 [-5 -7] "00",
 [-3 1] "-10",
 [-3 5] "01",
 [0 -6] "0-1",
 [3 1] "-1-1",
 [5 2] "11",
 [7 -4] "-11"}
nil
user=> (assert (= (into (sorted-map-by compare) m) (into (sorted-map) m)))
nil

答案 1 :(得分:1)

我刚刚添加(vec %)来保留元组向量 - 不应该改变任何东西。

你可以看到它在这里工作。

可能是你有一些旧的REPL东西 - 尤其是你别名clojure.core/compare

; using your compare function
(def inp (mapcat #(vector (vec %) (apply str %)) 
  (clojure.math.combinatorics/cartesian-product (range 0 4) (range 5 10))))
; => ([0 5] "05" [0 6] "06" [0 7] "07" [0 8] "08" ...
(count inp) 
; => 40 
(apply assoc structure inp)
; => {[0 9] "09", [0 8] "08", [0 7] "07", [0 6] "06", ....
(count (apply assoc structure inp))
; => 20