Skip to content

Commit 4da6622

Browse files
committed
Added basic algorithms for List.
1 parent 5587f6a commit 4da6622

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

Source/DFPSR/api/algorithmAPI_List.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,125 @@ String& string_toStreamIndented(String& target, const List<T>& collection, const
6060
return target;
6161
}
6262

63+
// Returns an index to the first element in list matching find, or -1 if none could be found.
64+
template <typename T>
65+
int64_t list_findFirst(const dsr::List<T> &list, const T &find) {
66+
for (int64_t e = 0; e < list.length(); e++) {
67+
if (list[e] == find) {
68+
return e;
69+
}
70+
}
71+
return -1;
72+
}
73+
74+
// Returns an index to the first element in list where condition returns true, or -1 if the condition returned false for all elements.
75+
template <typename T>
76+
int64_t list_findFirst(const dsr::List<T> &list, const TemporaryCallback<bool(const T &element)> &condition) {
77+
for (int64_t e = 0; e < list.length(); e++) {
78+
if (condition(list[e])) {
79+
return e;
80+
}
81+
}
82+
return -1;
83+
}
84+
85+
// Returns an index to the last element in list matching find, or -1 if none could be found.
86+
template <typename T>
87+
int64_t list_findLast(const dsr::List<T> &list, const T &find) {
88+
for (int64_t e = list.length() - 1; e >= 0; e--) {
89+
if (list[e] == find) {
90+
return e;
91+
}
92+
}
93+
return -1;
94+
}
95+
96+
// Returns an index to the last element in list where condition returns true, or -1 if the condition returned false for all elements.
97+
template <typename T>
98+
int64_t list_findLast(const dsr::List<T> &list, const TemporaryCallback<bool(const T &element)> &condition) {
99+
for (int64_t e = list.length() - 1; e >= 0; e--) {
100+
if (condition(list[e])) {
101+
return e;
102+
}
103+
}
104+
return -1;
105+
}
106+
107+
// Returns true iff find matches any element in list.
108+
template <typename T>
109+
bool list_elementExists(const dsr::List<T> &list, const T &find) {
110+
return list_findFirst(list, find) != -1;
111+
}
112+
113+
// Returns true iff condition is satisfied for any element in list.
114+
template <typename T>
115+
bool list_elementExists(const dsr::List<T> &list, const TemporaryCallback<bool(const T &element)> &condition) {
116+
return list_findFirst(list, condition) != -1;
117+
}
118+
119+
// Returns true iff find does not exist in list.
120+
template <typename T>
121+
bool list_elementIsMissing(const dsr::List<T> &list, const T &find) {
122+
return list_findFirst(list, find) == -1;
123+
}
124+
125+
// Returns true iff condition is not satisfied for any element in list.
126+
template <typename T>
127+
bool list_elementIsMissing(const dsr::List<T> &list, const TemporaryCallback<bool(const T &element)> &condition) {
128+
return list_findFirst(list, condition) == -1;
129+
}
130+
131+
// Pushes element to targetList and return true iff list_elementIsMissing.
132+
template <typename T>
133+
bool list_insertUnique_last(dsr::List<T> &targetList, const T &element) {
134+
if (list_elementIsMissing(targetList, element)) {
135+
targetList.push(element);
136+
return true;
137+
} else {
138+
return false;
139+
}
140+
}
141+
142+
// Pushes element to a sorted location in targetList and return true iff list_elementIsMissing.
143+
// Pre-condition; targetList is sorted according to the < operator when beginning the call.
144+
// Side-effect: targetList will remain sorted if it was sorted from the start.
145+
template <typename T>
146+
bool list_insertUnique_sorted(dsr::List<T> &targetList, const T &element) {
147+
if (list_elementIsMissing(targetList, element)) {
148+
targetList.push(element);
149+
int64_t at = targetList.length() - 1;
150+
while (at > 0 && targetList[at] < targetList[at - 1]) {
151+
targetList.swap(at, at - 1);
152+
at--;
153+
}
154+
return true;
155+
} else {
156+
return false;
157+
}
158+
}
159+
160+
// Pushes all elements in sourceList that does not already exist in targetList.
161+
// Returns true iff any element was pushed to targetList.
162+
template <typename T>
163+
bool list_insertUnion_last(dsr::List<T> &targetList, const dsr::List<T> &sourceList) {
164+
bool result = false;
165+
for (int64_t e = 0; e < sourceList.length(); e++) {
166+
result = result || list_insertUnique_last(targetList, sourceList[e]);
167+
}
168+
return result;
169+
}
170+
171+
// Pushes all elements in sourceList that does not already exist in targetList.
172+
// Returns true iff any element was pushed to targetList.
173+
template <typename T>
174+
bool list_insertUnion_sorted(dsr::List<T> &targetList, const dsr::List<T> &sourceList) {
175+
bool result = false;
176+
for (int64_t e = 0; e < sourceList.length(); e++) {
177+
result = result || list_insertUnique_sorted(targetList, sourceList[e]);
178+
}
179+
return result;
180+
}
181+
63182
}
64183

65184
#endif

0 commit comments

Comments
 (0)