Skip to content

Commit

Permalink
SortedSequence for C++19
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Mar 8, 2025
1 parent 9563427 commit 7017404
Showing 1 changed file with 100 additions and 1 deletion.
101 changes: 100 additions & 1 deletion libraries/SortedSequence.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// C++19

template<class _T>
class SortedSequence {
private:
Expand All @@ -23,6 +25,11 @@ class SortedSequence {
elements->insert(pos, x);
}

_T at(int i)
{
return elements->at(i);
}

void excluding(_T x)
{
auto pos = std::equal_range(elements->begin(), elements->end(), x);
Expand Down Expand Up @@ -114,5 +121,97 @@ class SortedSequence {
if (sze == 0) { return NULL; }
return elements->at(sze-1);
}
};

SortedSequence<_T>* subSequence(int i, int j)
{ /* OCL indexing i, j: 1..n */

int n = elements->size();
if (n == 0)
{
return new SortedSequence<_T>();
}

if (i < 1) { i = 1; }

vector<_T>* elems = new vector<_T>();
SortedSequence<_T>* res = new SortedSequence<_T>();

for (int k = i-1; k < j && k < n; k++)
{
elems->push_back(elements->at(k));
}
res->elements = elems;
return res;
}

SortedSequence<_T>* front()
{
int n = elements->size();
if (n == 0)
{
return new SortedSequence<_T>();
}
return subSequence(1, n - 1);
}

SortedSequence<_T>* tail()
{
int n = elements->size();
if (n == 0)
{
return new SortedSequence<_T>();
}
return subSequence(2, n);
}

SortedSequence<_T>* select(std::function<bool(_T)> f)
{
vector<_T>* elems = new vector<_T>();

for (int i = 0; i < elements->size(); ++i)
{
_T x = elements->at(i);
if (f(x))
{
elems->push_back(x);
}
}

SortedSequence<_T>* res = new SortedSequence<_T>();
res->elements = elems;
return res;
}

SortedSequence<_T>* reject(std::function<bool(_T)> f)
{
vector<_T>* elems = new vector<_T>();

for (int i = 0; i < elements->size(); ++i)
{
_T x = elements->at(i);
if (f(x)) {}
else
{
elems->push_back(x);
}
}

SortedSequence<_T>* res = new SortedSequence<_T>();
res->elements = elems;
return res;
}

template<class R>
vector<R>* collect(std::function<R(_T)> f)
{
vector<R>* res = new vector<R>();
for (int i = 0; i < elements->size(); i++)
{
_T x = elements->at(i);
res->push_back(f(x));
}

return res;
}

};

0 comments on commit 7017404

Please sign in to comment.