- Table implemented as optimized sorted hashed dictionary of {array[char]: Option[T]}, same size and API as a Table, 0 dependencies, ~300 lines.
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/tables --import:std/json
import src/dik doAssert sizeof(newDik[string]()) == sizeof(initOrderedTable[string, string]()) ## Sanity check, ignore. doAssert {"key": %*{"foo": 42, "bar": 3.14, "baz": true}}.toDik is Dik[JsonNode] ## Heterogeneous values.
Types
Dik[T] = object allocated, cap, len: uint32 ## `cap` and `len` are similar to `string` implementation. items: seq[Item[T]] ## `seq` of `Option[T]`. indices: Indices ## `pointer`
Procs
func `$`[T](self: Dik[T]; raw: static[bool] = false): string
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
doAssert $toDik({"key": 666, "other": 42}) == """{"key":666,"other":42}""" discard `$`(toDik({"key": 666, "other": 42}), raw = true)
func pretty[T](self: Dik[T]; raw: static[bool] = false): string
func toCsv[T](self: Dik[T]; raw: static[bool] = false): string
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
doAssert {"key": 666, "other": 42}.toDik.toCsv == "\"key\",\"other\"\n\"666\",\"42\"\n"
proc clear[T](self: var Dik[T]) {.inline.}
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
var dict: Dik[int] = {"key": 666, "other": 42}.toDik dict.clear() doAssert $dict == "{:}" and dict.len == 0
proc `=destroy`[T](self: var Dik[T])
func `==`[T](lhs, rhs: Dik[T]): bool
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
let a = {"foo": 1, "bar": 2}.toDik let b = {"foo": 1, "bar": 2}.toDik var c = {"foo": 1, "OwO": 666, "bar": 2}.toDik doAssert a == b doAssert a != c c.del "OwO" doAssert a == c
proc resized[T](self: var Dik[T]; newSize: Positive)
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
var dict: Dik[int] = {"key": 666, "other": 42}.toDik doAssert dict.cap == 2 ## Capacity is "Read-Only". dict.resized(8) doAssert dict.cap == 10 ## (2 + 8) == 10
proc newDik[T](): Dik[T] {.inline.}
proc newDikOfCap[T](capacity: Positive): Dik[T] {.inline.}
proc toDik[T](items: openArray[(string, T)]): Dik[T]
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
doAssert {"key": 666, "other": 42}.toDik is Dik[int]
func contains[T](self: Dik[T]; key: string or array[16, char]): bool
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
doAssert {"key": "value"}.toDik.contains "key"
func del[T](self: var Dik[T]; key: string or array[16, char])
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
var dict: Dik[string] = {"key": "value"}.toDik dict.del "key" doAssert dict.len == 0
proc add[T](self: var Dik[T]; key: string or array[16, char]; val: T or Option[T])
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
var dict: Dik[string] = {"key": "value"}.toDik dict.add "other", "value" doAssert "other" in dict dict.add "another", some "value" doAssert "another" in dict dict.add "duplicated", "0" dict.add "duplicated", "1" dict.add "duplicated", "2" doAssert $dict == """{"key":value,"other":value,"another":value,"duplicated":2}"""
func get[T](self: Dik[T]; key: string or array[16, char]): Option[T]
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
var dict: Dik[string] = {"key": "X"}.toDik doAssert dict.get"key" is Option[string] and dict.get"key".isSome and dict.get"key".get == "X"
func toSeq[T](self: Dik[T]): seq[Option[T]]
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
doAssert {"a": 0, "b": 1, "c": 2}.toDik.toSeq is seq[Option[int]]
proc `[]`[T](self: Dik[T]; slice: Slice[int]): seq[Option[T]] {.noinit.}
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
let dict = {"a": 0, "b": 1, "c": 2, "d": 3}.toDik doAssert dict[1..2] == @[some(1), some(2)]
func get[T](self: Dik[T]; value: Option[T] or T): seq[string]
-
Get keys by value, like a backwards get without reversing the dictionary.
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
doAssert {"a": 0, "b": 1, "c": 0}.toDik.get(some 0) == @["a", "c"] doAssert {"a": 0, "b": 1, "c": 0}.toDik.get( 0 ) == @["a", "c"]
Iterators
iterator enumerated[T](self: Dik[T]; raw: static[bool] = false): auto
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
for (index, key, value) in {"key": "value"}.toDik.enumerated: doAssert index == 0 and key == "key" and value.get == "value"
iterator pairs[T](self: Dik[T]; raw: static[bool] = false): auto
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
for (key, value) in {"key": "value"}.toDik.pairs: doAssert key == "key" and value.get == "value"
iterator keys[T](self: Dik[T]; raw: static[bool] = false): string or array[16, char]
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error
for key in {"key": "value"}.toDik.keys: doAssert key == "key"
iterator values[T](self: Dik[T]): Option[T]
-
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
for value in {"key": "value"}.toDik.values: doAssert value.get == "value"
Templates
template `[]=`[T](self: var Dik[T]; key: string or array[16, char]; val: T or Option[T])
- Alias for Dik.add.
template `[]`[T](self: Dik[T]; key: string or array[16, char]): Option[T]
- Alias for Dik.get.
template `[]`[T](self: Dik[T]; index: SomeInteger or BackwardsIndex): Option[T]
-
Get items by index or backwards index, you can use the dictionary as if it was an array.
Example: cmd: --gc:arc --experimental:strictFuncs --styleCheck:error --import:std/options
let dict = {"a": 0, "b": 1, "c": 2}.toDik doAssert dict[1] == some 1 doAssert dict[^1] == some 2 doAssert dict[uint8(2.0)] == some 2
template len(self: Dik): int
template cap(self: Dik): int
template `[]`(s: Indices; i: int): int
-
Warning: DO NOT USE.
template `[]=`(s: Indices; i: int; val: int)
-
Warning: DO NOT USE.