summaryrefslogtreecommitdiffstats
path: root/src/sbefw/vector
blob: 053d9bfc3af330d63cc92ce2df004a937ea0bf3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/sbefw/vector $                                            */
/*                                                                        */
/* OpenPOWER sbe Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016                             */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

#ifndef stl_vector
#define stl_vector

/**
 * @file vector
 * @brief simple stl vector template class declaration.
 */

#include <stddef.h>

#if !defined( __STDC_LIMIT_MACROS)
#define __STDC_LIMIT_MACROS
#endif
#include <stdint.h>
#include <pool.H>
#include <assert.h>
#include <new>
namespace std
{

    /**
     * @class vector
     * subset of stl vector
     * @note Does not support allocators, reverse iterators.
     */
    template <class T>
        class vector
        {
            public:

                typedef T * iterator;
                typedef const T * const_iterator;
                typedef T & reference;
                typedef const T & const_reference;
                typedef size_t size_type;
                typedef T value_type;
                typedef T * pointer;
                typedef const T * const_pointer;

            protected:

                pointer iv_start __attribute__ ((aligned (8)));
                pointer iv_finish;
                SBEVECTORPOOL::vectorMemPool_t *iv_poolPtr;
            public:

                /**
                 * Constructor default
                 * @post     The vector is created with storage of
                 *           G_BLOCKSIZE bytes.
                 */
                explicit vector(void)
                {
                    iv_poolPtr = SBEVECTORPOOL::allocMem();
                    assert ( NULL != iv_poolPtr)
                    iv_start = ( T* )iv_poolPtr->data;
                    iv_finish = iv_start;
                }


                /**
                 * MOVE COPY CTOR create a vector from another vector
                 * @param[in] x  source vector
                 * @post     Vector of x.size() is created from x with same
                 *           memory.
                 *           size() == capacity() == x.size()
                 * @note     move Copy construtor willuse shallow copy. So input
                 *           as well as output vector will point to same data
                 */
                vector(const vector<T>&& x)
                {
                        iv_start = x.iv_start;
                        iv_finish = x.iv_finish;
                        iv_poolPtr = x.iv_poolPtr;
                        iv_poolPtr->refCount++;
                }

                /**
                 * Reserve space for atleast n elements
                 * @param[in] n Number of elements
                 * @note    We are having fixed size vectors in ppe. Defining
                 *          this function to avoid compile issues in standard
                 *          library. This function is noop for less than 512
                 *          bytes requirement. For more than 512 bytes, it will
                 *          assert.
                 */
                void reserve(size_type n)
                {
                    assert(n < max_size());
                    return;
                 }
                /**
                 * DTOR
                 * @post     Storage released
                 */
                __attribute__ ((always_inline))
                ~vector()
                {
                    clear();  // call dtors
                    SBEVECTORPOOL::releaseMem(iv_poolPtr);
                }

                /**
                 * Move Assignment operator.
                 * @param[in] x   A vector.
                 * @return  A vector (for the purpose of multiple assigns).
                 * @pre     None.
                 * @post    *this == x, this->capacity() == x.size().
                 *           All previously obtained iterators are invalid.
                 */
                vector<T>& operator=(const vector<T>&& x)
                {
                    // Just check here for pool to make sure
                    // input vector and current vector are not same;
                    if( iv_poolPtr != x.iv_poolPtr)
                    {
                        clear();
                        SBEVECTORPOOL::releaseMem(iv_poolPtr);
                        iv_start = x.iv_start;
                        iv_finish = x.iv_finish;
                        iv_poolPtr = x.iv_poolPtr;
                        iv_poolPtr->refCount++;
                     }
                    return(*this);
                }

                // Iterators --------------------

                /**
                 * Get iterator to the first vector element
                 * @return iterator of rist vector element
                 * @pre None.
                 * @post None.
                 */
                __attribute__ ((always_inline))
                iterator begin()
                {
                    return (iv_start);
                }

                /**
                 * Get const_iterator to the first vector element
                 * @return const_iterator of rist vector element
                 * @pre None.
                 * @post None.
                 */
                __attribute__ ((always_inline))
                const_iterator begin() const
                {
                    return (iv_start);
                }

                /**
                 * Get iterator to the last vector element + 1
                 * @return iterator
                 * @pre None.
                 * @post None.
                 */
                __attribute__ ((always_inline))
                iterator end()
                {
                    return (iv_finish);
                }

                /**
                 * Get const_iterator to the last vector element + 1
                 * @return const_iterator
                 * @pre None.
                 * @post None.
                 */
                __attribute__ ((always_inline))
                const_iterator end() const
                {
                    return (iv_finish);
                }

