sets.nim 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. 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. s.data[i].key = default(typeof(s.data[i].key))
  347. proc union*[A](s1, s2: HashSet[A]): HashSet[A] =
  348. ## Returns the union of the sets `s1` and `s2`.
  349. ##
  350. ## The same as `s1 + s2 <#+,HashSet[A],HashSet[A]>`_.
  351. ##
  352. ## The union of two sets is represented mathematically as *A ∪ B* and is the
  353. ## set of all objects that are members of `s1`, `s2` or both.
  354. ##
  355. ## See also:
  356. ## * `intersection proc <#intersection,HashSet[A],HashSet[A]>`_
  357. ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
  358. ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
  359. runnableExamples:
  360. let
  361. a = toHashSet(["a", "b"])
  362. b = toHashSet(["b", "c"])
  363. c = union(a, b)
  364. assert c == toHashSet(["a", "b", "c"])
  365. result = s1
  366. incl(result, s2)
  367. proc intersection*[A](s1, s2: HashSet[A]): HashSet[A] =
  368. ## Returns the intersection of the sets `s1` and `s2`.
  369. ##
  370. ## The same as `s1 * s2 <#*,HashSet[A],HashSet[A]>`_.
  371. ##
  372. ## The intersection of two sets is represented mathematically as *A ∩ B* and
  373. ## is the set of all objects that are members of `s1` and `s2` at the same
  374. ## time.
  375. ##
  376. ## See also:
  377. ## * `union proc <#union,HashSet[A],HashSet[A]>`_
  378. ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
  379. ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
  380. runnableExamples:
  381. let
  382. a = toHashSet(["a", "b"])
  383. b = toHashSet(["b", "c"])
  384. c = intersection(a, b)
  385. assert c == toHashSet(["b"])
  386. result = initHashSet[A](max(min(s1.data.len, s2.data.len), 2))
  387. # iterate over the elements of the smaller set
  388. if s1.data.len < s2.data.len:
  389. for item in s1:
  390. if item in s2: incl(result, item)
  391. else:
  392. for item in s2:
  393. if item in s1: incl(result, item)
  394. proc difference*[A](s1, s2: HashSet[A]): HashSet[A] =
  395. ## Returns the difference of the sets `s1` and `s2`.
  396. ##
  397. ## The same as `s1 - s2 <#-,HashSet[A],HashSet[A]>`_.
  398. ##
  399. ## The difference of two sets is represented mathematically as *A ∖ B* and is
  400. ## the set of all objects that are members of `s1` and not members of `s2`.
  401. ##
  402. ## See also:
  403. ## * `union proc <#union,HashSet[A],HashSet[A]>`_
  404. ## * `intersection proc <#intersection,HashSet[A],HashSet[A]>`_
  405. ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
  406. runnableExamples:
  407. let
  408. a = toHashSet(["a", "b"])
  409. b = toHashSet(["b", "c"])
  410. c = difference(a, b)
  411. assert c == toHashSet(["a"])
  412. result = initHashSet[A]()
  413. for item in s1:
  414. if not contains(s2, item):
  415. incl(result, item)
  416. proc symmetricDifference*[A](s1, s2: HashSet[A]): HashSet[A] =
  417. ## Returns the symmetric difference of the sets `s1` and `s2`.
  418. ##
  419. ## The same as `s1 -+- s2 <#-+-,HashSet[A],HashSet[A]>`_.
  420. ##
  421. ## The symmetric difference of two sets is represented mathematically as *A △
  422. ## B* or *A ⊖ B* and is the set of all objects that are members of `s1` or
  423. ## `s2` but not both at the same time.
  424. ##
  425. ## See also:
  426. ## * `union proc <#union,HashSet[A],HashSet[A]>`_
  427. ## * `intersection proc <#intersection,HashSet[A],HashSet[A]>`_
  428. ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
  429. runnableExamples:
  430. let
  431. a = toHashSet(["a", "b"])
  432. b = toHashSet(["b", "c"])
  433. c = symmetricDifference(a, b)
  434. assert c == toHashSet(["a", "c"])
  435. result = s1
  436. for item in s2:
  437. if containsOrIncl(result, item): excl(result, item)
  438. proc `+`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  439. ## Alias for `union(s1, s2) <#union,HashSet[A],HashSet[A]>`_.
  440. result = union(s1, s2)
  441. proc `*`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  442. ## Alias for `intersection(s1, s2) <#intersection,HashSet[A],HashSet[A]>`_.
  443. result = intersection(s1, s2)
  444. proc `-`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  445. ## Alias for `difference(s1, s2) <#difference,HashSet[A],HashSet[A]>`_.
  446. result = difference(s1, s2)
  447. proc `-+-`*[A](s1, s2: HashSet[A]): HashSet[A] {.inline.} =
  448. ## Alias for `symmetricDifference(s1, s2)
  449. ## <#symmetricDifference,HashSet[A],HashSet[A]>`_.
  450. result = symmetricDifference(s1, s2)
  451. proc disjoint*[A](s1, s2: HashSet[A]): bool =
  452. ## Returns `true` if the sets `s1` and `s2` have no items in common.
  453. runnableExamples:
  454. let
  455. a = toHashSet(["a", "b"])
  456. b = toHashSet(["b", "c"])
  457. assert disjoint(a, b) == false
  458. assert disjoint(a, b - a) == true
  459. for item in s1:
  460. if item in s2: return false
  461. return true
  462. proc `<`*[A](s, t: HashSet[A]): bool =
  463. ## Returns true if `s` is a strict or proper subset of `t`.
  464. ##
  465. ## A strict or proper subset `s` has all of its members in `t` but `t` has
  466. ## more elements than `s`.
  467. runnableExamples:
  468. let
  469. a = toHashSet(["a", "b"])
  470. b = toHashSet(["b", "c"])
  471. c = intersection(a, b)
  472. assert c < a and c < b
  473. assert(not (a < a))
  474. s.counter != t.counter and s <= t
  475. proc `<=`*[A](s, t: HashSet[A]): bool =
  476. ## Returns true if `s` is a subset of `t`.
  477. ##
  478. ## A subset `s` has all of its members in `t` and `t` doesn't necessarily
  479. ## have more members than `s`. That is, `s` can be equal to `t`.
  480. runnableExamples:
  481. let
  482. a = toHashSet(["a", "b"])
  483. b = toHashSet(["b", "c"])
  484. c = intersection(a, b)
  485. assert c <= a and c <= b
  486. assert a <= a
  487. result = false
  488. if s.counter > t.counter: return
  489. result = true
  490. for item in items(s):
  491. if not(t.contains(item)):
  492. result = false
  493. return
  494. proc `==`*[A](s, t: HashSet[A]): bool =
  495. ## Returns true if both `s` and `t` have the same members and set size.
  496. runnableExamples:
  497. var
  498. a = toHashSet([1, 2])
  499. b = toHashSet([2, 1])
  500. assert a == b
  501. s.counter == t.counter and s <= t
  502. proc map*[A, B](data: HashSet[A], op: proc (x: A): B {.closure.}): HashSet[B] {.effectsOf: op.} =
  503. ## Returns a new set after applying `op` proc on each of the elements of
  504. ##`data` set.
  505. ##
  506. ## You can use this proc to transform the elements from a set.
  507. runnableExamples:
  508. let
  509. a = toHashSet([1, 2, 3])
  510. b = a.map(proc (x: int): string = $x)
  511. assert b == toHashSet(["1", "2", "3"])
  512. result = initHashSet[B]()
  513. for item in items(data): result.incl(op(item))
  514. proc hash*[A](s: HashSet[A]): Hash =
  515. ## Hashing of HashSet.
  516. for h in 0 .. high(s.data):
  517. result = result xor s.data[h].hcode
  518. result = !$result
  519. proc `$`*[A](s: HashSet[A]): string =
  520. ## Converts the set `s` to a string, mostly for logging and printing purposes.
  521. ##
  522. ## Don't use this proc for serialization, the representation may change at
  523. ## any moment and values are not escaped.
  524. ##
  525. ## **Examples:**
  526. ## ```Nim
  527. ## echo toHashSet([2, 4, 5])
  528. ## # --> {2, 4, 5}
  529. ## echo toHashSet(["no", "esc'aping", "is \" provided"])
  530. ## # --> {no, esc'aping, is " provided}
  531. ## ```
  532. dollarImpl()
  533. proc initSet*[A](initialSize = defaultInitialSize): HashSet[A] {.deprecated:
  534. "Deprecated since v0.20, use 'initHashSet'".} = initHashSet[A](initialSize)
  535. proc toSet*[A](keys: openArray[A]): HashSet[A] {.deprecated:
  536. "Deprecated since v0.20, use 'toHashSet'".} = toHashSet[A](keys)
  537. proc isValid*[A](s: HashSet[A]): bool {.deprecated:
  538. "Deprecated since v0.20; sets are initialized by default".} =
  539. ## Returns `true` if the set has been initialized (with `initHashSet proc
  540. ## <#initHashSet>`_ or `init proc <#init,HashSet[A]>`_).
  541. ##
  542. runnableExamples:
  543. proc savePreferences(options: HashSet[string]) =
  544. assert options.isValid, "Pass an initialized set!"
  545. # Do stuff here, may crash in release builds!
  546. result = s.data.len > 0
  547. # ---------------------------------------------------------------------
  548. # --------------------------- OrderedSet ------------------------------
  549. # ---------------------------------------------------------------------
  550. template forAllOrderedPairs(yieldStmt: untyped) {.dirty.} =
  551. if s.data.len > 0:
  552. var h = s.first
  553. var idx = 0
  554. while h >= 0:
  555. var nxt = s.data[h].next
  556. if isFilled(s.data[h].hcode):
  557. yieldStmt
  558. inc(idx)
  559. h = nxt
  560. proc init*[A](s: var OrderedSet[A], initialSize = defaultInitialSize) =
  561. ## Initializes an ordered hash set.
  562. ##
  563. ## Starting from Nim v0.20, sets are initialized by default and it is
  564. ## not necessary to call this function explicitly.
  565. ##
  566. ## You can call this proc on a previously initialized hash set, which will
  567. ## discard all its values. This might be more convenient than iterating over
  568. ## existing values and calling `excl() <#excl,HashSet[A],A>`_ on them.
  569. ##
  570. ## See also:
  571. ## * `initOrderedSet proc <#initOrderedSet>`_
  572. ## * `toOrderedSet proc <#toOrderedSet,openArray[A]>`_
  573. runnableExamples:
  574. var a: OrderedSet[int]
  575. init(a)
  576. initImpl(s, initialSize)
  577. proc initOrderedSet*[A](initialSize = defaultInitialSize): OrderedSet[A] =
  578. ## Wrapper around `init proc <#init,OrderedSet[A]>`_ for initialization of
  579. ## ordered hash sets.
  580. ##
  581. ## Returns an empty ordered hash set you can assign directly in `var` blocks
  582. ## in a single line.
  583. ##
  584. ## Starting from Nim v0.20, sets are initialized by default and it is
  585. ## not necessary to call this function explicitly.
  586. ##
  587. ## See also:
  588. ## * `toOrderedSet proc <#toOrderedSet,openArray[A]>`_
  589. runnableExamples:
  590. var a = initOrderedSet[int]()
  591. a.incl(3)
  592. assert len(a) == 1
  593. result.init(initialSize)
  594. proc toOrderedSet*[A](keys: openArray[A]): OrderedSet[A] =
  595. ## Creates a new hash set that contains the members of the given
  596. ## collection (seq, array, or string) `keys`.
  597. ##
  598. ## Duplicates are removed.
  599. ##
  600. ## See also:
  601. ## * `initOrderedSet proc <#initOrderedSet>`_
  602. runnableExamples:
  603. let
  604. a = toOrderedSet([5, 3, 2])
  605. b = toOrderedSet("abracadabra")
  606. assert len(a) == 3
  607. ## a == {5, 3, 2} # different than in HashSet
  608. assert len(b) == 5
  609. ## b == {'a', 'b', 'r', 'c', 'd'} # different than in HashSet
  610. result = initOrderedSet[A](keys.len)
  611. for key in items(keys): result.incl(key)
  612. proc contains*[A](s: OrderedSet[A], key: A): bool =
  613. ## Returns true if `key` is in `s`.
  614. ##
  615. ## This allows the usage of `in` operator.
  616. ##
  617. ## See also:
  618. ## * `incl proc <#incl,OrderedSet[A],A>`_
  619. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  620. runnableExamples:
  621. var values = initOrderedSet[int]()
  622. assert(not values.contains(2))
  623. assert 2 notin values
  624. values.incl(2)
  625. assert values.contains(2)
  626. assert 2 in values
  627. var hc: Hash
  628. var index = rawGet(s, key, hc)
  629. result = index >= 0
  630. proc incl*[A](s: var OrderedSet[A], key: A) =
  631. ## Includes an element `key` in `s`.
  632. ##
  633. ## This doesn't do anything if `key` is already in `s`.
  634. ##
  635. ## See also:
  636. ## * `excl proc <#excl,OrderedSet[A],A>`_ for excluding an element
  637. ## * `incl proc <#incl,HashSet[A],OrderedSet[A]>`_ for including other set
  638. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  639. runnableExamples:
  640. var values = initOrderedSet[int]()
  641. values.incl(2)
  642. values.incl(2)
  643. assert values.len == 1
  644. inclImpl()
  645. proc incl*[A](s: var HashSet[A], other: OrderedSet[A]) =
  646. ## Includes all elements from the OrderedSet `other` into
  647. ## HashSet `s` (must be declared as `var`).
  648. ##
  649. ## See also:
  650. ## * `incl proc <#incl,OrderedSet[A],A>`_ for including an element
  651. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  652. runnableExamples:
  653. var
  654. values = toHashSet([1, 2, 3])
  655. others = toOrderedSet([3, 4, 5])
  656. values.incl(others)
  657. assert values.len == 5
  658. for item in items(other): incl(s, item)
  659. proc containsOrIncl*[A](s: var OrderedSet[A], key: A): bool =
  660. ## Includes `key` in the set `s` and tells if `key` was already in `s`.
  661. ##
  662. ## The difference with regards to the `incl proc <#incl,OrderedSet[A],A>`_ is
  663. ## that this proc returns `true` if `s` already contained `key`. The
  664. ## proc will return false if `key` was added as a new value to `s` during
  665. ## this call.
  666. ##
  667. ## See also:
  668. ## * `incl proc <#incl,OrderedSet[A],A>`_ for including an element
  669. ## * `missingOrExcl proc <#missingOrExcl,OrderedSet[A],A>`_
  670. runnableExamples:
  671. var values = initOrderedSet[int]()
  672. assert values.containsOrIncl(2) == false
  673. assert values.containsOrIncl(2) == true
  674. assert values.containsOrIncl(3) == false
  675. containsOrInclImpl()
  676. proc excl*[A](s: var OrderedSet[A], key: A) =
  677. ## Excludes `key` from the set `s`. Efficiency: `O(n)`.
  678. ##
  679. ## This doesn't do anything if `key` is not found in `s`.
  680. ##
  681. ## See also:
  682. ## * `incl proc <#incl,OrderedSet[A],A>`_ for including an element
  683. ## * `missingOrExcl proc <#missingOrExcl,OrderedSet[A],A>`_
  684. runnableExamples:
  685. var s = toOrderedSet([2, 3, 6, 7])
  686. s.excl(2)
  687. s.excl(2)
  688. assert s.len == 3
  689. discard exclImpl(s, key)
  690. proc missingOrExcl*[A](s: var OrderedSet[A], key: A): bool =
  691. ## Excludes `key` in the set `s` and tells if `key` was already missing from `s`.
  692. ## Efficiency: O(n).
  693. ##
  694. ## The difference with regards to the `excl proc <#excl,OrderedSet[A],A>`_ is
  695. ## that this proc returns `true` if `key` was missing from `s`.
  696. ## The proc will return `false` if `key` was in `s` and it was removed
  697. ## during this call.
  698. ##
  699. ## See also:
  700. ## * `excl proc <#excl,OrderedSet[A],A>`_
  701. ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
  702. runnableExamples:
  703. var s = toOrderedSet([2, 3, 6, 7])
  704. assert s.missingOrExcl(4) == true
  705. assert s.missingOrExcl(6) == false
  706. assert s.missingOrExcl(6) == true
  707. exclImpl(s, key)
  708. proc clear*[A](s: var OrderedSet[A]) =
  709. ## Clears the OrderedSet back to an empty state, without shrinking
  710. ## any of the existing storage.
  711. ##
  712. ## `O(n)` operation where `n` is the size of the hash bucket.
  713. runnableExamples:
  714. var s = toOrderedSet([3, 5, 7])
  715. clear(s)
  716. assert len(s) == 0
  717. s.counter = 0
  718. s.first = -1
  719. s.last = -1
  720. for i in 0 ..< s.data.len:
  721. s.data[i].hcode = 0
  722. s.data[i].next = 0
  723. s.data[i].key = default(typeof(s.data[i].key))
  724. proc len*[A](s: OrderedSet[A]): int {.inline.} =
  725. ## Returns the number of elements in `s`.
  726. ##
  727. ## Due to an implementation detail you can call this proc on variables which
  728. ## have not been initialized yet. The proc will return zero as the length
  729. ## then.
  730. runnableExamples:
  731. var a: OrderedSet[string]
  732. assert len(a) == 0
  733. let s = toHashSet([3, 5, 7])
  734. assert len(s) == 3
  735. result = s.counter
  736. proc card*[A](s: OrderedSet[A]): int {.inline.} =
  737. ## Alias for `len() <#len,OrderedSet[A]>`_.
  738. ##
  739. ## Card stands for the `cardinality
  740. ## <http://en.wikipedia.org/wiki/Cardinality>`_ of a set.
  741. result = s.counter
  742. proc `==`*[A](s, t: OrderedSet[A]): bool =
  743. ## Equality for ordered sets.
  744. runnableExamples:
  745. let
  746. a = toOrderedSet([1, 2])
  747. b = toOrderedSet([2, 1])
  748. assert(not (a == b))
  749. if s.counter != t.counter: return false
  750. var h = s.first
  751. var g = t.first
  752. var compared = 0
  753. while h >= 0 and g >= 0:
  754. var nxh = s.data[h].next
  755. var nxg = t.data[g].next
  756. if isFilled(s.data[h].hcode) and isFilled(t.data[g].hcode):
  757. if s.data[h].key == t.data[g].key:
  758. inc compared
  759. else:
  760. return false
  761. h = nxh
  762. g = nxg
  763. result = compared == s.counter
  764. proc hash*[A](s: OrderedSet[A]): Hash =
  765. ## Hashing of OrderedSet.
  766. forAllOrderedPairs:
  767. result = result !& s.data[h].hcode
  768. result = !$result
  769. proc `$`*[A](s: OrderedSet[A]): string =
  770. ## Converts the ordered hash set `s` to a string, mostly for logging and
  771. ## printing purposes.
  772. ##
  773. ## Don't use this proc for serialization, the representation may change at
  774. ## any moment and values are not escaped.
  775. ##
  776. ## **Examples:**
  777. ## ```Nim
  778. ## echo toOrderedSet([2, 4, 5])
  779. ## # --> {2, 4, 5}
  780. ## echo toOrderedSet(["no", "esc'aping", "is \" provided"])
  781. ## # --> {no, esc'aping, is " provided}
  782. ## ```
  783. dollarImpl()
  784. iterator items*[A](s: OrderedSet[A]): A =
  785. ## Iterates over keys in the ordered set `s` in insertion order.
  786. ##
  787. ## If you need a sequence with the elements you can use `sequtils.toSeq
  788. ## template <sequtils.html#toSeq.t,untyped>`_.
  789. ##
  790. ## ```Nim
  791. ## var a = initOrderedSet[int]()
  792. ## for value in [9, 2, 1, 5, 1, 8, 4, 2]:
  793. ## a.incl(value)
  794. ## for value in a.items:
  795. ## echo "Got ", value
  796. ## # --> Got 9
  797. ## # --> Got 2
  798. ## # --> Got 1
  799. ## # --> Got 5
  800. ## # --> Got 8
  801. ## # --> Got 4
  802. ## ```
  803. let length = s.len
  804. forAllOrderedPairs:
  805. yield s.data[h].key
  806. assert(len(s) == length, "the length of the OrderedSet changed while iterating over it")
  807. iterator pairs*[A](s: OrderedSet[A]): tuple[a: int, b: A] =
  808. ## Iterates through (position, value) tuples of OrderedSet `s`.
  809. runnableExamples:
  810. let a = toOrderedSet("abracadabra")
  811. var p = newSeq[(int, char)]()
  812. for x in pairs(a):
  813. p.add(x)
  814. assert p == @[(0, 'a'), (1, 'b'), (2, 'r'), (3, 'c'), (4, 'd')]
  815. let length = s.len
  816. forAllOrderedPairs:
  817. yield (idx, s.data[h].key)
  818. assert(len(s) == length, "the length of the OrderedSet changed while iterating over it")