summary refs log tree commit diff stats
path: root/tests/accept/run
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-03-03 02:01:22 +0100
committerAraq <rumpf_a@web.de>2011-03-03 02:01:22 +0100
commite424e13bd9d05ab3fbcbe3572e49c1bc611a1fc9 (patch)
treee0c60da1e1ddd60d040f02508deb554f687f23aa /tests/accept/run
parentf93ca8e42a7c987bf03606319d9dfcc0772684f5 (diff)
downloadNim-e424e13bd9d05ab3fbcbe3572e49c1bc611a1fc9.tar.gz
various bugfixes for generics; added generic sort proc
Diffstat (limited to 'tests/accept/run')
-rwxr-xr-xtests/accept/run/tgenerics1.nim76
1 files changed, 36 insertions, 40 deletions
diff --git a/tests/accept/run/tgenerics1.nim b/tests/accept/run/tgenerics1.nim
index b30171831..e9ccd6917 100755
--- a/tests/accept/run/tgenerics1.nim
+++ b/tests/accept/run/tgenerics1.nim
@@ -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)
-
-when false:
-
-  proc parent(elem: int): int =
-      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]:
-              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])
+  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].priority < heap.heap[p].priority:
+      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 = priority
+  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].data
 
 var
-    heap: PBinHeap[int]
+  heap: PBinHeap[int]
 
 newBinHeap(heap, 256)
 add(heap, 1, 100)