Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gstl.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_GSTL_H
21 #define GALOIS_GSTL_H
22 
23 #include <algorithm>
24 #include <iterator>
25 #include <utility>
26 #include <cassert>
27 #include <vector>
28 #include <set>
29 #include <deque>
30 #include <map>
31 #include <list>
32 #include <string>
33 #include <sstream>
34 
35 #include "galois/config.h"
36 #include "galois/PriorityQueue.h"
37 
38 namespace galois {
39 
40 namespace gstl {
41 
43 template <typename T>
46 
47 template <typename T>
49 
51 template <typename T>
52 using Vector = std::vector<T, Pow2Alloc<T>>;
54 
55 template <typename T>
56 using Deque = std::deque<T, Pow2Alloc<T>>;
57 
58 template <typename T>
59 using List = std::list<T, FixedSizeAlloc<T>>;
60 
61 template <typename T, typename C = std::less<T>>
62 using Set = std::set<T, C, FixedSizeAlloc<T>>;
63 
64 template <typename K, typename V, typename C = std::less<K>>
65 using Map = std::map<K, V, C, FixedSizeAlloc<std::pair<const K, V>>>;
66 
67 template <typename K, typename V, typename Hash = std::hash<K>,
68  typename KeyEqual = std::equal_to<K>>
69 using UnorderedMap = std::unordered_map<K, V, Hash, KeyEqual,
71 
72 template <typename T, typename C = std::less<T>>
74 
75 using Str = std::basic_string<char, std::char_traits<char>, Pow2Alloc<char>>;
76 
77 template <typename T>
78 struct StrMaker {
79  Str operator()(const T& x) const {
80  std::basic_ostringstream<char, std::char_traits<char>, Pow2Alloc<char>> os;
81  os << x;
82  return Str(os.str());
83  }
84 };
85 
86 template <>
87 struct StrMaker<std::string> {
88  Str operator()(const std::string& x) const { return Str(x.begin(), x.end()); }
89 };
90 
91 template <>
92 struct StrMaker<Str> {
93  const Str& operator()(const Str& x) const { return x; }
94 };
95 
96 template <>
97 struct StrMaker<const char*> {
98  Str operator()(const char* x) const { return Str(x); }
99 };
100 
101 template <typename T>
102 Str makeStr(const T& x) {
103  return StrMaker<T>()(x);
104 }
105 } // end namespace gstl
106 
107 template <typename I>
108 class IterRange {
109  I m_beg;
110  I m_end;
111 
112 public:
113  IterRange(const I& b, const I& e) : m_beg(b), m_end(e) {}
114  const I& begin(void) const { return m_beg; }
115  const I& end(void) const { return m_end; }
116 };
117 
118 template <typename I>
119 auto makeIterRange(const I& beg, const I& end) {
120  return IterRange<I>(beg, end);
121 }
122 
123 template <typename C>
124 auto makeIterRange(C&& cont) {
125  using I = decltype(std::forward<C>(cont).begin());
126  return IterRange<I>(std::forward<C>(cont).begin(),
127  std::forward<C>(cont).end());
128 }
129 
130 namespace internal {
131 
132 template <typename T, typename C>
133 struct SerCont {
134  C m_q;
135 
136  explicit SerCont(const C& q = C()) : m_q(q) {}
137 
138  void push(const T& x) { m_q.push_back(x); }
139 
140  template <typename I>
141  void push(const I& beg, const I& end) {
142  for (I i = beg; i != end; ++i) {
143  push(*i);
144  }
145  }
146 
147  template <typename... Args>
148  void emplace(Args&&... args) {
149  m_q.emplace_back(std::forward<Args>(args)...);
150  }
151 
152  bool empty(void) const { return m_q.empty(); }
153 
154  void clear(void) { m_q.clear(); }
155 
156  using value_type = typename C::value_type;
157  using iterator = typename C::iterator;
158  using const_iterator = typename C::const_iterator;
159 
160  iterator begin(void) { return m_q.begin(); }
161  iterator end(void) { return m_q.end(); }
162 
163  const_iterator begin(void) const { return m_q.begin(); }
164  const_iterator end(void) const { return m_q.end(); }
165 
166  const_iterator cbegin(void) const { return m_q.cbegin(); }
167  const_iterator cend(void) const { return m_q.cend(); }
168 };
169 } // namespace internal
170 
171 template <typename T, typename C = std::deque<T>>
172 class SerFIFO : public internal::SerCont<T, C> {
173 
174  using Base = internal::SerCont<T, C>;
175 
176 public:
177  explicit SerFIFO(const C& q = C()) : Base(q) {}
178 
179  T pop(void) {
180  T ret = Base::m_q.front();
181  Base::m_q.pop_front();
182  return ret;
183  }
184 };
185 
186 template <typename T, typename C = std::vector<T>>
187 class SerStack : public internal::SerCont<T, C> {
188 
189  using Base = internal::SerCont<T, C>;
190 
191 public:
192  explicit SerStack(const C& q = C()) : Base(q) {}
193 
194  T pop(void) {
195  T ret = Base::m_q.back();
196  Base::m_q.pop_back();
197  return ret;
198  }
199 };
200 
201 template <typename IterTy, class Distance>
202 IterTy safe_advance_dispatch(IterTy b, IterTy e, Distance n,
203  std::random_access_iterator_tag) {
204  if (std::distance(b, e) >= n)
205  return b + n;
206  else
207  return e;
208 }
209 
210 template <typename IterTy, class Distance>
211 IterTy safe_advance_dispatch(IterTy b, IterTy e, Distance n,
212  std::input_iterator_tag) {
213  while (b != e && n--)
214  ++b;
215  return b;
216 }
217 
221 template <typename IterTy, class Distance>
222 IterTy safe_advance(IterTy b, IterTy e, Distance n) {
223  typename std::iterator_traits<IterTy>::iterator_category category;
224  return safe_advance_dispatch(b, e, n, category);
225 }
226 
231 template <typename IterTy>
232 IterTy split_range(IterTy b, IterTy e) {
233  std::advance(b, (std::distance(b, e) + 1) / 2);
234  return b;
235 }
236 
241 template <
242  typename IterTy,
243  typename std::enable_if<!std::is_integral<IterTy>::value>::type* = nullptr>
244 std::pair<IterTy, IterTy> block_range(IterTy b, IterTy e, unsigned id,
245  unsigned num) {
246  size_t dist = std::distance(b, e);
247  size_t numper = std::max((dist + num - 1) / num, (size_t)1); // round up
248  size_t A = std::min(numper * id, dist);
249  size_t B = std::min(numper * (id + 1), dist);
250  std::advance(b, A);
251 
252  if (dist != B) {
253  e = b;
254  std::advance(e, B - A);
255  }
256 
257  return std::make_pair(b, e);
258 }
259 
260 template <typename IntTy, typename std::enable_if<
261  std::is_integral<IntTy>::value>::type* = nullptr>
262 std::pair<IntTy, IntTy> block_range(IntTy b, IntTy e, unsigned id,
263  unsigned num) {
264  IntTy dist = e - b;
265  IntTy numper = std::max((dist + num - 1) / num, (IntTy)1); // round up
266  IntTy A = std::min(numper * id, dist);
267  IntTy B = std::min(numper * (id + 1), dist);
268  b += A;
269  if (dist != B) {
270  e = b;
271  e += (B - A);
272  }
273  return std::make_pair(b, e);
274 }
275 
276 namespace internal {
277 template <typename I>
278 using Val_ty = typename std::iterator_traits<I>::value_type;
279 } // namespace internal
280 
282 template <typename I>
283 std::enable_if_t<!std::is_scalar<internal::Val_ty<I>>::value>
284 uninitialized_destroy(I first, I last) {
285 
286  using T = internal::Val_ty<I>;
287  for (; first != last; ++first)
288  (&*first)->~T();
289 }
290 
291 template <class I>
292 std::enable_if_t<std::is_scalar<internal::Val_ty<I>>::value>
294 
295 } // namespace galois
296 #endif
typename runtime::Pow_2_BlockAllocator< T > Pow2Alloc
[define Pow_2_VarSizeAlloc]
Definition: gstl.h:44
IterRange(const I &b, const I &e)
Definition: gstl.h:113
const Str & operator()(const Str &x) const
Definition: gstl.h:93
T pop(void)
Definition: gstl.h:194
std::pair< IterTy, IterTy > block_range(IterTy b, IterTy e, unsigned id, unsigned num)
Returns a continuous block from the range based on the number of divisions and the id of the block re...
Definition: gstl.h:244
Definition: gstl.h:108
Definition: gstl.h:172
std::map< K, V, C, FixedSizeAlloc< std::pair< const K, V >>> Map
Definition: gstl.h:65
Str operator()(const char *x) const
Definition: gstl.h:98
SerFIFO(const C &q=C())
Definition: gstl.h:177
const I & end(void) const
Definition: gstl.h:115
std::set< T, C, FixedSizeAlloc< T >> Set
Definition: gstl.h:62
Definition: PriorityQueue.h:159
std::vector< T, Pow2Alloc< T >> Vector
[STL vector using Pow_2_VarSizeAlloc]
Definition: gstl.h:52
SerStack(const C &q=C())
Definition: gstl.h:192
const Ty max(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:40
IterTy safe_advance_dispatch(IterTy b, IterTy e, Distance n, std::random_access_iterator_tag)
Definition: gstl.h:202
typename runtime::FixedSizeAllocator< T > FixedSizeAlloc
[define Pow_2_VarSizeAlloc]
Definition: gstl.h:48
std::unordered_map< K, V, Hash, KeyEqual, FixedSizeAlloc< std::pair< const K, V >>> UnorderedMap
Definition: gstl.h:70
Definition: runtime/Mem.h:864
std::enable_if_t<!std::is_scalar< internal::Val_ty< I > >::value > uninitialized_destroy(I first, I last)
Destroy a range.
Definition: gstl.h:284
Str makeStr(const T &x)
Definition: gstl.h:102
Str operator()(const std::string &x) const
Definition: gstl.h:88
std::deque< T, Pow2Alloc< T >> Deque
[STL vector using Pow_2_VarSizeAlloc]
Definition: gstl.h:56
const I & begin(void) const
Definition: gstl.h:114
const Ty min(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:70
IterTy safe_advance(IterTy b, IterTy e, Distance n)
Like std::advance but returns end if end is closer than the advance amount.
Definition: gstl.h:222
T pop(void)
Definition: gstl.h:179
Definition: gstl.h:78
Definition: gstl.h:187
T value_type
Definition: Executor_ParaMeter.h:111
A fixed size block allocator.
Definition: runtime/Mem.h:703
Str operator()(const T &x) const
Definition: gstl.h:79
IterTy split_range(IterTy b, IterTy e)
Finds the midpoint of a range.
Definition: gstl.h:232
std::list< T, FixedSizeAlloc< T >> List
Definition: gstl.h:59
std::basic_string< char, std::char_traits< char >, Pow2Alloc< char >> Str
Definition: gstl.h:75
auto makeIterRange(const I &beg, const I &end)
Definition: gstl.h:119