Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LazyArray.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_LAZYARRAY_H
21 #define GALOIS_LAZYARRAY_H
22 
23 #include <algorithm>
24 #include <cstddef>
25 #include <iterator>
26 #include <stdexcept>
27 #include <type_traits>
28 #include <utility>
29 
30 #include "galois/config.h"
31 #include "galois/LazyObject.h"
32 
33 namespace galois {
34 
40 template <typename _Tp, unsigned _Size>
41 class LazyArray {
42  typedef typename std::aligned_storage<
43  sizeof(_Tp), std::alignment_of<_Tp>::value>::type CharData;
44 
45  LazyObject<_Tp> data_[(_Size > 0 ? _Size : 1)];
46 
47  _Tp* get(size_t __n) { return &data_[__n].get(); }
48  const _Tp* get(size_t __n) const { return &data_[__n].get(); }
49 
50 public:
51  typedef _Tp value_type;
52  typedef size_t size_type;
53  typedef ptrdiff_t difference_type;
55  typedef const value_type& const_reference;
56  typedef value_type* pointer;
57  typedef const value_type* const_pointer;
58  typedef pointer iterator;
60  typedef std::reverse_iterator<iterator> reverse_iterator;
61  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
62 
63  // iterators:
64  iterator begin() { return iterator(get(0)); }
65  const_iterator begin() const { return const_iterator(get(0)); }
66  iterator end() { return iterator(get(_Size)); }
67  const_iterator end() const { return const_iterator(get(_Size)); }
68 
71  return const_reverse_iterator(end());
72  }
75  return const_reverse_iterator(begin());
76  }
77 
78  const_iterator cbegin() const { return begin(); }
79  const_iterator cend() const { return end(); }
80  const_reverse_iterator crbegin() const { return rbegin(); }
81  const_reverse_iterator crend() const { return rend(); }
82 
83  // capacity:
84  size_type size() const { return _Size; }
85  size_type max_size() const { return _Size; }
86  bool empty() const { return _Size == 0; }
87 
88  // element access:
89  reference operator[](size_type __n) { return *get(__n); }
90  const_reference operator[](size_type __n) const { return *get(__n); }
92  if (__n >= _Size)
93  throw std::out_of_range("lazyArray::at");
94  return get(__n);
95  }
97  if (__n >= _Size)
98  throw std::out_of_range("lazyArray::at");
99  return get(__n);
100  }
101 
102  reference front() { return *get(0); }
103  const_reference front() const { return *get(0); }
104  reference back() { return *get(_Size > 0 ? _Size - 1 : 0); }
105  const_reference back() const { return *get(_Size > 0 ? _Size - 1 : 0); }
106 
107  pointer data() { return get(0); }
108  const_pointer data() const { return get(0); }
109 
110  // missing: fill swap
111 
112  template <typename... Args>
113  pointer emplace(size_type __n, Args&&... args) {
114  return new (get(__n)) _Tp(std::forward<Args>(args)...);
115  }
116 
117  pointer construct(size_type __n, const _Tp& val) { return emplace(__n, val); }
118  pointer construct(size_type __n, _Tp&& val) {
119  return emplace(__n, std::move(val));
120  }
121 
122  void destroy(size_type __n) { (get(__n))->~_Tp(); }
123 };
124 
125 } // namespace galois
126 #endif // GALOIS_LAZYARRAY_H
_Tp value_type
Definition: LazyArray.h:51
pointer data()
Definition: LazyArray.h:107
reference front()
Definition: LazyArray.h:102
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: LazyArray.h:61
pointer construct(size_type __n, _Tp &&val)
Definition: LazyArray.h:118
pointer emplace(size_type __n, Args &&...args)
Definition: LazyArray.h:113
bool empty() const
Definition: LazyArray.h:86
size_type size() const
Definition: LazyArray.h:84
const_reverse_iterator rend() const
Definition: LazyArray.h:74
ptrdiff_t difference_type
Definition: LazyArray.h:53
reference operator[](size_type __n)
Definition: LazyArray.h:89
const_iterator cend() const
Definition: LazyArray.h:79
const_reference front() const
Definition: LazyArray.h:103
reverse_iterator rbegin()
Definition: LazyArray.h:69
void destroy(size_type __n)
Definition: LazyArray.h:122
const value_type * const_pointer
Definition: LazyArray.h:57
const_reverse_iterator crbegin() const
Definition: LazyArray.h:80
const_iterator cbegin() const
Definition: LazyArray.h:78
iterator begin()
Definition: LazyArray.h:64
reference at(size_type __n)
Definition: LazyArray.h:91
const_reference at(size_type __n) const
Definition: LazyArray.h:96
const_reverse_iterator crend() const
Definition: LazyArray.h:81
reference back()
Definition: LazyArray.h:104
pointer construct(size_type __n, const _Tp &val)
Definition: LazyArray.h:117
const_pointer data() const
Definition: LazyArray.h:108
iterator end()
Definition: LazyArray.h:66
value_type & reference
Definition: LazyArray.h:54
This is a container that encapsulates space for a constant size array.
Definition: LazyArray.h:41
const_pointer const_iterator
Definition: LazyArray.h:59
size_type max_size() const
Definition: LazyArray.h:85
value_type * pointer
Definition: LazyArray.h:56
const_iterator begin() const
Definition: LazyArray.h:65
const_reverse_iterator rbegin() const
Definition: LazyArray.h:70
reverse_iterator rend()
Definition: LazyArray.h:73
const value_type & const_reference
Definition: LazyArray.h:55
std::reverse_iterator< iterator > reverse_iterator
Definition: LazyArray.h:60
const_reference operator[](size_type __n) const
Definition: LazyArray.h:90
pointer iterator
Definition: LazyArray.h:58
const_reference back() const
Definition: LazyArray.h:105
size_t size_type
Definition: LazyArray.h:52
const_iterator end() const
Definition: LazyArray.h:67
const_reference get() const
Definition: LazyObject.h:113