OSDN Git Service

invoke yamy{64,32} and yamyd32 from same directory of yamy.exe instead of current...
[yamy/yamy.git] / array.h
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r
2 // array.h\r
3 \r
4 \r
5 #ifndef _ARRAY_H\r
6 #  define _ARRAY_H\r
7 \r
8 #  include <memory>\r
9 \r
10 \r
11 ///\r
12 template <class T, class Allocator = std::allocator<T> >\r
13 class Array\r
14 {\r
15 public:\r
16         typedef typename Allocator::reference         reference; ///\r
17         typedef typename Allocator::const_reference   const_reference; ///\r
18         typedef typename Allocator::pointer           iterator; ///\r
19         typedef typename Allocator::const_pointer     const_iterator;   ///\r
20         typedef typename Allocator::size_type         size_type; ///\r
21         typedef typename Allocator::difference_type   difference_type; ///\r
22         typedef T                                     value_type;       ///\r
23         typedef Allocator                             allocator_type;   ///\r
24         typedef typename Allocator::pointer           pointer; ///\r
25         typedef typename Allocator::const_pointer     const_pointer; ///\r
26 #if 0\r
27         typedef std::reverse_iterator<iterator>       reverse_iterator; ///\r
28         typedef std::reverse_iterator<const_iterator> const_reverse_iterator;   ///\r
29 #endif\r
30 private:\r
31         Allocator m_allocator;                  ///\r
32         size_type m_size;                               ///\r
33         pointer m_buf;                          /// array buffer\r
34 \r
35 public:\r
36         /// constructor\r
37         explicit Array(const Allocator& i_allocator = Allocator())\r
38                         : m_allocator(i_allocator), m_size(0), m_buf(NULL)  { }\r
39 \r
40         /// constructor\r
41         explicit Array(size_type i_size, const T& i_value = T(),\r
42                                    const Allocator& i_allocator = Allocator())\r
43                         : m_allocator(i_allocator), m_size(i_size),\r
44                         m_buf(m_allocator.allocate(m_size, 0)) {\r
45                 std::uninitialized_fill_n(m_buf, i_size, i_value);\r
46         }\r
47 \r
48         /// constructor\r
49         template <class InputIterator>\r
50         Array(InputIterator i_begin, InputIterator i_end,\r
51                   const Allocator& i_allocator = Allocator())\r
52                         : m_allocator(i_allocator), m_size(distance(i_begin, i_end)),\r
53                         m_buf(Allocator::allocate(m_size, 0)) {\r
54                 std::uninitialized_copy(i_begin, i_end, m_buf);\r
55         }\r
56 \r
57         /// copy constructor\r
58         Array(const Array& i_o) : m_size(0), m_buf(NULL) {\r
59                 operator=(i_o);\r
60         }\r
61 \r
62         /// destractor\r
63         ~Array() {\r
64                 clear();\r
65         }\r
66 \r
67         ///\r
68         Array& operator=(const Array& i_o) {\r
69                 if (&i_o != this) {\r
70                         clear();\r
71                         m_size = i_o.m_size;\r
72                         m_buf = m_allocator.allocate(m_size, 0);\r
73                         std::uninitialized_copy(i_o.m_buf, i_o.m_buf + m_size, m_buf);\r
74                 }\r
75                 return *this;\r
76         }\r
77         ///\r
78         allocator_type get_allocator() const {\r
79                 return Allocator();\r
80         }\r
81         /// return pointer to the array buffer\r
82         typename allocator_type::pointer get() {\r
83                 return m_buf;\r
84         }\r
85         /// return pointer to the array buffer\r
86         typename allocator_type::const_pointer get() const {\r
87                 return m_buf;\r
88         }\r
89         ///\r
90         iterator begin() {\r
91                 return m_buf;\r
92         }\r
93         ///\r
94         const_iterator begin() const {\r
95                 return m_buf;\r
96         }\r
97         ///\r
98         iterator end() {\r
99                 return m_buf + m_size;\r
100         }\r
101         ///\r
102         const_iterator end() const {\r
103                 return m_buf + m_size;\r
104         }\r
105 #if 0\r
106         ///\r
107         reverse_iterator rbegin() {\r
108                 reverse_iterator(end());\r
109         }\r
110         ///\r
111         const_reverse_iterator rbegin() const {\r
112                 const_reverse_iterator(end());\r
113         }\r
114         ///\r
115         reverse_iterator rend() {\r
116                 reverse_iterator(begin());\r
117         }\r
118         ///\r
119         const_reverse_iterator rend() const {\r
120                 const_reverse_iterator(begin());\r
121         }\r
122 #endif\r
123         ///\r
124         size_type size() const {\r
125                 return m_size;\r
126         }\r
127         ///\r
128         size_type max_size() const {\r
129                 return -1;\r
130         }\r
131         /// resize the array buffer.  NOTE: the original contents are cleared.\r
132         void resize(size_type i_size, const T& i_value = T()) {\r
133                 clear();\r
134                 m_size = i_size;\r
135                 m_buf = m_allocator.allocate(m_size, 0);\r
136                 std::uninitialized_fill_n(m_buf, i_size, i_value);\r
137         }\r
138         /// resize the array buffer.\r
139         template <class InputIterator>\r
140         void resize(InputIterator i_begin, InputIterator i_end) {\r
141                 clear();\r
142                 m_size = distance(i_begin, i_end);\r
143                 m_buf = m_allocator.allocate(m_size, 0);\r
144                 std::uninitialized_copy(i_begin, i_end, m_buf);\r
145         }\r
146         /// expand the array buffer. the contents of it are copied to the new one\r
147         void expand(size_type i_size, const T& i_value = T()) {\r
148                 ASSERT( m_size <= i_size );\r
149                 if (!m_buf)\r
150                         resize(i_size, i_value);\r
151                 else {\r
152                         pointer buf = m_allocator.allocate(i_size, 0);\r
153                         std::uninitialized_copy(m_buf, m_buf + m_size, buf);\r
154                         std::uninitialized_fill_n(buf + m_size, i_size - m_size, i_value);\r
155                         clear();\r
156                         m_size = i_size;\r
157                         m_buf = buf;\r
158                 }\r
159         }\r
160         ///\r
161         bool empty() const {\r
162                 return !m_buf;\r
163         }\r
164         ///\r
165         reference operator[](size_type i_n) {\r
166                 return *(m_buf + i_n);\r
167         }\r
168         ///\r
169         const_reference operator[](size_type i_n) const {\r
170                 return *(m_buf + i_n);\r
171         }\r
172         ///\r
173         const_reference at(size_type i_n) const {\r
174                 return *(m_buf + i_n);\r
175         }\r
176         ///\r
177         reference at(size_type i_n) {\r
178                 return *(m_buf + i_n);\r
179         }\r
180         ///\r
181         reference front() {\r
182                 return *m_buf;\r
183         }\r
184         ///\r
185         const_reference front() const {\r
186                 return *m_buf;\r
187         }\r
188         ///\r
189         reference back() {\r
190                 return *(m_buf + m_size - 1);\r
191         }\r
192         ///\r
193         const_reference back() const {\r
194                 return *(m_buf + m_size - 1);\r
195         }\r
196         ///\r
197         void swap(Array &i_o) {\r
198                 if (&i_o != this) {\r
199                         pointer buf = m_buf;\r
200                         size_type size = m_size;\r
201                         m_buf = i_o.m_buf;\r
202                         m_size = i_o.m_size;\r
203                         i_o.m_buf = buf;\r
204                         i_o.m_size = size;\r
205                 }\r
206         }\r
207         ///\r
208         void clear() {\r
209                 if (m_buf) {\r
210                         for (size_type i = 0; i < m_size; i ++)\r
211                                 m_allocator.destroy(&m_buf[i]);\r
212                         m_allocator.deallocate(m_buf, m_size);\r
213                         m_buf = 0;\r
214                         m_size = 0;\r
215                 }\r
216         }\r
217 };\r
218 \r
219 #endif // _ARRAY_H\r