make tests green
This commit is contained in:
parent
3ba34f1762
commit
30823c1ce3
44 changed files with 415 additions and 403 deletions
|
|
@ -5,7 +5,7 @@ discard """
|
|||
# Test the case statement
|
||||
|
||||
type
|
||||
tenum = enum eA, eB, eC
|
||||
Tenum = enum eA, eB, eC
|
||||
|
||||
var
|
||||
x: string = "yyy"
|
||||
|
|
|
|||
|
|
@ -20,46 +20,46 @@ proc threadFunc(interval: tuple[a, b: int]) {.thread.} =
|
|||
when nodeadlocks:
|
||||
case i mod 6
|
||||
of 0:
|
||||
Acquire(L) # lock stdout
|
||||
Acquire(M)
|
||||
Acquire(N)
|
||||
acquire(L) # lock stdout
|
||||
acquire(M)
|
||||
acquire(N)
|
||||
of 1:
|
||||
Acquire(L)
|
||||
Acquire(N) # lock stdout
|
||||
Acquire(M)
|
||||
acquire(L)
|
||||
acquire(N) # lock stdout
|
||||
acquire(M)
|
||||
of 2:
|
||||
Acquire(M)
|
||||
Acquire(L)
|
||||
Acquire(N)
|
||||
acquire(M)
|
||||
acquire(L)
|
||||
acquire(N)
|
||||
of 3:
|
||||
Acquire(M)
|
||||
Acquire(N)
|
||||
Acquire(L)
|
||||
acquire(M)
|
||||
acquire(N)
|
||||
acquire(L)
|
||||
of 4:
|
||||
Acquire(N)
|
||||
Acquire(M)
|
||||
Acquire(L)
|
||||
acquire(N)
|
||||
acquire(M)
|
||||
acquire(L)
|
||||
of 5:
|
||||
Acquire(N)
|
||||
Acquire(L)
|
||||
Acquire(M)
|
||||
acquire(N)
|
||||
acquire(L)
|
||||
acquire(M)
|
||||
else: assert false
|
||||
else:
|
||||
Acquire(L) # lock stdout
|
||||
Acquire(M)
|
||||
acquire(L) # lock stdout
|
||||
acquire(M)
|
||||
|
||||
echo i
|
||||
os.sleep(10)
|
||||
when nodeadlocks:
|
||||
echo "deadlocks prevented: ", deadlocksPrevented
|
||||
when nodeadlocks:
|
||||
Release(N)
|
||||
Release(M)
|
||||
Release(L)
|
||||
release(N)
|
||||
release(M)
|
||||
release(L)
|
||||
|
||||
InitLock(L)
|
||||
InitLock(M)
|
||||
InitLock(N)
|
||||
initLock(L)
|
||||
initLock(M)
|
||||
initLock(N)
|
||||
|
||||
proc main =
|
||||
for i in 0..high(thr):
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ type
|
|||
s: string,
|
||||
x, y: int,
|
||||
z: float,
|
||||
chars: set[Char]]
|
||||
chars: set[char]]
|
||||
|
||||
proc testSem =
|
||||
var
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
discard """
|
||||
file: "tfloat1.nim"
|
||||
outputsub: "Error: unhandled exception: FPU operation caused an overflow [EFloatOverflow]"
|
||||
outputsub: "Error: unhandled exception: FPU operation caused an overflow [FloatOverflowError]"
|
||||
exitcode: "1"
|
||||
"""
|
||||
# Test new floating point exceptions
|
||||
|
|
@ -10,6 +10,6 @@ discard """
|
|||
var x = 0.8
|
||||
var y = 0.0
|
||||
|
||||
echo x / y #OUT Error: unhandled exception: FPU operation caused an overflow [EFloatOverflow]
|
||||
echo x / y #OUT Error: unhandled exception: FPU operation caused an overflow
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
discard """
|
||||
file: "tfloat2.nim"
|
||||
outputsub: "Error: unhandled exception: FPU operation caused a NaN result [EFloatInvalidOp]"
|
||||
outputsub: "Error: unhandled exception: FPU operation caused a NaN result [FloatInvalidOpError]"
|
||||
exitcode: "1"
|
||||
"""
|
||||
# Test new floating point exceptions
|
||||
|
|
@ -10,6 +10,6 @@ discard """
|
|||
var x = 0.0
|
||||
var y = 0.0
|
||||
|
||||
echo x / y #OUT Error: unhandled exception: FPU operation caused a NaN result [EFloatInvalidOp]
|
||||
echo x / y #OUT Error: unhandled exception: FPU operation caused a NaN result
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ void printFloats(void) {
|
|||
}
|
||||
""".}
|
||||
|
||||
proc c_printf(frmt: CString) {.importc: "printf", header: "<stdio.h>", varargs.}
|
||||
proc c_printf(frmt: cstring) {.importc: "printf", header: "<stdio.h>", varargs.}
|
||||
proc printFloats {.importc, nodecl.}
|
||||
|
||||
var x: float = 1.234567890123456789
|
||||
|
|
|
|||
|
|
@ -29,5 +29,5 @@ for i in 0 .. <10:
|
|||
f.func = proc =
|
||||
echo f.id
|
||||
|
||||
gc_fullcollect()
|
||||
GC_fullcollect()
|
||||
echo alive_foos.len <= 3
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ proc newDict*[TKey, TValue](): PDict[TKey, TValue] =
|
|||
proc add*[TKey, TValue](d: PDict[TKey, TValue], k: TKey, v: TValue) =
|
||||
d.data.add((k, v))
|
||||
|
||||
iterator items*[Tkey, tValue](d: PDict[TKey, TValue]): tuple[k: TKey,
|
||||
iterator items*[Tkey, TValue](d: PDict[TKey, TValue]): tuple[k: TKey,
|
||||
v: TValue] =
|
||||
for k, v in items(d.data): yield (k, v)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import tables
|
||||
|
||||
type
|
||||
TX = TTable[string, int]
|
||||
TX = Table[string, int]
|
||||
|
||||
proc foo(models: seq[TX]): seq[int] =
|
||||
result = @[]
|
||||
|
|
@ -9,7 +9,7 @@ proc foo(models: seq[TX]): seq[int] =
|
|||
result.add model["foobar"]
|
||||
|
||||
type
|
||||
obj = object
|
||||
field: TTable[string, string]
|
||||
Obj = object
|
||||
field: Table[string, string]
|
||||
var t: Obj
|
||||
discard initTable[type(t.field), string]()
|
||||
|
|
|
|||
|
|
@ -7,17 +7,17 @@ type
|
|||
value: D
|
||||
node: PNode[T,D]
|
||||
when not (D is string):
|
||||
val_set: Bool
|
||||
val_set: bool
|
||||
|
||||
TItems[T,D] = seq[ref TItem[T,D]]
|
||||
TNode {.acyclic, pure, final, shallow.} [T,D] = object
|
||||
slots: TItems[T,D]
|
||||
left: PNode[T,D]
|
||||
count: Int32
|
||||
count: int32
|
||||
|
||||
|
||||
RPath[T,D] = tuple[
|
||||
Xi: Int,
|
||||
Xi: int,
|
||||
Nd: PNode[T,D] ]
|
||||
|
||||
const
|
||||
|
|
@ -29,41 +29,41 @@ const
|
|||
cLenMax = 128
|
||||
cCenter = cLenMax div 2
|
||||
|
||||
proc len[T,D] (n:PNode[T,D]): Int {.inline.} =
|
||||
return n.Count
|
||||
proc len[T,D] (n:PNode[T,D]): int {.inline.} =
|
||||
return n.count
|
||||
|
||||
proc clean[T: TOrdinal|TNumber](o: var T) {.inline.} = discard
|
||||
proc clean[T: SomeOrdinal|SomeNumber](o: var T) {.inline.} = discard
|
||||
|
||||
proc clean[T: string|seq](o: var T) {.inline.} =
|
||||
o = nil
|
||||
|
||||
proc clean[T,D] (o: ref TItem[T,D]) {.inline.} =
|
||||
when (D is string) :
|
||||
o.Value = nil
|
||||
o.value = nil
|
||||
else :
|
||||
o.val_set = false
|
||||
|
||||
proc isClean[T,D] (it: ref TItem[T,D]): Bool {.inline.} =
|
||||
proc isClean[T,D] (it: ref TItem[T,D]): bool {.inline.} =
|
||||
when (D is string) :
|
||||
return it.Value == nil
|
||||
return it.value == nil
|
||||
else :
|
||||
return not it.val_set
|
||||
|
||||
proc isClean[T,D] (n: PNode[T,D], x: Int): Bool {.inline.} =
|
||||
when (D is string) :
|
||||
return n.slots[x].Value == nil
|
||||
else :
|
||||
proc isClean[T,D](n: PNode[T,D], x: int): bool {.inline.} =
|
||||
when (D is string):
|
||||
return n.slots[x].value == nil
|
||||
else:
|
||||
return not n.slots[x].val_set
|
||||
|
||||
proc setItem[T,D] (AKey: T, AValue: D, ANode: PNode[T,D]): ref TItem[T,D] {.inline.} =
|
||||
new(Result)
|
||||
Result.Key = AKey
|
||||
Result.Value = AValue
|
||||
Result.Node = ANode
|
||||
proc setItem[T,D](Akey: T, Avalue: D, ANode: PNode[T,D]): ref TItem[T,D] {.inline.} =
|
||||
new(result)
|
||||
result.key = Akey
|
||||
result.value = Avalue
|
||||
result.node = ANode
|
||||
when not (D is string) :
|
||||
Result.val_set = true
|
||||
result.val_set = true
|
||||
|
||||
proc cmp[T:Int8|Int16|Int32|Int64|Int] (a,b: T): T {.inline.} =
|
||||
proc cmp[T:int8|int16|int32|int64|int] (a,b: T): T {.inline.} =
|
||||
return a-b
|
||||
|
||||
template binSearchImpl *(docmp: expr) {.immediate.} =
|
||||
|
|
@ -76,41 +76,41 @@ template binSearchImpl *(docmp: expr) {.immediate.} =
|
|||
if SW < 0: result = I + 1
|
||||
else:
|
||||
H = I - 1
|
||||
if SW == 0 : bFound = True
|
||||
if SW == 0 : bFound = true
|
||||
if bFound: inc(result)
|
||||
else: result = - result
|
||||
|
||||
proc bSearch[T,D] (haystack: PNode[T,D], needle:T): Int {.inline.} =
|
||||
proc bSearch[T,D] (haystack: PNode[T,D], needle:T): int {.inline.} =
|
||||
binSearchImpl(haystack.slots[I].key.cmp(needle))
|
||||
|
||||
proc DeleteItem[T,D] (n: PNode[T,D], x: Int): PNode[T,D] {.inline.} =
|
||||
proc DeleteItem[T,D] (n: PNode[T,D], x: int): PNode[T,D] {.inline.} =
|
||||
var w = n.slots[x]
|
||||
if w.Node != nil :
|
||||
if w.node != nil :
|
||||
clean(w)
|
||||
return n
|
||||
dec(n.Count)
|
||||
if n.Count > 0 :
|
||||
for i in countup(x, n.Count -1) : n.slots[i] = n.slots[i + 1]
|
||||
n.slots[n.Count] = nil
|
||||
case n.Count
|
||||
dec(n.count)
|
||||
if n.count > 0 :
|
||||
for i in countup(x, n.count -1) : n.slots[i] = n.slots[i + 1]
|
||||
n.slots[n.count] = nil
|
||||
case n.count
|
||||
of cLen1 : setLen(n.slots, cLen1)
|
||||
of cLen2 : setLen(n.slots, cLen2)
|
||||
of cLen3 : setLen(n.slots, cLen3)
|
||||
of cLenCenter : setLen(n.slots, cLenCenter)
|
||||
of cLen4 : setLen(n.slots, cLen4)
|
||||
else: discard
|
||||
Result = n
|
||||
result = n
|
||||
|
||||
else :
|
||||
Result = n.Left
|
||||
result = n.left
|
||||
n.slots = nil
|
||||
n.Left = nil
|
||||
n.left = nil
|
||||
|
||||
proc InternalDelete[T,D] (ANode: PNode[T,D], key: T, AValue: var D): PNode[T,D] =
|
||||
proc internalDelete[T,D] (ANode: PNode[T,D], key: T, Avalue: var D): PNode[T,D] =
|
||||
var Path: array[0..20, RPath[T,D]]
|
||||
var n = ANode
|
||||
Result = n
|
||||
clean(AValue)
|
||||
result = n
|
||||
clean(Avalue)
|
||||
var h = 0
|
||||
while n != nil:
|
||||
var x = bSearch(n, key)
|
||||
|
|
@ -119,17 +119,17 @@ proc InternalDelete[T,D] (ANode: PNode[T,D], key: T, AValue: var D): PNode[T,D]
|
|||
Path[h].Xi = - x
|
||||
inc(h)
|
||||
if x == 0 :
|
||||
n = n.Left
|
||||
n = n.left
|
||||
else :
|
||||
x = (-x) -1
|
||||
if x < n.Count :
|
||||
n = n.slots[x].Node
|
||||
if x < n.count :
|
||||
n = n.slots[x].node
|
||||
else :
|
||||
n = nil
|
||||
else :
|
||||
dec(x)
|
||||
if isClean(n, x) : return
|
||||
AValue = n.slots[x].Value
|
||||
Avalue = n.slots[x].value
|
||||
var n2 = DeleteItem(n, x)
|
||||
dec(h)
|
||||
while (n2 != n) and (h >=0) :
|
||||
|
|
@ -139,30 +139,30 @@ proc InternalDelete[T,D] (ANode: PNode[T,D], key: T, AValue: var D): PNode[T,D]
|
|||
if x >= 0 :
|
||||
if (n == nil) and isClean(w.Nd, x) :
|
||||
n = w.Nd
|
||||
n.slots[x].Node = nil
|
||||
n.slots[x].node = nil
|
||||
n2 = DeleteItem(n, x)
|
||||
else :
|
||||
w.Nd.slots[x].Node = n
|
||||
w.Nd.slots[x].node = n
|
||||
return
|
||||
else :
|
||||
w.Nd.Left = n
|
||||
w.Nd.left = n
|
||||
return
|
||||
dec(h)
|
||||
if h < 0:
|
||||
Result = n2
|
||||
result = n2
|
||||
return
|
||||
|
||||
proc InternalFind[T,D] (n: PNode[T,D], key: T): ref TItem[T,D] {.inline.} =
|
||||
proc internalFind[T,D] (n: PNode[T,D], key: T): ref TItem[T,D] {.inline.} =
|
||||
var wn = n
|
||||
while wn != nil :
|
||||
var x = bSearch(wn, key)
|
||||
if x <= 0 :
|
||||
if x == 0 :
|
||||
wn = wn.Left
|
||||
wn = wn.left
|
||||
else :
|
||||
x = (-x) -1
|
||||
if x < wn.Count :
|
||||
wn = wn.slots[x].Node
|
||||
if x < wn.count :
|
||||
wn = wn.slots[x].node
|
||||
else :
|
||||
return nil
|
||||
|
||||
|
|
@ -171,32 +171,32 @@ proc InternalFind[T,D] (n: PNode[T,D], key: T): ref TItem[T,D] {.inline.} =
|
|||
return nil
|
||||
|
||||
proc traceTree[T,D](root: PNode[T,D]) =
|
||||
proc traceX(x: Int) =
|
||||
proc traceX(x: int) =
|
||||
write stdout, "("
|
||||
write stdout, x
|
||||
write stdout, ") "
|
||||
|
||||
proc traceEl(el: ref TItem[T,D]) =
|
||||
write stdout, " key: "
|
||||
write stdout, el.Key
|
||||
write stdout, el.key
|
||||
write stdout, " value: "
|
||||
write stdout, el.Value
|
||||
write stdout, el.value
|
||||
|
||||
|
||||
proc traceln(space: string) =
|
||||
writeln stdout, ""
|
||||
write stdout, space
|
||||
|
||||
proc doTrace(n: PNode[T,D], level: Int) =
|
||||
proc doTrace(n: PNode[T,D], level: int) =
|
||||
var space = repeatChar(2 * level)
|
||||
traceln(space)
|
||||
write stdout, "node: "
|
||||
if n == nil:
|
||||
writeln stdout, "is empty"
|
||||
return
|
||||
write stdout, n.Count
|
||||
write stdout, n.count
|
||||
write stdout, " elements: "
|
||||
if n.Left != nil:
|
||||
if n.left != nil:
|
||||
traceln(space)
|
||||
write stdout, "left: "
|
||||
doTrace(n.left, level +1)
|
||||
|
|
@ -204,188 +204,188 @@ proc traceTree[T,D](root: PNode[T,D]) =
|
|||
if el != nil and not isClean(el):
|
||||
traceln(space)
|
||||
traceX(i)
|
||||
if i >= n.Count:
|
||||
if i >= n.count:
|
||||
write stdout, "error "
|
||||
else:
|
||||
traceEl(el)
|
||||
if el.Node != nil: doTrace(el.Node, level +1)
|
||||
if el.node != nil: doTrace(el.node, level +1)
|
||||
else : write stdout, " empty "
|
||||
elif i < n.Count :
|
||||
elif i < n.count :
|
||||
traceln(space)
|
||||
traceX(i)
|
||||
write stdout, "clean: "
|
||||
when T is string :
|
||||
if el.Key != nil: write stdout, el.Key
|
||||
else : write stdout, el.Key
|
||||
if el.Node != nil: doTrace(el.Node, level +1)
|
||||
if el.key != nil: write stdout, el.key
|
||||
else : write stdout, el.key
|
||||
if el.node != nil: doTrace(el.node, level +1)
|
||||
else : write stdout, " empty "
|
||||
writeln stdout,""
|
||||
|
||||
doTrace(root, 0)
|
||||
|
||||
proc InsertItem[T,D](APath: RPath[T,D], ANode:PNode[T,D], AKey: T, AValue: D) =
|
||||
proc InsertItem[T,D](APath: RPath[T,D], ANode:PNode[T,D], Akey: T, Avalue: D) =
|
||||
var x = - APath.Xi
|
||||
inc(APath.Nd.Count)
|
||||
case APath.Nd.Count
|
||||
inc(APath.Nd.count)
|
||||
case APath.Nd.count
|
||||
of cLen1: setLen(APath.Nd.slots, cLen2)
|
||||
of cLen2: setLen(APath.Nd.slots, cLen3)
|
||||
of cLen3: setLen(APath.Nd.slots, cLenCenter)
|
||||
of cLenCenter: setLen(APath.Nd.slots, cLen4)
|
||||
of cLen4: setLen(APath.Nd.slots, cLenMax)
|
||||
else: discard
|
||||
for i in countdown(APath.Nd.Count.int - 1, x + 1): shallowCopy(APath.Nd.slots[i], APath.Nd.slots[i - 1])
|
||||
APath.Nd.slots[x] = setItem(AKey, AValue, ANode)
|
||||
for i in countdown(APath.Nd.count.int - 1, x + 1): shallowCopy(APath.Nd.slots[i], APath.Nd.slots[i - 1])
|
||||
APath.Nd.slots[x] = setItem(Akey, Avalue, ANode)
|
||||
|
||||
|
||||
proc SplitPage[T,D](n, left: PNode[T,D], xi: Int, AKey:var T, AValue:var D): PNode[T,D] =
|
||||
var x = -Xi
|
||||
proc SplitPage[T,D](n, left: PNode[T,D], xi: int, Akey:var T, Avalue:var D): PNode[T,D] =
|
||||
var x = -xi
|
||||
var it1: TItems[T,D]
|
||||
it1.newSeq(cLenCenter)
|
||||
new(Result)
|
||||
Result.slots.newSeq(cLenCenter)
|
||||
Result.Count = cCenter
|
||||
new(result)
|
||||
result.slots.newSeq(cLenCenter)
|
||||
result.count = cCenter
|
||||
if x == cCenter:
|
||||
for i in 0..cCenter -1: shallowCopy(it1[i], left.slots[i])
|
||||
for i in 0..cCenter -1: shallowCopy(Result.slots[i], left.slots[cCenter + i])
|
||||
Result.Left = n
|
||||
for i in 0..cCenter -1: shallowCopy(result.slots[i], left.slots[cCenter + i])
|
||||
result.left = n
|
||||
else :
|
||||
if x < cCenter :
|
||||
for i in 0..x-1: shallowCopy(it1[i], left.slots[i])
|
||||
it1[x] = setItem(AKey, AValue, n)
|
||||
it1[x] = setItem(Akey, Avalue, n)
|
||||
for i in x+1 .. cCenter -1: shallowCopy(it1[i], left.slots[i-1])
|
||||
var w = left.slots[cCenter -1]
|
||||
AKey = w.Key
|
||||
AValue = w.Value
|
||||
Result.Left = w.Node
|
||||
for i in 0..cCenter -1: shallowCopy(Result.slots[i], left.slots[cCenter + i])
|
||||
Akey = w.key
|
||||
Avalue = w.value
|
||||
result.left = w.node
|
||||
for i in 0..cCenter -1: shallowCopy(result.slots[i], left.slots[cCenter + i])
|
||||
else :
|
||||
for i in 0..cCenter -1: shallowCopy(it1[i], left.slots[i])
|
||||
x = x - (cCenter + 1)
|
||||
for i in 0..x-1: shallowCopy(Result.slots[i], left.slots[cCenter + i + 1])
|
||||
Result.slots[x] = setItem(AKey, AValue, n)
|
||||
for i in x+1 .. cCenter -1: shallowCopy(Result.slots[i], left.slots[cCenter + i])
|
||||
for i in 0..x-1: shallowCopy(result.slots[i], left.slots[cCenter + i + 1])
|
||||
result.slots[x] = setItem(Akey, Avalue, n)
|
||||
for i in x+1 .. cCenter -1: shallowCopy(result.slots[i], left.slots[cCenter + i])
|
||||
var w = left.slots[cCenter]
|
||||
AKey = w.Key
|
||||
AValue = w.Value
|
||||
Result.Left = w.Node
|
||||
left.Count = cCenter
|
||||
Akey = w.key
|
||||
Avalue = w.value
|
||||
result.left = w.node
|
||||
left.count = cCenter
|
||||
shallowCopy(left.slots, it1)
|
||||
|
||||
|
||||
proc InternalPut[T,D](ANode: ref TNode[T,D], AKey: T, AValue: D, OldValue: var D): ref TNode[T,D] =
|
||||
var h: Int
|
||||
proc internalPut[T,D](ANode: ref TNode[T,D], Akey: T, Avalue: D, Oldvalue: var D): ref TNode[T,D] =
|
||||
var h: int
|
||||
var Path: array[0..30, RPath[T,D]]
|
||||
var left: PNode[T,D]
|
||||
var n = ANode
|
||||
|
||||
|
||||
Result = ANode
|
||||
result = ANode
|
||||
h = 0
|
||||
while n != nil:
|
||||
var x = bSearch[T,D](n, AKey)
|
||||
var x = bSearch[T,D](n, Akey)
|
||||
if x <= 0 :
|
||||
Path[h].Nd = n
|
||||
Path[h].Xi = x
|
||||
inc(h)
|
||||
if x == 0 :
|
||||
n = n.Left
|
||||
n = n.left
|
||||
else :
|
||||
x = (-x) -1
|
||||
if x < n.Count :
|
||||
n = n.slots[x].Node
|
||||
if x < n.count :
|
||||
n = n.slots[x].node
|
||||
else :
|
||||
n = nil
|
||||
else :
|
||||
var w = n.slots[x - 1]
|
||||
OldValue = w.Value
|
||||
w.Value = AValue
|
||||
Oldvalue = w.value
|
||||
w.value = Avalue
|
||||
return
|
||||
|
||||
dec(h)
|
||||
left = nil
|
||||
var lKey = AKey
|
||||
var lValue = AValue
|
||||
var lkey = Akey
|
||||
var lvalue = Avalue
|
||||
while h >= 0 :
|
||||
if Path[h].Nd.Count < cLenMax :
|
||||
InsertItem(Path[h], n, lKey, lValue)
|
||||
if Path[h].Nd.count < cLenMax :
|
||||
InsertItem(Path[h], n, lkey, lvalue)
|
||||
return
|
||||
else :
|
||||
left = Path[h].Nd
|
||||
n = SplitPage(n, left, Path[h].Xi, lKey, lValue)
|
||||
n = SplitPage(n, left, Path[h].Xi, lkey, lvalue)
|
||||
dec(h)
|
||||
|
||||
new(Result)
|
||||
Result.slots.newSeq(cLen1)
|
||||
Result.Count = 1
|
||||
Result.Left = left
|
||||
Result.slots[0] = setItem(lKey, lValue, n)
|
||||
new(result)
|
||||
result.slots.newSeq(cLen1)
|
||||
result.count = 1
|
||||
result.left = left
|
||||
result.slots[0] = setItem(lkey, lvalue, n)
|
||||
|
||||
|
||||
proc CleanTree[T,D](n: PNode[T,D]): PNode[T,D] =
|
||||
if n.Left != nil :
|
||||
n.Left = CleanTree(n.Left)
|
||||
for i in 0 .. n.Count - 1 :
|
||||
if n.left != nil :
|
||||
n.left = CleanTree(n.left)
|
||||
for i in 0 .. n.count - 1 :
|
||||
var w = n.slots[i]
|
||||
if w.Node != nil :
|
||||
w.Node = CleanTree(w.Node)
|
||||
clean(w.Value)
|
||||
clean(w.Key)
|
||||
if w.node != nil :
|
||||
w.node = CleanTree(w.node)
|
||||
clean(w.value)
|
||||
clean(w.key)
|
||||
n.slots = nil
|
||||
return nil
|
||||
|
||||
|
||||
proc VisitAllNodes[T,D](n: PNode[T,D], visit: proc(n: PNode[T,D]): PNode[T,D] {.closure.} ): PNode[T,D] =
|
||||
if n != nil :
|
||||
if n.Left != nil :
|
||||
n.Left = VisitAllNodes(n.Left, visit)
|
||||
for i in 0 .. n.Count - 1 :
|
||||
if n.left != nil :
|
||||
n.left = VisitAllNodes(n.left, visit)
|
||||
for i in 0 .. n.count - 1 :
|
||||
var w = n.slots[i]
|
||||
if w.Node != nil :
|
||||
w.Node = VisitAllNodes(w.Node, visit)
|
||||
if w.node != nil :
|
||||
w.node = VisitAllNodes(w.node, visit)
|
||||
return visit(n)
|
||||
return nil
|
||||
|
||||
proc VisitAllNodes[T,D](n: PNode[T,D], visit: proc(n: PNode[T,D]) {.closure.} ) =
|
||||
if n != nil:
|
||||
if n.Left != nil :
|
||||
VisitAllNodes(n.Left, visit)
|
||||
for i in 0 .. n.Count - 1 :
|
||||
if n.left != nil :
|
||||
VisitAllNodes(n.left, visit)
|
||||
for i in 0 .. n.count - 1 :
|
||||
var w = n.slots[i]
|
||||
if w.Node != nil :
|
||||
VisitAllNodes(w.Node, visit)
|
||||
if w.node != nil :
|
||||
VisitAllNodes(w.node, visit)
|
||||
visit(n)
|
||||
|
||||
proc VisitAll[T,D](n: PNode[T,D], visit: proc(AKey: T, AValue: D) {.closure.} ) =
|
||||
proc VisitAll[T,D](n: PNode[T,D], visit: proc(Akey: T, Avalue: D) {.closure.} ) =
|
||||
if n != nil:
|
||||
if n.Left != nil :
|
||||
VisitAll(n.Left, visit)
|
||||
for i in 0 .. n.Count - 1 :
|
||||
if n.left != nil :
|
||||
VisitAll(n.left, visit)
|
||||
for i in 0 .. n.count - 1 :
|
||||
var w = n.slots[i]
|
||||
if not w.isClean :
|
||||
visit(w.Key, w.Value)
|
||||
if w.Node != nil :
|
||||
VisitAll(w.Node, visit)
|
||||
visit(w.key, w.value)
|
||||
if w.node != nil :
|
||||
VisitAll(w.node, visit)
|
||||
|
||||
proc VisitAll[T,D](n: PNode[T,D], visit: proc(AKey: T, AValue: var D):Bool {.closure.} ): PNode[T,D] =
|
||||
proc VisitAll[T,D](n: PNode[T,D], visit: proc(Akey: T, Avalue: var D):bool {.closure.} ): PNode[T,D] =
|
||||
if n != nil:
|
||||
var n1 = n.Left
|
||||
var n1 = n.left
|
||||
if n1 != nil :
|
||||
var n2 = VisitAll(n1, visit)
|
||||
if n1 != n2 :
|
||||
n.Left = n2
|
||||
n.left = n2
|
||||
var i = 0
|
||||
while i < n.Count :
|
||||
while i < n.count :
|
||||
var w = n.slots[i]
|
||||
if not w.isClean :
|
||||
if visit(w.Key, w.Value) :
|
||||
Result = DeleteItem(n, i)
|
||||
if Result == nil : return
|
||||
if visit(w.key, w.value) :
|
||||
result = DeleteItem(n, i)
|
||||
if result == nil : return
|
||||
dec(i)
|
||||
n1 = w.Node
|
||||
n1 = w.node
|
||||
if n1 != nil :
|
||||
var n2 = VisitAll(n1, visit)
|
||||
if n1 != n2 :
|
||||
w.Node = n2
|
||||
w.node = n2
|
||||
inc(i)
|
||||
return n
|
||||
|
||||
|
|
@ -396,20 +396,20 @@ iterator keys* [T,D] (n: PNode[T,D]): T =
|
|||
var nd = n
|
||||
var i = -1
|
||||
while true :
|
||||
if i < nd.Count :
|
||||
if i < nd.count :
|
||||
Path[level].Nd = nd
|
||||
Path[level].Xi = i
|
||||
if i < 0 :
|
||||
if nd.Left != nil :
|
||||
nd = nd.Left
|
||||
if nd.left != nil :
|
||||
nd = nd.left
|
||||
inc(level)
|
||||
else : inc(i)
|
||||
else :
|
||||
var w = nd.slots[i]
|
||||
if not w.isClean() :
|
||||
yield w.Key
|
||||
if w.Node != nil :
|
||||
nd = w.Node
|
||||
yield w.key
|
||||
if w.node != nil :
|
||||
nd = w.node
|
||||
i = -1
|
||||
inc(level)
|
||||
else : inc(i)
|
||||
|
|
@ -424,22 +424,22 @@ iterator keys* [T,D] (n: PNode[T,D]): T =
|
|||
when isMainModule:
|
||||
|
||||
proc test() =
|
||||
var oldValue: Int
|
||||
var root = InternalPut[int, int](nil, 312, 312, oldValue)
|
||||
var someOtherRoot = InternalPut[string, int](nil, "312", 312, oldValue)
|
||||
var it1 = InternalFind(root, 312)
|
||||
echo it1.Value
|
||||
var oldvalue: int
|
||||
var root = internalPut[int, int](nil, 312, 312, oldvalue)
|
||||
var someOtherRoot = internalPut[string, int](nil, "312", 312, oldvalue)
|
||||
var it1 = internalFind(root, 312)
|
||||
echo it1.value
|
||||
|
||||
for i in 1..1_000_000:
|
||||
root = InternalPut(root, i, i, oldValue)
|
||||
root = internalPut(root, i, i, oldvalue)
|
||||
|
||||
var cnt = 0
|
||||
oldValue = -1
|
||||
oldvalue = -1
|
||||
when true : # code compiles, when this or the other when is switched to false
|
||||
for k in root.keys :
|
||||
if k <= oldValue :
|
||||
if k <= oldvalue :
|
||||
echo k
|
||||
oldValue = k
|
||||
oldvalue = k
|
||||
inc(cnt)
|
||||
echo cnt
|
||||
when true :
|
||||
|
|
@ -450,21 +450,21 @@ when isMainModule:
|
|||
root = VisitAll(root, proc(key: int, value: var int): bool =
|
||||
return key mod 2 == 0 )
|
||||
cnt = 0
|
||||
oldValue = -1
|
||||
oldvalue = -1
|
||||
VisitAll(root, proc(key: int, value: int) {.closure.} =
|
||||
if key <= oldValue :
|
||||
if key <= oldvalue :
|
||||
echo key
|
||||
oldValue = key
|
||||
oldvalue = key
|
||||
inc(cnt) )
|
||||
echo cnt
|
||||
root = VisitAll(root, proc(key: int, value: var int): bool =
|
||||
return key mod 2 != 0 )
|
||||
cnt = 0
|
||||
oldValue = -1
|
||||
oldvalue = -1
|
||||
VisitAll(root, proc(key: int, value: int) {.closure.} =
|
||||
if key <= oldValue :
|
||||
if key <= oldvalue :
|
||||
echo "error ", key
|
||||
oldValue = key
|
||||
oldvalue = key
|
||||
inc(cnt) )
|
||||
echo cnt
|
||||
#traceTree(root)
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ proc filter[T,D](data: seq[T], env:D, pred: TFilterProc[T,D]): seq[T] =
|
|||
for e in data:
|
||||
if pred(e, env): result.add(e)
|
||||
|
||||
proc predTest(item: int, value: int): Bool =
|
||||
proc predTest(item: int, value: int): bool =
|
||||
return item <= value
|
||||
|
||||
proc test(data: seq[int], value: int): seq[int] =
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
type
|
||||
TMaybe[T] = object
|
||||
case empty: Bool
|
||||
of False: value: T
|
||||
case empty: bool
|
||||
of false: value: T
|
||||
else: nil
|
||||
|
||||
proc Just*[T](val: T): TMaybe[T] =
|
||||
result.empty = False
|
||||
result.empty = false
|
||||
result.value = val
|
||||
|
||||
proc Nothing[T](): TMaybe[T] =
|
||||
result.empty = True
|
||||
result.empty = true
|
||||
|
||||
proc safeReadLine(): TMaybe[string] =
|
||||
var r = stdin.readLine()
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ block Test1:
|
|||
# a non-generic iterator!
|
||||
|
||||
var t = initTable[int, string]()
|
||||
for k, v in t.pairs: nil
|
||||
for k, v in t.pairs: nil
|
||||
for k, v in t.pairs: discard
|
||||
for k, v in t.pairs: discard
|
||||
|
||||
echo "true"
|
||||
|
||||
|
|
|
|||
|
|
@ -21,13 +21,13 @@ iterator tokenize2(s: string, seps: set[char] = Whitespace): tuple[
|
|||
yield (substr(s, i, j-1), false)
|
||||
i = j
|
||||
|
||||
for word, isSep in tokenize2("ta da", whiteSpace):
|
||||
for word, isSep in tokenize2("ta da", WhiteSpace):
|
||||
var titer2TestVar = 0
|
||||
stdout.write(titer2TestVar)
|
||||
|
||||
proc wordWrap2(s: string, maxLineWidth = 80,
|
||||
splitLongWords = true,
|
||||
seps: set[char] = whitespace,
|
||||
seps: set[char] = Whitespace,
|
||||
newLine = "\n"): string =
|
||||
result = ""
|
||||
for word, isSep in tokenize2(s, seps):
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ iterator count3(): int {.closure.} =
|
|||
yield 2
|
||||
yield 3
|
||||
|
||||
for word, isSep in tokenize2("ta da", whiteSpace):
|
||||
for word, isSep in tokenize2("ta da", WhiteSpace):
|
||||
if not isSep:
|
||||
stdout.write(word)
|
||||
echo ""
|
||||
|
|
@ -56,7 +56,7 @@ proc inProc() =
|
|||
for c in count3():
|
||||
echo c
|
||||
|
||||
for word, isSep in tokenize2("ta da", whiteSpace):
|
||||
for word, isSep in tokenize2("ta da", WhiteSpace):
|
||||
stdout.write(word)
|
||||
|
||||
for c in count3():
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ discard """
|
|||
output: '''x: 0 y: 0'''
|
||||
"""
|
||||
|
||||
proc ToString*[T](x: T): string = return $x
|
||||
proc toString*[T](x: T): string = return $x
|
||||
|
||||
|
||||
type
|
||||
|
|
|
|||
|
|
@ -6,13 +6,13 @@ import
|
|||
proc main() =
|
||||
var
|
||||
a, b: TTime
|
||||
a = getLastModificationTime(ParamStr(1))
|
||||
b = getLastModificationTime(ParamStr(2))
|
||||
a = getLastModificationTime(paramStr(1))
|
||||
b = getLastModificationTime(paramStr(2))
|
||||
writeln(stdout, $a)
|
||||
writeln(stdout, $b)
|
||||
if a < b:
|
||||
Write(stdout, "$2 is newer than $1\n" % [ParamStr(1), ParamStr(2)])
|
||||
write(stdout, "$2 is newer than $1\n" % [paramStr(1), paramStr(2)])
|
||||
else:
|
||||
Write(stdout, "$1 is newer than $2\n" % [ParamStr(1), ParamStr(2)])
|
||||
write(stdout, "$1 is newer than $2\n" % [paramStr(1), paramStr(2)])
|
||||
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ proc TestLoops() =
|
|||
break
|
||||
break
|
||||
|
||||
while True:
|
||||
while true:
|
||||
break
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -5,16 +5,16 @@ discard """
|
|||
"""
|
||||
# BUG: following compiles, but should not:
|
||||
|
||||
proc nodeOfDegree(x: Int): bool =
|
||||
proc nodeOfDegree(x: int): bool =
|
||||
result = false
|
||||
|
||||
proc main =
|
||||
for j in 0..2:
|
||||
for i in 0..10:
|
||||
if not nodeOfDegree(1) >= 0: #ERROR_MSG type mismatch
|
||||
Echo "Yes"
|
||||
echo "Yes"
|
||||
else:
|
||||
Echo "No"
|
||||
echo "No"
|
||||
|
||||
main()
|
||||
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ proc mypos(sub, s: string, start: int = 0): int =
|
|||
if i >= N:
|
||||
result = -1
|
||||
else:
|
||||
while True:
|
||||
while true:
|
||||
if s[i] == sub[j]:
|
||||
Inc(i)
|
||||
Inc(j)
|
||||
inc(i)
|
||||
inc(j)
|
||||
else:
|
||||
i = i - j + 1
|
||||
j = 0
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ else:
|
|||
|
||||
var
|
||||
s: string
|
||||
write(stdout, "compiled at " & system.compileDate &
|
||||
" " & compileTime & "\n")
|
||||
write(stdout, "compiled at " & system.CompileDate &
|
||||
" " & CompileTime & "\n")
|
||||
echo getDateStr()
|
||||
echo getClockStr()
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
## We use a radix tree with node compression.
|
||||
## There are two node kinds:
|
||||
|
||||
const bitsPerUnit = 8*sizeof(int)
|
||||
const BitsPerUnit = 8*sizeof(int)
|
||||
|
||||
type
|
||||
TRadixNodeKind = enum rnLinear, rnFull, rnLeafBits, rnLeafLinear
|
||||
|
|
@ -42,13 +42,13 @@ proc testBit(w, i: int): bool {.inline.} =
|
|||
result = (w and (1 shl (i %% BitsPerUnit))) != 0
|
||||
|
||||
proc setBit(w: var int, i: int) {.inline.} =
|
||||
w = w or (1 shl (i %% bitsPerUnit))
|
||||
w = w or (1 shl (i %% BitsPerUnit))
|
||||
|
||||
proc resetBit(w: var int, i: int) {.inline.} =
|
||||
w = w and not (1 shl (i %% bitsPerUnit))
|
||||
w = w and not (1 shl (i %% BitsPerUnit))
|
||||
|
||||
proc testOrSetBit(w: var int, i: int): bool {.inline.} =
|
||||
var x = (1 shl (i %% bitsPerUnit))
|
||||
var x = (1 shl (i %% BitsPerUnit))
|
||||
if (w and x) != 0: return true
|
||||
w = w or x
|
||||
|
||||
|
|
@ -78,7 +78,7 @@ proc exclLeaf(r: PRadixNode, a: int) =
|
|||
return
|
||||
else: assert(false)
|
||||
|
||||
proc contains*(r: PRadixNode, a: TAddress): bool =
|
||||
proc contains*(r: PRadixNode, a: ByteAddress): bool =
|
||||
if r == nil: return false
|
||||
var x = searchInner(r, a shr 24 and 0xff)
|
||||
if x == nil: return false
|
||||
|
|
@ -88,7 +88,7 @@ proc contains*(r: PRadixNode, a: TAddress): bool =
|
|||
if x == nil: return false
|
||||
return searchLeaf(x, a and 0xff)
|
||||
|
||||
proc excl*(r: PRadixNode, a: TAddress): bool =
|
||||
proc excl*(r: PRadixNode, a: ByteAddress): bool =
|
||||
if r == nil: return false
|
||||
var x = searchInner(r, a shr 24 and 0xff)
|
||||
if x == nil: return false
|
||||
|
|
@ -167,10 +167,10 @@ proc addInner(r: var PRadixNode, a: int, d: int): bool =
|
|||
return addInner(x.b[k], a, d-8)
|
||||
else: assert(false)
|
||||
|
||||
proc incl*(r: var PRadixNode, a: TAddress) {.inline.} =
|
||||
proc incl*(r: var PRadixNode, a: ByteAddress) {.inline.} =
|
||||
discard addInner(r, a, 24)
|
||||
|
||||
proc testOrIncl*(r: var PRadixNode, a: TAddress): bool {.inline.} =
|
||||
proc testOrIncl*(r: var PRadixNode, a: ByteAddress): bool {.inline.} =
|
||||
return addInner(r, a, 24)
|
||||
|
||||
iterator innerElements(r: PRadixNode): tuple[prefix: int, n: PRadixNode] =
|
||||
|
|
@ -204,7 +204,7 @@ iterator leafElements(r: PRadixNode): int =
|
|||
yield ze(r.keys[i])
|
||||
else: assert(false)
|
||||
|
||||
iterator elements*(r: PRadixNode): TAddress {.inline.} =
|
||||
iterator elements*(r: PRadixNode): ByteAddress {.inline.} =
|
||||
for p1, n1 in innerElements(r):
|
||||
for p2, n2 in innerElements(n1):
|
||||
for p3, n3 in innerElements(n2):
|
||||
|
|
@ -297,7 +297,7 @@ when false:
|
|||
result = ze(r.keys[i.x])
|
||||
inc(i.x)
|
||||
|
||||
iterator elements(r: PRadixNode): TAddress {.inline.} =
|
||||
iterator elements(r: PRadixNode): ByteAddress {.inline.} =
|
||||
var
|
||||
a, b, c, d: TRadixIter
|
||||
init(a, r)
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
# Macintosh, Unix or Windows text format.
|
||||
|
||||
var
|
||||
inp: TFile
|
||||
inp: File
|
||||
line: string
|
||||
|
||||
if open(inp, "readme.txt"):
|
||||
while not EndOfFile(inp):
|
||||
while not endOfFile(inp):
|
||||
line = readLine(inp)
|
||||
echo("#" & line & "#")
|
||||
close(inp)
|
||||
|
|
|
|||
|
|
@ -4,11 +4,7 @@ discard """
|
|||
|
||||
import hashes, math
|
||||
|
||||
|
||||
when defined(shallowADT):
|
||||
{.pragma: myShallow, shallow.}
|
||||
else:
|
||||
{.pragma: myShallow.}
|
||||
{.pragma: myShallow.}
|
||||
|
||||
type
|
||||
TSlotEnum = enum seEmpty, seFilled, seDeleted
|
||||
|
|
@ -63,7 +59,7 @@ template rawInsertImpl() =
|
|||
data[h].val = val
|
||||
data[h].slot = seFilled
|
||||
|
||||
proc RawGet[A, B](t: TTable[A, B], key: A): int =
|
||||
proc rawGet[A, B](t: TTable[A, B], key: A): int =
|
||||
rawGetImpl()
|
||||
|
||||
proc `[]`*[A, B](t: TTable[A, B], key: A): B =
|
||||
|
|
@ -71,31 +67,31 @@ proc `[]`*[A, B](t: TTable[A, B], key: A): B =
|
|||
## default empty value for the type `B` is returned
|
||||
## and no exception is raised. One can check with ``hasKey`` whether the key
|
||||
## exists.
|
||||
var index = RawGet(t, key)
|
||||
var index = rawGet(t, key)
|
||||
if index >= 0: result = t.data[index].val
|
||||
|
||||
proc hasKey*[A, B](t: TTable[A, B], key: A): bool =
|
||||
## returns true iff `key` is in the table `t`.
|
||||
result = rawGet(t, key) >= 0
|
||||
|
||||
proc RawInsert[A, B](t: var TTable[A, B], data: var TKeyValuePairSeq[A, B],
|
||||
proc rawInsert[A, B](t: var TTable[A, B], data: var TKeyValuePairSeq[A, B],
|
||||
key: A, val: B) =
|
||||
rawInsertImpl()
|
||||
|
||||
proc Enlarge[A, B](t: var TTable[A, B]) =
|
||||
proc enlarge[A, B](t: var TTable[A, B]) =
|
||||
var n: TKeyValuePairSeq[A, B]
|
||||
newSeq(n, len(t.data) * growthFactor)
|
||||
for i in countup(0, high(t.data)):
|
||||
if t.data[i].slot == seFilled: RawInsert(t, n, t.data[i].key, t.data[i].val)
|
||||
if t.data[i].slot == seFilled: rawInsert(t, n, t.data[i].key, t.data[i].val)
|
||||
swap(t.data, n)
|
||||
|
||||
template PutImpl() =
|
||||
var index = RawGet(t, key)
|
||||
template putImpl() =
|
||||
var index = rawGet(t, key)
|
||||
if index >= 0:
|
||||
t.data[index].val = val
|
||||
else:
|
||||
if mustRehash(len(t.data), t.counter): Enlarge(t)
|
||||
RawInsert(t, t.data, key, val)
|
||||
if mustRehash(len(t.data), t.counter): enlarge(t)
|
||||
rawInsert(t, t.data, key, val)
|
||||
inc(t.counter)
|
||||
|
||||
proc `[]=`*[A, B](t: var TTable[A, B], key: A, val: B) =
|
||||
|
|
@ -104,7 +100,7 @@ proc `[]=`*[A, B](t: var TTable[A, B], key: A, val: B) =
|
|||
|
||||
proc del*[A, B](t: var TTable[A, B], key: A) =
|
||||
## deletes `key` from hash table `t`.
|
||||
var index = RawGet(t, key)
|
||||
var index = rawGet(t, key)
|
||||
if index >= 0:
|
||||
t.data[index].slot = seDeleted
|
||||
dec(t.counter)
|
||||
|
|
@ -183,24 +179,24 @@ proc hasKey*[A](t: TCountTable[A], key: A): bool =
|
|||
## returns true iff `key` is in the table `t`.
|
||||
result = rawGet(t, key) >= 0
|
||||
|
||||
proc RawInsert[A](t: TCountTable[A], data: var seq[tuple[key: A, val: int]],
|
||||
proc rawInsert[A](t: TCountTable[A], data: var seq[tuple[key: A, val: int]],
|
||||
key: A, val: int) =
|
||||
var h: THash = hash(key) and high(data)
|
||||
while data[h].val != 0: h = nextTry(h, high(data))
|
||||
data[h].key = key
|
||||
data[h].val = val
|
||||
|
||||
proc Enlarge[A](t: var TCountTable[A]) =
|
||||
proc enlarge[A](t: var TCountTable[A]) =
|
||||
var n: seq[tuple[key: A, val: int]]
|
||||
newSeq(n, len(t.data) * growthFactor)
|
||||
for i in countup(0, high(t.data)):
|
||||
if t.data[i].val != 0: RawInsert(t, n, t.data[i].key, t.data[i].val)
|
||||
if t.data[i].val != 0: rawInsert(t, n, t.data[i].key, t.data[i].val)
|
||||
swap(t.data, n)
|
||||
|
||||
proc `[]=`*[A](t: var TCountTable[A], key: A, val: int) =
|
||||
## puts a (key, value)-pair into `t`. `val` has to be positive.
|
||||
assert val > 0
|
||||
PutImpl()
|
||||
putImpl()
|
||||
|
||||
proc initCountTable*[A](initialSize=64): TCountTable[A] =
|
||||
## creates a new count table that is empty. `initialSize` needs to be
|
||||
|
|
@ -224,11 +220,11 @@ proc inc*[A](t: var TCountTable[A], key: A, val = 1) =
|
|||
if index >= 0:
|
||||
inc(t.data[index].val, val)
|
||||
else:
|
||||
if mustRehash(len(t.data), t.counter): Enlarge(t)
|
||||
RawInsert(t, t.data, key, val)
|
||||
if mustRehash(len(t.data), t.counter): enlarge(t)
|
||||
rawInsert(t, t.data, key, val)
|
||||
inc(t.counter)
|
||||
|
||||
proc Smallest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
|
||||
proc smallest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
|
||||
## returns the largest (key,val)-pair. Efficiency: O(n)
|
||||
assert t.len > 0
|
||||
var minIdx = 0
|
||||
|
|
@ -237,7 +233,7 @@ proc Smallest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
|
|||
result.key = t.data[minIdx].key
|
||||
result.val = t.data[minIdx].val
|
||||
|
||||
proc Largest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
|
||||
proc largest*[A](t: TCountTable[A]): tuple[key: A, val: int] =
|
||||
## returns the (key,val)-pair with the largest `val`. Efficiency: O(n)
|
||||
assert t.len > 0
|
||||
var maxIdx = 0
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ proc toNum(b: TBuffer): int32 =
|
|||
while (ze(b[i]) and 128) != 0:
|
||||
inc(i)
|
||||
result = result or ((int32(ze(b[i])) and 127'i32) shl Shift)
|
||||
Shift = shift + 7'i32
|
||||
Shift = Shift + 7'i32
|
||||
if (ze(b[0]) and (1 shl 6)) != 0: # sign bit set?
|
||||
result = (not result) +% 1'i32
|
||||
# this is the same as ``- result``
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ some(10)'''
|
|||
import strutils
|
||||
|
||||
type Option[A] = object
|
||||
case isDefined*: Bool
|
||||
case isDefined*: bool
|
||||
of true:
|
||||
value*: A
|
||||
of false:
|
||||
|
|
@ -19,7 +19,7 @@ proc some[A](value: A): Option[A] =
|
|||
proc none[A](): Option[A] =
|
||||
Option[A](isDefined: false)
|
||||
|
||||
proc `$`[A](o: Option[A]): String =
|
||||
proc `$`[A](o: Option[A]): string =
|
||||
if o.isDefined:
|
||||
"some($1)" % [$o.value]
|
||||
else:
|
||||
|
|
@ -27,14 +27,14 @@ proc `$`[A](o: Option[A]): String =
|
|||
|
||||
let x = some("str")
|
||||
let y = some(5)
|
||||
let z = none[Int]()
|
||||
let z = none[int]()
|
||||
|
||||
echo x, ", ", y, ", ", z
|
||||
|
||||
proc intOrString[A : Int | String](o: Option[A]): Option[A] =
|
||||
when A is Int:
|
||||
proc intOrString[A : int | string](o: Option[A]): Option[A] =
|
||||
when A is int:
|
||||
some(o.value + 5)
|
||||
elif A is String:
|
||||
elif A is string:
|
||||
some(o.value & "!")
|
||||
else:
|
||||
o
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ a = high(int)
|
|||
b = -2
|
||||
try:
|
||||
writeln(stdout, b - a)
|
||||
except EOverflow:
|
||||
except OverflowError:
|
||||
writeln(stdout, "the computation overflowed")
|
||||
|
||||
{.pop.} # overflow check
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
discard """
|
||||
file: "toverflw2.nim"
|
||||
outputsub: "Error: unhandled exception: over- or underflow [EOverflow]"
|
||||
outputsub: "Error: unhandled exception: over- or underflow [OverflowError]"
|
||||
exitcode: "1"
|
||||
"""
|
||||
var a : int32 = 2147483647
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
proc doSomething(v: Int, x: proc(v:Int):Int): Int = return x(v)
|
||||
proc doSomething(v: Int, x: proc(v:Int)) = x(v)
|
||||
proc doSomething(v: int, x: proc(v:int):int): int = return x(v)
|
||||
proc doSomething(v: int, x: proc(v:int)) = x(v)
|
||||
|
||||
|
||||
echo doSomething(10, proc(v: Int): Int = return v div 2)
|
||||
echo doSomething(10, proc(v: int): int = return v div 2)
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
type
|
||||
PSDL_semaphore = ptr TSDL_semaphore
|
||||
TSDL_semaphore {.final.} = object
|
||||
sem: Pointer #PSem_t;
|
||||
sem: pointer #PSem_t;
|
||||
when not defined(USE_NAMED_SEMAPHORES):
|
||||
sem_data: int
|
||||
when defined(BROKEN_SEMGETVALUE):
|
||||
|
|
|
|||
|
|
@ -11,6 +11,6 @@ type
|
|||
kids: seq[TLegal]
|
||||
|
||||
TIllegal {.final.} = object #ERROR_MSG illegal recursion in type 'TIllegal'
|
||||
y: Int
|
||||
y: int
|
||||
x: array[0..3, TIllegal]
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ proc main =
|
|||
elif name == "name":
|
||||
echo("Very funny, your name is name.")
|
||||
else:
|
||||
Echo("Hi, ", name, "!")
|
||||
echo("Hi, ", name, "!")
|
||||
|
||||
let (x, y) = ("abc", name)
|
||||
echo y, x
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import strutils
|
|||
const
|
||||
HelpText = """
|
||||
+-----------------------------------------------------------------+
|
||||
| Maintenance program for Nim |
|
||||
| Maintenance program for Nim |
|
||||
| Version $1|
|
||||
| (c) 2012 Andreas Rumpf |
|
||||
+-----------------------------------------------------------------+
|
||||
|
|
@ -27,5 +27,5 @@ Possible Commands:
|
|||
""" % [NimVersion & repeatChar(44-len(NimVersion)),
|
||||
CompileDate, CompileTime]
|
||||
|
||||
echo helpText
|
||||
echo HelpText
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue