31 #include <tr1/functional>
47 template <
typename T>
class Array
65 template <
class InputIterator>
66 Array(InputIterator begin, InputIterator end) : data_(begin, end) {}
82 #ifndef INTOLERANT_TO_OUT_OF_RANGE_INDEXES
83 if (i >= data_.size()) {
99 size_t index = std::find(data_.begin(), data_.end(), item) - data_.begin();
101 return index < data_.size() ? index : -1;
111 data_.push_back(value);
121 data_.insert(data_.begin(), value);
131 T result = data_.back();
143 T result = data_.front();
144 data_.erase(data_.begin());
166 ssize_t
length = data_.size();
177 begin = (length + begin) % length;
178 end = (length + end) % length;
184 return Array<T>(data_.begin() + begin, data_.begin() + end);
193 void sort(std::tr1::function<
bool(
const T &,
const T &)> comparator = std::less<T>()) {
194 std::sort(data_.begin(), data_.end(), comparator);
204 void forEach(std::tr1::function<
void(T &)> callback) {
205 std::for_each(data_.begin(), data_.end(), callback);
216 template <
typename R>
220 std::transform(data_.begin(), data_.end(),
221 result.data_.begin(), callback);
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();
244 bool some(std::tr1::function<
bool(
const T &)> condition)
const {
245 return std::find_if(data_.begin(), data_.end(), condition) != data_.end();
258 std::remove_copy_if(data_.begin(), data_.end(),
259 std::back_inserter(result.data_),
272 T
reduce(std::tr1::function<T(
const T &,
const T &)> callback = std::plus<T>())
const {
273 return reduce(callback, data_[0], 1);
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);
300 std::vector<T> data_;