What is the data structure behind Clojure's sets?

后端 未结 4 1667
难免孤独
难免孤独 2020-12-12 20:08

I recently listened to Rich Hickey\'s interview on Software Engineering Radio. During the interview Rich mentioned that Clojure\'s collections are implemented as trees. I\'m

4条回答
  •  春和景丽
    2020-12-12 20:38

    You should really read Clojure Programming, it covers this in great detail, including pictures. Briefly though, collections are depth first searches through trees. We can show your examples like this:

    (def x #{1 2 3})
    
    x
    |
    | \
    |\ 3
    1 \
       2
    
    (def y (conj x 4))
    
     x  y
     | / \
     | \   4
     |\ 3
     1 \
        2
    
     (def z (difference y #{1}))
    
     x  y
     | / \
     | \  4
     |\ 3
     1/\
    z-  2
    

    Note that these are just indicative, I'm not saying that this is exactly the layout Clojure uses internally. It's the gist though.

提交回复
热议问题