summary refs log tree commit diff stats
path: root/compiler/pbraces.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-02-01 13:39:40 +0100
committerAraq <rumpf_a@web.de>2014-02-01 13:39:40 +0100
commit6d62503e5d8d195e3da7d9cb30782410a5d3c3ea (patch)
tree9d45451bb6dac4e810ea42d43c2424f3a5ac5f97 /compiler/pbraces.nim
parentd8d93218fa509b8980d5ecb4b8af4e70e9f4926e (diff)
downloadNim-6d62503e5d8d195e3da7d9cb30782410a5d3c3ea.tar.gz
documented new symbol binding rules for templates
Diffstat (limited to 'compiler/pbraces.nim')
0 files changed, 0 insertions, 0 deletions
0200 big rename' href='/ahoang/Nim/commit/lib/pure/algorithm.nim?h=devel&id=11b69587554a99deb93ca2447ee3aeeacdb647c5'>11b695875 ^
27869b6c7 ^
e424e13bd ^

e6b3f50c7 ^

e424e13bd ^















c591db16c ^

8b796763a ^
c591db16c ^

8b796763a ^
c591db16c ^







92b8fac94 ^
2633e3fb2 ^





a71c5f98e ^
2633e3fb2 ^









e424e13bd ^
32b4192b3 ^
e424e13bd ^
2f43fdb83 ^
baa304f37 ^






491291ae2 ^
76dfa611e ^




baa304f37 ^














2f43fdb83 ^
e424e13bd ^
27869b6c7 ^
e424e13bd ^
9083f01fd ^
8d19a93f1 ^

cfb107f34 ^
e424e13bd ^












cfb107f34 ^
e424e13bd ^


92b8fac94 ^
e424e13bd ^














cfb107f34 ^
e424e13bd ^




e6b3f50c7 ^
8d99753d6 ^
27869b6c7 ^
d05df2173 ^
e424e13bd ^


d05df2173 ^
e6b3f50c7 ^


4523b29d7 ^
e6b3f50c7 ^

76886432d ^


e6b3f50c7 ^

7da3c5e71 ^


4523b29d7 ^
7da3c5e71 ^




e424e13bd ^










2f43fdb83 ^
ac0f15379 ^















e01fb17d0 ^
ac0f15379 ^














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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

 
                                  
                                         







                                                         
                                   

                          


                                       
                                                      

                                                    

                                                                      















                                                         

                                                              
                                      

               
                  







                                            
                                                     





                                                             
                                                      









                                                          
     
                     
 
                                                                                       






                                                                                                
     




                                                          














                            
                                                                              
                                                      
                                                                       
                               
               

                      
                       












                                                                    
                   


             
                                                      














                                    
                  




                                                              
                                  
                                                   
                                            
                                                                  


                                                                
                                                             


                                                                      
                        

                                         


                                                                       

                                    


                                                                
                        




                                            










                                                    
                                                     















                                                                     
                             














                                     
#
#
#            Nim's Runtime Library
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements some common generic algorithms.

type
  SortOrder* = enum   ## sort order
    Descending, Ascending 

{.deprecated: [TSortOrder: SortOrder].}


proc `*`*(x: int, order: SortOrder): int {.inline.} = 
  ## flips `x` if ``order == Descending``;
  ## if ``order == Ascending`` then `x` is returned.
  ## `x` is supposed to be the result of a comparator, ie ``< 0`` for 
  ## *less than*, ``== 0`` for *equal*, ``> 0`` for *greater than*.
  var y = order.ord - 1
  result = (x xor y) - y

proc reverse*[T](a: var openArray[T], first, last: int) =
  ## reverses the array ``a[first..last]``.
  var x = first
  var y = last
  while x < y:
    swap(a[x], a[y])
    dec(y)
    inc(x)

proc reverse*[T](a: var openArray[T]) =
  ## reverses the array `a`.
  reverse(a, 0, a.high)

proc reversed*[T](a: openArray[T], first, last: int): seq[T] =
  ## returns the reverse of the array `a[first..last]`.
  result = newSeq[T](last - first + 1)
  var x = first
  var y = last
  while x <= last:
    result[x] = a[y]
    dec(y)
    inc(x)

proc reversed*[T](a: openArray[T]): seq[T] =
  ## returns the reverse of the array `a`.
  reversed(a, 0, a.high)

proc binarySearch*[T](a: openArray[T], key: T): int =
  ## binary search for `key` in `a`. Returns -1 if not found.
  var b = len(a)
  while result < b:
    var mid = (result + b) div 2
    if a[mid] < key: result = mid + 1
    else: b = mid
  if result >= len(a) or a[result] != key: result = -1

proc smartBinarySearch*[T](a: openArray[T], key: T): int =
  ## ``a.len`` must be a power of 2 for this to work.
  var step = a.len div 2
  while step > 0:
    if a[result or step] <= key:
      result = result or step
    step = step shr 1
  if a[result] != key: result = -1

const
  onlySafeCode = true

