forked from translunar/nmatrix
/
sl_list.h
242 lines (184 loc) · 5.86 KB
/
sl_list.h
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/////////////////////////////////////////////////////////////////////
// = NMatrix
//
// A linear algebra library for scientific computation in Ruby.
// NMatrix is part of SciRuby.
//
// NMatrix was originally inspired by and derived from NArray, by
// Masahiro Tanaka: http://narray.rubyforge.org
//
// == Copyright Information
//
// SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
// NMatrix is Copyright (c) 2012, Ruby Science Foundation
//
// Please see LICENSE.txt for additional copyright notices.
//
// == Contributing
//
// By contributing source code to SciRuby, you agree to be bound by
// our Contributor Agreement:
//
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
//
// == sl_list.h
//
// Singly-linked list implementation
#ifndef SL_LIST_H
#define SL_LIST_H
/*
* Standard Includes
*/
#include <cstdlib>
/*
* Project Includes
*/
#include "types.h"
#include "data/data.h"
#include "nmatrix.h"
/*
* Macros
*/
/*
* Types
*/
/*
* Data
*/
/*
* Functions
*/
////////////////
// Lifecycle //
///////////////
LIST* list_create(void);
void list_delete(LIST* list, size_t recursions);
void list_mark(LIST* list, size_t recursions);
///////////////
// Accessors //
///////////////
NODE* list_insert(LIST* list, bool replace, size_t key, void* val);
NODE* list_insert_after(NODE* node, size_t key, void* val);
void* list_remove(LIST* list, size_t key);
template <typename Type>
inline NODE* list_insert_val_helper(LIST* list, NODE* node, size_t key, Type val) {
Type* val_mem = ALLOC(Type);
*val_mem = val;
if (node == NULL) {
return list_insert(list, false, key, val_mem);
} else {
return list_insert_after(node, key, val_mem);
}
}
inline NODE* list_insert_ptr_helper(LIST* list, NODE* node, size_t key, void* ptr) {
if (node == NULL) {
return list_insert(list, false, key, ptr);
} else {
return list_insert_after(node, key, ptr);
}
}
///////////
// Tests //
///////////
/*
* Do all values in a list == some value?
*
* Note that the template parameters should line up with the first two function parameters. This differs from most
* other eqeq functions, which use left and right dtypes.
*/
template <typename ListDType, typename ValueDType>
bool list_eqeq_value_template(const LIST* l, const ValueDType* v, size_t recursions, size_t& checked) {
NODE *next, *curr = l->first;
while (curr) {
next = curr->next;
if (recursions == 0) {
++checked;
if (*reinterpret_cast<ListDType*>(curr->val) != *v) return false;
} else if (!list_eqeq_value_template<ListDType,ValueDType>((LIST*)curr->val, v, recursions - 1, checked)) {
return false;
}
curr = next;
}
return true;
}
/*
* Are all values in the two lists equal? If one is missing a value, but the
* other isn't, does the value in the list match the default value?
*/
template <typename LDType, typename RDType>
bool list_eqeq_list_template(const LIST* left, const LIST* right, const LDType* left_val, const RDType* right_val, size_t recursions, size_t& checked) {
NODE *lnext = NULL, *lcurr = left->first, *rnext = NULL, *rcurr = right->first;
if (lcurr) lnext = lcurr->next;
if (rcurr) rnext = rcurr->next;
while (lcurr && rcurr) {
if (lcurr->key == rcurr->key) {
// MATCHING KEYS
if (recursions == 0) {
++checked;
if (*reinterpret_cast<LDType*>(lcurr->val) != *reinterpret_cast<RDType*>(rcurr->val)) return false;
} else if (!list_eqeq_list_template<LDType,RDType>(reinterpret_cast<LIST*>(lcurr->val), (LIST*)rcurr->val, left_val, right_val, recursions - 1, checked)) {
return false;
}
// increment both iterators
rcurr = rnext;
if (rcurr) rnext = rcurr->next;
lcurr = lnext;
if (lcurr) lnext = lcurr->next;
} else if (lcurr->key < rcurr->key) {
// NON-MATCHING KEYS
if (recursions == 0) {
// compare left entry to right default value
++checked;
if (*reinterpret_cast<LDType*>(lcurr->val) != *right_val) return false;
} else if (!list_eqeq_value_template<LDType,RDType>(reinterpret_cast<LIST*>(lcurr->val), right_val, recursions - 1, checked)) {
return false;
}
// increment left iterator
lcurr = lnext;
if (lcurr) lnext = lcurr->next;
} else {
// if (rcurr->key < lcurr->key)
if (recursions == 0) {
// compare right entry to left default value
++checked;
if (*reinterpret_cast<RDType*>(rcurr->val) != *left_val) return false;
} else if (!list_eqeq_value_template<RDType,LDType>(reinterpret_cast<LIST*>(rcurr->val), left_val, recursions - 1, checked)) {
return false;
}
// increment right iterator
rcurr = rnext;
if (rcurr) rnext = rcurr->next;
}
}
/*
* One final check, in case we get to the end of one list but not the other
* one.
*/
if (lcurr) {
// nothing left in right-hand list
if (*reinterpret_cast<LDType*>(lcurr->val) != *right_val) return false;
} else if (rcurr) {
// nothing left in left-hand list
if (*reinterpret_cast<RDType*>(rcurr->val) != *left_val) return false;
}
/*
* Nothing different between the two lists -- but make sure after this return
* that you compare the default values themselves, if we haven't visited
* every value in the two matrices.
*/
return true;
}
/////////////
// Utility //
/////////////
NODE* list_find(LIST* list, size_t key);
NODE* list_find_preceding_from(NODE* prev, size_t key);
NODE* list_find_nearest(LIST* list, size_t key);
NODE* list_find_nearest_from(NODE* prev, size_t key);
/////////////////////////
// Copying and Casting //
/////////////////////////
template <typename LDType, typename RDType>
void list_cast_copy_contents_template(LIST* lhs, const LIST* rhs, size_t recursions);
void list_cast_copy_contents(LIST* lhs, const LIST* rhs, dtype_t lhs_dtype, dtype_t rhs_dtype, size_t recursions);
#endif // SL_LIST_H