vbl_array_1d.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_array_1d.h
2 #ifndef vbl_array_1d_h_
3 #define vbl_array_1d_h_
4 //:
5 // \file
6 // \brief A simple container.
7 // \author fsm
8 //
9 // \verbatim
10 // Modifications
11 // Peter Vanroose 5apr2001 added operator==
12 // Amitha Perera jan2003 fixed possible alignment issues.
13 // \endverbatim
14 
15 #include <new>
16 #include <iosfwd>
17 #include <cstddef>
18 #ifdef _MSC_VER
19 # include <vcl_msvc_warnings.h>
20 #endif
21 #include <cassert>
22 
23 //: A simple container.
24 // This container stores its elements in contiguous
25 // storage and whose iterator types are raw pointers. There is
26 // no requirement that the element type have a default constructor.
27 template <class T>
29 {
30  public:
31  typedef std::size_t size_type;
32  typedef T element_type;
33 
34  private:
35  element_type *begin_, *end_, *alloc_; // begin_ <= end_ <= alloc_
36 
37  public:
38  typedef T *iterator;
39  typedef T const *const_iterator;
40 
41  typedef T &reference;
42  typedef T const &const_reference;
43  public:
44 
45  vbl_array_1d() : begin_(nullptr), end_(nullptr), alloc_(nullptr) { }
46 
48  std::ptrdiff_t n = e - b;
49  assert(n>=0);
50  // alignment guaranteed by 18.4.1.1
51  begin_ = (T*) operator new( n * sizeof(T) );
52  end_ = begin_ + n;
53  alloc_ = begin_ + n;
54  for (std::ptrdiff_t i=0; i<n; ++i)
55  new (begin_ + i) T(b[i]);
56  }
57 
59  new (this) vbl_array_1d<T>(that.begin_, that.end_);
60  }
61 
62 //: Construct an array with n elements, all equal to v
63  vbl_array_1d(size_type n, const T &v) {
64  // alignment guaranteed by 18.4.1.1
65  begin_ = (T*) operator new( n * sizeof(T) );
66  end_ = begin_ + n;
67  alloc_ = begin_ + n;
68  for (size_type i=0; i<n; ++i)
69  new (begin_ + i) T(v);
70  }
71 
72 
74  this->~vbl_array_1d();
75  new (this) vbl_array_1d<T>(that.begin_, that.end_);
76  return *this;
77  }
78 
79  bool operator==(vbl_array_1d<T> const& that) const {
80  T* i = begin_;
81  T* j = that.begin_;
82  for ( ; i!=end_ && j!=that.end_; ++i, ++j)
83  if (!(*i == *j)) return false;
84  return i == end_ && j == that.end_;
85  }
86 
88  if (begin_) {
89  clear();
90  operator delete( begin_ );
91  }
92  }
93 
94  void reserve(std::ptrdiff_t new_n) {
95  std::ptrdiff_t n = end_ - begin_;
96  assert(n>=0);
97  if (new_n <= n)
98  return;
99 
100  // alignment guaranteed by 18.4.1.1
101  T *new_begin_ = (T*) operator new( new_n * sizeof(T) );
102  T *new_end_ = new_begin_ + n;
103  T *new_alloc_ = new_begin_ + new_n;
104 
105  for (std::ptrdiff_t i=0; i<n; ++i) {
106  new (new_begin_ + i) T(begin_[i]);
107  begin_[i].~T();
108  }
109 
110  operator delete( begin_ );
111 
112  begin_ = new_begin_;
113  end_ = new_end_;
114  alloc_ = new_alloc_;
115  }
116 
117  void push_back(T const &x) {
118  if (end_ == alloc_)
119  reserve(2*size() + 1);
120  new (end_) T(x);
121  ++end_;
122  }
123 
124  void pop_back() {
125  end_->~T();
126  --end_;
127  }
128 
129  reference back() { return end_[-1]; }
130  const_reference back() const { return end_[-1]; }
131 
132  reference front() { return *begin_; }
133  const_reference front() const { return *begin_; }
134 
135  void clear() {
136  for (T *p = begin_; p!=end_; ++p)
137  p->~T();
138  end_ = begin_;
139  }
140 
141  iterator begin() { return begin_; }
142  iterator end() { return end_; }
143 
144  const_iterator begin() const { return begin_; }
145  const_iterator end() const { return end_; }
146 
147  bool empty() const { return begin_ == end_; }
148  size_type size() const { return end_ - begin_; }
149  size_type capacity() const { return alloc_ - begin_; }
150 
151  //: Get the ith element.
152  // #define NDEBUG to turn bounds checking off.
153  reference operator[](std::ptrdiff_t i)
154  {
155  assert (i >= 0 && i < end_ - begin_);
156  return begin_[i];
157  }
158 
159  //: Get the ith element.
160  // #define NDEBUG to turn bounds checking off.
161  const_reference operator[](std::ptrdiff_t i) const
162  {
163  assert (i >= 0 && i < end_ - begin_);
164  return begin_[i];
165  }
166 };
167 
168 template <class T>
169 std::ostream& operator<<(std::ostream &, vbl_array_1d<T> const &);
170 
171 #endif // vbl_array_1d_h_
size_type size() const
Definition: vbl_array_1d.h:148
bool empty() const
Definition: vbl_array_1d.h:147
T const & const_reference
Definition: vbl_array_1d.h:42
iterator end()
Definition: vbl_array_1d.h:142
size_type capacity() const
Definition: vbl_array_1d.h:149
A simple container.
Definition: vbl_array_1d.h:28
element_type * end_
Definition: vbl_array_1d.h:35
reference front()
Definition: vbl_array_1d.h:132
reference operator[](std::ptrdiff_t i)
Get the ith element.
Definition: vbl_array_1d.h:153
vbl_array_1d(size_type n, const T &v)
Construct an array with n elements, all equal to v.
Definition: vbl_array_1d.h:63
T const * const_iterator
Definition: vbl_array_1d.h:39
vbl_array_1d(vbl_array_1d< T > const &that)
Definition: vbl_array_1d.h:58
void push_back(T const &x)
Definition: vbl_array_1d.h:117
const_reference front() const
Definition: vbl_array_1d.h:133
const_reference back() const
Definition: vbl_array_1d.h:130
reference back()
Definition: vbl_array_1d.h:129
void pop_back()
Definition: vbl_array_1d.h:124
iterator begin()
Definition: vbl_array_1d.h:141
std::size_t size_type
Definition: vbl_array_1d.h:31
vbl_array_1d(const_iterator b, const_iterator e)
Definition: vbl_array_1d.h:47
const_reference operator[](std::ptrdiff_t i) const
Get the ith element.
Definition: vbl_array_1d.h:161
const_iterator begin() const
Definition: vbl_array_1d.h:144
const_iterator end() const
Definition: vbl_array_1d.h:145
element_type * begin_
Definition: vbl_array_1d.h:35
void reserve(std::ptrdiff_t new_n)
Definition: vbl_array_1d.h:94
std::ostream & operator<<(std::ostream &, vbl_array_1d< T > const &)
vbl_array_1d< T > & operator=(vbl_array_1d< T > const &that)
Definition: vbl_array_1d.h:73
element_type * alloc_
Definition: vbl_array_1d.h:35
bool operator==(vbl_array_1d< T > const &that) const
Definition: vbl_array_1d.h:79