summary refs log tree commit diff stats
path: root/tests/generics/tgeneric0.nim
blob: 44c34917d1ac0b57925bc70b55309c838915b3e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
discard """
  output: '''
100
0
float32
float32
(name: "Resource 1", readers: ..., writers: ...)
'''
"""


import tables


block tgeneric0:
  type
    TX = Table[string, int]

  proc foo(models: seq[Table[string, float]]): seq[float] =
    result = @[]
    for model in models.items:
      result.add model["foobar"]

  # bug #686
  type TType[T; A] = array[A, T]

  proc foo[T](p: TType[T, range[0..1]]) =
    echo "foo"
  proc foo[T](p: TType[T, range[0..2]]) =
    echo "bar"

  #bug #1366

  proc reversed(x: auto) =
    for i in countdown(x.low, x.high):
      echo i

  reversed(@[-19, 7, -4, 6])



block tgeneric1:
  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].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):
      stdout.write($heap.heap[i].data, "\n")

  var heap: PBinHeap[int]

  newBinHeap(heap, 256)
  add(heap, 1, 100)
  print(heap)



block tgeneric2:
  type
    TX = Table[string, int]

  proc foo(models: seq[TX]): seq[int] =
    result = @[]
    for model in models.items:
      result.add model["foobar"]

  type
    Obj = object
      field: Table[string, string]
  var t: Obj
  discard initTable[type(t.field), string]()



block tgeneric4:
  type
    TIDGen[A: Ordinal] = object
      next: A
      free: seq[A]

  proc newIDGen[A]: TIDGen[A] =
      newSeq result.free, 0

  var x = newIDGen[int]()

block tgeneric5:
  # bug #12528
  proc foo[T](a: T; b: T) =
    echo T

  foo(0.0'f32, 0.0)

  proc bar[T](a: T; b: T = 0.0) =
    echo T

  bar(0.0'f32)

# bug #13378

type
  Resource = ref object of RootObj
    name: string
    readers, writers: seq[RenderTask]

  RenderTask = ref object
    name: string

var res = Resource(name: "Resource 1")

(proc (r: typeof(res)) =
   echo r[])(res)

# bug #4061

type List[T] = object
  e: T
  n: ptr List[T]

proc zip*[T,U](xs: List[T], ys: List[U]): List[(T,U)] = discard

proc unzip*[T,U](xs: List[tuple[t: T, u: U]]): (List[T], List[U]) = discard

proc unzip2*[T,U](xs: List[(T,U)]): (List[T], List[U]) = discard
n>d = chr(idx - 62 + 128) if v != 0: self(v, result) add(result, d) proc encodeVBiggestIntAux(x: BiggestInt, result: var string) = ## encode a biggest int as a variable length base 190 int. encodeIntImpl(encodeVBiggestIntAux) proc encodeVBiggestInt*(x: BiggestInt, result: var string) = ## encode a biggest int as a variable length base 190 int. encodeVBiggestIntAux(x +% vintDelta, result) # encodeIntImpl(encodeVBiggestInt) proc encodeVIntAux(x: int, result: var string) = ## encode an int as a variable length base 190 int. encodeIntImpl(encodeVIntAux) proc encodeVInt*(x: int, result: var string) = ## encode an int as a variable length base 190 int. encodeVIntAux(x +% vintDelta, result) template decodeIntImpl() = var i = pos var sign = - 1 assert(s[i] in {'a'..'z', 'A'..'Z', '0'..'9', '-', '\x80'..'\xFF'}) if s[i] == '-': inc(i) sign = 1 result = 0 while true: case s[i] of '0'..'9': result = result * 190 - (ord(s[i]) - ord('0')) of 'a'..'z': result = result * 190 - (ord(s[i]) - ord('a') + 10) of 'A'..'Z': result = result * 190 - (ord(s[i]) - ord('A') + 36) of '\x80'..'\xFF': result = result * 190 - (ord(s[i]) - 128 + 62) else: break inc(i) result = result * sign -% vintDelta pos = i proc decodeVInt*(s: cstring, pos: var int): int = decodeIntImpl() proc decodeVBiggestInt*(s: cstring, pos: var int): biggestInt = decodeIntImpl() iterator decodeVIntArray*(s: cstring): int = var i = 0 while s[i] != '\0': yield decodeVInt(s, i) if s[i] == ' ': inc i iterator decodeStrArray*(s: cstring): string = var i = 0 while s[i] != '\0': yield decodeStr(s, i) if s[i] == ' ': inc i