2aae2c6c0b680a2160ced8ca78f60791e857b71b
[chaz/yoink] / src / Moof / cml / core / dynamic_1D.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 */
12
13 #ifndef dynamic_1D_h
14 #define dynamic_1D_h
15
16 #include <vector>
17 #include <cml/core/common.h>
18 #include <cml/dynamic.h>
19
20 namespace cml {
21
22 /** Dynamically-sized and allocated 1D array.
23 *
24 * @note The allocator should be an STL-compatible allocator.
25 *
26 * @internal The internal array type <em>must</em> have the proper copy
27 * semantics, otherwise copy construction will fail.
28 */
29 template<typename Element, class Alloc>
30 class dynamic_1D
31 {
32 public:
33
34 /* Record the allocator type: */
35 typedef typename Alloc::template rebind<Element>::other allocator_type;
36
37 /* Record the generator: */
38 typedef dynamic<Alloc> generator_type;
39
40 /* Array implementation: */
41 typedef std::vector<Element,allocator_type> array_impl;
42
43 /* Standard: */
44 typedef typename array_impl::value_type value_type;
45 typedef typename array_impl::pointer pointer;
46 typedef typename array_impl::reference reference;
47 typedef typename array_impl::const_reference const_reference;
48 typedef typename array_impl::const_pointer const_pointer;
49
50 /* For matching by memory type: */
51 typedef dynamic_memory_tag memory_tag;
52
53 /* For matching by size type: */
54 typedef dynamic_size_tag size_tag;
55
56 /* For matching by resizability: */
57 typedef resizable_tag resizing_tag;
58
59 /* For matching by dimensions: */
60 typedef oned_tag dimension_tag;
61
62
63 public:
64
65 /** Dynamic arrays have no fixed size. */
66 enum { array_size = -1 };
67
68
69 public:
70
71 /** Construct a dynamic array with no size. */
72 dynamic_1D() {}
73
74 /** Construct a dynamic array given the size. */
75 explicit dynamic_1D(size_t size) : m_data(size) {}
76
77
78 public:
79
80 /** Return the number of elements in the array. */
81 size_t size() const { return this->m_data.size(); }
82
83 /** Access to the data as a C array.
84 *
85 * @param i a size_t index into the array.
86 * @return a mutable reference to the array value at i.
87 *
88 * @note This function does not range-check the argument.
89 */
90 reference operator[](size_t i) { return this->m_data[i]; }
91
92 /** Const access to the data as a C array.
93 *
94 * @param i a size_t index into the array.
95 * @return a const reference to the array value at i.
96 *
97 * @note This function does not range-check the argument.
98 */
99 const_reference operator[](size_t i) const { return this->m_data[i]; }
100
101 /** Return access to the data as a raw pointer. */
102 pointer data() { return &m_data[0]; }
103
104 /** Return access to the data as a raw pointer. */
105 const_pointer data() const { return &m_data[0]; }
106
107
108 public:
109
110 /** Set the array size to the given value.
111 *
112 * @warning This is not guaranteed to preserve the original data.
113 */
114 void resize(size_t s) { this->m_data.resize(s); }
115
116
117 protected:
118
119 array_impl m_data;
120 };
121
122 } // namespace cml
123
124 #endif
125
126 // -------------------------------------------------------------------------
127 // vim:ft=cpp
This page took 0.03393 seconds and 3 git commands to generate.