]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: | |
3 | #ident "$Id$" | |
4 | /*====== | |
5 | This file is part of PerconaFT. | |
6 | ||
7 | ||
8 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. | |
9 | ||
10 | PerconaFT is free software: you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License, version 2, | |
12 | as published by the Free Software Foundation. | |
13 | ||
14 | PerconaFT is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. | |
21 | ||
22 | ---------------------------------------- | |
23 | ||
24 | PerconaFT is free software: you can redistribute it and/or modify | |
25 | it under the terms of the GNU Affero General Public License, version 3, | |
26 | as published by the Free Software Foundation. | |
27 | ||
28 | PerconaFT is distributed in the hope that it will be useful, | |
29 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
31 | GNU Affero General Public License for more details. | |
32 | ||
33 | You should have received a copy of the GNU Affero General Public License | |
34 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. | |
35 | ||
36 | ---------------------------------------- | |
37 | ||
38 | Licensed under the Apache License, Version 2.0 (the "License"); | |
39 | you may not use this file except in compliance with the License. | |
40 | You may obtain a copy of the License at | |
41 | ||
42 | http://www.apache.org/licenses/LICENSE-2.0 | |
43 | ||
44 | Unless required by applicable law or agreed to in writing, software | |
45 | distributed under the License is distributed on an "AS IS" BASIS, | |
46 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
47 | See the License for the specific language governing permissions and | |
48 | limitations under the License. | |
49 | ======= */ | |
50 | ||
51 | #ident \ | |
52 | "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." | |
53 | ||
54 | #pragma once | |
55 | ||
56 | #include <memory.h> | |
57 | ||
58 | //****************************************************************************** | |
59 | // | |
60 | // Overview: A growable array is a little bit like std::vector except that | |
61 | // it doesn't have constructors (hence can be used in static constructs, since | |
62 | // the google style guide says no constructors), and it's a little simpler. | |
63 | // Operations: | |
64 | // init and deinit (we don't have constructors and destructors). | |
65 | // fetch_unchecked to get values out. | |
66 | // store_unchecked to put values in. | |
67 | // push to add an element at the end | |
68 | // get_size to find out the size | |
69 | // get_memory_size to find out how much memory the data stucture is using. | |
70 | // | |
71 | //****************************************************************************** | |
72 | ||
73 | namespace toku { | |
74 | ||
75 | template <typename T> | |
76 | class GrowableArray { | |
77 | public: | |
78 | void init(void) | |
79 | // Effect: Initialize the array to contain no elements. | |
80 | { | |
81 | m_array = NULL; | |
82 | m_size = 0; | |
83 | m_size_limit = 0; | |
84 | } | |
85 | ||
86 | void deinit(void) | |
87 | // Effect: Deinitialize the array (freeing any memory it uses, for example). | |
88 | { | |
89 | toku_free(m_array); | |
90 | m_array = NULL; | |
91 | m_size = 0; | |
92 | m_size_limit = 0; | |
93 | } | |
94 | ||
95 | T fetch_unchecked(size_t i) const | |
96 | // Effect: Fetch the ith element. If i is out of range, the system asserts. | |
97 | { | |
98 | return m_array[i]; | |
99 | } | |
100 | ||
101 | void store_unchecked(size_t i, T v) | |
102 | // Effect: Store v in the ith element. If i is out of range, the system | |
103 | // asserts. | |
104 | { | |
105 | paranoid_invariant(i < m_size); | |
106 | m_array[i] = v; | |
107 | } | |
108 | ||
109 | void push(T v) | |
110 | // Effect: Add v to the end of the array (increasing the size). The amortized | |
111 | // cost of this operation is constant. Implementation hint: Double the size | |
112 | // of the array when it gets too big so that the amortized cost stays | |
113 | // constant. | |
114 | { | |
115 | if (m_size >= m_size_limit) { | |
116 | if (m_array == NULL) { | |
117 | m_size_limit = 1; | |
118 | } else { | |
119 | m_size_limit *= 2; | |
120 | } | |
121 | XREALLOC_N(m_size_limit, m_array); | |
122 | } | |
123 | m_array[m_size++] = v; | |
124 | } | |
125 | ||
126 | size_t get_size(void) const | |
127 | // Effect: Return the number of elements in the array. | |
128 | { | |
129 | return m_size; | |
130 | } | |
131 | size_t memory_size(void) const | |
132 | // Effect: Return the size (in bytes) that the array occupies in memory. This | |
133 | // is really only an estimate. | |
134 | { | |
135 | return sizeof(*this) + sizeof(T) * m_size_limit; | |
136 | } | |
137 | ||
138 | private: | |
139 | T *m_array; | |
140 | size_t m_size; | |
141 | size_t m_size_limit; // How much space is allocated in array. | |
142 | }; | |
143 | ||
144 | } // namespace toku |