-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_algorithm.nim
196 lines (155 loc) · 4.47 KB
/
cpp_algorithm.nim
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
# TODO: add openarray type and int for each
let v = @[1.0,2.0,3.0,3.0,3.0]
#[ Implementations as I would prefer them (not direct translations from C++) ]#
# Find algorithms
template find*(sequence, search_for: untyped): int =
var result = 0
for it {.inject.} in items(sequence):
if it == search_for:
break
inc result
result
template find_if*(sequence, search_for: untyped): int =
var result = 0
for it {.inject.} in items(sequence):
if search_for:
break
inc result
result
template find_if_not*(sequence, search_for: untyped): int =
var result = 0
for it {.inject.} in items(sequence):
if search_for == false:
break
inc result
result
echo v.find(3.0)
echo v.find_if(it > 2)
echo v.find_if_not(it < 2)
# Any algorithms
template any_of*(sequence, search_for: untyped): bool =
var result = false
for it {.inject.} in items(sequence):
if search_for:
result = true
break
result
template all_of*(sequence, search_for: untyped): bool =
var result = false
for it {.inject.} in items(sequence):
if search_for:
result = true
break
result
template none_of*(sequence, search_for: untyped): bool =
var result = true
for it {.inject.} in items(sequence):
if search_for:
result = false
break
result
echo v.any_of(it > 3)
echo v.all_of(it < 20)
echo v.none_of(it > 5)
# Count Algorithms
template count*(sequence, search_for: untyped): int =
var result = 0
for it {.inject.} in items(sequence):
if search_for:
inc result
result
template count_if*(sequence, search_for: untyped): int =
var result = 0
for it {.inject.} in items(sequence):
if search_for:
inc result
result
echo v.count(it == 3.0)
echo v.count_if(it > 2)
#[ Implementations Directly from C++ ]#
# Find Algorithms
template find*(sequence, first, last, search_for: untyped): int =
var result = first
for it {.inject.} in sequence[first..last]:
if search_for:
break
inc result
result
template find_if*(sequence, first, last, search_for: untyped): int =
var result = first
for it {.inject.} in sequence[first..last]:
if search_for:
break
inc result
result
template find_if_not*(sequence, first, last, search_for: untyped): int =
var result = first
for it {.inject.} in sequence[first..last]:
if search_for == false:
break
inc result
result
echo v.find(v.low, v.high, it == 3.0)
echo v.find_if(v.low, v.high, it > 2)
echo v.find_if_not(v.low, v.high, it < 2)
# Any Algorithms
template all_of*(sequence, first, last, search_for: untyped): bool =
var result = false
if find_if_not(sequence, first, last, search_for) == sequence.len:
result = true
result
template any_of*(sequence, first, last, search_for: untyped): bool =
var result = false
if find_if(sequence, first, last, search_for) != sequence.len:
result = true
result
template none_of*(sequence, first, last, search_for: untyped): bool =
var result = false
if find(sequence, first, last, search_for) == sequence.len:
result = true
result
echo v.any_of(v.low, v.high, it > 3)
echo v.all_of(v.low, v.high, it < 20)
echo v.none_of(v.low, v.high, it > 5)
# Count Algorithms
template count*(sequence, first, last, search_for: untyped): int =
var result = 0
for it {.inject.} in sequence[first..last]:
if search_for:
inc result
result
template count_if*(sequence, first, last, search_for: untyped): int =
var result = 0
for it {.inject.} in sequence[first..last]:
if search_for:
inc result
result
echo v.count(v.low, v.high, it == 3.0)
echo v.count_if(v.low, v.high, it > 2)
# Mismatch Algorithms
var x = @[1.0,3.0,3.0,3.0,3.0]
template mismatch*(sequence, first1, last1, first2: untyped): tuple =
var
f1 = first1
l1 = last1
f2 = first2
result = (sequence[f1], sequence[f2])
while f1 != l1 and f1 == f2:
inc f1, f2
result
echo v.mismatch(v.low, v.high, x.low)
echo v.mismatch(v.low, v.high, x.low)
# For Each change these not to modify in place
proc twos(a: var float): float =
a * 2.0
template for_each*[T](sequence: var seq[T]; first, last: int; procedure: proc): seq[T] =
for i in first..last:
sequence[i] = procedure(sequence[i])
sequence
template for_each_n*[T](sequence: var seq[T]; first, n: int; procedure: proc): seq[T] =
# raise exception if n > high
for i in first..<first + n:
sequence[i] = procedure(sequence[i])
sequence
echo x.for_each(x.low, x.high, twos)
echo x.for_each_n(x.low, 2, twos)