mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-15 23:54:19 +00:00
various bugfixes for generics; added generic sort proc
This commit is contained in:
69
tests/accept/compile/tsortdev.nim
Normal file
69
tests/accept/compile/tsortdev.nim
Normal 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"
|
||||
|
||||
@@ -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
11
tests/gc/gcleak.nim
Normal file
@@ -0,0 +1,11 @@
|
||||
type
|
||||
TTestObj = object of TObject
|
||||
x: string
|
||||
|
||||
proc MakeObj(): TTestObj =
|
||||
result.x = "Hello"
|
||||
|
||||
while true:
|
||||
var obj = MakeObj()
|
||||
|
||||
|
||||
55
tests/reject/ttypenoval.nim
Normal file
55
tests/reject/ttypenoval.nim
Normal 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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user