-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector.h
89 lines (80 loc) · 1.72 KB
/
Vector.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
#ifndef SFL_VECTOR_H
#define SFL_VECTOR_H
#include <vector>
#include "sfl/Maybe.h"
#include "sfl/Prelude.h"
namespace sfl
{
namespace Vector
{
/*
* O(1) Vector with exactly one element
*/
template<typename A>
std::vector<A> singleton(const A &a)
{
return std::vector<A>{a};
}
/**
* O(1) Empty vector
*/
template<typename A>
std::vector<A> empty()
{
return std::vector<A>();
}
/**
* O(1) Empty vector. The arguement left untouched, only it's type interfered for convenience.
*/
template<typename A>
std::vector<A> empty(const A &)
{
return std::vector<A>();
}
}
/*
* Returns the element at index, or undefined behaviour if not present.
*/
template<typename A>
A at(const std::vector<A> &v, int index)
{
return v.at(index);
}
/*
* Yield Just the first element matching the predicate or Nothing if no such element exists.
*/
template<typename F,typename V,typename A = typename V::value_type>
Maybe<A> find(F &&predicate, const V &v)
{
auto it = find_if(v.begin(), v.end(), predicate);
if (it == v.end()) {
return Nothing();
}
return *it;
}
/*
* Return Just the element at index if present, or Nothing if not.
*/
template<typename A>
Maybe<A> maybeAt(const std::vector<A> &v, int index)
{
if (index < v.size()) {
return v.at(index);
}
return Nothing();
}
/*
* Yield Just the index of the first occurence of the given element or Nothing if the
* vector does not contain the element.
*/
template<typename A>
Maybe<size_t> elemIndex(const A &elem, const std::vector<A> &v)
{
auto it = find(v.begin(),v.end(),elem);
if (it == v.end()) {
return Nothing();
}
return std::distance(v.begin(),it);
}
}
#endif