proc lowerBound*[T](a: openArray[T], key: T, cmp: proc(x,y: T): int {.closure.}): int =
  ## same as binarySearch except that if key is not in `a` then this 
  ## returns the location where `key` would be if it were. In other
  ## words if you have a sorted sequence and you call insert(thing, elm, lowerBound(thing, elm))
  ## the sequence will still be sorted
  ##
  ## `cmp` is the comparator function to use, the expected return values are the same as
  ## that of system.cmp
  ## 
  ## example::
  ##
  ##   var arr = @[1,2,3,5,6,7,8,9]
  ##   arr.insert(4, arr.lowerBound(4))
  ## `after running the above arr is `[1,2,3,4,5,6,7,8,9]`
  result = a.low
  var pos = result
  var count, step: int
  count = a.high - a.low + 1
  while count != 0:
    pos = result
    step = count div 2
    pos += step
    if cmp(a[pos], key) < 0:
      pos.inc
      result = pos
      count -= step + 1
    else:
      count = step

proc lowerBound*[T](a: openArray[T], key: T): int = lowerBound(a, key, cmp[T])
proc merge[T](a, b: var openArray[T], lo, m, hi: int, 
              cmp: proc (x, y: T): int {.closure.}, order: SortOrder) =
  template `<-` (a, b: expr) = 
    when false:
      a = b
    elif onlySafeCode:
      shallowCopy(a, b)
    else:
      copyMem(addr(a), addr(b), sizeof(T))
  # optimization: If max(left) <= min(right) there is nothing to do!
  # 1 2 3 4  ## 5 6 7 8
  # -> O(n) for sorted arrays.
  # On random data this safes up to 40% of merge calls
  if cmp(a[m], a[m+1]) * order <= 0: return
  var j = lo
  # copy a[j..m] into b:
  assert j <= m
  when onlySafeCode:
    var bb = 0
    while j <= m:
      b[bb] <- a[j]
      inc(bb)
      inc(j)
  else:
    copyMem(addr(b[0]), addr(a[j]), sizeof(T)*(m-j+1))
    j = m+1
  var i = 0
  var k = lo
  # copy proper element back:
  while k < j and j <= hi:
    if cmp(b[i], a[j]) * order <= 0:
      a[k] <- b[i]
      inc(i)
    else:
      a[k] <- a[j]
      inc(j)
    inc(k)
  # copy rest of b:
  when onlySafeCode:
    while k < j:
      a[k] <- b[i]
      inc(k)
      inc(i)
  else:
    if k < j: copyMem(addr(a[k]), addr(b[i]), sizeof(T)*(j-k))

proc sort*[T](a: var openArray[T],
              cmp: proc (x, y: T): int {.closure.},
              order = SortOrder.Ascending) =
  ## Default Nim sort. The sorting is guaranteed to be stable and 
  ## the worst case is guaranteed to be O(n log n).
  ## The current implementation uses an iterative
  ## mergesort to achieve this. It uses a temporary sequence of 
  ## length ``a.len div 2``. Currently Nim does not support a
  ## sensible default argument for ``cmp``, so you have to provide one
  ## of your own. However, the ``system.cmp`` procs can be used:
  ##
  ## .. code-block:: nim
  ##
  ##    sort(myIntArray, system.cmp[int])
  ##
  ##    # do not use cmp[string] here as we want to use the specialized
  ##    # overload:
  ##    sort(myStrArray, system.cmp)
  ##
  ## You can inline adhoc comparison procs with the `do notation
  ## <manual.html#do-notation>`_. Example:
  ##
  ## .. code-block:: nim
  ##
  ##   people.sort do (x, y: Person) -> int:
  ##     result = cmp(x.surname, y.surname)
  ##     if result == 0:
  ##       result = cmp(x.name, y.name)
  var n = a.len
  var b: seq[T]
  newSeq(b, n div 2)
  var s = 1
  while s < n:
    var m = n-1-s
    while m >= 0:
      merge(a, b, max(m-s+1, 0), m, m+s, cmp, order)
      dec(m, s*2)
    s = s*2

proc product*[T](x: openArray[seq[T]]): seq[seq[T]] =
  ## produces the Cartesian product of the array. Warning: complexity
  ## may explode.
  result = @[]
  if x.len == 0:
    return
  if x.len == 1:
    result = @x
    return
  var
    indexes = newSeq[int](x.len)
    initial = newSeq[int](x.len)
    index = 0
  # replace with newSeq as soon as #853 is fixed
  var next: seq[T] = @[]
  next.setLen(x.len)
  for i in 0..(x.len-1):
    if len(x[i]) == 0: return
    initial[i] = len(x[i])-1
  indexes = initial
  while true:
    while indexes[index] == -1:
      indexes[index] = initial[index]
      index +=1
      if index == x.len: return
      indexes[index] -=1
    for ni, i in indexes:
      next[ni] = x[ni][i]
    var res: seq[T]
    shallowCopy(res, next)
    result.add(res)
    index = 0
    indexes[index] -=1