ledger-core
Sequence.hpp
1 /*
2  *
3  * Sequence
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 08/03/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_SEQUENCE_HPP
32 #define LEDGER_CORE_SEQUENCE_HPP
33 
34 #include <vector>
35 #include <list>
36 #include "../utils/Exception.hpp"
37 #include <iterator>
38 
39 namespace ledger {
40  namespace core {
41  template <typename T, typename Container>
42  class Sequence {
43  public:
44  Sequence() {
45 
46  }
47 
48  Sequence(const Container& container) : _container(container) {
49 
50  }
51 
53  optional<T&> get(size_t index) {
54  if (index < size()) {
55  return this->operator[](index);
56  } else {
57  return optional<T&>();
58  }
59  }
60 
62  optional<const T&> get(size_t index) const {
63  if (index < size()) {
64  return this->operator[](index);
65  } else {
66  return optional<const T&>();
67  }
68  }
69 
70 
71  T& operator[](size_t index) {
72  return _container[index];
73  }
74 
75  const T& operator[](size_t index) const {
76  return _container[index];
77  }
78 
79  size_t size() const {
80  return _container.size();
81  }
82 
83  void remove(size_t index) {
84  if (index > size())
85  throw Exception(api::ErrorCode::OUT_OF_RANGE, fmt::format("Index {} is out of range ({})", index, size()));
86  auto it = _container.begin();
87  std::advance(it, index);
88  _container.erase(it);
89  }
90 
91  Sequence<T, Container>& operator+=(const T& v) {
92  _container.push_back(v);
93  return *this;
94  }
95 
96  Container& getContainer() {
97  return _container;
98  }
99 
100  const Container& getContainer() const {
101  return _container;
102  }
103 
104  template<typename Result>
105  Option<Result> join(std::function<Result (const T&, const Option<Result>&)> f) const {
106  Option<T> carry;
107  for (auto& item : _container) {
108  carry = Option<T>(f(item, carry));
109  }
110  return carry;
111  }
112 
113  private:
114  Container _container;
115  };
116 
117  template <typename T>
119 
120  template <typename T>
122 
123  }
124 }
125 
126 #endif //LEDGER_CORE_SEQUENCE_HPP
Definition: Option.hpp:49
Definition: FutureUtils.hpp:48
Definition: Account.cpp:8
Definition: optional.hpp:177
Definition: Sequence.hpp:42
Definition: Exception.hpp:45