tables.nim 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## The ``tables`` module implements variants of an efficient `hash table`:idx:
  10. ## (also often named `dictionary`:idx: in other programming languages) that is
  11. ## a mapping from keys to values. ``Table`` is the usual hash table,
  12. ## ``OrderedTable`` is like ``Table`` but remembers insertion order
  13. ## and ``CountTable`` is a mapping from a key to its number of occurrences.
  14. ##
  15. ## For consistency with every other data type in Nim these have **value**
  16. ## semantics, this means that ``=`` performs a copy of the hash table.
  17. ## For **reference** semantics use the ``Ref`` variant: ``TableRef``,
  18. ## ``OrderedTableRef``, ``CountTableRef``.
  19. ##
  20. ## To give an example, when ``a`` is a Table, then ``var b = a`` gives ``b``
  21. ## as a new independent table. ``b`` is initialised with the contents of ``a``.
  22. ## Changing ``b`` does not affect ``a`` and vice versa:
  23. ##
  24. ## .. code-block::
  25. ## import tables
  26. ##
  27. ## var
  28. ## a = {1: "one", 2: "two"}.toTable # creates a Table
  29. ## b = a
  30. ##
  31. ## echo a, b # output: {1: one, 2: two}{1: one, 2: two}
  32. ##
  33. ## b[3] = "three"
  34. ## echo a, b # output: {1: one, 2: two}{1: one, 2: two, 3: three}
  35. ## echo a == b # output: false
  36. ##
  37. ## On the other hand, when ``a`` is a TableRef instead, then changes to ``b``
  38. ## also affect ``a``. Both ``a`` and ``b`` reference the same data structure:
  39. ##
  40. ## .. code-block::
  41. ## import tables
  42. ##
  43. ## var
  44. ## a = {1: "one", 2: "two"}.newTable # creates a TableRef
  45. ## b = a
  46. ##
  47. ## echo a, b # output: {1: one, 2: two}{1: one, 2: two}
  48. ##
  49. ## b[3] = "three"
  50. ## echo a, b # output: {1: one, 2: two, 3: three}{1: one, 2: two, 3: three}
  51. ## echo a == b # output: true
  52. ##
  53. ##
  54. ## Here is an example of ``CountTable`` usage:
  55. ##
  56. ## .. code-block:: nim
  57. ## let myString = "abracadabra"
  58. ## var myTable = initCountTable[char]()
  59. ##
  60. ## for c in myString:
  61. ## myTable.inc(c)
  62. ##
  63. ## echo myTable # output: {'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2}
  64. ##
  65. ##
  66. ## If you are using simple standard types like ``int`` or ``string`` for the
  67. ## keys of the table you won't have any problems, but as soon as you try to use
  68. ## a more complex object as a key you will be greeted by a strange compiler
  69. ## error::
  70. ##
  71. ## Error: type mismatch: got (Person)
  72. ## but expected one of:
  73. ## hashes.hash(x: openarray[A]): Hash
  74. ## hashes.hash(x: int): Hash
  75. ## hashes.hash(x: float): Hash
  76. ## …
  77. ##
  78. ## What is happening here is that the types used for table keys require to have
  79. ## a ``hash()`` proc which will convert them to a `Hash <hashes.html#Hash>`_
  80. ## value, and the compiler is listing all the hash functions it knows.
  81. ## Additionally there has to be a ``==`` operator that provides the same
  82. ## semantics as its corresponding ``hash`` proc.
  83. ##
  84. ## After you add ``hash`` and ``==`` for your custom type everything will work.
  85. ## Currently, however, ``hash`` for objects is not defined, whereas
  86. ## ``system.==`` for objects does exist and performs a "deep" comparison (every
  87. ## field is compared) which is usually what you want. So in the following
  88. ## example implementing only ``hash`` suffices:
  89. ##
  90. ## .. code-block::
  91. ## type
  92. ## Person = object
  93. ## firstName, lastName: string
  94. ##
  95. ## proc hash(x: Person): Hash =
  96. ## ## Piggyback on the already available string hash proc.
  97. ## ##
  98. ## ## Without this proc nothing works!
  99. ## result = x.firstName.hash !& x.lastName.hash
  100. ## result = !$result
  101. ##
  102. ## var
  103. ## salaries = initTable[Person, int]()
  104. ## p1, p2: Person
  105. ##
  106. ## p1.firstName = "Jon"
  107. ## p1.lastName = "Ross"
  108. ## salaries[p1] = 30_000
  109. ##
  110. ## p2.firstName = "소진"
  111. ## p2.lastName = "박"
  112. ## salaries[p2] = 45_000
  113. import
  114. hashes, math
  115. include "system/inclrtl"
  116. type
  117. KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B]
  118. KeyValuePairSeq[A, B] = seq[KeyValuePair[A, B]]
  119. Table*[A, B] = object ## generic hash table
  120. data: KeyValuePairSeq[A, B]
  121. counter: int
  122. TableRef*[A,B] = ref Table[A, B]
  123. {.deprecated: [TTable: Table, PTable: TableRef].}
  124. template maxHash(t): untyped = high(t.data)
  125. template dataLen(t): untyped = len(t.data)
  126. include tableimpl
  127. proc clear*[A, B](t: var Table[A, B]) =
  128. ## resets the table so that it is empty.
  129. clearImpl()
  130. proc clear*[A, B](t: TableRef[A, B]) =
  131. ## resets the ref table so that it is empty.
  132. clearImpl()
  133. proc rightSize*(count: Natural): int {.inline.} =
  134. ## return the value of ``initialSize`` to support ``count`` items.
  135. ##
  136. ## If more items are expected to be added, simply add that
  137. ## expected extra amount to the parameter before calling this.
  138. ##
  139. ## Internally, we want mustRehash(rightSize(x), x) == false.
  140. result = nextPowerOfTwo(count * 3 div 2 + 4)
  141. proc len*[A, B](t: Table[A, B]): int =
  142. ## returns the number of keys in ``t``.
  143. result = t.counter
  144. template get(t, key): untyped =
  145. ## retrieves the value at ``t[key]``. The value can be modified.
  146. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  147. mixin rawGet
  148. var hc: Hash
  149. var index = rawGet(t, key, hc)
  150. if index >= 0: result = t.data[index].val
  151. else:
  152. when compiles($key):
  153. raise newException(KeyError, "key not found: " & $key)
  154. else:
  155. raise newException(KeyError, "key not found")
  156. template getOrDefaultImpl(t, key): untyped =
  157. mixin rawGet
  158. var hc: Hash
  159. var index = rawGet(t, key, hc)
  160. if index >= 0: result = t.data[index].val
  161. template getOrDefaultImpl(t, key, default: untyped): untyped =
  162. mixin rawGet
  163. var hc: Hash
  164. var index = rawGet(t, key, hc)
  165. result = if index >= 0: t.data[index].val else: default
  166. proc `[]`*[A, B](t: Table[A, B], key: A): B {.deprecatedGet.} =
  167. ## retrieves the value at ``t[key]``. If ``key`` is not in ``t``, the
  168. ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
  169. ## the key exists.
  170. get(t, key)
  171. proc `[]`*[A, B](t: var Table[A, B], key: A): var B {.deprecatedGet.} =
  172. ## retrieves the value at ``t[key]``. The value can be modified.
  173. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  174. get(t, key)
  175. proc mget*[A, B](t: var Table[A, B], key: A): var B {.deprecated.} =
  176. ## retrieves the value at ``t[key]``. The value can be modified.
  177. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  178. ## Use ``[]`` instead.
  179. get(t, key)
  180. proc getOrDefault*[A, B](t: Table[A, B], key: A): B =
  181. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, the
  182. ## default initialization value for type ``B`` is returned (e.g. 0 for any
  183. ## integer type).
  184. getOrDefaultImpl(t, key)
  185. proc getOrDefault*[A, B](t: Table[A, B], key: A, default: B): B =
  186. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``.
  187. ## Otherwise, ``default`` is returned.
  188. getOrDefaultImpl(t, key, default)
  189. template withValue*[A, B](t: var Table[A, B], key: A, value, body: untyped) =
  190. ## retrieves the value at ``t[key]``.
  191. ## ``value`` can be modified in the scope of the ``withValue`` call.
  192. ##
  193. ## .. code-block:: nim
  194. ##
  195. ## sharedTable.withValue(key, value) do:
  196. ## # block is executed only if ``key`` in ``t``
  197. ## value.name = "username"
  198. ## value.uid = 1000
  199. ##
  200. mixin rawGet
  201. var hc: Hash
  202. var index = rawGet(t, key, hc)
  203. let hasKey = index >= 0
  204. if hasKey:
  205. var value {.inject.} = addr(t.data[index].val)
  206. body
  207. template withValue*[A, B](t: var Table[A, B], key: A,
  208. value, body1, body2: untyped) =
  209. ## retrieves the value at ``t[key]``.
  210. ## ``value`` can be modified in the scope of the ``withValue`` call.
  211. ##
  212. ## .. code-block:: nim
  213. ##
  214. ## table.withValue(key, value) do:
  215. ## # block is executed only if ``key`` in ``t``
  216. ## value.name = "username"
  217. ## value.uid = 1000
  218. ## do:
  219. ## # block is executed when ``key`` not in ``t``
  220. ## raise newException(KeyError, "Key not found")
  221. ##
  222. mixin rawGet
  223. var hc: Hash
  224. var index = rawGet(t, key, hc)
  225. let hasKey = index >= 0
  226. if hasKey:
  227. var value {.inject.} = addr(t.data[index].val)
  228. body1
  229. else:
  230. body2
  231. iterator allValues*[A, B](t: Table[A, B]; key: A): B =
  232. ## iterates over any value in the table ``t`` that belongs to the given ``key``.
  233. var h: Hash = genHash(key) and high(t.data)
  234. while isFilled(t.data[h].hcode):
  235. if t.data[h].key == key:
  236. yield t.data[h].val
  237. h = nextTry(h, high(t.data))
  238. proc hasKey*[A, B](t: Table[A, B], key: A): bool =
  239. ## returns true iff ``key`` is in the table ``t``.
  240. var hc: Hash
  241. result = rawGet(t, key, hc) >= 0
  242. proc contains*[A, B](t: Table[A, B], key: A): bool =
  243. ## alias of ``hasKey`` for use with the ``in`` operator.
  244. return hasKey[A, B](t, key)
  245. iterator pairs*[A, B](t: Table[A, B]): (A, B) =
  246. ## iterates over any ``(key, value)`` pair in the table ``t``.
  247. for h in 0..high(t.data):
  248. if isFilled(t.data[h].hcode): yield (t.data[h].key, t.data[h].val)
  249. iterator mpairs*[A, B](t: var Table[A, B]): (A, var B) =
  250. ## iterates over any ``(key, value)`` pair in the table ``t``. The values
  251. ## can be modified.
  252. for h in 0..high(t.data):
  253. if isFilled(t.data[h].hcode): yield (t.data[h].key, t.data[h].val)
  254. iterator keys*[A, B](t: Table[A, B]): A =
  255. ## iterates over any key in the table ``t``.
  256. for h in 0..high(t.data):
  257. if isFilled(t.data[h].hcode): yield t.data[h].key
  258. iterator values*[A, B](t: Table[A, B]): B =
  259. ## iterates over any value in the table ``t``.
  260. for h in 0..high(t.data):
  261. if isFilled(t.data[h].hcode): yield t.data[h].val
  262. iterator mvalues*[A, B](t: var Table[A, B]): var B =
  263. ## iterates over any value in the table ``t``. The values can be modified.
  264. for h in 0..high(t.data):
  265. if isFilled(t.data[h].hcode): yield t.data[h].val
  266. proc del*[A, B](t: var Table[A, B], key: A) =
  267. ## deletes ``key`` from hash table ``t``. Does nothing if the key does not exist.
  268. delImpl()
  269. proc take*[A, B](t: var Table[A, B], key: A, val: var B): bool =
  270. ## deletes the ``key`` from the table.
  271. ## Returns ``true``, if the ``key`` existed, and sets ``val`` to the
  272. ## mapping of the key. Otherwise, returns ``false``, and the ``val`` is
  273. ## unchanged.
  274. var hc: Hash
  275. var index = rawGet(t, key, hc)
  276. result = index >= 0
  277. if result:
  278. shallowCopy(val, t.data[index].val)
  279. delImplIdx(t, index)
  280. proc enlarge[A, B](t: var Table[A, B]) =
  281. var n: KeyValuePairSeq[A, B]
  282. newSeq(n, len(t.data) * growthFactor)
  283. swap(t.data, n)
  284. for i in countup(0, high(n)):
  285. let eh = n[i].hcode
  286. if isFilled(eh):
  287. var j: Hash = eh and maxHash(t)
  288. while isFilled(t.data[j].hcode):
  289. j = nextTry(j, maxHash(t))
  290. rawInsert(t, t.data, n[i].key, n[i].val, eh, j)
  291. proc mgetOrPut*[A, B](t: var Table[A, B], key: A, val: B): var B =
  292. ## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
  293. ## returning a value which can be modified.
  294. mgetOrPutImpl(enlarge)
  295. proc hasKeyOrPut*[A, B](t: var Table[A, B], key: A, val: B): bool =
  296. ## returns true iff ``key`` is in the table, otherwise inserts ``value``.
  297. hasKeyOrPutImpl(enlarge)
  298. proc `[]=`*[A, B](t: var Table[A, B], key: A, val: B) =
  299. ## puts a ``(key, value)`` pair into ``t``.
  300. putImpl(enlarge)
  301. proc add*[A, B](t: var Table[A, B], key: A, val: B) =
  302. ## puts a new ``(key, value)`` pair into ``t`` even if ``t[key]`` already exists.
  303. ## This can introduce duplicate keys into the table!
  304. addImpl(enlarge)
  305. proc len*[A, B](t: TableRef[A, B]): int =
  306. ## returns the number of keys in ``t``.
  307. result = t.counter
  308. proc initTable*[A, B](initialSize=64): Table[A, B] =
  309. ## creates a new hash table that is empty.
  310. ##
  311. ## ``initialSize`` needs to be a power of two. If you need to accept runtime
  312. ## values for this you could use the ``nextPowerOfTwo`` proc from the
  313. ## `math <math.html>`_ module or the ``rightSize`` proc from this module.
  314. assert isPowerOfTwo(initialSize)
  315. result.counter = 0
  316. newSeq(result.data, initialSize)
  317. proc toTable*[A, B](pairs: openArray[(A, B)]): Table[A, B] =
  318. ## creates a new hash table that contains the given ``pairs``.
  319. result = initTable[A, B](rightSize(pairs.len))
  320. for key, val in items(pairs): result[key] = val
  321. template dollarImpl(): untyped {.dirty.} =
  322. if t.len == 0:
  323. result = "{:}"
  324. else:
  325. result = "{"
  326. for key, val in pairs(t):
  327. if result.len > 1: result.add(", ")
  328. result.addQuoted(key)
  329. result.add(": ")
  330. result.addQuoted(val)
  331. result.add("}")
  332. proc `$`*[A, B](t: Table[A, B]): string =
  333. ## the ``$`` operator for hash tables.
  334. dollarImpl()
  335. proc hasKey*[A, B](t: TableRef[A, B], key: A): bool =
  336. ## returns true iff ``key`` is in the table ``t``.
  337. result = t[].hasKey(key)
  338. template equalsImpl(s, t: typed): typed =
  339. if s.counter == t.counter:
  340. # different insertion orders mean different 'data' seqs, so we have
  341. # to use the slow route here:
  342. for key, val in s:
  343. if not t.hasKey(key): return false
  344. if t.getOrDefault(key) != val: return false
  345. return true
  346. proc `==`*[A, B](s, t: Table[A, B]): bool =
  347. ## The ``==`` operator for hash tables. Returns ``true`` iff the content of both
  348. ## tables contains the same key-value pairs. Insert order does not matter.
  349. equalsImpl(s, t)
  350. proc indexBy*[A, B, C](collection: A, index: proc(x: B): C): Table[C, B] =
  351. ## Index the collection with the proc provided.
  352. # TODO: As soon as supported, change collection: A to collection: A[B]
  353. result = initTable[C, B]()
  354. for item in collection:
  355. result[index(item)] = item
  356. iterator pairs*[A, B](t: TableRef[A, B]): (A, B) =
  357. ## iterates over any ``(key, value)`` pair in the table ``t``.
  358. for h in 0..high(t.data):
  359. if isFilled(t.data[h].hcode): yield (t.data[h].key, t.data[h].val)
  360. iterator mpairs*[A, B](t: TableRef[A, B]): (A, var B) =
  361. ## iterates over any ``(key, value)`` pair in the table ``t``. The values
  362. ## can be modified.
  363. for h in 0..high(t.data):
  364. if isFilled(t.data[h].hcode): yield (t.data[h].key, t.data[h].val)
  365. iterator keys*[A, B](t: TableRef[A, B]): A =
  366. ## iterates over any key in the table ``t``.
  367. for h in 0..high(t.data):
  368. if isFilled(t.data[h].hcode): yield t.data[h].key
  369. iterator values*[A, B](t: TableRef[A, B]): B =
  370. ## iterates over any value in the table ``t``.
  371. for h in 0..high(t.data):
  372. if isFilled(t.data[h].hcode): yield t.data[h].val
  373. iterator mvalues*[A, B](t: TableRef[A, B]): var B =
  374. ## iterates over any value in the table ``t``. The values can be modified.
  375. for h in 0..high(t.data):
  376. if isFilled(t.data[h].hcode): yield t.data[h].val
  377. proc `[]`*[A, B](t: TableRef[A, B], key: A): var B {.deprecatedGet.} =
  378. ## retrieves the value at ``t[key]``. If ``key`` is not in ``t``, the
  379. ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
  380. ## the key exists.
  381. result = t[][key]
  382. proc mget*[A, B](t: TableRef[A, B], key: A): var B {.deprecated.} =
  383. ## retrieves the value at ``t[key]``. The value can be modified.
  384. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  385. ## Use ``[]`` instead.
  386. t[][key]
  387. proc getOrDefault*[A, B](t: TableRef[A, B], key: A): B =
  388. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, the
  389. ## default initialization value for type ``B`` is returned (e.g. 0 for any
  390. ## integer type).
  391. getOrDefault(t[], key)
  392. proc getOrDefault*[A, B](t: TableRef[A, B], key: A, default: B): B =
  393. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``.
  394. ## Otherwise, ``default`` is returned.
  395. getOrDefault(t[], key, default)
  396. proc mgetOrPut*[A, B](t: TableRef[A, B], key: A, val: B): var B =
  397. ## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
  398. ## returning a value which can be modified.
  399. t[].mgetOrPut(key, val)
  400. proc hasKeyOrPut*[A, B](t: var TableRef[A, B], key: A, val: B): bool =
  401. ## returns true iff ``key`` is in the table, otherwise inserts ``value``.
  402. t[].hasKeyOrPut(key, val)
  403. proc contains*[A, B](t: TableRef[A, B], key: A): bool =
  404. ## Alias of ``hasKey`` for use with the ``in`` operator.
  405. return hasKey[A, B](t, key)
  406. proc `[]=`*[A, B](t: TableRef[A, B], key: A, val: B) =
  407. ## puts a ``(key, value)`` pair into ``t``.
  408. t[][key] = val
  409. proc add*[A, B](t: TableRef[A, B], key: A, val: B) =
  410. ## puts a new ``(key, value)`` pair into ``t`` even if ``t[key]`` already exists.
  411. ## This can introduce duplicate keys into the table!
  412. t[].add(key, val)
  413. proc del*[A, B](t: TableRef[A, B], key: A) =
  414. ## deletes ``key`` from hash table ``t``. Does nothing if the key does not exist.
  415. t[].del(key)
  416. proc take*[A, B](t: TableRef[A, B], key: A, val: var B): bool =
  417. ## deletes the ``key`` from the table.
  418. ## Returns ``true``, if the ``key`` existed, and sets ``val`` to the
  419. ## mapping of the key. Otherwise, returns ``false``, and the ``val`` is
  420. ## unchanged.
  421. result = t[].take(key, val)
  422. proc newTable*[A, B](initialSize=64): TableRef[A, B] =
  423. new(result)
  424. result[] = initTable[A, B](initialSize)
  425. proc newTable*[A, B](pairs: openArray[(A, B)]): TableRef[A, B] =
  426. ## creates a new hash table that contains the given ``pairs``.
  427. new(result)
  428. result[] = toTable[A, B](pairs)
  429. proc `$`*[A, B](t: TableRef[A, B]): string =
  430. ## The ``$`` operator for hash tables.
  431. dollarImpl()
  432. proc `==`*[A, B](s, t: TableRef[A, B]): bool =
  433. ## The ``==`` operator for hash tables. Returns ``true`` iff either both tables
  434. ## are ``nil`` or none is ``nil`` and the content of both tables contains the
  435. ## same key-value pairs. Insert order does not matter.
  436. if isNil(s): result = isNil(t)
  437. elif isNil(t): result = false
  438. else: equalsImpl(s[], t[])
  439. proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): TableRef[C, B] =
  440. ## Index the collection with the proc provided.
  441. # TODO: As soon as supported, change collection: A to collection: A[B]
  442. result = newTable[C, B]()
  443. for item in collection:
  444. result[index(item)] = item
  445. # ------------------------------ ordered table ------------------------------
  446. type
  447. OrderedKeyValuePair[A, B] = tuple[
  448. hcode: Hash, next: int, key: A, val: B]
  449. OrderedKeyValuePairSeq[A, B] = seq[OrderedKeyValuePair[A, B]]
  450. OrderedTable* [A, B] = object ## table that remembers insertion order
  451. data: OrderedKeyValuePairSeq[A, B]
  452. counter, first, last: int
  453. OrderedTableRef*[A, B] = ref OrderedTable[A, B]
  454. {.deprecated: [TOrderedTable: OrderedTable, POrderedTable: OrderedTableRef].}
  455. proc len*[A, B](t: OrderedTable[A, B]): int {.inline.} =
  456. ## returns the number of keys in ``t``.
  457. result = t.counter
  458. proc clear*[A, B](t: var OrderedTable[A, B]) =
  459. ## resets the table so that it is empty.
  460. clearImpl()
  461. t.first = -1
  462. t.last = -1
  463. proc clear*[A, B](t: var OrderedTableRef[A, B]) =
  464. ## resets the table so that is is empty.
  465. clear(t[])
  466. template forAllOrderedPairs(yieldStmt: untyped): typed {.dirty.} =
  467. var h = t.first
  468. while h >= 0:
  469. var nxt = t.data[h].next
  470. if isFilled(t.data[h].hcode): yieldStmt
  471. h = nxt
  472. iterator pairs*[A, B](t: OrderedTable[A, B]): (A, B) =
  473. ## iterates over any ``(key, value)`` pair in the table ``t`` in insertion
  474. ## order.
  475. forAllOrderedPairs:
  476. yield (t.data[h].key, t.data[h].val)
  477. iterator mpairs*[A, B](t: var OrderedTable[A, B]): (A, var B) =
  478. ## iterates over any ``(key, value)`` pair in the table ``t`` in insertion
  479. ## order. The values can be modified.
  480. forAllOrderedPairs:
  481. yield (t.data[h].key, t.data[h].val)
  482. iterator keys*[A, B](t: OrderedTable[A, B]): A =
  483. ## iterates over any key in the table ``t`` in insertion order.
  484. forAllOrderedPairs:
  485. yield t.data[h].key
  486. iterator values*[A, B](t: OrderedTable[A, B]): B =
  487. ## iterates over any value in the table ``t`` in insertion order.
  488. forAllOrderedPairs:
  489. yield t.data[h].val
  490. iterator mvalues*[A, B](t: var OrderedTable[A, B]): var B =
  491. ## iterates over any value in the table ``t`` in insertion order. The values
  492. ## can be modified.
  493. forAllOrderedPairs:
  494. yield t.data[h].val
  495. proc rawGetKnownHC[A, B](t: OrderedTable[A, B], key: A, hc: Hash): int =
  496. rawGetKnownHCImpl()
  497. proc rawGetDeep[A, B](t: OrderedTable[A, B], key: A, hc: var Hash): int {.inline.} =
  498. rawGetDeepImpl()
  499. proc rawGet[A, B](t: OrderedTable[A, B], key: A, hc: var Hash): int =
  500. rawGetImpl()
  501. proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B {.deprecatedGet.} =
  502. ## retrieves the value at ``t[key]``. If ``key`` is not in ``t``, the
  503. ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
  504. ## the key exists.
  505. get(t, key)
  506. proc `[]`*[A, B](t: var OrderedTable[A, B], key: A): var B{.deprecatedGet.} =
  507. ## retrieves the value at ``t[key]``. The value can be modified.
  508. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  509. get(t, key)
  510. proc mget*[A, B](t: var OrderedTable[A, B], key: A): var B {.deprecated.} =
  511. ## retrieves the value at ``t[key]``. The value can be modified.
  512. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  513. ## Use ``[]`` instead.
  514. get(t, key)
  515. proc getOrDefault*[A, B](t: OrderedTable[A, B], key: A): B =
  516. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, the
  517. ## default initialization value for type ``B`` is returned (e.g. 0 for any
  518. ## integer type).
  519. getOrDefaultImpl(t, key)
  520. proc getOrDefault*[A, B](t: OrderedTable[A, B], key: A, default: B): B =
  521. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise,
  522. ## ``default`` is returned.
  523. getOrDefaultImpl(t, key, default)
  524. proc hasKey*[A, B](t: OrderedTable[A, B], key: A): bool =
  525. ## returns true iff ``key`` is in the table ``t``.
  526. var hc: Hash
  527. result = rawGet(t, key, hc) >= 0
  528. proc contains*[A, B](t: OrderedTable[A, B], key: A): bool =
  529. ## Alias of ``hasKey`` for use with the ``in`` operator.
  530. return hasKey[A, B](t, key)
  531. proc rawInsert[A, B](t: var OrderedTable[A, B],
  532. data: var OrderedKeyValuePairSeq[A, B],
  533. key: A, val: B, hc: Hash, h: Hash) =
  534. rawInsertImpl()
  535. data[h].next = -1
  536. if t.first < 0: t.first = h
  537. if t.last >= 0: data[t.last].next = h
  538. t.last = h
  539. proc enlarge[A, B](t: var OrderedTable[A, B]) =
  540. var n: OrderedKeyValuePairSeq[A, B]
  541. newSeq(n, len(t.data) * growthFactor)
  542. var h = t.first
  543. t.first = -1
  544. t.last = -1
  545. swap(t.data, n)
  546. while h >= 0:
  547. var nxt = n[h].next
  548. let eh = n[h].hcode
  549. if isFilled(eh):
  550. var j: Hash = eh and maxHash(t)
  551. while isFilled(t.data[j].hcode):
  552. j = nextTry(j, maxHash(t))
  553. rawInsert(t, t.data, n[h].key, n[h].val, n[h].hcode, j)
  554. h = nxt
  555. proc `[]=`*[A, B](t: var OrderedTable[A, B], key: A, val: B) =
  556. ## puts a ``(key, value)`` pair into ``t``.
  557. putImpl(enlarge)
  558. proc add*[A, B](t: var OrderedTable[A, B], key: A, val: B) =
  559. ## puts a new ``(key, value)`` pair into ``t`` even if ``t[key]`` already exists.
  560. ## This can introduce duplicate keys into the table!
  561. addImpl(enlarge)
  562. proc mgetOrPut*[A, B](t: var OrderedTable[A, B], key: A, val: B): var B =
  563. ## retrieves value at ``t[key]`` or puts ``value`` if not present, either way
  564. ## returning a value which can be modified.
  565. mgetOrPutImpl(enlarge)
  566. proc hasKeyOrPut*[A, B](t: var OrderedTable[A, B], key: A, val: B): bool =
  567. ## returns true iff ``key`` is in the table, otherwise inserts ``value``.
  568. hasKeyOrPutImpl(enlarge)
  569. proc initOrderedTable*[A, B](initialSize=64): OrderedTable[A, B] =
  570. ## creates a new ordered hash table that is empty.
  571. ##
  572. ## ``initialSize`` needs to be a power of two. If you need to accept runtime
  573. ## values for this you could use the ``nextPowerOfTwo`` proc from the
  574. ## `math <math.html>`_ module or the ``rightSize`` proc from this module.
  575. assert isPowerOfTwo(initialSize)
  576. result.counter = 0
  577. result.first = -1
  578. result.last = -1
  579. newSeq(result.data, initialSize)
  580. proc toOrderedTable*[A, B](pairs: openArray[(A, B)]): OrderedTable[A, B] =
  581. ## creates a new ordered hash table that contains the given ``pairs``.
  582. result = initOrderedTable[A, B](rightSize(pairs.len))
  583. for key, val in items(pairs): result[key] = val
  584. proc `$`*[A, B](t: OrderedTable[A, B]): string =
  585. ## The ``$`` operator for ordered hash tables.
  586. dollarImpl()
  587. proc `==`*[A, B](s, t: OrderedTable[A, B]): bool =
  588. ## The ``==`` operator for ordered hash tables. Returns true iff both the
  589. ## content and the order are equal.
  590. if s.counter != t.counter:
  591. return false
  592. var ht = t.first
  593. var hs = s.first
  594. while ht >= 0 and hs >= 0:
  595. var nxtt = t.data[ht].next
  596. var nxts = s.data[hs].next
  597. if isFilled(t.data[ht].hcode) and isFilled(s.data[hs].hcode):
  598. if (s.data[hs].key != t.data[ht].key) or (s.data[hs].val != t.data[ht].val):
  599. return false
  600. ht = nxtt
  601. hs = nxts
  602. return true
  603. proc sort*[A, B](t: var OrderedTable[A, B], cmp: proc (x,y: (A, B)): int) =
  604. ## sorts ``t`` according to ``cmp``. This modifies the internal list
  605. ## that kept the insertion order, so insertion order is lost after this
  606. ## call but key lookup and insertions remain possible after ``sort`` (in
  607. ## contrast to the ``sort`` for count tables).
  608. var list = t.first
  609. var
  610. p, q, e, tail, oldhead: int
  611. nmerges, psize, qsize, i: int
  612. if t.counter == 0: return
  613. var insize = 1
  614. while true:
  615. p = list; oldhead = list
  616. list = -1; tail = -1; nmerges = 0
  617. while p >= 0:
  618. inc(nmerges)
  619. q = p
  620. psize = 0
  621. i = 0
  622. while i < insize:
  623. inc(psize)
  624. q = t.data[q].next
  625. if q < 0: break
  626. inc(i)
  627. qsize = insize
  628. while psize > 0 or (qsize > 0 and q >= 0):
  629. if psize == 0:
  630. e = q; q = t.data[q].next; dec(qsize)
  631. elif qsize == 0 or q < 0:
  632. e = p; p = t.data[p].next; dec(psize)
  633. elif cmp((t.data[p].key, t.data[p].val),
  634. (t.data[q].key, t.data[q].val)) <= 0:
  635. e = p; p = t.data[p].next; dec(psize)
  636. else:
  637. e = q; q = t.data[q].next; dec(qsize)
  638. if tail >= 0: t.data[tail].next = e
  639. else: list = e
  640. tail = e
  641. p = q
  642. t.data[tail].next = -1
  643. if nmerges <= 1: break
  644. insize = insize * 2
  645. t.first = list
  646. t.last = tail
  647. proc len*[A, B](t: OrderedTableRef[A, B]): int {.inline.} =
  648. ## returns the number of keys in ``t``.
  649. result = t.counter
  650. iterator pairs*[A, B](t: OrderedTableRef[A, B]): (A, B) =
  651. ## iterates over any ``(key, value)`` pair in the table ``t`` in insertion
  652. ## order.
  653. forAllOrderedPairs:
  654. yield (t.data[h].key, t.data[h].val)
  655. iterator mpairs*[A, B](t: OrderedTableRef[A, B]): (A, var B) =
  656. ## iterates over any ``(key, value)`` pair in the table ``t`` in insertion
  657. ## order. The values can be modified.
  658. forAllOrderedPairs:
  659. yield (t.data[h].key, t.data[h].val)
  660. iterator keys*[A, B](t: OrderedTableRef[A, B]): A =
  661. ## iterates over any key in the table ``t`` in insertion order.
  662. forAllOrderedPairs:
  663. yield t.data[h].key
  664. iterator values*[A, B](t: OrderedTableRef[A, B]): B =
  665. ## iterates over any value in the table ``t`` in insertion order.
  666. forAllOrderedPairs:
  667. yield t.data[h].val
  668. iterator mvalues*[A, B](t: OrderedTableRef[A, B]): var B =
  669. ## iterates over any value in the table ``t`` in insertion order. The values
  670. ## can be modified.
  671. forAllOrderedPairs:
  672. yield t.data[h].val
  673. proc `[]`*[A, B](t: OrderedTableRef[A, B], key: A): var B =
  674. ## retrieves the value at ``t[key]``. If ``key`` is not in ``t``, the
  675. ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
  676. ## the key exists.
  677. result = t[][key]
  678. proc mget*[A, B](t: OrderedTableRef[A, B], key: A): var B {.deprecated.} =
  679. ## retrieves the value at ``t[key]``. The value can be modified.
  680. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  681. ## Use ``[]`` instead.
  682. result = t[][key]
  683. proc getOrDefault*[A, B](t: OrderedTableRef[A, B], key: A): B =
  684. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, the
  685. ## default initialization value for type ``B`` is returned (e.g. 0 for any
  686. ## integer type).
  687. getOrDefault(t[], key)
  688. proc getOrDefault*[A, B](t: OrderedTableRef[A, B], key: A, default: B): B =
  689. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise,
  690. ## ``default`` is returned.
  691. getOrDefault(t[], key, default)
  692. proc mgetOrPut*[A, B](t: OrderedTableRef[A, B], key: A, val: B): var B =
  693. ## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
  694. ## returning a value which can be modified.
  695. result = t[].mgetOrPut(key, val)
  696. proc hasKeyOrPut*[A, B](t: var OrderedTableRef[A, B], key: A, val: B): bool =
  697. ## returns true iff ``key`` is in the table, otherwise inserts ``val``.
  698. result = t[].hasKeyOrPut(key, val)
  699. proc hasKey*[A, B](t: OrderedTableRef[A, B], key: A): bool =
  700. ## returns true iff ``key`` is in the table ``t``.
  701. result = t[].hasKey(key)
  702. proc contains*[A, B](t: OrderedTableRef[A, B], key: A): bool =
  703. ## Alias of ``hasKey`` for use with the ``in`` operator.
  704. return hasKey[A, B](t, key)
  705. proc `[]=`*[A, B](t: OrderedTableRef[A, B], key: A, val: B) =
  706. ## puts a ``(key, value)`` pair into ``t``.
  707. t[][key] = val
  708. proc add*[A, B](t: OrderedTableRef[A, B], key: A, val: B) =
  709. ## puts a new ``(key, value)`` pair into ``t`` even if ``t[key]`` already exists.
  710. ## This can introduce duplicate keys into the table!
  711. t[].add(key, val)
  712. proc newOrderedTable*[A, B](initialSize=64): OrderedTableRef[A, B] =
  713. ## creates a new ordered hash table that is empty.
  714. ##
  715. ## ``initialSize`` needs to be a power of two. If you need to accept runtime
  716. ## values for this you could use the ``nextPowerOfTwo`` proc from the
  717. ## `math <math.html>`_ module or the ``rightSize`` proc from this module.
  718. new(result)
  719. result[] = initOrderedTable[A, B](initialSize)
  720. proc newOrderedTable*[A, B](pairs: openArray[(A, B)]): OrderedTableRef[A, B] =
  721. ## creates a new ordered hash table that contains the given ``pairs``.
  722. result = newOrderedTable[A, B](rightSize(pairs.len))
  723. for key, val in items(pairs): result.add(key, val)
  724. proc `$`*[A, B](t: OrderedTableRef[A, B]): string =
  725. ## The ``$`` operator for ordered hash tables.
  726. dollarImpl()
  727. proc `==`*[A, B](s, t: OrderedTableRef[A, B]): bool =
  728. ## The ``==`` operator for ordered hash tables. Returns true iff either both
  729. ## tables are ``nil`` or none is ``nil`` and the content and the order of
  730. ## both are equal.
  731. if isNil(s): result = isNil(t)
  732. elif isNil(t): result = false
  733. else: result = s[] == t[]
  734. proc sort*[A, B](t: OrderedTableRef[A, B], cmp: proc (x,y: (A, B)): int) =
  735. ## sorts ``t`` according to ``cmp``. This modifies the internal list
  736. ## that kept the insertion order, so insertion order is lost after this
  737. ## call but key lookup and insertions remain possible after ``sort`` (in
  738. ## contrast to the ``sort`` for count tables).
  739. t[].sort(cmp)
  740. proc del*[A, B](t: var OrderedTable[A, B], key: A) =
  741. ## deletes ``key`` from ordered hash table ``t``. O(n) complexity. Does nothing
  742. ## if the key does not exist.
  743. var n: OrderedKeyValuePairSeq[A, B]
  744. newSeq(n, len(t.data))
  745. var h = t.first
  746. t.first = -1
  747. t.last = -1
  748. swap(t.data, n)
  749. let hc = genHash(key)
  750. while h >= 0:
  751. var nxt = n[h].next
  752. if isFilled(n[h].hcode):
  753. if n[h].hcode == hc and n[h].key == key:
  754. dec t.counter
  755. else:
  756. var j = -1 - rawGetKnownHC(t, n[h].key, n[h].hcode)
  757. rawInsert(t, t.data, n[h].key, n[h].val, n[h].hcode, j)
  758. h = nxt
  759. proc del*[A, B](t: var OrderedTableRef[A, B], key: A) =
  760. ## deletes ``key`` from ordered hash table ``t``. O(n) complexity. Does nothing
  761. ## if the key does not exist.
  762. t[].del(key)
  763. # ------------------------------ count tables -------------------------------
  764. type
  765. CountTable* [
  766. A] = object ## table that counts the number of each key
  767. data: seq[tuple[key: A, val: int]]
  768. counter: int
  769. CountTableRef*[A] = ref CountTable[A]
  770. {.deprecated: [TCountTable: CountTable, PCountTable: CountTableRef].}
  771. proc len*[A](t: CountTable[A]): int =
  772. ## returns the number of keys in ``t``.
  773. result = t.counter
  774. proc clear*[A](t: CountTableRef[A]) =
  775. ## resets the table so that it is empty.
  776. clearImpl()
  777. proc clear*[A](t: var CountTable[A]) =
  778. ## resets the table so that it is empty.
  779. clearImpl()
  780. iterator pairs*[A](t: CountTable[A]): (A, int) =
  781. ## iterates over any ``(key, value)`` pair in the table ``t``.
  782. for h in 0..high(t.data):
  783. if t.data[h].val != 0: yield (t.data[h].key, t.data[h].val)
  784. iterator mpairs*[A](t: var CountTable[A]): (A, var int) =
  785. ## iterates over any ``(key, value)`` pair in the table ``t``. The values can
  786. ## be modified.
  787. for h in 0..high(t.data):
  788. if t.data[h].val != 0: yield (t.data[h].key, t.data[h].val)
  789. iterator keys*[A](t: CountTable[A]): A =
  790. ## iterates over any key in the table ``t``.
  791. for h in 0..high(t.data):
  792. if t.data[h].val != 0: yield t.data[h].key
  793. iterator values*[A](t: CountTable[A]): int =
  794. ## iterates over any value in the table ``t``.
  795. for h in 0..high(t.data):
  796. if t.data[h].val != 0: yield t.data[h].val
  797. iterator mvalues*[A](t: CountTable[A]): var int =
  798. ## iterates over any value in the table ``t``. The values can be modified.
  799. for h in 0..high(t.data):
  800. if t.data[h].val != 0: yield t.data[h].val
  801. proc rawGet[A](t: CountTable[A], key: A): int =
  802. var h: Hash = hash(key) and high(t.data) # start with real hash value
  803. while t.data[h].val != 0:
  804. if t.data[h].key == key: return h
  805. h = nextTry(h, high(t.data))
  806. result = -1 - h # < 0 => MISSING; insert idx = -1 - result
  807. template ctget(t, key: untyped): untyped =
  808. var index = rawGet(t, key)
  809. if index >= 0: result = t.data[index].val
  810. else:
  811. when compiles($key):
  812. raise newException(KeyError, "key not found: " & $key)
  813. else:
  814. raise newException(KeyError, "key not found")
  815. proc `[]`*[A](t: CountTable[A], key: A): int {.deprecatedGet.} =
  816. ## retrieves the value at ``t[key]``. If ``key`` is not in ``t``,
  817. ## the ``KeyError`` exception is raised. One can check with ``hasKey``
  818. ## whether the key exists.
  819. ctget(t, key)
  820. proc `[]`*[A](t: var CountTable[A], key: A): var int {.deprecatedGet.} =
  821. ## retrieves the value at ``t[key]``. The value can be modified.
  822. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  823. ctget(t, key)
  824. proc mget*[A](t: var CountTable[A], key: A): var int {.deprecated.} =
  825. ## retrieves the value at ``t[key]``. The value can be modified.
  826. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  827. ## Use ``[]`` instead.
  828. ctget(t, key)
  829. proc getOrDefault*[A](t: CountTable[A], key: A): int =
  830. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, 0 (the
  831. ## default initialization value of ``int``), is returned.
  832. var index = rawGet(t, key)
  833. if index >= 0: result = t.data[index].val
  834. proc getOrDefault*[A](t: CountTable[A], key: A, default: int): int =
  835. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, the
  836. ## integer value of ``default`` is returned.
  837. var index = rawGet(t, key)
  838. result = if index >= 0: t.data[index].val else: default
  839. proc hasKey*[A](t: CountTable[A], key: A): bool =
  840. ## returns true iff ``key`` is in the table ``t``.
  841. result = rawGet(t, key) >= 0
  842. proc contains*[A](t: CountTable[A], key: A): bool =
  843. ## Alias of ``hasKey`` for use with the ``in`` operator.
  844. return hasKey[A](t, key)
  845. proc rawInsert[A](t: CountTable[A], data: var seq[tuple[key: A, val: int]],
  846. key: A, val: int) =
  847. var h: Hash = hash(key) and high(data)
  848. while data[h].val != 0: h = nextTry(h, high(data))
  849. data[h].key = key
  850. data[h].val = val
  851. proc enlarge[A](t: var CountTable[A]) =
  852. var n: seq[tuple[key: A, val: int]]
  853. newSeq(n, len(t.data) * growthFactor)
  854. for i in countup(0, high(t.data)):
  855. if t.data[i].val != 0: rawInsert(t, n, t.data[i].key, t.data[i].val)
  856. swap(t.data, n)
  857. proc `[]=`*[A](t: var CountTable[A], key: A, val: int) =
  858. ## puts a ``(key, value)`` pair into ``t``.
  859. assert val >= 0
  860. var h = rawGet(t, key)
  861. if h >= 0:
  862. t.data[h].val = val
  863. else:
  864. if mustRehash(len(t.data), t.counter): enlarge(t)
  865. rawInsert(t, t.data, key, val)
  866. inc(t.counter)
  867. #h = -1 - h
  868. #t.data[h].key = key
  869. #t.data[h].val = val
  870. proc inc*[A](t: var CountTable[A], key: A, val = 1) =
  871. ## increments ``t[key]`` by ``val``.
  872. var index = rawGet(t, key)
  873. if index >= 0:
  874. inc(t.data[index].val, val)
  875. if t.data[index].val == 0: dec(t.counter)
  876. else:
  877. if mustRehash(len(t.data), t.counter): enlarge(t)
  878. rawInsert(t, t.data, key, val)
  879. inc(t.counter)
  880. proc initCountTable*[A](initialSize=64): CountTable[A] =
  881. ## creates a new count table that is empty.
  882. ##
  883. ## ``initialSize`` needs to be a power of two. If you need to accept runtime
  884. ## values for this you could use the ``nextPowerOfTwo`` proc from the
  885. ## `math <math.html>`_ module or the ``rightSize`` proc in this module.
  886. assert isPowerOfTwo(initialSize)
  887. result.counter = 0
  888. newSeq(result.data, initialSize)
  889. proc toCountTable*[A](keys: openArray[A]): CountTable[A] =
  890. ## creates a new count table with every key in ``keys`` having a count
  891. ## of how many times it occurs in ``keys``.
  892. result = initCountTable[A](rightSize(keys.len))
  893. for key in items(keys): result.inc(key)
  894. proc `$`*[A](t: CountTable[A]): string =
  895. ## The ``$`` operator for count tables.
  896. dollarImpl()
  897. proc `==`*[A](s, t: CountTable[A]): bool =
  898. ## The ``==`` operator for count tables. Returns ``true`` iff both tables
  899. ## contain the same keys with the same count. Insert order does not matter.
  900. equalsImpl(s, t)
  901. proc smallest*[A](t: CountTable[A]): tuple[key: A, val: int] =
  902. ## returns the ``(key, value)`` pair with the smallest ``val``. Efficiency: O(n)
  903. assert t.len > 0
  904. var minIdx = -1
  905. for h in 0..high(t.data):
  906. if t.data[h].val > 0 and (minIdx == -1 or t.data[minIdx].val > t.data[h].val):
  907. minIdx = h
  908. result.key = t.data[minIdx].key
  909. result.val = t.data[minIdx].val
  910. proc largest*[A](t: CountTable[A]): tuple[key: A, val: int] =
  911. ## returns the ``(key, value)`` pair with the largest ``val``. Efficiency: O(n)
  912. assert t.len > 0
  913. var maxIdx = 0
  914. for h in 1..high(t.data):
  915. if t.data[maxIdx].val < t.data[h].val: maxIdx = h
  916. result.key = t.data[maxIdx].key
  917. result.val = t.data[maxIdx].val
  918. proc sort*[A](t: var CountTable[A]) =
  919. ## sorts the count table so that the entry with the highest counter comes
  920. ## first. This is destructive! You must not modify ``t`` afterwards!
  921. ## You can use the iterators ``pairs``, ``keys``, and ``values`` to iterate over
  922. ## ``t`` in the sorted order.
  923. # we use shellsort here; fast enough and simple
  924. var h = 1
  925. while true:
  926. h = 3 * h + 1
  927. if h >= high(t.data): break
  928. while true:
  929. h = h div 3
  930. for i in countup(h, high(t.data)):
  931. var j = i
  932. while t.data[j-h].val <= t.data[j].val:
  933. swap(t.data[j], t.data[j-h])
  934. j = j-h
  935. if j < h: break
  936. if h == 1: break
  937. proc len*[A](t: CountTableRef[A]): int =
  938. ## returns the number of keys in ``t``.
  939. result = t.counter
  940. iterator pairs*[A](t: CountTableRef[A]): (A, int) =
  941. ## iterates over any ``(key, value)`` pair in the table ``t``.
  942. for h in 0..high(t.data):
  943. if t.data[h].val != 0: yield (t.data[h].key, t.data[h].val)
  944. iterator mpairs*[A](t: CountTableRef[A]): (A, var int) =
  945. ## iterates over any ``(key, value)`` pair in the table ``t``. The values can
  946. ## be modified.
  947. for h in 0..high(t.data):
  948. if t.data[h].val != 0: yield (t.data[h].key, t.data[h].val)
  949. iterator keys*[A](t: CountTableRef[A]): A =
  950. ## iterates over any key in the table ``t``.
  951. for h in 0..high(t.data):
  952. if t.data[h].val != 0: yield t.data[h].key
  953. iterator values*[A](t: CountTableRef[A]): int =
  954. ## iterates over any value in the table ``t``.
  955. for h in 0..high(t.data):
  956. if t.data[h].val != 0: yield t.data[h].val
  957. iterator mvalues*[A](t: CountTableRef[A]): var int =
  958. ## iterates over any value in the table ``t``. The values can be modified.
  959. for h in 0..high(t.data):
  960. if t.data[h].val != 0: yield t.data[h].val
  961. proc `[]`*[A](t: CountTableRef[A], key: A): var int {.deprecatedGet.} =
  962. ## retrieves the value at ``t[key]``. The value can be modified.
  963. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  964. result = t[][key]
  965. proc mget*[A](t: CountTableRef[A], key: A): var int {.deprecated.} =
  966. ## retrieves the value at ``t[key]``. The value can be modified.
  967. ## If ``key`` is not in ``t``, the ``KeyError`` exception is raised.
  968. ## Use ``[]`` instead.
  969. result = t[][key]
  970. proc getOrDefault*[A](t: CountTableRef[A], key: A): int =
  971. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, 0 (the
  972. ## default initialization value of ``int``), is returned.
  973. result = t[].getOrDefault(key)
  974. proc getOrDefault*[A](t: CountTableRef[A], key: A, default: int): int =
  975. ## retrieves the value at ``t[key]`` iff ``key`` is in ``t``. Otherwise, the
  976. ## integer value of ``default`` is returned.
  977. result = t[].getOrDefault(key, default)
  978. proc hasKey*[A](t: CountTableRef[A], key: A): bool =
  979. ## returns true iff ``key`` is in the table ``t``.
  980. result = t[].hasKey(key)
  981. proc contains*[A](t: CountTableRef[A], key: A): bool =
  982. ## Alias of ``hasKey`` for use with the ``in`` operator.
  983. return hasKey[A](t, key)
  984. proc `[]=`*[A](t: CountTableRef[A], key: A, val: int) =
  985. ## puts a ``(key, value)`` pair into ``t``. ``val`` has to be positive.
  986. assert val > 0
  987. t[][key] = val
  988. proc inc*[A](t: CountTableRef[A], key: A, val = 1) =
  989. ## increments ``t[key]`` by ``val``.
  990. t[].inc(key, val)
  991. proc newCountTable*[A](initialSize=64): CountTableRef[A] =
  992. ## creates a new count table that is empty.
  993. ##
  994. ## ``initialSize`` needs to be a power of two. If you need to accept runtime
  995. ## values for this you could use the ``nextPowerOfTwo`` proc from the
  996. ## `math <math.html>`_ module or the ``rightSize`` method in this module.
  997. new(result)
  998. result[] = initCountTable[A](initialSize)
  999. proc newCountTable*[A](keys: openArray[A]): CountTableRef[A] =
  1000. ## creates a new count table with every key in ``keys`` having a count
  1001. ## of how many times it occurs in ``keys``.
  1002. result = newCountTable[A](rightSize(keys.len))
  1003. for key in items(keys): result.inc(key)
  1004. proc `$`*[A](t: CountTableRef[A]): string =
  1005. ## The ``$`` operator for count tables.
  1006. dollarImpl()
  1007. proc `==`*[A](s, t: CountTableRef[A]): bool =
  1008. ## The ``==`` operator for count tables. Returns ``true`` iff either both tables
  1009. ## are ``nil`` or none is ``nil`` and both contain the same keys with the same
  1010. ## count. Insert order does not matter.
  1011. if isNil(s): result = isNil(t)
  1012. elif isNil(t): result = false
  1013. else: result = s[] == t[]
  1014. proc smallest*[A](t: CountTableRef[A]): (A, int) =
  1015. ## returns the ``(key, value)`` pair with the smallest ``val``. Efficiency: O(n)
  1016. t[].smallest
  1017. proc largest*[A](t: CountTableRef[A]): (A, int) =
  1018. ## returns the ``(key, value)`` pair with the largest ``val``. Efficiency: O(n)
  1019. t[].largest
  1020. proc sort*[A](t: CountTableRef[A]) =
  1021. ## sorts the count table so that the entry with the highest counter comes
  1022. ## first. This is destructive! You must not modify ``t`` afterwards!
  1023. ## You can use the iterators ``pairs``, ``keys``, and ``values`` to iterate over
  1024. ## ``t`` in the sorted order.
  1025. t[].sort
  1026. proc merge*[A](s: var CountTable[A], t: CountTable[A]) =
  1027. ## merges the second table into the first one.
  1028. for key, value in t:
  1029. s.inc(key, value)
  1030. proc merge*[A](s, t: CountTable[A]): CountTable[A] =
  1031. ## merges the two tables into a new one.
  1032. result = initCountTable[A](nextPowerOfTwo(max(s.len, t.len)))
  1033. for table in @[s, t]:
  1034. for key, value in table:
  1035. result.inc(key, value)
  1036. proc merge*[A](s, t: CountTableRef[A]) =
  1037. ## merges the second table into the first one.
  1038. s[].merge(t[])
  1039. when isMainModule:
  1040. type
  1041. Person = object
  1042. firstName, lastName: string
  1043. proc hash(x: Person): Hash =
  1044. ## Piggyback on the already available string hash proc.
  1045. ##
  1046. ## Without this proc nothing works!
  1047. result = x.firstName.hash !& x.lastName.hash
  1048. result = !$result
  1049. var
  1050. salaries = initTable[Person, int]()
  1051. p1, p2: Person
  1052. p1.firstName = "Jon"
  1053. p1.lastName = "Ross"
  1054. salaries[p1] = 30_000
  1055. p2.firstName = "소진"
  1056. p2.lastName = "박"
  1057. salaries[p2] = 45_000
  1058. var
  1059. s2 = initOrderedTable[Person, int]()
  1060. s3 = initCountTable[Person]()
  1061. s2[p1] = 30_000
  1062. s2[p2] = 45_000
  1063. s3[p1] = 30_000
  1064. s3[p2] = 45_000
  1065. block: # Ordered table should preserve order after deletion
  1066. var
  1067. s4 = initOrderedTable[int, int]()
  1068. s4[1] = 1
  1069. s4[2] = 2
  1070. s4[3] = 3
  1071. var prev = 0
  1072. for i in s4.values:
  1073. doAssert(prev < i)
  1074. prev = i
  1075. s4.del(2)
  1076. doAssert(2 notin s4)
  1077. doAssert(s4.len == 2)
  1078. prev = 0
  1079. for i in s4.values:
  1080. doAssert(prev < i)
  1081. prev = i
  1082. block: # Deletion from OrderedTable should account for collision groups. See issue #5057.
  1083. # The bug is reproducible only with exact keys
  1084. const key1 = "boy_jackpot.inGamma"
  1085. const key2 = "boy_jackpot.outBlack"
  1086. var t = {
  1087. key1: 0,
  1088. key2: 0
  1089. }.toOrderedTable()
  1090. t.del(key1)
  1091. assert(t.len == 1)
  1092. assert(key2 in t)
  1093. var
  1094. t1 = initCountTable[string]()
  1095. t2 = initCountTable[string]()
  1096. t1.inc("foo")
  1097. t1.inc("bar", 2)
  1098. t1.inc("baz", 3)
  1099. t2.inc("foo", 4)
  1100. t2.inc("bar")
  1101. t2.inc("baz", 11)
  1102. merge(t1, t2)
  1103. assert(t1["foo"] == 5)
  1104. assert(t1["bar"] == 3)
  1105. assert(t1["baz"] == 14)
  1106. let
  1107. t1r = newCountTable[string]()
  1108. t2r = newCountTable[string]()
  1109. t1r.inc("foo")
  1110. t1r.inc("bar", 2)
  1111. t1r.inc("baz", 3)
  1112. t2r.inc("foo", 4)
  1113. t2r.inc("bar")
  1114. t2r.inc("baz", 11)
  1115. merge(t1r, t2r)
  1116. assert(t1r["foo"] == 5)
  1117. assert(t1r["bar"] == 3)
  1118. assert(t1r["baz"] == 14)
  1119. var
  1120. t1l = initCountTable[string]()
  1121. t2l = initCountTable[string]()
  1122. t1l.inc("foo")
  1123. t1l.inc("bar", 2)
  1124. t1l.inc("baz", 3)
  1125. t2l.inc("foo", 4)
  1126. t2l.inc("bar")
  1127. t2l.inc("baz", 11)
  1128. let
  1129. t1merging = t1l
  1130. t2merging = t2l
  1131. let merged = merge(t1merging, t2merging)
  1132. assert(merged["foo"] == 5)
  1133. assert(merged["bar"] == 3)
  1134. assert(merged["baz"] == 14)
  1135. block:
  1136. const testKey = "TESTKEY"
  1137. let t: CountTableRef[string] = newCountTable[string]()
  1138. # Before, does not compile with error message:
  1139. #test_counttable.nim(7, 43) template/generic instantiation from here
  1140. #lib/pure/collections/tables.nim(117, 21) template/generic instantiation from here
  1141. #lib/pure/collections/tableimpl.nim(32, 27) Error: undeclared field: 'hcode
  1142. doAssert 0 == t.getOrDefault(testKey)
  1143. t.inc(testKey, 3)
  1144. doAssert 3 == t.getOrDefault(testKey)
  1145. block:
  1146. # Clear tests
  1147. var clearTable = newTable[int, string]()
  1148. clearTable[42] = "asd"
  1149. clearTable[123123] = "piuyqwb "
  1150. doAssert clearTable[42] == "asd"
  1151. clearTable.clear()
  1152. doAssert(not clearTable.hasKey(123123))
  1153. doAssert clearTable.getOrDefault(42) == ""
  1154. block: #5482
  1155. var a = [("wrong?","foo"), ("wrong?", "foo2")].newOrderedTable()
  1156. var b = newOrderedTable[string, string](initialSize=2)
  1157. b.add("wrong?", "foo")
  1158. b.add("wrong?", "foo2")
  1159. assert a == b
  1160. block: #5482
  1161. var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
  1162. var b = newOrderedTable[string, string](initialSize=2)
  1163. b.add("wrong?", "foo")
  1164. b.add("wrong?", "foo2")
  1165. assert a == b
  1166. block: #5487
  1167. var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
  1168. var b = newOrderedTable[string, string]() # notice, default size!
  1169. b.add("wrong?", "foo")
  1170. b.add("wrong?", "foo2")
  1171. assert a == b
  1172. block: #5487
  1173. var a = [("wrong?","foo"), ("wrong?", "foo2")].newOrderedTable()
  1174. var b = newOrderedTable[string, string]() # notice, default size!
  1175. b.add("wrong?", "foo")
  1176. b.add("wrong?", "foo2")
  1177. assert a == b
  1178. block:
  1179. var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
  1180. var b = [("wrong?","foo"), ("wrong?", "foo2")].newOrderedTable()
  1181. var c = newOrderedTable[string, string]() # notice, default size!
  1182. c.add("wrong?", "foo")
  1183. c.add("wrong?", "foo2")
  1184. assert a == b
  1185. assert a == c
  1186. block: #6250
  1187. let
  1188. a = {3: 1}.toOrderedTable
  1189. b = {3: 2}.toOrderedTable
  1190. assert((a == b) == false)
  1191. assert((b == a) == false)
  1192. block: #6250
  1193. let
  1194. a = {3: 2}.toOrderedTable
  1195. b = {3: 2}.toOrderedTable
  1196. assert((a == b) == true)
  1197. assert((b == a) == true)
  1198. block: # CountTable.smallest
  1199. let t = toCountTable([0, 0, 5, 5, 5])
  1200. doAssert t.smallest == (0, 2)
  1201. block:
  1202. var tp: Table[string, string] = initTable[string, string]()
  1203. doAssert "test1" == tp.getOrDefault("test1", "test1")
  1204. tp["test2"] = "test2"
  1205. doAssert "test2" == tp.getOrDefault("test2", "test1")
  1206. var tr: TableRef[string, string] = newTable[string, string]()
  1207. doAssert "test1" == tr.getOrDefault("test1", "test1")
  1208. tr["test2"] = "test2"
  1209. doAssert "test2" == tr.getOrDefault("test2", "test1")
  1210. var op: OrderedTable[string, string] = initOrderedTable[string, string]()
  1211. doAssert "test1" == op.getOrDefault("test1", "test1")
  1212. op["test2"] = "test2"
  1213. doAssert "test2" == op.getOrDefault("test2", "test1")
  1214. var orf: OrderedTableRef[string, string] = newOrderedTable[string, string]()
  1215. doAssert "test1" == orf.getOrDefault("test1", "test1")
  1216. orf["test2"] = "test2"
  1217. doAssert "test2" == orf.getOrDefault("test2", "test1")