[other] prettify collections (#11695)
This commit is contained in:
parent
55e8aefbea
commit
2255d8795b
14 changed files with 209 additions and 186 deletions
|
|
@ -177,7 +177,8 @@ proc resize[K,V](self: PConcTable[K,V]): PConcTable[K,V] =
|
||||||
success = atomic_compare_exchange_n(self.next.addr, next.addr, newTable,
|
success = atomic_compare_exchange_n(self.next.addr, next.addr, newTable,
|
||||||
false, ATOMIC_RELAXED, ATOMIC_RELAXED)
|
false, ATOMIC_RELAXED, ATOMIC_RELAXED)
|
||||||
if not success:
|
if not success:
|
||||||
echo("someone beat us to it! delete table we just created and return his " & $cast[int](next))
|
echo("someone beat us to it! delete table we just created and return his " &
|
||||||
|
$cast[int](next))
|
||||||
deleteConcTable(newTable)
|
deleteConcTable(newTable)
|
||||||
return next
|
return next
|
||||||
else:
|
else:
|
||||||
|
|
@ -209,7 +210,8 @@ proc keyEQ[K](key1: int, key2: int): bool {.inline.} =
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
proc copySlot[K,V](idx: int, oldTbl: var PConcTable[K,V], newTbl: var PConcTable[K,V]): bool =
|
proc copySlot[K, V](idx: int, oldTbl: var PConcTable[K, V],
|
||||||
|
newTbl: var PConcTable[K, V]): bool =
|
||||||
#echo("Copy idx " & $idx)
|
#echo("Copy idx " & $idx)
|
||||||
var
|
var
|
||||||
oldVal = 0
|
oldVal = 0
|
||||||
|
|
@ -297,7 +299,8 @@ proc checkAndPromote[K,V](table: var PConcTable[K,V], workDone: int): bool =
|
||||||
proc copySlotAndCheck[K, V](table: var PConcTable[K, V], idx: int):
|
proc copySlotAndCheck[K, V](table: var PConcTable[K, V], idx: int):
|
||||||
PConcTable[K, V] =
|
PConcTable[K, V] =
|
||||||
var
|
var
|
||||||
newTable = cast[PConcTable[K,V]](atomic_load_n(table.next.addr, ATOMIC_RELAXED))
|
newTable = cast[PConcTable[K, V]](atomic_load_n(table.next.addr,
|
||||||
|
ATOMIC_RELAXED))
|
||||||
result = newTable
|
result = newTable
|
||||||
if newTable != nil and copySlot(idx, table, newTable):
|
if newTable != nil and copySlot(idx, table, newTable):
|
||||||
#echo("copied a single slot, idx = " & $idx)
|
#echo("copied a single slot, idx = " & $idx)
|
||||||
|
|
@ -308,7 +311,8 @@ proc copySlotAndCheck[K,V](table: var PConcTable[K,V], idx: int):
|
||||||
|
|
||||||
proc helpCopy[K, V](table: var PConcTable[K, V]): PConcTable[K, V] =
|
proc helpCopy[K, V](table: var PConcTable[K, V]): PConcTable[K, V] =
|
||||||
var
|
var
|
||||||
newTable = cast[PConcTable[K,V]](atomic_load_n(table.next.addr, ATOMIC_RELAXED))
|
newTable = cast[PConcTable[K, V]](atomic_load_n(table.next.addr,
|
||||||
|
ATOMIC_RELAXED))
|
||||||
result = newTable
|
result = newTable
|
||||||
if newTable != nil:
|
if newTable != nil:
|
||||||
var
|
var
|
||||||
|
|
|
||||||
|
|
@ -237,7 +237,8 @@ iterator mpairs*[T](c: var CritBitTree[T]): tuple[key: string, val: var T] =
|
||||||
## yields all (key, value)-pairs of `c`. The yielded values can be modified.
|
## yields all (key, value)-pairs of `c`. The yielded values can be modified.
|
||||||
for x in leaves(c.root): yield (x.key, x.val)
|
for x in leaves(c.root): yield (x.key, x.val)
|
||||||
|
|
||||||
proc allprefixedAux[T](c: CritBitTree[T], key: string; longestMatch: bool): Node[T] =
|
proc allprefixedAux[T](c: CritBitTree[T], key: string;
|
||||||
|
longestMatch: bool): Node[T] =
|
||||||
var p = c.root
|
var p = c.root
|
||||||
var top = p
|
var top = p
|
||||||
if p != nil:
|
if p != nil:
|
||||||
|
|
|
||||||
|
|
@ -186,7 +186,8 @@ proc contains*(s: IntSet, key: int): bool =
|
||||||
var t = intSetGet(s, `shr`(key, TrunkShift))
|
var t = intSetGet(s, `shr`(key, TrunkShift))
|
||||||
if t != nil:
|
if t != nil:
|
||||||
var u = key and TrunkMask
|
var u = key and TrunkMask
|
||||||
result = (t.bits[u shr IntShift] and (BitScalar(1) shl (u and IntMask))) != 0
|
result = (t.bits[u shr IntShift] and
|
||||||
|
(BitScalar(1) shl (u and IntMask))) != 0
|
||||||
else:
|
else:
|
||||||
result = false
|
result = false
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,8 @@ when not defined(nimhygiene):
|
||||||
{.pragma: dirty.}
|
{.pragma: dirty.}
|
||||||
|
|
||||||
type
|
type
|
||||||
DoublyLinkedNodeObj*[T] = object ## A node a doubly linked list consists of.
|
DoublyLinkedNodeObj*[T] = object ## \
|
||||||
|
## A node a doubly linked list consists of.
|
||||||
##
|
##
|
||||||
## It consists of a `value` field, and pointers to `next` and `prev`.
|
## It consists of a `value` field, and pointers to `next` and `prev`.
|
||||||
next*: <//>(ref DoublyLinkedNodeObj[T])
|
next*: <//>(ref DoublyLinkedNodeObj[T])
|
||||||
|
|
@ -85,35 +86,40 @@ type
|
||||||
value*: T
|
value*: T
|
||||||
DoublyLinkedNode*[T] = ref DoublyLinkedNodeObj[T]
|
DoublyLinkedNode*[T] = ref DoublyLinkedNodeObj[T]
|
||||||
|
|
||||||
SinglyLinkedNodeObj*[T] = object ## A node a singly linked list consists of.
|
SinglyLinkedNodeObj*[T] = object ## \
|
||||||
|
## A node a singly linked list consists of.
|
||||||
##
|
##
|
||||||
## It consists of a `value` field, and a pointer to `next`.
|
## It consists of a `value` field, and a pointer to `next`.
|
||||||
next*: <//>(ref SinglyLinkedNodeObj[T])
|
next*: <//>(ref SinglyLinkedNodeObj[T])
|
||||||
value*: T
|
value*: T
|
||||||
SinglyLinkedNode*[T] = ref SinglyLinkedNodeObj[T]
|
SinglyLinkedNode*[T] = ref SinglyLinkedNodeObj[T]
|
||||||
|
|
||||||
SinglyLinkedList*[T] = object ## A singly linked list.
|
SinglyLinkedList*[T] = object ## \
|
||||||
|
## A singly linked list.
|
||||||
##
|
##
|
||||||
## Use `initSinglyLinkedList proc <#initSinglyLinkedList>`_ to create
|
## Use `initSinglyLinkedList proc <#initSinglyLinkedList>`_ to create
|
||||||
## a new empty list.
|
## a new empty list.
|
||||||
head*: <//>(SinglyLinkedNode[T])
|
head*: <//>(SinglyLinkedNode[T])
|
||||||
tail*: SinglyLinkedNode[T]
|
tail*: SinglyLinkedNode[T]
|
||||||
|
|
||||||
DoublyLinkedList*[T] = object ## A doubly linked list.
|
DoublyLinkedList*[T] = object ## \
|
||||||
|
## A doubly linked list.
|
||||||
##
|
##
|
||||||
## Use `initDoublyLinkedList proc <#initDoublyLinkedList>`_ to create
|
## Use `initDoublyLinkedList proc <#initDoublyLinkedList>`_ to create
|
||||||
## a new empty list.
|
## a new empty list.
|
||||||
head*: <//>(DoublyLinkedNode[T])
|
head*: <//>(DoublyLinkedNode[T])
|
||||||
tail*: DoublyLinkedNode[T]
|
tail*: DoublyLinkedNode[T]
|
||||||
|
|
||||||
SinglyLinkedRing*[T] = object ## A singly linked ring.
|
SinglyLinkedRing*[T] = object ## \
|
||||||
|
## A singly linked ring.
|
||||||
##
|
##
|
||||||
## Use `initSinglyLinkedRing proc <#initSinglyLinkedRing>`_ to create
|
## Use `initSinglyLinkedRing proc <#initSinglyLinkedRing>`_ to create
|
||||||
## a new empty ring.
|
## a new empty ring.
|
||||||
head*: <//>(SinglyLinkedNode[T])
|
head*: <//>(SinglyLinkedNode[T])
|
||||||
tail*: SinglyLinkedNode[T]
|
tail*: SinglyLinkedNode[T]
|
||||||
|
|
||||||
DoublyLinkedRing*[T] = object ## A doubly linked ring.
|
DoublyLinkedRing*[T] = object ## \
|
||||||
|
## A doubly linked ring.
|
||||||
##
|
##
|
||||||
## Use `initDoublyLinkedRing proc <#initDoublyLinkedRing>`_ to create
|
## Use `initDoublyLinkedRing proc <#initDoublyLinkedRing>`_ to create
|
||||||
## a new empty ring.
|
## a new empty ring.
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,8 @@ when not defined(nimhygiene):
|
||||||
{.pragma: dirty.}
|
{.pragma: dirty.}
|
||||||
|
|
||||||
|
|
||||||
macro evalOnceAs(expAlias, exp: untyped, letAssigneable: static[bool]): untyped =
|
macro evalOnceAs(expAlias, exp: untyped,
|
||||||
|
letAssigneable: static[bool]): untyped =
|
||||||
## Injects ``expAlias`` in caller scope, to avoid bugs involving multiple
|
## Injects ``expAlias`` in caller scope, to avoid bugs involving multiple
|
||||||
## substitution in macro arguments such as
|
## substitution in macro arguments such as
|
||||||
## https://github.com/nim-lang/Nim/issues/7187
|
## https://github.com/nim-lang/Nim/issues/7187
|
||||||
|
|
@ -1331,8 +1332,10 @@ when isMainModule:
|
||||||
block: # mapLiterals tests
|
block: # mapLiterals tests
|
||||||
let x = mapLiterals([0.1, 1.2, 2.3, 3.4], int)
|
let x = mapLiterals([0.1, 1.2, 2.3, 3.4], int)
|
||||||
doAssert x is array[4, int]
|
doAssert x is array[4, int]
|
||||||
doAssert mapLiterals((1, ("abc"), 2), float, nested=false) == (float(1), "abc", float(2))
|
doAssert mapLiterals((1, ("abc"), 2), float, nested = false) ==
|
||||||
doAssert mapLiterals(([1], ("abc"), 2), `$`, nested=true) == (["1"], "abc", "2")
|
(float(1), "abc", float(2))
|
||||||
|
doAssert mapLiterals(([1], ("abc"), 2), `$`, nested = true) ==
|
||||||
|
(["1"], "abc", "2")
|
||||||
|
|
||||||
block: # mapIt with openArray
|
block: # mapIt with openArray
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
|
||||||
|
|
@ -604,13 +604,13 @@ proc rightSize*(count: Natural): int {.inline.} =
|
||||||
|
|
||||||
|
|
||||||
proc initSet*[A](initialSize = defaultInitialSize): HashSet[A] {.deprecated:
|
proc initSet*[A](initialSize = defaultInitialSize): HashSet[A] {.deprecated:
|
||||||
"Deprecated since v0.20, use 'initHashSet'"} = initHashSet[A](initialSize)
|
"Deprecated since v0.20, use 'initHashSet'".} = initHashSet[A](initialSize)
|
||||||
|
|
||||||
proc toSet*[A](keys: openArray[A]): HashSet[A] {.deprecated:
|
proc toSet*[A](keys: openArray[A]): HashSet[A] {.deprecated:
|
||||||
"Deprecated since v0.20, use 'toHashSet'"} = toHashSet[A](keys)
|
"Deprecated since v0.20, use 'toHashSet'".} = toHashSet[A](keys)
|
||||||
|
|
||||||
proc isValid*[A](s: HashSet[A]): bool {.deprecated:
|
proc isValid*[A](s: HashSet[A]): bool {.deprecated:
|
||||||
"Deprecated since v0.20; sets are initialized by default"} =
|
"Deprecated since v0.20; sets are initialized by default".} =
|
||||||
## Returns `true` if the set has been initialized (with `initHashSet proc
|
## Returns `true` if the set has been initialized (with `initHashSet proc
|
||||||
## <#initHashSet,int>`_ or `init proc <#init,HashSet[A],int>`_).
|
## <#initHashSet,int>`_ or `init proc <#init,HashSet[A],int>`_).
|
||||||
##
|
##
|
||||||
|
|
@ -937,7 +937,7 @@ iterator pairs*[A](s: OrderedSet[A]): tuple[a: int, b: A] =
|
||||||
|
|
||||||
|
|
||||||
proc isValid*[A](s: OrderedSet[A]): bool {.deprecated:
|
proc isValid*[A](s: OrderedSet[A]): bool {.deprecated:
|
||||||
"Deprecated since v0.20; sets are initialized by default"} =
|
"Deprecated since v0.20; sets are initialized by default".} =
|
||||||
##
|
##
|
||||||
## Returns `true` if the set has been initialized (with `initHashSet proc
|
## Returns `true` if the set has been initialized (with `initHashSet proc
|
||||||
## <#initOrderedSet,int>`_ or `init proc <#init,OrderedSet[A],int>`_).
|
## <#initOrderedSet,int>`_ or `init proc <#init,OrderedSet[A],int>`_).
|
||||||
|
|
|
||||||
|
|
@ -1527,7 +1527,8 @@ proc clear*[A, B](t: var OrderedTable[A, B]) =
|
||||||
t.first = -1
|
t.first = -1
|
||||||
t.last = -1
|
t.last = -1
|
||||||
|
|
||||||
proc sort*[A, B](t: var OrderedTable[A, B], cmp: proc (x,y: (A, B)): int, order = SortOrder.Ascending) =
|
proc sort*[A, B](t: var OrderedTable[A, B], cmp: proc (x, y: (A, B)): int,
|
||||||
|
order = SortOrder.Ascending) =
|
||||||
## Sorts ``t`` according to the function ``cmp``.
|
## Sorts ``t`` according to the function ``cmp``.
|
||||||
##
|
##
|
||||||
## This modifies the internal list
|
## This modifies the internal list
|
||||||
|
|
@ -1660,7 +1661,8 @@ iterator mpairs*[A, B](t: var OrderedTable[A, B]): (A, var B) =
|
||||||
}.toOrderedTable
|
}.toOrderedTable
|
||||||
for k, v in a.mpairs:
|
for k, v in a.mpairs:
|
||||||
v.add(v[0] + 10)
|
v.add(v[0] + 10)
|
||||||
doAssert a == {'o': @[1, 5, 7, 9, 11], 'e': @[2, 4, 6, 8, 12]}.toOrderedTable
|
doAssert a == {'o': @[1, 5, 7, 9, 11],
|
||||||
|
'e': @[2, 4, 6, 8, 12]}.toOrderedTable
|
||||||
|
|
||||||
let L = len(t)
|
let L = len(t)
|
||||||
forAllOrderedPairs:
|
forAllOrderedPairs:
|
||||||
|
|
@ -1680,7 +1682,8 @@ iterator keys*[A, B](t: OrderedTable[A, B]): A =
|
||||||
}.toOrderedTable
|
}.toOrderedTable
|
||||||
for k in a.keys:
|
for k in a.keys:
|
||||||
a[k].add(99)
|
a[k].add(99)
|
||||||
doAssert a == {'o': @[1, 5, 7, 9, 99], 'e': @[2, 4, 6, 8, 99]}.toOrderedTable
|
doAssert a == {'o': @[1, 5, 7, 9, 99],
|
||||||
|
'e': @[2, 4, 6, 8, 99]}.toOrderedTable
|
||||||
|
|
||||||
let L = len(t)
|
let L = len(t)
|
||||||
forAllOrderedPairs:
|
forAllOrderedPairs:
|
||||||
|
|
@ -1722,7 +1725,8 @@ iterator mvalues*[A, B](t: var OrderedTable[A, B]): var B =
|
||||||
}.toOrderedTable
|
}.toOrderedTable
|
||||||
for v in a.mvalues:
|
for v in a.mvalues:
|
||||||
v.add(99)
|
v.add(99)
|
||||||
doAssert a == {'o': @[1, 5, 7, 9, 99], 'e': @[2, 4, 6, 8, 99]}.toOrderedTable
|
doAssert a == {'o': @[1, 5, 7, 9, 99],
|
||||||
|
'e': @[2, 4, 6, 8, 99]}.toOrderedTable
|
||||||
|
|
||||||
let L = len(t)
|
let L = len(t)
|
||||||
forAllOrderedPairs:
|
forAllOrderedPairs:
|
||||||
|
|
@ -1964,7 +1968,8 @@ proc clear*[A, B](t: var OrderedTableRef[A, B]) =
|
||||||
|
|
||||||
clear(t[])
|
clear(t[])
|
||||||
|
|
||||||
proc sort*[A, B](t: OrderedTableRef[A, B], cmp: proc (x,y: (A, B)): int, order = SortOrder.Ascending) =
|
proc sort*[A, B](t: OrderedTableRef[A, B], cmp: proc (x, y: (A, B)): int,
|
||||||
|
order = SortOrder.Ascending) =
|
||||||
## Sorts ``t`` according to the function ``cmp``.
|
## Sorts ``t`` according to the function ``cmp``.
|
||||||
##
|
##
|
||||||
## This modifies the internal list
|
## This modifies the internal list
|
||||||
|
|
@ -2050,7 +2055,8 @@ iterator mpairs*[A, B](t: OrderedTableRef[A, B]): (A, var B) =
|
||||||
}.newOrderedTable
|
}.newOrderedTable
|
||||||
for k, v in a.mpairs:
|
for k, v in a.mpairs:
|
||||||
v.add(v[0] + 10)
|
v.add(v[0] + 10)
|
||||||
doAssert a == {'o': @[1, 5, 7, 9, 11], 'e': @[2, 4, 6, 8, 12]}.newOrderedTable
|
doAssert a == {'o': @[1, 5, 7, 9, 11],
|
||||||
|
'e': @[2, 4, 6, 8, 12]}.newOrderedTable
|
||||||
|
|
||||||
let L = len(t)
|
let L = len(t)
|
||||||
forAllOrderedPairs:
|
forAllOrderedPairs:
|
||||||
|
|
@ -2070,7 +2076,8 @@ iterator keys*[A, B](t: OrderedTableRef[A, B]): A =
|
||||||
}.newOrderedTable
|
}.newOrderedTable
|
||||||
for k in a.keys:
|
for k in a.keys:
|
||||||
a[k].add(99)
|
a[k].add(99)
|
||||||
doAssert a == {'o': @[1, 5, 7, 9, 99], 'e': @[2, 4, 6, 8, 99]}.newOrderedTable
|
doAssert a == {'o': @[1, 5, 7, 9, 99], 'e': @[2, 4, 6, 8,
|
||||||
|
99]}.newOrderedTable
|
||||||
|
|
||||||
let L = len(t)
|
let L = len(t)
|
||||||
forAllOrderedPairs:
|
forAllOrderedPairs:
|
||||||
|
|
@ -2111,7 +2118,8 @@ iterator mvalues*[A, B](t: OrderedTableRef[A, B]): var B =
|
||||||
}.newOrderedTable
|
}.newOrderedTable
|
||||||
for v in a.mvalues:
|
for v in a.mvalues:
|
||||||
v.add(99)
|
v.add(99)
|
||||||
doAssert a == {'o': @[1, 5, 7, 9, 99], 'e': @[2, 4, 6, 8, 99]}.newOrderedTable
|
doAssert a == {'o': @[1, 5, 7, 9, 99],
|
||||||
|
'e': @[2, 4, 6, 8, 99]}.newOrderedTable
|
||||||
|
|
||||||
let L = len(t)
|
let L = len(t)
|
||||||
forAllOrderedPairs:
|
forAllOrderedPairs:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue