MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector.hpp
1 /*
2  Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 
20 /* mySTL vector implements simple vector, w/ swap
21  *
22  */
23 
24 #ifndef mySTL_VECTOR_HPP
25 #define mySTL_VECTOR_HPP
26 
27 #include "helpers.hpp" // construct, destory, fill, etc.
28 #include "algorithm.hpp" // swap
29 
30 
31 namespace mySTL {
32 
33 
34 template <typename T>
35 struct vector_base {
36  T* start_;
37  T* finish_;
38  T* end_of_storage_;
39 
40  vector_base() : start_(0), finish_(0), end_of_storage_(0) {}
41  vector_base(size_t n)
42  {
43  start_ = GetArrayMemory<T>(n);
44  finish_ = start_;
45  end_of_storage_ = start_ + n;
46  }
47 
48  ~vector_base()
49  {
50  FreeArrayMemory(start_);
51  }
52 
53  void Swap(vector_base& that)
54  {
55  swap(start_, that.start_);
56  swap(finish_, that.finish_);
57  swap(end_of_storage_, that.end_of_storage_);
58  }
59 };
60 
61 
62 
63 template <typename T>
64 class vector {
65 public:
66  typedef T* iterator;
67  typedef const T* const_iterator;
68 
69  vector() {}
70  explicit vector(size_t n) : vec_(n)
71  {
72  vec_.finish_ = uninit_fill_n(vec_.start_, n, T());
73  }
74 
75  ~vector() { destroy(vec_.start_, vec_.finish_); }
76 
77  vector(const vector& other) : vec_(other.size())
78  {
79  vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
80  vec_.start_);
81  }
82 
83  size_t capacity() const { return vec_.end_of_storage_ - vec_.start_; }
84 
85  size_t size() const { return vec_.finish_ - vec_.start_; }
86 
87  T& operator[](size_t idx) { return *(vec_.start_ + idx); }
88  const T& operator[](size_t idx) const { return *(vec_.start_ + idx); }
89 
90  const T* begin() const { return vec_.start_; }
91  const T* end() const { return vec_.finish_; }
92 
93  void push_back(const T& v)
94  {
95  if (vec_.finish_ != vec_.end_of_storage_) {
96  construct(vec_.finish_, v);
97  ++vec_.finish_;
98  }
99  else {
100  vector tmp(size() * 2 + 1, *this);
101  construct(tmp.vec_.finish_, v);
102  ++tmp.vec_.finish_;
103  Swap(tmp);
104  }
105  }
106 
107  void resize(size_t n, const T& v)
108  {
109  if (n == size()) return;
110 
111  if (n < size()) {
112  T* first = vec_.start_ + n;
113  destroy(first, vec_.finish_);
114  vec_.finish_ -= vec_.finish_ - first;
115  }
116  else {
117  vector tmp(n, *this);
118  tmp.vec_.finish_ = uninit_fill_n(tmp.vec_.finish_, n - size(), v);
119  Swap(tmp);
120  }
121  }
122 
123  void reserve(size_t n)
124  {
125  if (capacity() < n) {
126  vector tmp(n, *this);
127  Swap(tmp);
128  }
129  }
130 
131  void Swap(vector& that)
132  {
133  vec_.Swap(that.vec_);
134  }
135 private:
136  vector_base<T> vec_;
137 
138  vector& operator=(const vector&); // hide assign
139 
140  // for growing, n must be bigger than other size
141  vector(size_t n, const vector& other) : vec_(n)
142  {
143  if (n > other.size())
144  vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
145  vec_.start_);
146  }
147 };
148 
149 
150 
151 } // namespace mySTL
152 
153 #endif // mySTL_VECTOR_HPP