                //  Capacity -----------------------------------------------

                /**
                 * Get the number of elements in the container
                 * @return number of elements in the container
                 */
                __attribute__ ((always_inline))
                size_type size() const
                {
                    return(iv_finish - iv_start);
                }

                /**
                 * Return the maximum potential size the container could reach.
                 * @return number of the maximum element count this container
                 *         could reach
                 */
                __attribute__ ((always_inline))
                size_type max_size() const
                {
                    return SBEVECTORPOOL::G_BLOCKSIZE/(sizeof(T));
                }

                /**
                 * Query for empty container
                 * @return bool, true if size()==0 else false.
                 * @pre none
                 * @post none
                 */
                __attribute__ ((always_inline))
                bool empty() const
                {
                    return(size() == 0);
                }

                // - Element Access -----------------------------------

                /**
                 * Access a mutable reference to an element in the container
                 * @param An index into the vector
                 * @return A reference to an element
                 * @pre    0 <= n < size()
                 * @post   None.
                 */
                __attribute__ ((always_inline))
                reference operator[](size_type n)
                {
                    assert(n < size());
                    return(*(iv_start + n));
                }

                /**
                 * Access a mutable reference to an element in the container
                 * @param[in] index An index into the vector
                 * @return A reference to an element
                 * @pre    0 <= n < size()
                 * @post   None.
                 * @note no exception handling
                 */
                __attribute__ ((always_inline))
                reference at(size_type index)
                {
                    assert(index < size());
                    return(*(iv_start + index));
                }

                /**
                 * Get an immutable reference to an element in the container
                 * @param[in] index An index into the vector
                 * @return A const_reference to an object or type T
                 * @pre    0 <= n < size()
                 * @post   None.
                 */
                __attribute__ ((always_inline))
                const_reference operator[](size_type index) const
                {
                    assert(index < size());
                    return(*(iv_start + index));
                }

                /**
                 * Get an immutable reference to an element in the container
                 * @param[in] index An index into the vector
                 * @return A const_reference to an object or type T
                 * @pre    0 <= n < size()
                 * @post   None.
                 * @note no exception handling
                 */
                __attribute__ ((always_inline))
                const_reference at(size_type index) const
                {
                    assert(index < size());
                    return(*(iv_start + index));
                }

                /**
                 * Get a mutable reference to the first element in the container
                 * @return reference to first element
                 * @pre none
                 * @post None
                 */
                __attribute__ ((always_inline))
                reference front()
                {
                    return *iv_start;
                }

                /**
                 * Get an Immutable reference to the first element in the
                 * container
                 * @return const_reference to first element
                 * @pre none
                 * @post None
                 */
                __attribute__ ((always_inline))
                const_reference front() const
                {
                    return *iv_start;
                }

                /**
                 * Get a mutable reference to the last element in the container
                 * @return reference to last element
                 * @pre none
                 * @post None
                 */
                __attribute__ ((always_inline))
                reference back()
                {
                    return *(iv_finish-1);
                }

                /**
                 * Get an Immutable reference to the last element in the
                 * container
                 * @return reference to last element
                 * @pre none
                 * @post None
                 */
                __attribute__ ((always_inline))
                const_reference back() const
                {
                    return *(iv_finish-1);
                }

                /**
                 * Add element to the back of the container
                 * @param[in] x reference to object used to create new element
                 * @pre none
                 * @post All previously obtained iterators are invalid.
                 */
                __attribute__ ((always_inline))
                void push_back(const T& x)
                {
                    assert(max_size() > size());
                    new (iv_finish++) T(x);
                }

                /**
                 * Clear the vector
                 * @pre none.
                 * @post size() = 0, All previously obtained iterators are
                 *       invalid
                 * @note capacity unchanged
                 */
                void clear ()
                {
                    while(iv_finish != iv_start)
                    {
                        --iv_finish;
                        (iv_finish)->~T();
                    }
                }

                /*
                 * Assign new content to the vector object
                 * @param[in] n number of elements to assign
                 * @param[in] x reference to element to copy in
                 */
                void assign ( size_type n, const T& x)
                {
                    assert(n < max_size());
                    clear();
                    for (  ; n> 0; n--)
                        push_back( x);
                }

            private:
                vector(const vector<T>& x);
                vector<T>& operator=(const vector<T>& x);
};

}; // end namespace std


#endif
/* vim: set filetype=cpp : */
OpenPOWER on IntegriCloud