2
* Copyright © 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Alan Griffiths <alan@octopull.co.uk>
20
#ifndef MIR_ANDROID_UBUNTU_VECTOR_H_
21
#define MIR_ANDROID_UBUNTU_VECTOR_H_
27
template <typename ValueType>
28
class Vector : std::vector<ValueType> // NB private inheritance of implementation
30
typedef std::vector<ValueType> Impl;
33
using typename Impl::value_type;
34
using typename Impl::iterator;
35
using typename Impl::const_iterator;
44
using Impl::push_back;
48
using Impl::operator[];
51
* the following inlines add some level of compatibility with android utils.
52
* Stuff that is commented out isn't used in the input stack
56
* Constructors and destructors
59
Vector(const Vector<ValueType>& /* rhs */) = default;
60
// explicit Vector(const SortedVector<ValueType>& rhs);
64
// const Vector<ValueType>& operator = (const Vector<ValueType>& rhs) const;
65
Vector<ValueType>& operator=(const Vector<ValueType>& /* rhs */) = default;
67
// const Vector<ValueType>& operator = (const SortedVector<ValueType>& rhs) const;
68
// Vector<ValueType>& operator = (const SortedVector<ValueType>& rhs);
75
//! returns wether or not the vector is empty
76
inline bool isEmpty() const { return empty(); }
77
//! setst the capacity. capacity can never be reduced less than size()
78
inline ssize_t setCapacity(size_t size) { reserve(size); return size; }
81
* C-style array access
84
//! read-only C-style access
85
inline const ValueType* array() const { return Impl::data(); }
86
//! read-write C-style access
87
ValueType* editArray() { return Impl::data(); }
93
//! alternate name for operator []
94
inline const ValueType& itemAt(size_t index) const { return at(index); }
95
//! stack-usage of the vector. returns the top of the stack (last element)
96
const ValueType& top() const { return Impl::back(); }
97
// //! same as operator [], but allows to access the vector backward (from the end) with a negative index
98
// const ValueType& mirrorItemAt(ssize_t index) const { return at((index >= 0) ? index : size()+index); }
104
//! copy-on write support, grants write access to an item
105
ValueType& editItemAt(size_t index) { return Impl::operator[](index); }
106
//! grants right acces to the top of the stack (last element)
107
ValueType& editTop() { return Impl::back(); }
110
* append/insert another vector
113
// //! insert another vector at a given index
114
// ssize_t insertVectorAt(const Vector<ValueType>& vector, size_t index);
116
//! append another vector at the end of this one
117
ssize_t appendVector(const Vector<ValueType>& vector)
119
auto result = size();
120
insert(end(), vector.begin(), vector.end());
125
// //! insert an array at a given index
126
// ssize_t insertArrayAt(const ValueType* array, size_t index, size_t length);
128
//! append an array at the end of this vector
129
ssize_t appendArray(const ValueType* array, size_t length)
131
auto result = size();
132
insert(end(), array, array + length);
137
* add/insert/replace items
140
//! insert one or several items initialized with their default constructor
141
inline ssize_t insertAt(size_t index, size_t numItems = 1) { return insertAt(ValueType(), index, numItems); }
142
//! insert one or several items initialized from a prototype item
143
ssize_t insertAt(const ValueType& prototype_item, size_t index, size_t numItems = 1)
144
{ insert(begin()+index, numItems, prototype_item); return index; }
145
//! pop the top of the stack (removes the last element). No-op if the stack's empty
146
inline void pop() { if (!empty()) erase(--end()); }
147
//! pushes an item initialized with its default constructor
148
inline void push() { push_back(ValueType()); }
149
//! pushes an item on the top of the stack
150
void push(const ValueType& item) { push_back(item); }
151
//! same as push() but returns the index the item was added at (or an error)
152
inline ssize_t add() { auto result = size(); push(); return result; }
154
//! same as push() but returns the index the item was added at (or an error)
155
ssize_t add(const ValueType& item) { auto result = size(); push(item); return result; }
156
// //! replace an item with a new one initialized with its default constructor
157
// inline ssize_t replaceAt(size_t index);
158
// //! replace an item with a new one
159
// ssize_t replaceAt(const ValueType& item, size_t index);
165
//! remove several items
166
inline ssize_t removeItemsAt(size_t index, size_t count = 1)
167
{ auto i = begin() + index; erase(i, i+count); return index; }
169
inline ssize_t removeAt(size_t index)
170
{ auto i = begin() + index; erase(i); return index; }
173
// * sort (stable) the array
176
// typedef int (*compar_t)(const ValueType* lhs, const ValueType* rhs);
177
// typedef int (*compar_r_t)(const ValueType* lhs, const ValueType* rhs, void* state);
179
// inline status_t sort(compar_t cmp);
180
// inline status_t sort(compar_r_t cmp, void* state);
182
// // for debugging only
183
// inline size_t getItemSize() const { return itemSize(); }
188
using ::mir_input::Vector;
191
#endif /* MIR_ANDROID_UBUNTU_VECTOR_H_ */