TPIE

2362a60
array_view_base.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef __ARRAY_VIEW_BASE_H__
21 #define __ARRAY_VIEW_BASE_H__
22 
28 
29 #include <boost/iterator/iterator_facade.hpp>
30 #include <cassert>
31 namespace tpie {
32 
38 
39 template <typename T>
41 private:
42  T * m_start;
43  T * m_end;
44 public:
45  class iterator: public boost::iterator_facade<iterator, T, boost::random_access_traversal_tag> {
46  private:
47  friend class array_view_base;
48  friend class boost::iterator_core_access;
49  explicit iterator(T * e): elm(e) {}
50  inline T & dereference() const {return * elm;}
51  inline bool equal(iterator const& o) const {return elm == o.elm;}
52  inline void increment() {++elm;}
53  inline void decrement() {--elm;}
54  inline void advance(size_t n) {elm += n;}
55  inline ptrdiff_t distance_to(iterator const & o) const {return o.elm - elm;}
56  T * elm;
57  public:
58  iterator(): elm(0) {};
59  };
60 
61 
68  array_view_base(T * start, T * end): m_start(start), m_end(end) {}
69 
73  typedef T value_type;
74 
81  iterator find(size_t idx) const throw () {
82  assert(idx <= size());
83  return iterator(m_start + idx);
84  }
85 
91  T & at(size_t i) const throw() {
92  assert(i < size());
93  return *find(i);
94  }
95 
101  inline bool empty() const {return m_end == m_start;}
102 
107  inline size_t size() const {return m_end - m_start;}
108 
115  inline T & operator[](size_t i) const {
116  assert(i < size());
117  return at(i);
118  }
119 
127  inline bool operator==(const array_view_base & other) const {
128  if (size() != other.size()) return false;
129  for (size_t i=0; i < size(); ++i) if (at(i) != other.at(i)) return false;
130  return true;
131  }
132 
139  inline bool operator!=(const array_view_base & other) const {
140  if (size() != other.size()) return true;
141  for (size_t i=0; i< size(); ++i) if (at(i) != other.at(i)) return true;
142  return false;
143  }
144 
150  inline iterator begin() const {return iterator(m_start);}
151 
157  inline iterator end() const {return iterator(m_end);}
158 
162  inline T & front() const {return *m_start;}
163 
167  inline T & back() const {return *(m_end-1);}
168 };
169 
170 } //namespace tpie
171 
172 #endif //__ARRAY_VIEW_BASE_H__
bool empty() const
Check if the array is empty.
T & at(size_t i) const
Return the element located at the given index.
T & back() const
Return the last element in the array.
Base class for array_view.
bool operator!=(const array_view_base &other) const
Check if the two arrays differ.
T & front() const
Return the first element in the array.
iterator find(size_t idx) const
Return an iterator to the i'th element of the array.
bool operator==(const array_view_base &other) const
Check if the other array has the same elements in the same order as this.
T & operator[](size_t i) const
Return a reference to an array entry.
array_view_base(T *start, T *end)
Pointer constructor.
size_t size() const
Get number of elements in the array.
iterator begin() const
Return an iterator to the beginning of the array.
T value_type
Type of values contained in the array.
iterator end() const
Return an iterator to the end of the array.