ScummVM API documentation
vector.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /********************************************
23  DISCLAIMER:
24 
25  This is a wrapper code to mimic the relevant std:: class
26  Please use it ONLY when porting an existing code e.g. from the original sources
27 
28  For all new development please use classes from Common::
29  *********************************************/
30 
31 #ifndef COMMON_STD_VECTOR_H
32 #define COMMON_STD_VECTOR_H
33 
34 #include "common/array.h"
35 
36 namespace Std {
37 
38 template<class T>
39 class vector : public Common::Array<T> {
40 public:
42  private:
43  vector<T> *_owner;
44  int _index;
45  public:
46  reverse_iterator(vector<T> *owner, int index) : _owner(owner), _index(index) {
47  }
48  reverse_iterator() : _owner(0), _index(-1) {
49  }
50 
51  T &operator*() {
52  return (*_owner)[_index];
53  }
54 
55  reverse_iterator &operator++() {
56  --_index;
57  return *this;
58  }
59 
60  bool operator==(const reverse_iterator &rhs) {
61  return _owner == rhs._owner && _index == rhs._index;
62  }
63  bool operator!=(const reverse_iterator &rhs) {
64  return !operator==(rhs);
65  }
66  };
67 
69  private:
70  const vector<T> *_owner;
71  int _index;
72  public:
73  const_reverse_iterator(const vector<T> *owner, int index) : _owner(owner), _index(index) {
74  }
75  const_reverse_iterator() : _owner(0), _index(-1) {
76  }
77 
78  const T operator*() const {
79  return (*_owner)[_index];
80  }
81 
82  const_reverse_iterator &operator++() {
83  --_index;
84  return *this;
85  }
86 
87  bool operator==(const const_reverse_iterator &rhs) const {
88  return _owner == rhs._owner && _index == rhs._index;
89  }
90  bool operator!=(const const_reverse_iterator &rhs) const {
91  return !operator==(rhs);
92  }
93  bool operator<(const const_reverse_iterator &rhs) const {
94  return _index > rhs._index;
95  }
96  };
97 public:
98  using iterator = typename Common::Array<T>::iterator;
100 
101  constexpr vector() : Common::Array<T>() {}
102  explicit vector(size_t newSize) : Common::Array<T>(newSize) {}
103  vector(size_t newSize, const T &elem) : Common::Array<T>(newSize, elem) {}
104  vector(std::initializer_list<T> list) : Common::Array<T>(list) {}
105 
107 
108  void insert(const T &element) {
109  this->push_back(element);
110  }
111 
115  void insert(iterator position, const_iterator first, const_iterator last) {
116  int destIndex = position - this->begin();
117  for (; first != last; ++first) {
118  this->insert_at(destIndex++, *first);
119  }
120  }
121 
122  T &at(size_t index) {
123  return (*this)[index];
124  }
125  const T &at(size_t index) const {
126  return (*this)[index];
127  }
128 
132  void remove(T element) {
133  for (uint i = 0; i < this->size(); ++i) {
134  if (this->operator[](i) == element) {
135  this->remove_at(i);
136  return;
137  }
138  }
139  }
140 
145  void rotate(iterator it) {
146  if (it != this->end()) {
147  size_t count = it - this->begin();
148  for (size_t ctr = 0; ctr < count; ++ctr) {
149  this->push_back(this->front());
150  this->remove_at(0);
151  }
152  }
153  }
154 
155  const_iterator cbegin() {
156  return this->begin();
157  }
158  const_iterator cend() {
159  return this->end();
160  }
161  reverse_iterator rbegin() {
162  return reverse_iterator(this, (int)this->size() - 1);
163  }
164  reverse_iterator rend() {
165  return reverse_iterator(this, -1);
166  }
167  const_reverse_iterator rbegin() const {
168  return const_reverse_iterator(this, (int)this->size() - 1);
169  }
170  const_reverse_iterator rend() const {
171  return const_reverse_iterator(this, -1);
172  }
173  const_reverse_iterator crbegin() const {
174  return const_reverse_iterator(this, (int)this->size() - 1);
175  }
176  const_reverse_iterator crend() const {
177  return const_reverse_iterator(this, -1);
178  }
179 };
180 
181 } // namespace Std
182 
183 #endif
Definition: vector.h:39
void rotate(iterator it)
Definition: vector.h:145
void insert_at(size_type idx, const T &element)
Definition: array.h:241
void insert(iterator position, const_iterator first, const_iterator last)
Definition: vector.h:115
Definition: array.h:52
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
Definition: list.h:39
T * iterator
Definition: array.h:54
T & front()
Definition: array.h:217
void push_back(const T &element)
Definition: array.h:180
const T * const_iterator
Definition: array.h:55
size_type size() const
Definition: array.h:315
T remove_at(size_type idx)
Definition: array.h:260
Definition: vector.h:41
Definition: algorithm.h:37