various bugfixes for generics; added generic sort proc

This commit is contained in:
Araq
2011-03-03 02:01:22 +01:00
parent f93ca8e42a
commit e424e13bd9
18 changed files with 392 additions and 113 deletions

View File

@@ -0,0 +1,69 @@
import math, algorithm
proc sorted[T](a: openArray[T], order: TSortOrder): bool =
result = true
for i in 0 .. < a.high:
if cmp(a[i], a[i+1]) * order > 0:
echo "Out of order: ", a[i], " ", a[i+1]
result = false
proc bubbleSort[T](a: var openArray[T],
cmp: proc (x, y: T): int = cmp,
order = TSortOrder.Ascending) =
while true:
var sorted = true
for i in 0 .. a.len-2:
if cmp(a[i], a[i+1]) * order > 0:
swap(a[i], a[i+1])
sorted = false
if sorted: break
when isMainModule:
proc main() =
const order = Ascending
var data: seq[string] = @[]
for i in 0..10_000:
var L = random(59)
setLen(data, L)
for j in 0 .. L-1:
data[j] = $(math.random(90) - 10)
var copy = data
sort(data, cmp, order)
if not sorted(data, order):
#for x in items(data): echo x
break
else:
echo "SUCCESS!"
bubblesort(copy, cmp, order)
if copy.len != data.len:
quit "lengths differ!"
for i in 0 .. copy.high:
if copy[i] != data[i]:
quit "algorithms differ!"
for i in 0..10_000:
var data: seq[int] = @[]
var L = random(59)
setLen(data, L)
for j in 0 .. L-1:
data[j] = (math.random(90) - 10)
var copy = data
sort(data, cmp[int, int], order)
if not sorted(data, order):
#for x in items(data): echo x
break
else:
echo "SUCCESS!"
bubblesort(copy)
if copy.len != data.len:
quit "lengths differ!"
for i in 0 .. copy.high:
if copy[i] != data[i]:
quit "algorithms differ!"
main()
echo "done"

View File

@@ -4,51 +4,47 @@ discard """
# A min-heap.
type
TNode[T] = tuple[priority: int, data: T]
TNode[T] = tuple[priority: int, data: T]
TBinHeap[T] = object
heap: seq[TNode[T]]
last: int
PBinHeap[T] = ref TBinHeap[T]
TBinHeap[T] = object
heap: seq[TNode[T]]
last: int
PBinHeap[T] = ref TBinHeap[T]
proc newBinHeap*[T](heap: var PBinHeap[T], size: int) =
new(heap)
heap.last = 0
newSeq(heap.seq, size)
new(heap)
heap.last = 0
newSeq(heap.heap, size)
#newSeq(heap.seq, size)
proc parent(elem: int): int {.inline.} =
return (elem-1) div 2
when false:
proc siftUp[T](heap: PBinHeap[T], elem: int) =
var idx = elem
while idx != 0:
var p = parent(idx)
if heap.heap[idx].priority < heap.heap[p].priority:
swap(heap.heap[idx], heap.heap[p])
idx = p
else:
break
proc parent(elem: int): int =
return (elem-1) div 2
proc add*[T](heap: PBinHeap[T], priority: int, data: T) =
var node: TNode[T]
node.priority = priority
node.data = data
heap.heap[heap.last] = node
siftUp(heap, heap.last)
inc(heap.last)
proc siftUp[T](heap: PBinHeap[T], elem: int) =
var idx = elem
while idx != 0:
var p = parent(idx)
if heap.heap[idx] < heap.heap[p]:
var tmp = heap.heap[idx]
heap.heap[idx] = heap.heap[p]
heap.heap[p] = tmp
idx = p
else:
break
proc add*[T](heap: PBinHeap[T], priority: int, data: T) =
var node: TNode
new(node)
node.priority = int
node.data = data
heap.heap[heap.last] = node
siftUp(heap, heap.last)
inc(heap.last)
proc print*[T](heap: PBinHeap[T]) =
for i in countup(0, heap.last):
echo($heap.heap[i])
proc print*[T](heap: PBinHeap[T]) =
for i in countup(0, heap.last):
echo heap.heap[i].data
var
heap: PBinHeap[int]
heap: PBinHeap[int]
newBinHeap(heap, 256)
add(heap, 1, 100)

11
tests/gc/gcleak.nim Normal file
View File

@@ -0,0 +1,11 @@
type
TTestObj = object of TObject
x: string
proc MakeObj(): TTestObj =
result.x = "Hello"
while true:
var obj = MakeObj()

View File

@@ -0,0 +1,55 @@
discard """
file: "tambsym.nim"
line: 36
errormsg: "a type has no value"
"""
# A min-heap.
type
TNode[T] = tuple[priority: int, data: T]
TBinHeap[T] = object
heap: seq[TNode[T]]
last: int
PBinHeap[T] = ref TBinHeap[T]
proc newBinHeap*[T](heap: var PBinHeap[T], size: int) =
new(heap)
heap.last = 0
newSeq(heap.heap, size)
#newSeq(heap.seq, size)
proc parent(elem: int): int {.inline.} =
return (elem-1) div 2
proc siftUp[T](heap: PBinHeap[T], elem: int) =
var idx = elem
while idx != 0:
var p = parent(idx)
if heap.heap[idx] < heap.heap[p]:
swap(heap.heap[idx], heap.heap[p])
idx = p
else:
break
proc add*[T](heap: PBinHeap[T], priority: int, data: T) =
var node: TNode[T]
node.priority = int
node.data = data
heap.heap[heap.last] = node
siftUp(heap, heap.last)
inc(heap.last)
proc print*[T](heap: PBinHeap[T]) =
for i in countup(0, heap.last):
echo($heap.heap[i])
var
heap: PBinHeap[int]
newBinHeap(heap, 256)
add(heap, 1, 100)
print(heap)