js4cpp
Bunch of C++ classes with JavaScript flavor
 All Classes Namespaces Files Functions Pages
array.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2012 Kirill Dmitrenko <kdmitrenko@gmail.com>
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is furnished
8  * to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all
11  * copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
16  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
18  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
26 #pragma once
27 
28 #include <algorithm>
29 #include <cstdlib>
30 #include <functional>
31 #include <tr1/functional>
32 #include <iterator>
33 #include <numeric>
34 #include <vector>
35 
39 namespace js4cpp {
40 
47 template <typename T> class Array
48 {
49 public:
56  explicit Array(size_t length = 0) : data_(length) {}
57 
65  template <class InputIterator>
66  Array(InputIterator begin, InputIterator end) : data_(begin, end) {}
67 
74  T & operator [](size_t i) {
75  // This is to provide an opportunity to disable JS-like Array
76  // behavior:
77  //
78  // var a = new Array();
79  // a.length; // -> 0
80  // a[99] = 1;
81  // a.length; // -> 100
82 #ifndef INTOLERANT_TO_OUT_OF_RANGE_INDEXES
83  if (i >= data_.size()) {
84  data_.resize(i + 1);
85  }
86 #endif
87  return data_[i];
88  }
89 
90 
98  ssize_t indexOf(const T & item) const {
99  size_t index = std::find(data_.begin(), data_.end(), item) - data_.begin();
100 
101  return index < data_.size() ? index : -1;
102  }
103 
110  void push(const T & value) {
111  data_.push_back(value);
112  }
113 
120  void unshift(const T & value) {
121  data_.insert(data_.begin(), value);
122  }
123 
130  T pop() {
131  T result = data_.back();
132  data_.pop_back();
133  return result;
134  }
135 
142  T shift() {
143  T result = data_.front();
144  data_.erase(data_.begin());
145  return result;
146  }
147 
152  void reverse() {
153  std::reverse(data_.begin(), data_.end());
154  }
155 
156 
165  Array<T> slice(ssize_t begin, ssize_t end = 0) const {
166  ssize_t length = data_.size();
167 
168  if (
169  begin >= length ||
170  begin < -length ||
171  end >= length ||
172  end < -length
173  ) {
174  return Array<T>();
175  }
176 
177  begin = (length + begin) % length;
178  end = (length + end) % length;
179 
180  if (end < begin) {
181  return Array<T>();
182  }
183 
184  return Array<T>(data_.begin() + begin, data_.begin() + end);
185  }
186 
193  void sort(std::tr1::function<bool(const T &, const T &)> comparator = std::less<T>()) {
194  std::sort(data_.begin(), data_.end(), comparator);
195  }
196 
197 
204  void forEach(std::tr1::function<void(T &)> callback) {
205  std::for_each(data_.begin(), data_.end(), callback);
206  }
207 
216  template <typename R>
217  Array<R> map(std::tr1::function<R(const T &)> callback) const {
218  Array<R> result(length());
219 
220  std::transform(data_.begin(), data_.end(),
221  result.data_.begin(), callback);
222 
223  return result;
224  }
225 
233  bool every(std::tr1::function<bool(const T &)> condition) const {
234  return std::find_if(data_.begin(), data_.end(), std::not1(condition)) == data_.end();
235  }
236 
244  bool some(std::tr1::function<bool(const T &)> condition) const {
245  return std::find_if(data_.begin(), data_.end(), condition) != data_.end();
246  }
247 
255  Array<T> filter(std::tr1::function<bool(const T &)> test) const {
256  Array<T> result;
257 
258  std::remove_copy_if(data_.begin(), data_.end(),
259  std::back_inserter(result.data_),
260  std::not1(test));
261 
262  return result;
263  }
264 
272  T reduce(std::tr1::function<T(const T &, const T &)> callback = std::plus<T>()) const {
273  return reduce(callback, data_[0], 1);
274  }
275 
285  T reduce(std::tr1::function<T(const T &, const T &)> callback, const T & initialValue, size_t startFrom = 0) const {
286  return std::accumulate(data_.begin() + startFrom, data_.end(), initialValue, callback);
287  }
288 
295  size_t length() const {
296  return data_.size();
297  }
298 
299 private:
300  std::vector<T> data_;
301 };
302 
303 } // namespace js4cpp