MADNESS  version 0.9
stdarray_bits.h
Go to the documentation of this file.
1 #ifndef MADNESS_STDARRAY_BITS_H
2 #define MADNESS_STDARRAY_BITS_H
3 
4 #include <stdexcept>
5 
6 namespace madness {
7  namespace tr1 {
8  namespace array {
10  template <typename T, std::size_t N>
11  class array {
12  public:
13  T elems[N]; // fixed-size array of elements of type T
14 
15  public:
16  // type definitions
17  typedef T value_type;
18  typedef T* iterator;
19  typedef const T* const_iterator;
20  typedef T& reference;
21  typedef const T& const_reference;
22  typedef std::size_t size_type;
23  typedef std::ptrdiff_t difference_type;
24 
25  // iterator support
26  iterator begin() { return elems; }
27  const_iterator begin() const { return elems; }
28  iterator end() { return elems+N; }
29  const_iterator end() const { return elems+N; }
30 
31  // reverse iterator support
32  typedef std::reverse_iterator<iterator> reverse_iterator;
33  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
34 
35  reverse_iterator rbegin() { return reverse_iterator(end()); }
36  const_reverse_iterator rbegin() const {
37  return const_reverse_iterator(end());
38  }
39  reverse_iterator rend() { return reverse_iterator(begin()); }
40  const_reverse_iterator rend() const {
41  return const_reverse_iterator(begin());
42  }
43 
44  // operator[]
45  reference operator[](size_type i) { return elems[i]; }
46  const_reference operator[](size_type i) const { return elems[i]; }
47 
48  // at() with range check
49  reference at(size_type i) { rangecheck(i); return elems[i]; }
50  const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
51 
52  // front() and back()
53  reference front() { return elems[0]; }
54  const_reference front() const { return elems[0]; }
55  reference back() { return elems[N-1]; }
56  const_reference back() const { return elems[N-1]; }
57 
58  // size is constant
59  static size_type size() { return N; }
60  static bool empty() { return false; }
61  static size_type max_size() { return N; }
62  enum { static_size = N };
63 
64  // swap (note: linear complexity in N, constant for given instantiation)
65  void swap (array<T,N>& y) {
66  std::swap_ranges(begin(),end(),y.begin());
67  }
68 
69  // direct access to data (read-only)
70  const T* data() const { return elems; }
71 
72  // use array as C array (direct read/write access to data)
73  T* data() { return elems; }
74 
75  // assignment with type conversion
76  template <typename T2>
78  std::copy(rhs.begin(),rhs.end(), begin());
79  return *this;
80  }
81 
82  // assign one value to all elements
83  void assign (const T& value)
84  {
85  std::fill_n(begin(),size(),value);
86  }
87 
88  private:
89  // check range (may be private because it is static)
90  static void rangecheck (size_type i) {
91  if (i >= size()) {
92  throw std::out_of_range("array<>: index out of range");
93  }
94  }
95  };
96 
97  template <typename T>
98  class array<T,0> {
99  public:
100  char c; // to ensure different array intances return unique values for begin/end
101 
102  public:
103  // type definitions
104  typedef T value_type;
105  typedef T* iterator;
106  typedef const T* const_iterator;
107  typedef T& reference;
108  typedef const T& const_reference;
109  typedef std::size_t size_type;
110  typedef std::ptrdiff_t difference_type;
111 
112  // iterator support
113  iterator begin() { return reinterpret_cast< iterator >( &c ); }
114  const_iterator begin() const { return reinterpret_cast< const_iterator >( &c ); }
115  iterator end() { return reinterpret_cast< iterator >( &c ); }
116  const_iterator end() const { return reinterpret_cast< const_iterator >( &c ); }
117 
118  // reverse iterator support
119  typedef std::reverse_iterator<iterator> reverse_iterator;
120  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
121 
122  reverse_iterator rbegin() { return reverse_iterator(end()); }
123  const_reverse_iterator rbegin() const {
124  return const_reverse_iterator(end());
125  }
126  reverse_iterator rend() { return reverse_iterator(begin()); }
127  const_reverse_iterator rend() const {
128  return const_reverse_iterator(begin());
129  }
130 
131  // at() with range check
132  reference at(size_type i) {
133  makes_no_sense();
134  }
135  const_reference at(size_type i) const {
136  makes_no_sense();
137  }
138 
139  // size is constant
140  static size_type size() { return 0; }
141  static bool empty() { return true; }
142  static size_type max_size() { return 0; }
143  enum { static_size = 0 };
144 
145  // swap
146  void swap (array<T,0>& y) {
147  // could swap value of c, but value is not part of documented array state
148  }
149 
150  // direct access to data
151  const T* data() const { return NULL; }
152  T* data() { return NULL; }
153 
154  // assignment with type conversion
155  template < typename T2 >
157  return *this;
158  }
159 
160  // Calling these operations are undefined behaviour for 0-size arrays,
161  // but Library TR1 requires their presence.
162  // operator[]
163  reference operator[](size_type i) { makes_no_sense(); }
164  const_reference operator[](size_type i) const { makes_no_sense(); }
165 
166  // front() and back()
167  reference front() { makes_no_sense(); }
168  const_reference front() const { makes_no_sense(); }
169  reference back() { makes_no_sense(); }
170  const_reference back() const { makes_no_sense(); }
171 
172  private:
173  // helper for operations that have undefined behaviour for 0-size arrays,
174  // assert( false ); added to make lack of support clear
175  static void makes_no_sense () {
176  //assert(true);
177  throw std::out_of_range("array<0>: index out of range");
178  }
179  };
180 
181  // comparisons
182  template<class T, std::size_t N>
183  bool operator== (const array<T,N>& x, const array<T,N>& y) {
184  return std::equal(x.begin(), x.end(), y.begin());
185  }
186  template<class T, std::size_t N>
187  bool operator< (const array<T,N>& x, const array<T,N>& y) {
188  return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
189  }
190  template<class T, std::size_t N>
191  bool operator!= (const array<T,N>& x, const array<T,N>& y) {
192  return !(x==y);
193  }
194  template<class T, std::size_t N>
195  bool operator> (const array<T,N>& x, const array<T,N>& y) {
196  return y<x;
197  }
198  template<class T, std::size_t N>
199  bool operator<= (const array<T,N>& x, const array<T,N>& y) {
200  return !(y<x);
201  }
202  template<class T, std::size_t N>
203  bool operator>= (const array<T,N>& x, const array<T,N>& y) {
204  return !(x<y);
205  }
206 
207  // global swap()
208  template<class T, std::size_t N>
209  inline void swap (array<T,N>& x, array<T,N>& y) {
210  x.swap(y);
211  }
212  }
213  }
214 }
215 
216 #endif
void assign(const T &value)
Definition: stdarray_bits.h:83
const_reverse_iterator rend() const
Definition: stdarray_bits.h:40
T * iterator
Definition: stdarray_bits.h:105
bool operator>(const array< T, N > &x, const array< T, N > &y)
Definition: stdarray_bits.h:195
T * data()
Definition: stdarray_bits.h:152
reverse_iterator rbegin()
Definition: stdarray_bits.h:122
std::ptrdiff_t difference_type
Definition: stdarray_bits.h:110
void swap(array< T, N > &y)
Definition: stdarray_bits.h:65
static size_type max_size()
Definition: stdarray_bits.h:61
const_reverse_iterator rend() const
Definition: stdarray_bits.h:127
reference back()
Definition: stdarray_bits.h:169
bool operator==(const array< T, N > &x, const array< T, N > &y)
Definition: stdarray_bits.h:183
reference at(size_type i)
Definition: stdarray_bits.h:49
reference at(size_type i)
Definition: stdarray_bits.h:132
std::reverse_iterator< iterator > reverse_iterator
Definition: stdarray_bits.h:32
const_iterator end() const
Definition: stdarray_bits.h:116
std::size_t size_type
Definition: stdarray_bits.h:109
void swap(array< T, N > &x, array< T, N > &y)
Definition: stdarray_bits.h:209
reference back()
Definition: stdarray_bits.h:55
reverse_iterator rend()
Definition: stdarray_bits.h:126
const T * data() const
Definition: stdarray_bits.h:151
reverse_iterator rbegin()
Definition: stdarray_bits.h:35
const_reference back() const
Definition: stdarray_bits.h:56
const T * const_iterator
Definition: stdarray_bits.h:106
static bool empty()
Definition: stdarray_bits.h:141
const_reverse_iterator rbegin() const
Definition: stdarray_bits.h:36
const_iterator end() const
Definition: stdarray_bits.h:29
Array idential to C++0X arrays.
Definition: stdarray_bits.h:11
const T * const_iterator
Definition: stdarray_bits.h:19
const T & const_reference
Definition: stdarray_bits.h:108
static bool empty()
Definition: stdarray_bits.h:60
const_reference operator[](size_type i) const
Definition: stdarray_bits.h:46
void swap(array< T, 0 > &y)
Definition: stdarray_bits.h:146
iterator begin()
Definition: stdarray_bits.h:113
T * iterator
Definition: stdarray_bits.h:18
T elems[N]
Definition: stdarray_bits.h:13
reference operator[](size_type i)
Definition: stdarray_bits.h:163
const_reverse_iterator rbegin() const
Definition: stdarray_bits.h:123
iterator end()
Definition: stdarray_bits.h:115
std::reverse_iterator< iterator > reverse_iterator
Definition: stdarray_bits.h:119
const_reference operator[](size_type i) const
Definition: stdarray_bits.h:164
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
std::ptrdiff_t difference_type
Definition: stdarray_bits.h:23
const_reference back() const
Definition: stdarray_bits.h:170
const_reference front() const
Definition: stdarray_bits.h:168
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence. ...
Definition: mra.h:1835
static size_type size()
Definition: stdarray_bits.h:59
bool operator!=(const array< T, N > &x, const array< T, N > &y)
Definition: stdarray_bits.h:191
T & reference
Definition: stdarray_bits.h:20
const_iterator begin() const
Definition: stdarray_bits.h:114
char c
Definition: stdarray_bits.h:100
const_reference at(size_type i) const
Definition: stdarray_bits.h:50
const_reference at(size_type i) const
Definition: stdarray_bits.h:135
const T & const_reference
Definition: stdarray_bits.h:21
reference front()
Definition: stdarray_bits.h:53
Definition: stdarray_bits.h:98
const T * data() const
Definition: stdarray_bits.h:70
iterator begin()
Definition: stdarray_bits.h:26
reference front()
Definition: stdarray_bits.h:167
T & reference
Definition: stdarray_bits.h:107
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: stdarray_bits.h:33
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: stdarray_bits.h:120
T * data()
Definition: stdarray_bits.h:73
const_iterator begin() const
Definition: stdarray_bits.h:27
array< T, N > & operator=(const array< T2, N > &rhs)
Definition: stdarray_bits.h:77
bool operator>=(const array< T, N > &x, const array< T, N > &y)
Definition: stdarray_bits.h:203
T value_type
Definition: stdarray_bits.h:17
Definition: stdarray_bits.h:62
const_reference front() const
Definition: stdarray_bits.h:54
T value_type
Definition: stdarray_bits.h:104
reverse_iterator rend()
Definition: stdarray_bits.h:39
static size_type max_size()
Definition: stdarray_bits.h:142
reference operator[](size_type i)
Definition: stdarray_bits.h:45
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
const double c
Definition: gfit.cc:200
static size_type size()
Definition: stdarray_bits.h:140
std::size_t size_type
Definition: stdarray_bits.h:22
iterator end()
Definition: stdarray_bits.h:28