sets.nim 29 KB

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