sets.nim 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## The `sets` module implements an efficient `hash set`:idx: and
  10. ## ordered hash set.
  11. ##
  12. ## Hash sets are different from the `built in set type
  13. ## <manual.html#types-set-type>`_. Sets allow you to store any value that can be
  14. ## `hashed <hashes.html>`_ and they don't contain duplicate entries.
  15. ##
  16. ## Common usages of sets:
  17. ## * removing duplicates from a container by converting it with `toHashSet proc
  18. ## <#toHashSet,openArray[A]>`_ (see also `sequtils.deduplicate func
  19. ## <sequtils.html#deduplicate,openArray[T],bool>`_)
  20. ## * membership testing
  21. ## * mathematical operations on two sets, such as
  22. ## `union <#union,HashSet[A],HashSet[A]>`_,
  23. ## `intersection <#intersection,HashSet[A],HashSet[A]>`_,
  24. ## `difference <#difference,HashSet[A],HashSet[A]>`_, and
  25. ## `symmetric difference <#symmetricDifference,HashSet[A],HashSet[A]>`_
  26. ##
  27. ## **Examples:**
  28. ##
  29. ## ```Nim
  30. ## echo toHashSet([9, 5, 1]) # {9, 1, 5}
  31. ## echo toOrderedSet([9, 5, 1]) # {9, 5, 1}
  32. ##
  33. ## let
  34. ## s1 = toHashSet([9, 5, 1])
  35. ## s2 = toHashSet([3, 5, 7])
  36. ##
  37. ## echo s1 + s2 # {9, 1, 3, 5, 7}
  38. ## echo s1 - s2 # {1, 9}
  39. ## echo s1 * s2 # {5}
  40. ## echo s1 -+- s2 # {9, 1, 3, 7}
  41. ## ```
  42. ##
  43. ## Note: The data types declared here have *value semantics*: This means
  44. ## that `=` performs a copy of the set.
  45. ##
  46. ## **See also:**
  47. ## * `intsets module <intsets.html>`_ for efficient int sets
  48. ## * `tables module <tables.html>`_ for hash tables
  49. import
  50. std/[hashes, math]
  51. when not defined(nimHasEffectsOf):
  52. {.pragma: effectsOf.}
  53. {.pragma: myShallow.}
  54. # For "integer-like A" that are too big for intsets/bit-vectors to be practical,
  55. # it would be best to shrink hcode to the same size as the integer. Larger
  56. # codes should never be needed, and this can pack more entries per cache-line.
  57. # Losing hcode entirely is also possible - if some element value is forbidden.
  58. type
  59. KeyValuePair[A] = tuple[hcode: Hash, key: A]
  60. KeyValuePairSeq[A] = seq[KeyValuePair[A]]
  61. HashSet*[A] {.myShallow.} = object ## \
  62. ## A generic hash set.
  63. ##
  64. ## Use `init proc <#init,HashSet[A]>`_ or `initHashSet proc <#initHashSet>`_
  65. ## before calling other procs on it.
  66. data: KeyValuePairSeq[A]
  67. counter: int
  68. type
  69. OrderedKeyValuePair[A] = tuple[
  70. hcode: Hash, next: int, key: A]
  71. OrderedKeyValuePairSeq[A] = seq[OrderedKeyValuePair[A]]
  72. OrderedSet*[A] {.myShallow.} = object ## \
  73. ## A generic hash set that remembers insertion order.
  74. ##
  75. ## Use `init proc <#init,OrderedSet[A]>`_ or `initOrderedSet proc
  76. ## <#initOrderedSet>`_ before calling other procs on it.
  77. data: OrderedKeyValuePairSeq[A]
  78. counter, first, last: int
  79. SomeSet*[A] = HashSet[A] | OrderedSet[A]
  80. ## Type union representing `HashSet` or `OrderedSet`.
  81. const
  82. defaultInitialSize* = 64
  83. include setimpl
  84. # ---------------------------------------------------------------------
  85. # ------------------------------ HashSet ------------------------------
  86. # ---------------------------------------------------------------------
  87. proc init*[A](s: var HashSet[A], initialSize = defaultInitialSize) =
  88. ## Initializes a hash set.
  89. ##
  90. ## Starting from Nim v0.20, sets are initialized by default and it is
  91. ## not necessary to call this function explicitly.
  92. ##
  93. ## You can call this proc on a previously initialized hash set, which will
  94. ## discard all its values. This might be more convenient than iterating over
  95. ## existing values and calling `excl() <#excl,HashSet[A],A>`_ on them.
  96. ##
  97. ## See also:
  98. ## * `initHashSet proc <#initHashSet>`_
  99. ## * `toHashSet proc <#toHashSet,openArray[A]>`_
  100. runnableExamples:
  101. var a: HashSet[int]
  102. init(a)
  103. initImpl(s, initialSize)
  104. proc initHashSet*[A](initialSize = defaultInitialSize): HashSet[A] =
  105. ## Wrapper around `init proc <#init,HashSet[A]>`_ for initialization of
  106. ## hash sets.
  107. ##
  108. ## Returns an empty hash set you can assign directly in `var` blocks in a
  109. ## single line.
  110. ##
  111. ## Starting from Nim v0.20, sets are initialized by default and it is
  112. ## not necessary to call this function explicitly.
  113. ##
  114. ## See also:
  115. ## * `toHashSet proc <#toHashSet,openArray[A]>`_
  116. runnableExamples:
  117. var a = initHashSet[int]()
  118. a.incl(3)
  119. assert len(a) == 1
  120. result = default(HashSet[A])
  121. result.init(initialSize)
  122. proc `[]`*[A](s: var HashSet[A], key: A): var A =
  123. ## Returns the element that is actually stored in `s` which has the same
  124. ## value as `key` or raises the `KeyError` exception.
  125. ##
  126. ## This is useful when one overloaded `hash` and `==` but still needs
  127. ## reference semantics for sharing.
  128. var hc: Hash
  129. var index = rawGet(s, key, hc)
  130. if index >= 0: result = s.data[index].key
  131. else:
  132. when compiles($key):
  133. raise newException(KeyError, "key not found: " & $key)
  134. else:
  135. raise newException(KeyError, "key not found")
  136. proc contains*[A](s: HashSet[A], key: A): bool =
  137. ## Returns true if `key` is in `s`.
  138. ##
  139. ## This allows the usage of `in` operator.
  140. ##
  141. ## See also:
  142. ## * `incl proc <#incl,HashSet[A],A>`_
  143. ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
  144. runnableExamples:
  145. var values = initHashSet[int]()
  146. assert(not values.contains(2))
  147. assert 2 notin values
  148. values.incl(2)
  149. assert values.contains(2)
  150. assert 2 in values
  151. var hc: Hash
  152. var index = rawGet(s, key, hc)
  153. result = index >= 0
  154. proc len*[A](s: HashSet[A]): int =
  155. ## Returns the number of elements in `s`.
  156. ##
  157. ## Due to an implementation detail you can call this proc on variables which
  158. ## have not been initialized yet. The proc will return zero as the length
  159. ## then.
  160. runnableExamples:
  161. var a: HashSet[string]
  162. assert len(a) == 0
  163. let s = toHashSet([3, 5, 7])
  164. assert len(s) == 3
  165. result = s.counter
  166. proc card*[A](s: HashSet[A]): int =
  167. ## Alias for `len() <#len,HashSet[A]>`_.
  168. ##
  169. ## Card stands for the `cardinality
  170. ## <http://en.wikipedia.org/wiki/Cardinality>`_ of a set.
  171. result = s.counter
  172. proc incl*[A](s: var HashSet[A], key: A) =
  173. ## Includes an element `key` in `s`.
  174. ##
  175. ## This doesn't do anything if `key` is already in `s`.
  176. ##
  177. ## See also:
  178. ## * `excl proc <#excl,HashSet[A],A>`_ for excluding an element
  179. ## * `incl proc <#incl,HashSet[A],HashSet[A]>`_ for including other set
  180. ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
  181. runnableExamples:
  182. var values = initHashSet[int]()
  183. values.incl(2)
  184. values.incl(2)
  185. assert values.len == 1
  186. inclImpl()
  187. proc incl*[A](s: var HashSet[A], other: HashSet[A]) =
  188. ## Includes all elements from `other` set into `s` (must be declared as `var`).
  189. ##
  190. ## This is the in-place version of `s + other <#+,HashSet[A],HashSet[A]>`_.
  191. ##
  192. ## See also:
  193. ## * `excl proc <#excl,HashSet[A],HashSet[A]>`_ for excluding other set
  194. ## * `incl proc <#incl,HashSet[A],A>`_ for including an element
  195. ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
  196. runnableExamples:
  197. var
  198. values = toHashSet([1, 2, 3])
  199. others = toHashSet([3, 4, 5])
  200. values.incl(others)
  201. assert values.len == 5
  202. for item in other: incl(s, item)
  203. proc toHashSet*[A](keys: openArray[A]): HashSet[A] =
  204. ## Creates a new hash set that contains the members of the given
  205. ## collection (seq, array, or string) `keys`.
  206. ##
  207. ## Duplicates are removed.
  208. ##
  209. ## See also:
  210. ## * `initHashSet proc <#initHashSet>`_
  211. runnableExamples:
  212. let
  213. a = toHashSet([5, 3, 2])
  214. b = toHashSet("abracadabra")
  215. assert len(a) == 3
  216. ## a == {2, 3, 5}
  217. assert len(b) == 5
  218. ## b == {'a', 'b', 'c', 'd', 'r'}
  219. result = initHashSet[A](keys.len)
  220. for key in items(keys): result.incl(key)
  221. iterator items*[A](s: HashSet[A]): A =
  222. ## Iterates over elements of the set `s`.
  223. ##
  224. ## If you need a sequence with the elements you can use `sequtils.toSeq
  225. ## template <sequtils.html#toSeq.t,untyped>`_.
  226. ##
  227. ## ```Nim
  228. ## type
  229. ## pair = tuple[a, b: int]
  230. ## var
  231. ## a, b = initHashSet[pair]()
  232. ## a.incl((2, 3))
  233. ## a.incl((3, 2))
  234. ## a.incl((2, 3))
  235. ## for x, y in a.items:
  236. ## b.incl((x - 2, y + 1))
  237. ## assert a.len == 2
  238. ## echo b
  239. ## # --> {(a: 1, b: 3), (a: 0, b: 4)}
  240. ## ```
  241. let length = s.len
  242. for h in 0 .. high(s.data):
  243. if isFilled(s.data[h].hcode):
  244. yield s.data[h].key
  245. assert(len(s) == length, "the length of the HashSet changed while iterating over it")
  246. proc containsOrIncl*[A](s: var HashSet[A], key: A): bool =
  247. ## Includes `key` in the set `s` and tells if `key` was already in `s`.
  248. ##
  249. ## The difference with regards to the `incl proc <#incl,HashSet[A],A>`_ is
  250. ## that this proc returns `true` if `s` already contained `key`. The
  251. ## proc will return `false` if `key` was added as a new value to `s` during
  252. ## this call.
  253. ##
  254. ## See also:
  255. ## * `incl proc <#incl,HashSet[A],A>`_ for including an element
  256. ## * `incl proc <#incl,HashSet[A],HashSet[A]>`_ for including other set
  257. ## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_
  258. runnableExamples:
  259. var values = initHashSet[int]()
  260. assert values.containsOrIncl(2) == false
  261. assert values.containsOrIncl(2) == true
  262. assert values.containsOrIncl(3) == false
  263. containsOrInclImpl()
  264. proc excl*[A](s: var HashSet[A], key: A) =
  265. ## Excludes `key` from the set `s`.
  266. ##
  267. ## This doesn't do anything if `key` is not found in `s`.
  268. ##
  269. ## See also:
  270. ## * `incl proc <#incl,HashSet[A],A>`_ for including an element
  271. ## * `excl proc <#excl,HashSet[A],HashSet[A]>`_ for excluding other set
  272. ## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_
  273. runnableExamples:
  274. var s = toHashSet([2, 3, 6, 7])
  275. s.excl(2)
  276. s.excl(2)
  277. assert s.len == 3
  278. discard exclImpl(s, key)
  279. proc excl*[A](s: var HashSet[A], other: HashSet[A]) =
  280. ## Excludes all elements of `other` set from `s`.
  281. ##
  282. ## This is the in-place version of `s - other <#-,HashSet[A],HashSet[A]>`_.
  283. ##
  284. ## See also:
  285. ## * `incl proc <#incl,HashSet[A],HashSet[A]>`_ for including other set
  286. ## * `excl proc <#excl,HashSet[A],A>`_ for excluding an element
  287. ## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_
  288. runnableExamples:
  289. var
  290. numbers = toHashSet([1, 2, 3, 4, 5])
  291. even = toHashSet([2, 4, 6, 8])
  292. numbers.excl(even)
  293. assert len(numbers) == 3
  294. ## numbers == {1, 3, 5}
  295. for item in other: discard exclImpl(s, item)
  296. proc missingOrExcl*[A](s: var HashSet[A], key: A): bool =
  297. ## Excludes `key` in the set `s` and tells if `key` was already missing from `s`.
  298. ##
  299. ## The difference with regards to the `excl proc <#excl,HashSet[A],A>`_ is
  300. ## that this proc returns `true` if `key` was missing from `s`.
  301. ## The proc will return `false` if `key` was in `s` and it was removed
  302. ## during this call.
  303. ##
  304. ## See also:
  305. ## * `excl proc <#excl,HashSet[A],A>`_ for excluding an element
  306. ## * `excl proc <#excl,HashSet[A],HashSet[A]>`_ for excluding other set
  307. ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
  308. runnableExamples:
  309. var s = toHashSet([2, 3, 6, 7])
  310. assert s.missingOrExcl(4) == true
  311. assert s.missingOrExcl(6) == false
  312. assert s.missingOrExcl(6) == true
  313. exclImpl(s, key)
  314. proc pop*[A](s: var HashSet[A]): A =
  315. ## Removes and returns an arbitrary element from the set `s`.
  316. ##
  317. ## Raises `KeyError` if the set `s` is empty.
  318. ##
  319. ## See also:
  320. ## * `clear proc <#clear,HashSet[A]>`_
  321. runnableExamples:
  322. var s = toHashSet([2, 1])
  323. assert [s.pop, s.pop] in [[1, 2], [2,1]] # order unspecified
  324. doAssertRaises(KeyError, echo s.pop)
  325. for h in 0 .. high(s.data):
  326. if isFilled(s.data[h].hcode):
  327. result = s.data[h].key
  328. excl(s, result)
  329. return result
  330. raise newException(KeyError, "set is empty")
  331. proc clear*[A](s: var HashSet[A]) =
  332. ## Clears the HashSet back to an empty state, without shrinking
  333. ## any of the existing storage.
  334. ##
  335. ## `O(n)` operation, where `n` is the size of the hash bucket.
  336. ##
  337. ## See also:
  338. ## * `pop proc <#pop,HashSet[A]>`_
  339. runnableExamples:
  340. var s = toHashSet([3, 5, 7])
  341. clear(s)
  342. assert len(s) == 0
  343. s.counter = 0
  344. for i in 0 ..< s.data.len:
  345. s.data[i].hcode = 0
  346. {.push warning[UnsafeDefault]:off.}
  347. reset(s.data[i].key)
  348. {.pop.}
  349. proc union*[A](s1, s2: HashSet[A]): HashSet[A] =
  350. ## Returns the union of the sets `s1` and `s2`.
  351. ##
  352. ## The same as `s1 + s2 <#+,HashSet[A],HashSet[A]>`_.
  353. ##
  354. ## The union of two sets is represented mathematically as *A ∪ B* and is the
  355. ## set of all objects that are members of `s1`, `s2` or both.
  356. ##
  357. ## See also:
  358. ## * `intersection proc <#intersection,HashSet[A],HashSet[A]>`_
  359. ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
  360. ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
  361. runnableExamples:
  362. let
  363. a = toHashSet(["a", "b"])
  364. b = toHashSet(["b", "c"])
  365. c = union(a, b)
  366. assert c == toHashSet(["a", "b", "c"])
  367. result = s1
  368. incl(result, s2)
  369. proc intersection*[A](s1, s2: HashSet[A]): HashSet[A] =
  370. ## Returns the intersection of the sets `s1` and `s2`.
  371. ##
  372. ## The same as `s1 * s2 <#*,HashSet[A],HashSet[A]>`_.
  373. ##
  374. ## The intersection of two sets is represented mathematically as *A ∩ B* and
  375. ## is the set of all objects that are members of `s1` and `s2` at the same
  376. ## time.
  377. ##
  378. ## See also:
  379. ## * `union proc <#union,HashSet[A],HashSet[A]>`_
  380. ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
  381. ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
  382. runnableExamples:
  383. let
  384. a = toHashSet(["a", "b"])
  385. b = toHashSet(["b", "c"])
  386. c = intersection(a, b)
  387. assert c == toHashSet(["b"])
  388. result = initHashSet[A](max(min(s1.data.len, s2.data.len), 2))
  389. # iterate over the elements of the smaller set
  390. if s1.data.len < s2.data.len:
  391. for item in s1:
  392. if item in s2: incl(result, item)
  393. else:
  394. for item in s2:
  395. if item in s1: incl(result, item)
  396. proc difference*[A](s1, s2: HashSet[A]): HashSet[A] =
  397. ## Returns the difference of the sets `s1` and `s2`.
  398. ##
  399. ## The same as `s1 - s2 <#-,HashSet[A],HashSet[A]>`_.
  400. ##
  401. ## The difference of two sets is represented mathematically as *A ∖ B* and is
  402. ## the set of all objects that are members of `s1` and not members of `s2`.
  403. ##
  404. ## See also:
  405. ## * `union proc <#union,HashSet[A],HashSet[A]>`_
  406. ## * `intersection proc <#intersection,HashSet[A],HashSet[A]>`_
  407. ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
  408. runnableExamples:
  409. let
  410. a = toHashSet(["a", "b"])
  411. b = toHashSet(["b", "c"])
  412. c = difference(a, b)
  413. assert c == toHashSet(["a"])
  414. result = initHashSet[A]()
  415. for item in s1:
  416. if not contains(s2, item):
  417. incl(result, item)
  418. proc symmetricDifference*[A](s1, s2: HashSet[A]): HashSet[A] =
  419. ## Returns the symmetric difference of the sets `s1` and `s2`.
  420. ##
  421. ## The same as `s1 -+- s2 <#-+-,HashSet[A],HashSet[A]>`_.
  422. ##
  423. ## The symmetric difference of two sets is represented mathematically as *A △
  424. ## B* or *A ⊖ B* and is the set of all objects that are members of `s1` or
  425. ## `s2` but not both at the same time.
  426. ##
  427. ## See also:
  428. ## * `union proc <#union,HashSet[A],HashSet[A]>`_
  429. ## * `intersection proc <#intersection,HashSet[A],HashSet[A]>`_
  430. ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
  431. runnableExamples:
  432. let
  433. a = toHashSet(["a", "b"])
  434. b = toHashSet(["b", "c"])
  435. c = symmetricDifference(a, b)
  436. assert c == toHashSet(["a", "c"])
  437. result = s1
  438. for item in s2:
  439. if containsOrIncl(result, item): excl(result, item)
  440. proc `+`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  441. ## Alias for `union(s1, s2) <#union,HashSet[A],HashSet[A]>`_.
  442. result = union(s1, s2)
  443. proc `*`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  444. ## Alias for `intersection(s1, s2) <#intersection,HashSet[A],HashSet[A]>`_.
  445. result = intersection(s1, s2)
  446. proc `-`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  447. ## Alias for `difference(s1, s2) <#difference,HashSet[A],HashSet[A]>`_.
  448. result = difference(s1, s2)
  449. proc `-+-`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  450. ## Alias for `symmetricDifference(s1, s2)
  451. ## <#symmetricDifference,HashSet[A],HashSet[A]>`_.
  452. result = symmetricDifference(s1, s2)
  453. proc disjoint*[A](s1, s2: HashSet[A]): bool =
  454. ## Returns `true` if the sets `s1` and `s2` have no items in common.
  455. runnableExamples:
  456. let
  457. a = toHashSet(["a", "b"])
  458. b = toHashSet(["b", "c"])
  459. assert disjoint(a, b) == false
  460. assert disjoint(a, b - a) == true
  461. for item in s1:
  462. if item in s2: return false
  463. return true
  464. proc `<`*[A](s, t: HashSet[A]): bool =
  465. ## Returns true if `s` is a strict or proper subset of `t`.
  466. ##
  467. ## A strict or proper subset `s` has all of its members in `t` but `t` has
  468. ## more elements than `s`.
  469. runnableExamples:
  470. let
  471. a = toHashSet(["a", "b"])
  472. b = toHashSet(["b", "c"])
  473. c = intersection(a, b)
  474. assert c < a and c < b
  475. assert(not (a < a))
  476. s.counter != t.counter and s <= t
  477. proc `<=`*[A](s, t: HashSet[A]): bool =
  478. ## Returns true if `s` is a subset of `t`.
  479. ##
  480. ## A subset `s` has all of its members in `t` and `t` doesn't necessarily
  481. ## have more members than `s`. That is, `s` can be equal to `t`.
  482. runnableExamples:
  483. let
  484. a = toHashSet(["a", "b"])
  485. b = toHashSet(["b", "c"])
  486. c = intersection(a, b)
  487. assert c <= a and c <= b
  488. assert a <= a
  489. result = false
  490. if s.counter > t.counter: return
  491. result = true
  492. for item in items(s):
  493. if not(t.contains(item)):
  494. result = false
  495. return
  496. proc `==`*[A](s, t: HashSet[A]): bool =
  497. ## Returns true if both `s` and `t` have the same members and set size.
  498. runnableExamples:
  499. var
  500. a = toHashSet([1, 2])
  501. b = toHashSet([2, 1])
  502. assert a == b
  503. s.counter == t.counter and s <= t
  504. proc map*[A, B](data: HashSet[A], op: proc (x: A): B {.closure.}): HashSet[B] {.effectsOf: op.} =
  505. ## Returns a new set after applying `op` proc on each of the elements of
  506. ##`data` set.
  507. ##
  508. ## You can use this proc to transform the elements from a set.
  509. runnableExamples:
  510. let
  511. a = toHashSet([1, 2, 3])
  512. b = a.map(proc (x: int): string = $x)
  513. assert b == toHashSet(["1", "2", "3"])
  514. result = initHashSet[B]()
  515. for item in items(data): result.incl(op(item))
  516. proc hash*[A](s: HashSet[A]): Hash =
  517. ## Hashing of HashSet.
  518. for h in 0 .. high(s.data):
  519. result = result xor s.data[h].hcode
  520. result = !$result
  521. proc `$`*[A](s: HashSet[A]): string =
  522. ## Converts the set `s` to a string, mostly for logging and printing purposes.
  523. ##
  524. ## Don't use this proc for serialization, the representation may change at
  525. ## any moment and values are not escaped.
  526. ##
  527. ## **Examples:**
  528. ## ```Nim
  529. ## echo toHashSet([2, 4, 5])
  530. ## # --> {2, 4, 5}
  531. ## echo toHashSet(["no", "esc'aping", "is \" provided"])
  532. ## # --> {no, esc'aping, is " provided}
  533. ## ```
  534. dollarImpl()
  535. proc initSet*[A](initialSize = defaultInitialSize): HashSet[A] {.deprecated:
  536. "Deprecated since v0.20, use 'initHashSet'".} = initHashSet[A](initialSize)
  537. proc toSet*[A](keys: openArray[A]): HashSet[A] {.deprecated:
  538. "Deprecated since v0.20, use 'toHashSet'".} = toHashSet[A](keys)
  539. proc isValid*[A](s: HashSet[A]): bool {.deprecated:
  540. "Deprecated since v0.20; sets are initialized by default".} =
  541. ## Returns `true` if the set has been initialized (with `initHashSet proc
  542. ## <#initHashSet>`_ or `init proc <#init,HashSet[A]>`_).
  543. ##
  544. runnableExamples:
  545. proc savePreferences(options: HashSet[string]) =
  546. assert options.isValid, "Pass an initialized set!"
  547. # Do stuff here, may crash in release builds!
  548. result = s.data.len > 0
  549. # ---------------------------------------------------------------------
  550. # --------------------------- OrderedSet ------------------------------
  551. # ---------------------------------------------------------------------
  552. template forAllOrderedPairs(yieldStmt: untyped) {.dirty.} =
  553. if s.data.len > 0:
  554. var h = s.first
  555. var idx = 0
  556. while h >= 0:
  557. var nxt = s.data[h].next
  558. if isFilled(s.data[h].hcode):
  559. yieldStmt
  560. inc(idx)
  561. h = nxt
  562. proc init*[A](s: var OrderedSet[A], initialSize = defaultInitialSize) =
  563. ## Initializes an ordered hash set.
  564. ##
  565. ## Starting from Nim v0.20, sets are initialized by default and it is
  566. ## not necessary to call this function explicitly.
  567. ##
  568. ## You can call this proc on a previously initialized hash set, which will
  569. ## discard all its values. This might be more convenient than iterating over
  570. ## existing values and calling `excl() <#excl,HashSet[A],A>`_ on them.
  571. ##
  572. ## See also:
  573. ## * `initOrderedSet proc <#initOrderedSet>`_
  574. ## * `toOrderedSet proc <#toOrderedSet,openArray[A]>`_
  575. runnableExamples:
  576. var a: OrderedSet[int]
  577. init(a)
  578. initImpl(s, initialSize)
  579. proc initOrderedSet*[A](initialSize = defaultInitialSize): OrderedSet[A] =
  580. ## Wrapper around `init proc <#init,OrderedSet[A]>`_ for initialization of
  581. ## ordered hash sets.
  582. ##
  583. ## Returns an empty ordered hash set you can assign directly in `var` blocks
  584. ## in a single line.
  585. ##
  586. ## Starting from Nim v0.20, sets are initialized by default and it is
  587. ## not necessary to call this function explicitly.
  588. ##
  589. ## See also:
  590. ## * `toOrderedSet proc <#toOrderedSet,openArray[A]>`_
  591. runnableExamples:
  592. var a = initOrderedSet[int]()
  593. a.incl(3)
  594. assert len(a) == 1
  595. result.init(initialSize)
  596. proc toOrderedSet*[A](keys: openArray[A]): OrderedSet[A] =
  597. ## Creates a new hash set that contains the members of the given
  598. ## collection (seq, array, or string) `keys`.
  599. ##
  600. ## Duplicates are removed.
  601. ##
  602. ## See also:
  603. ## * `initOrderedSet proc <#initOrderedSet>`_
  604. runnableExamples:
  605. let
  606. a = toOrderedSet([5, 3, 2])
  607. b = toOrderedSet("abracadabra")
  608. assert len(a) == 3
  609. ## a == {5, 3, 2} # different than in HashSet
  610. assert len(b) == 5
  611. ## b == {'a', 'b', 'r', 'c', 'd'} # different than in HashSet
  612. result = initOrderedSet[A](keys.len)
  613. for key in items(keys): result.incl(key)
  614. proc contains*[A](s: OrderedSet[A], key: A): bool =
  615. ## Returns true if `key` is in `s`.
  616. ##
  617. ## This allows the usage of `in` operator.
  618. ##
  619. ## See also:
  620. ## * `incl proc <#incl,OrderedSet[A],A>`_
  621. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  622. runnableExamples:
  623. var values = initOrderedSet[int]()
  624. assert(not values.contains(2))
  625. assert 2 notin values
  626. values.incl(2)
  627. assert values.contains(2)
  628. assert 2 in values
  629. var hc: Hash
  630. var index = rawGet(s, key, hc)
  631. result = index >= 0
  632. proc incl*[A](s: var OrderedSet[A], key: A) =
  633. ## Includes an element `key` in `s`.
  634. ##
  635. ## This doesn't do anything if `key` is already in `s`.
  636. ##
  637. ## See also:
  638. ## * `excl proc <#excl,OrderedSet[A],A>`_ for excluding an element
  639. ## * `incl proc <#incl,HashSet[A],OrderedSet[A]>`_ for including other set
  640. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  641. runnableExamples:
  642. var values = initOrderedSet[int]()
  643. values.incl(2)
  644. values.incl(2)
  645. assert values.len == 1
  646. inclImpl()
  647. proc incl*[A](s: var HashSet[A], other: OrderedSet[A]) =
  648. ## Includes all elements from the OrderedSet `other` into
  649. ## HashSet `s` (must be declared as `var`).
  650. ##
  651. ## See also:
  652. ## * `incl proc <#incl,OrderedSet[A],A>`_ for including an element
  653. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  654. runnableExamples:
  655. var
  656. values = toHashSet([1, 2, 3])
  657. others = toOrderedSet([3, 4, 5])
  658. values.incl(others)
  659. assert values.len == 5
  660. for item in items(other): incl(s, item)
  661. proc containsOrIncl*[A](s: var OrderedSet[A], key: A): bool =
  662. ## Includes `key` in the set `s` and tells if `key` was already in `s`.
  663. ##
  664. ## The difference with regards to the `incl proc <#incl,OrderedSet[A],A>`_ is
  665. ## that this proc returns `true` if `s` already contained `key`. The
  666. ## proc will return false if `key` was added as a new value to `s` during
  667. ## this call.
  668. ##
  669. ## See also:
  670. ## * `incl proc <#incl,OrderedSet[A],A>`_ for including an element
  671. ## * `missingOrExcl proc <#missingOrExcl,OrderedSet[A],A>`_
  672. runnableExamples:
  673. var values = initOrderedSet[int]()
  674. assert values.containsOrIncl(2) == false
  675. assert values.containsOrIncl(2) == true
  676. assert values.containsOrIncl(3) == false
  677. containsOrInclImpl()
  678. proc excl*[A](s: var OrderedSet[A], key: A) =
  679. ## Excludes `key` from the set `s`. Efficiency: `O(n)`.
  680. ##
  681. ## This doesn't do anything if `key` is not found in `s`.
  682. ##
  683. ## See also:
  684. ## * `incl proc <#incl,OrderedSet[A],A>`_ for including an element
  685. ## * `missingOrExcl proc <#missingOrExcl,OrderedSet[A],A>`_
  686. runnableExamples:
  687. var s = toOrderedSet([2, 3, 6, 7])
  688. s.excl(2)
  689. s.excl(2)
  690. assert s.len == 3
  691. discard exclImpl(s, key)
  692. proc missingOrExcl*[A](s: var OrderedSet[A], key: A): bool =
  693. ## Excludes `key` in the set `s` and tells if `key` was already missing from `s`.
  694. ## Efficiency: O(n).
  695. ##
  696. ## The difference with regards to the `excl proc <#excl,OrderedSet[A],A>`_ is
  697. ## that this proc returns `true` if `key` was missing from `s`.
  698. ## The proc will return `false` if `key` was in `s` and it was removed
  699. ## during this call.
  700. ##
  701. ## See also:
  702. ## * `excl proc <#excl,OrderedSet[A],A>`_
  703. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  704. runnableExamples:
  705. var s = toOrderedSet([2, 3, 6, 7])
  706. assert s.missingOrExcl(4) == true
  707. assert s.missingOrExcl(6) == false
  708. assert s.missingOrExcl(6) == true
  709. exclImpl(s, key)
  710. proc clear*[A](s: var OrderedSet[A]) =
  711. ## Clears the OrderedSet back to an empty state, without shrinking
  712. ## any of the existing storage.
  713. ##
  714. ## `O(n)` operation where `n` is the size of the hash bucket.
  715. runnableExamples:
  716. var s = toOrderedSet([3, 5, 7])
  717. clear(s)
  718. assert len(s) == 0
  719. s.counter = 0
  720. s.first = -1
  721. s.last = -1
  722. for i in 0 ..< s.data.len:
  723. s.data[i].hcode = 0
  724. s.data[i].next = 0
  725. {.push warning[UnsafeDefault]:off.}
  726. reset(s.data[i].key)
  727. {.pop.}
  728. proc len*[A](s: OrderedSet[A]): int {.inline.} =
  729. ## Returns the number of elements in `s`.
  730. ##
  731. ## Due to an implementation detail you can call this proc on variables which
  732. ## have not been initialized yet. The proc will return zero as the length
  733. ## then.
  734. runnableExamples:
  735. var a: OrderedSet[string]
  736. assert len(a) == 0
  737. let s = toHashSet([3, 5, 7])
  738. assert len(s) == 3
  739. result = s.counter
  740. proc card*[A](s: OrderedSet[A]): int {.inline.} =
  741. ## Alias for `len() <#len,OrderedSet[A]>`_.
  742. ##
  743. ## Card stands for the `cardinality
  744. ## <http://en.wikipedia.org/wiki/Cardinality>`_ of a set.
  745. result = s.counter
  746. proc `==`*[A](s, t: OrderedSet[A]): bool =
  747. ## Equality for ordered sets.
  748. runnableExamples:
  749. let
  750. a = toOrderedSet([1, 2])
  751. b = toOrderedSet([2, 1])
  752. assert(not (a == b))
  753. if s.counter != t.counter: return false
  754. var h = s.first
  755. var g = t.first
  756. var compared = 0
  757. while h >= 0 and g >= 0:
  758. var nxh = s.data[h].next
  759. var nxg = t.data[g].next
  760. if isFilled(s.data[h].hcode) and isFilled(t.data[g].hcode):
  761. if s.data[h].key == t.data[g].key:
  762. inc compared
  763. else:
  764. return false
  765. h = nxh
  766. g = nxg
  767. result = compared == s.counter
  768. proc hash*[A](s: OrderedSet[A]): Hash =
  769. ## Hashing of OrderedSet.
  770. forAllOrderedPairs:
  771. result = result !& s.data[h].hcode
  772. result = !$result
  773. proc `$`*[A](s: OrderedSet[A]): string =
  774. ## Converts the ordered hash set `s` to a string, mostly for logging and
  775. ## printing purposes.
  776. ##
  777. ## Don't use this proc for serialization, the representation may change at
  778. ## any moment and values are not escaped.
  779. ##
  780. ## **Examples:**
  781. ## ```Nim
  782. ## echo toOrderedSet([2, 4, 5])
  783. ## # --> {2, 4, 5}
  784. ## echo toOrderedSet(["no", "esc'aping", "is \" provided"])
  785. ## # --> {no, esc'aping, is " provided}
  786. ## ```
  787. dollarImpl()
  788. iterator items*[A](s: OrderedSet[A]): A =
  789. ## Iterates over keys in the ordered set `s` in insertion order.
  790. ##
  791. ## If you need a sequence with the elements you can use `sequtils.toSeq
  792. ## template <sequtils.html#toSeq.t,untyped>`_.
  793. ##
  794. ## ```Nim
  795. ## var a = initOrderedSet[int]()
  796. ## for value in [9, 2, 1, 5, 1, 8, 4, 2]:
  797. ## a.incl(value)
  798. ## for value in a.items:
  799. ## echo "Got ", value
  800. ## # --> Got 9
  801. ## # --> Got 2
  802. ## # --> Got 1
  803. ## # --> Got 5
  804. ## # --> Got 8
  805. ## # --> Got 4
  806. ## ```
  807. let length = s.len
  808. forAllOrderedPairs:
  809. yield s.data[h].key
  810. assert(len(s) == length, "the length of the OrderedSet changed while iterating over it")
  811. iterator pairs*[A](s: OrderedSet[A]): tuple[a: int, b: A] =
  812. ## Iterates through (position, value) tuples of OrderedSet `s`.
  813. runnableExamples:
  814. let a = toOrderedSet("abracadabra")
  815. var p = newSeq[(int, char)]()
  816. for x in pairs(a):
  817. p.add(x)
  818. assert p == @[(0, 'a'), (1, 'b'), (2, 'r'), (3, 'c'), (4, 'd')]
  819. let length = s.len
  820. forAllOrderedPairs:
  821. yield (idx, s.data[h].key)
  822. assert(len(s) == length, "the length of the OrderedSet changed while iterating over it")