Phosphor
dyn_array.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
17 //===-------------------------- dynarray ----------------------------------===//
18 //
19 // The LLVM Compiler Infrastructure
20 //
21 // This file is dual licensed under the MIT and the University of Illinois Open
22 // Source Licenses. See LICENSE.TXT for details.
23 //
24 //===----------------------------------------------------------------------===//
25 
27 //==============================================================================
28 //libc++ License
29 //==============================================================================
30 //
31 //The libc++ library is dual licensed under both the University of Illinois
32 //"BSD-Like" license and the MIT license. As a user of this code you may choose
33 //to use it under either license. As a contributor, you agree to allow your code
34 //to be used under both.
35 //
36 //Full text of the relevant licenses is included below.
37 //
38 //==============================================================================
39 //
40 //University of Illinois/NCSA
41 // Open Source License
42 //
43 //Copyright (c) 2009-2016 by the contributors listed in CREDITS.TXT
44 //
45 // All rights reserved.
46 //
47 //Developed by:
48 //
49 //LLVM Team
50 //
51 //University of Illinois at Urbana-Champaign
52 //
53 // http://llvm.org
54 //
55 //Permission is hereby granted, free of charge, to any person obtaining a copy of
56 //this software and associated documentation files (the "Software"), to deal with
57 // the Software without restriction, including without limitation the rights to
58 //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
59 //of the Software, and to permit persons to whom the Software is furnished to do
60 //so, subject to the following conditions:
61 //
62 //* Redistributions of source code must retain the above copyright notice,
63 //this list of conditions and the following disclaimers.
64 //
65 //* Redistributions in binary form must reproduce the above copyright notice,
66 //this list of conditions and the following disclaimers in the
67 //documentation and/or other materials provided with the distribution.
68 //
69 //* Neither the names of the LLVM Team, University of Illinois at
70 //Urbana-Champaign, nor the names of its contributors may be used to
71 //endorse or promote products derived from this Software without specific
72 // prior written permission.
73 //
74 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
75 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
76 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
77 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
78 //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
79 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
80 //SOFTWARE.
81 //
82 //==============================================================================
83 //
84 //Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT
85 //
86 // Permission is hereby granted, free of charge, to any person obtaining a copy
87 //of this software and associated documentation files (the "Software"), to deal
88 //in the Software without restriction, including without limitation the rights
89 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
90 // copies of the Software, and to permit persons to whom the Software is
91 //furnished to do so, subject to the following conditions:
92 //
93 // The above copyright notice and this permission notice shall be included in
94 //all copies or substantial portions of the Software.
95 //
96 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
98 //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
99 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
100 //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
101 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
102 // THE SOFTWARE.
103 
105 //This file is a partial list of people who have contributed to the LLVM/libc++
106 //project. If you have contributed a patch or made some other contribution to
107 // LLVM/libc++, please submit a patch to this file to add yourself, and it will be
108 //done!
109 //
110 //The list is sorted by surname and formatted to allow easy grepping and
111 // beautification by scripts. The fields are: name (N), email (E), web-address
112 //(W), PGP key ID and fingerprint (P), description (D), and snail-mail address
113 // (S).
114 //
115 //N: Saleem Abdulrasool
116 // E: compnerd@compnerd.org
117 // D: Minor patches and Linux fixes.
118 //
119 //N: Dan Albert
120 // E: danalbert@google.com
121 // D: Android support and test runner improvements.
122 //
123 //N: Dimitry Andric
124 // E: dimitry@andric.com
125 // D: Visibility fixes, minor FreeBSD portability patches.
126 //
127 //N: Holger Arnold
128 // E: holgerar@gmail.com
129 // D: Minor fix.
130 //
131 //N: Ruben Van Boxem
132 //E: vanboxem dot ruben at gmail dot com
133 //D: Initial Windows patches.
134 //
135 //N: David Chisnall
136 // E: theraven at theravensnest dot org
137 // D: FreeBSD and Solaris ports, libcxxrt support, some atomics work.
138 //
139 //N: Marshall Clow
140 // E: mclow.lists@gmail.com
141 // E: marshall@idio.com
142 // D: C++14 support, patches and bug fixes.
143 //
144 //N: Eric Fiselier
145 // E: eric@efcs.ca
146 // D: LFTS support, patches and bug fixes.
147 //
148 //N: Bill Fisher
149 // E: william.w.fisher@gmail.com
150 // D: Regex bug fixes.
151 //
152 //N: Matthew Dempsky
153 // E: matthew@dempsky.org
154 // D: Minor patches and bug fixes.
155 //
156 //N: Google Inc.
157 //D: Copyright owner and contributor of the CityHash algorithm
158 //
159 //N: Howard Hinnant
160 // E: hhinnant@apple.com
161 // D: Architect and primary author of libc++
162 //
163 //N: Hyeon-bin Jeong
164 //E: tuhertz@gmail.com
165 // D: Minor patches and bug fixes.
166 //
167 //N: Argyrios Kyrtzidis
168 // E: kyrtzidis@apple.com
169 // D: Bug fixes.
170 //
171 //N: Bruce Mitchener, Jr.
172 //E: bruce.mitchener@gmail.com
173 // D: Emscripten-related changes.
174 //
175 //N: Michel Morin
176 // E: mimomorin@gmail.com
177 // D: Minor patches to is_convertible.
178 //
179 //N: Andrew Morrow
180 // E: andrew.c.morrow@gmail.com
181 // D: Minor patches and Linux fixes.
182 //
183 //N: Arvid Picciani
184 // E: aep at exys dot org
185 // D: Minor patches and musl port.
186 //
187 //N: Bjorn Reese
188 // E: breese@users.sourceforge.net
189 // D: Initial regex prototype
190 //
191 // N: Nico Rieck
192 //E: nico.rieck@gmail.com
193 // D: Windows fixes
194 //
195 //N: Jon Roelofs
196 // E: jonathan@codesourcery.com
197 // D: Remote testing, Newlib port, baremetal/single-threaded support.
198 //
199 //N: Jonathan Sauer
200 // D: Minor patches, mostly related to constexpr
201 //
202 //N: Craig Silverstein
203 // E: csilvers@google.com
204 // D: Implemented Cityhash as the string hash function on 64-bit machines
205 //
206 //N: Richard Smith
207 // D: Minor patches.
208 //
209 //N: Joerg Sonnenberger
210 // E: joerg@NetBSD.org
211 // D: NetBSD port.
212 //
213 //N: Stephan Tolksdorf
214 // E: st@quanttec.com
215 // D: Minor <atomic> fix
216 //
217 //N: Michael van der Westhuizen
218 // E: r1mikey at gmail dot com
219 //
220 // N: Larisse Voufo
221 //D: Minor patches.
222 //
223 //N: Klaas de Vries
224 //E: klaas at klaasgaaf dot nl
225 //D: Minor bug fix.
226 //
227 //N: Zhang Xiongpang
228 // E: zhangxiongpang@gmail.com
229 // D: Minor patches and bug fixes.
230 //
231 //N: Xing Xue
232 // E: xingxue@ca.ibm.com
233 // D: AIX port
234 //
235 //N: Zhihao Yuan
236 // E: lichray@gmail.com
237 // D: Standard compatibility fixes.
238 //
239 //N: Jeffrey Yasskin
240 // E: jyasskin@gmail.com
241 // E: jyasskin@google.com
242 // D: Linux fixes.
243 
244 #ifndef _GSLP_dyn_array
245 #define _GSLP_dyn_array
246 
247 /*
248  dyn_array synopsis
249 
250 namespace std { namespace experimental {
251 
252 template< typename T >
253 class dyn_array
254 {
255  // types:
256  typedef T value_type;
257  typedef T& reference;
258  typedef const T& const_reference;
259  typedef T* pointer;
260  typedef const T* const_pointer;
261  typedef implementation-defined iterator;
262  typedef implementation-defined const_iterator;
263  typedef reverse_iterator<iterator> reverse_iterator;
264  typedef reverse_iterator<const_iterator> const_reverse_iterator;
265  typedef size_t size_type;
266  typedef ptrdiff_t difference_type;
267 
268 public:
269  // construct/copy/destroy:
270  explicit dyn_array(size_type c);
271  dyn_array(size_type c, const T& v);
272  dyn_array(const dyn_array& d);
273  dyn_array(initializer_list<T>);
274 
275  template <class Alloc>
276  dyn_array(allocator_arg_t, const Alloc& a, size_type c, const Alloc& alloc);
277  template <class Alloc>
278  dyn_array(allocator_arg_t, const Alloc& a, size_type c, const T& v, const Alloc& alloc);
279  template <class Alloc>
280  dyn_array(allocator_arg_t, const Alloc& a, const dyn_array& d, const Alloc& alloc);
281  template <class Alloc>
282  dyn_array(allocator_arg_t, const Alloc& a, initializer_list<T>, const Alloc& alloc);
283  dyn_array& operator=(const dyn_array&) = delete;
284  ~dyn_array();
285 
286  // iterators:
287  iterator begin() noexcept;
288  const_iterator begin() const noexcept;
289  const_iterator cbegin() const noexcept;
290  iterator end() noexcept;
291  const_iterator end() const noexcept;
292  const_iterator cend() const noexcept;
293 
294  reverse_iterator rbegin() noexcept;
295  const_reverse_iterator rbegin() const noexcept;
296  const_reverse_iterator crbegin() const noexcept;
297  reverse_iterator rend() noexcept;
298  const_reverse_iterator rend() const noexcept;
299  const_reverse_iterator crend() const noexcept;
300 
301  // capacity:
302  size_type size() const noexcept;
303  size_type max_size() const noexcept;
304  bool empty() const noexcept;
305 
306  // element access:
307  reference operator[](size_type n);
308  const_reference operator[](size_type n) const;
309 
310  reference front();
311  const_reference front() const;
312  reference back();
313  const_reference back() const;
314 
315  const_reference at(size_type n) const;
316  reference at(size_type n);
317 
318  // data access:
319  T* data() noexcept;
320  const T* data() const noexcept;
321 
322  // mutating member functions:
323  void fill(const T& v);
324 };
325 
326 }} // std::experimental
327 
328 */
329 
330 #include <cstddef>
331 #include <iterator>
332 #include <stdexcept>
333 #include <initializer_list>
334 #include <new>
335 #include <algorithm>
336 
337 namespace gsl_p {
338 
339 template <class _Tp>
340 struct dyn_array
341 {
342 public:
343  // types:
344  typedef dyn_array __self;
345  typedef _Tp value_type;
346  typedef value_type& reference;
347  typedef const value_type& const_reference;
348  typedef value_type* iterator;
349  typedef const value_type* const_iterator;
350  typedef value_type* pointer;
351  typedef const value_type* const_pointer;
352  typedef size_t size_type;
353  typedef ptrdiff_t difference_type;
354  typedef std::reverse_iterator<iterator> reverse_iterator;
355  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
356 
357 private:
358  size_t __size_;
359  value_type * __base_;
360  dyn_array () noexcept : __size_(0), __base_(nullptr) {}
361 
362  static inline value_type* __allocate ( size_t count )
363  {
364  if (std::numeric_limits<size_t>::max() / sizeof (value_type) <= count )
365  {
366  throw std::out_of_range("out_of_range");
367  }
368  return static_cast<value_type *> (::operator new(sizeof(value_type) * count));
369  }
370 
371  static inline void __deallocate ( value_type* __ptr ) noexcept
372  {
373  ::operator delete(static_cast<void *> (__ptr));
374  }
375 
376 public:
377 
378  explicit dyn_array(size_type __c);
379  dyn_array(size_type __c, const value_type& __v);
380  dyn_array(const dyn_array& __d);
381  dyn_array(std::initializer_list<value_type>);
382  dyn_array& operator=(const dyn_array&) = delete;
383  ~dyn_array();
384 
385  // iterators:
386  inline iterator begin() noexcept { return iterator(data()); }
387  inline const_iterator begin() const noexcept { return const_iterator(data()); }
388  inline const_iterator cbegin() const noexcept { return const_iterator(data()); }
389  inline iterator end() noexcept { return iterator(data() + __size_); }
390  inline const_iterator end() const noexcept { return const_iterator(data() + __size_); }
391  inline const_iterator cend() const noexcept { return const_iterator(data() + __size_); }
392 
393  inline reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
394  inline const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
395  inline const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
396  inline reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
397  inline const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
398  inline const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
399 
400  // capacity:
401  inline size_type size() const noexcept { return __size_; }
402  inline size_type max_size() const noexcept { return __size_; }
403  inline bool empty() const noexcept { return __size_ == 0; }
404 
405  // element access:
406  inline reference operator[](size_type __n) { return data()[__n]; }
407  inline const_reference operator[](size_type __n) const { return data()[__n]; }
408 
409  inline reference front() { return data()[0]; }
410  inline const_reference front() const { return data()[0]; }
411  inline reference back() { return data()[__size_-1]; }
412  inline const_reference back() const { return data()[__size_-1]; }
413 
414  inline const_reference at(size_type __n) const;
415  inline reference at(size_type __n);
416 
417  // data access:
418  inline _Tp* data() noexcept { return __base_; }
419  inline const _Tp* data() const noexcept { return __base_; }
420 
421  // mutating member functions:
422  inline void fill(const value_type& __v) { std::fill_n(begin(), __size_, __v); }
423 };
424 
425 template <class _Tp>
426 inline
427 dyn_array<_Tp>::dyn_array(size_type __c) : dyn_array ()
428 {
429  __base_ = __allocate (__c);
430  value_type *__data = data ();
431  for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
432  ::new (__data) value_type;
433 }
434 
435 template <class _Tp>
436 inline
437 dyn_array<_Tp>::dyn_array(size_type __c, const value_type& __v) : dyn_array ()
438 {
439  __base_ = __allocate (__c);
440  value_type *__data = data ();
441  for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
442  ::new (__data) value_type (__v);
443 }
444 
445 template <class _Tp>
446 inline
447 dyn_array<_Tp>::dyn_array(std::initializer_list<value_type> __il) : dyn_array ()
448 {
449  size_t sz = __il.size();
450  __base_ = __allocate (sz);
451  value_type *__data = data ();
452  auto src = __il.begin();
453  for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
454  ::new (__data) value_type (*src);
455 }
456 
457 template <class _Tp>
458 inline
460 {
461  size_t sz = __d.size();
462  __base_ = __allocate (sz);
463  value_type *__data = data ();
464  auto src = __d.begin();
465  for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
466  ::new (__data) value_type (*src);
467 }
468 
469 template <class _Tp>
470 inline
472 {
473  value_type *__data = data () + __size_;
474  for ( size_t i = 0; i < __size_; ++i )
475  (--__data)->_Tp::~_Tp();
476  __deallocate ( __base_ );
477 }
478 
479 template <class _Tp>
480 inline
481 typename dyn_array<_Tp>::reference
482 dyn_array<_Tp>::at(size_type __n)
483 {
484  if (__n >= __size_)
485  {
486  throw std::out_of_range("dyn_array::at");
487  }
488  return data()[__n];
489 }
490 
491 template <class _Tp>
492 inline
493 typename dyn_array<_Tp>::const_reference
494 dyn_array<_Tp>::at(size_type __n) const
495 {
496  if (__n >= __size_)
497  {
498  throw std::out_of_range("dyn_array::at");
499  }
500  return data()[__n];
501 }
502 
503 }
504 
505 #endif // _GSLP_dyn_array
Definition: dyn_array.h:340
Definition: dyn_array.h:337