sets.nim 29 KB

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