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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
|
#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 <new>
#include <algorithm>
#include <assert.h>
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 size_t difference_type;
// typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T * pointer;
typedef const T * const_pointer;
protected:
pointer iv_start;
pointer iv_finish;
pointer iv_end_of_storage;
public:
/**
* Constructor default
* @post The vector is created with no elements. size() == 0, capacity() == 0
*/
__attribute__ ((always_inline))
explicit vector(void)
:
iv_start(NULL),
iv_finish(NULL),
iv_end_of_storage(NULL)
{}
/**
* Constructor to create a vector of size n elements of value.
* @param[in] n number of elements to create
* @param[in] value used to create the n elements
* @post The vector is created with n elements of value.
* Storage allocated. size() == n, capacity() == n
*/
explicit vector(size_type n, const T& value = T())
:
iv_start(NULL),
iv_finish(NULL),
iv_end_of_storage(NULL)
{
reserve(n);
iv_finish = iv_start+n;
ctor_fill(iv_start,iv_finish,value);
}
/**
* COPY CTOR create a vector from another vector
* @param[in] x source vector
* @post vector of x.size() is created from x with same # nodes
* size() == capacity() == x.size()
*/
vector(const vector<T>& x)
:
iv_start(NULL),
iv_finish(NULL),
iv_end_of_storage(NULL)
{
reserve(x.size());
iv_finish = ctor_copy(x.iv_start, x.iv_finish, iv_start);
}
/**
* CTOR create a vector from a container slice
* @param[in] first iterator first in source sequence
* @param[in] last iterator one past end of source sequence
* @returns None.
* @pre last > first; first,last contained within source vector
* @post vector is created from slice given
*/
template<typename InputIterator>
vector(InputIterator first, InputIterator last)
:
iv_start(NULL),
iv_finish(NULL),
iv_end_of_storage(NULL)
{
// assert(last >= first);
// input iterators only support operator ( ++i, i++,==,!=,*,->,=)
size_type n = 0;
for(InputIterator i = first; i != last; ++i) ++n;
reserve(n);
iv_finish = ctor_copy(first,last,iv_start);
}
/**
* DTOR
* @post Storage released
*/
__attribute__ ((always_inline))
~vector()
{
clear(); // call dtors
free_storage(iv_start);
}
/**
* 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)
{
clear();
reserve(x.size());
iv_finish = ctor_copy(x.iv_start, x.iv_finish, iv_start);
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);
}
/* TODO - Implement only if needed
reverse_iterator rbegin()
{
return(iv_finish -1);
}
const_reverse_iterator rend()
{
return (iv_start - 1);
}
*/
// 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 UINT64_MAX/sizeof(T);
}
/**
* Resize the vector to contain n elements
* @param[in] n new size
* @param[in] x object used to copy to any added elements if size() is increased
* @post All previously obtained iterators are invalid.
* @node if n < size(), vector is truncated.
* if n > size(), vector is padded with copies of x
*/
void resize( size_type n, T x = T());
/**
* Get the number of elements the vector can hold before needing to reallocate storage.
* @return element capacity of the vector
* @pre None.
* @post None.
*/
__attribute__ ((always_inline))
size_type capacity() const
{
return(iv_end_of_storage - iv_start);
}
/**
* 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);
}
/**
* Reserve storage for a given number of elements
* @param[in] n The requested capacity of the vector
* @pre None
* @post If current cpacity() < n then new capcity == n; else no change.
* All previously obtained iterators are invalid
*/
void reserve(size_type n);
// - 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)
{
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);
}
// -- Modifiers -----------------------------
/*
* Assign new content to the vector object
* @param[n] first iterator to first element to copy in
* @param[n] last iterator to last element + 1 to copy in
*/
template <class InputIterator>
void assign (InputIterator first, InputIterator last)
{
clear();
size_type n = 0;
for(InputIterator i = first; i != last; ++i) ++n;
reserve(n);
iv_finish = ctor_copy(first,last,iv_start);
}
/*
* 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)
{
clear();
reserve(n);
ctor_fill_n(iv_start,n,x);
iv_finish = iv_start + n;
}
/**
* 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)
{
reserve(size() + 1);
new (iv_finish++) T(x);
}
/**
* Remove the last element in the container
* @return nothing
* @pre size() > 0
* @post size() decreased by one
*/
__attribute__ ((always_inline))
void pop_back()
{
erase(iv_finish-1,iv_finish);
}
/**
* Insert an element into the container at a given position
* @param[in] position iterator to position to insert
* @param[in] x reference of element to insert
* @pre begin() <= position < end()
* @post Element inserted at position, storage adjusted as needed.
* All previously obtained iterators are invalid.
*/
iterator insert(iterator position, const T& x)
{
// iv_start will change if the vector gets resized - so save the offset for
// return.
difference_type offset = position - iv_start;
insert(position, 1, x);
return (iv_start + offset);
}
/**
* Insert a number of copies of a given elements at a given position
* @param[in] postion iterator, postion to insert elements
* @param[in] n number of elements to insert
* @param[in] x A reference to the object to uses to create the new elements
* @pre begin() <= postion < end()
* @post All previously obtained iterators are invalid.
*/
void insert (iterator position, size_type n, const T& x);
/**
* Insert a slice into the current container at a given position
* @param[in] position iterator, position to insert slice
* @param[in] first iterator to first element of slice insert
* @param[in] last iterator to last element + 1 of slice to insert
* @pre begin() <= postion <= end(), first < last.
* @post Elements inserted at postition. Storage adjusted as needed.
* All previously obtained iterators are invalid.
* @note element pointed to by last is not inserted.
*/
template <class InputIterator>
void insert (iterator position, InputIterator first,
InputIterator last);
/**
* Remove an element from the container
* @param[in] position iterator, position of element to remove
* @pre begin() <= position < end()
* @post All previously obtained iterators are invalid.
*/
__attribute__ ((always_inline))
void erase(iterator position)
{
erase(position,position+1);
}
/**
* Remove a slice of elements from the container
* @param[in] first iterator, postion of the first element to remove
* @param[in] last iterator, postion of the last element + 1 to remove
* @pre begin() <= first,last <= end(), first < last.
* @post All previously obtained iterators are invalid.
* @note The element pointed to be last is not deleted.
*/
void erase(iterator first, iterator last)
{
assert(last >= first);
assert(first >= iv_start);
assert(first < iv_finish);
assert(last > iv_start);
assert(last <= iv_finish);
first = copy(last,iv_finish,first);
while(first != iv_finish)
{
--iv_finish;
iv_finish->~T();
}
}
/**
* Swap this vector with another
* @param reference to another vector of this type
*/
void swap(vector<T>& x)
{
std::swap(iv_start,x.iv_start);
std::swap(iv_finish,x.iv_finish);
std::swap(iv_end_of_storage,x.iv_end_of_storage);
}
/**
* 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();
}
}
private:
/**
* Copy constructs elements into raw storage
* @param[in] first iterator of first element to copy
* @pararm[in] last iterator of last element + 1 to copy
* @param[in] destination iterator of destination
* @post elements moved
*/
template <class InputIterator, class OutputIterator>
OutputIterator
ctor_copy(InputIterator first,
InputIterator last,
OutputIterator destination)
{
while(first != last)
{
new (destination) T(*first);
++destination;
++first;
}
return(destination);
}
/**
* Copy constructs elements into raw storage
* @param[in] first iterator of first element to copy
* @param[in] last iterator of last element + 1 to copy
* @param[in] destination iterator to end of destination + 1
* @post elements moved
*/
template <class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2
ctor_copy_backward ( BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 destination)
{
while(last != first)
{
--destination;
--last;
new(destination) T(*last);
}
return destination;
}
/**
* fill by copy construct ino raw storage
* @param[in] first itertor fo first element
* @param[in] last iterator to last element + 1
* @param[in] value to use to fill
*/
template < class ForwardIterator, class Tp >
void
ctor_fill (ForwardIterator first, ForwardIterator last, const Tp& value )
{
while (first != last)
{
new (first) T(value);
++first;
}
}
/**
* fill by copy construct into raw storage
* @param[in] first iterator first location to fill
* @param[in] n number of elements to fill
* @param[in] value to use to fill
*/
template < class OutputIterator, class Size, class Tp >
void
ctor_fill_n( OutputIterator first, Size n, const Tp& value )
{
for(; n>0; --n)
{
new (first) T(value);
++first;
}
}
/**
* Free all the storage allocated to this vector
* @param[in] i_start iterator to start of storage block
*/
__attribute__ ((always_inline))
void free_storage(iterator i_start)
{
delete [] (uint8_t *)i_start;
}
/**
* Allocate storage for this vector
* @param[in] n, number of elements required
*/
__attribute__ ((always_inline))
iterator allocate_storage(size_type n)
{
return (iterator) new uint8_t[n * sizeof(T)];
}
/**
* debug dump
*/
//void dump(const char * msg = "")
//{
// puts(msg);
// printf("vector_dump::start 0x%016lx finish 0x%016lx eos 0x%016lx\n",
// (uint64_t)iv_start, (uint64_t)iv_finish, (uint64_t)iv_end_of_storage);
//}
};
}; // end namespace std
// ------------------------------------------------------------------------------------------------
template <class T>
void std::vector<T>::reserve(size_type n)
{
size_type c = capacity();
if(n > c)
{
// if requested new capacity < 10% of current capacity then increase by 10%
size_type dif = n - c;
size_type inc = 1 + (c/size_type(10));
if(dif < inc)
{
n += inc;
}
iterator newStart = allocate_storage(n);
if(NULL == iv_start)
{
iv_finish = newStart;
}
else
{
iterator newFinish = ctor_copy(iv_start, iv_finish, newStart);
clear();
iv_finish = newFinish;
free_storage(iv_start);
}
iv_end_of_storage = newStart + n;
iv_start = newStart;
}
}
// ------------------------------------------------------------------------------------------------
template <class T>
void std::vector<T>::insert (iterator position, size_type n, const T& x)
{
//assert (position >= iv_start);
//assert (position <= iv_finish);
size_type new_size = size() + n;
if(position == end())
{
reserve(new_size);
while(n--) new (iv_finish++) T(x);
}
else if(new_size > capacity())
{
vector<T> new_vec;
new_vec.reserve(new_size);
for(const_iterator i = begin(); i != end(); ++i)
{
if(i == position)
{
while(n--) new_vec.push_back(x);
}
new_vec.push_back(*i);
}
swap(new_vec); // swap this with new_vec
}
else // already have enough space
{
size_type m = iv_finish - position; // # of existing elements to move
pointer new_finish = iv_finish + n;
if(m < n)
{
ctor_copy_backward(position,iv_finish,new_finish);
while(n--)
{
if(position < iv_finish) *position = x;
else new (position) T(x);
++position;
}
}
else // n <= m
{
ctor_copy_backward(iv_finish-n,iv_finish,new_finish); // raw storage copy
copy_backward(position, iv_finish-n, iv_finish); // operator= copy
fill_n(position,n,x);
}
iv_finish = new_finish;
}
}
// ------------------------------------------------------------------------------------------------
template <class T>
template <class InputIterator>
void std::vector<T>::insert (iterator position,
InputIterator first,
InputIterator last)
// Should only move storage if there is not room
// InputIterators are not random access (eg. can't do diff = last - first)
{
size_type n = 0;
for(InputIterator i = first; i != last; ++i) ++n;
size_type new_size = size() + n;
if(position == end())
{
reserve(new_size);
iv_finish = ctor_copy(first,last,iv_finish);
}
else if(new_size > capacity()) // make a new vector
{
vector<T> new_vec;
new_vec.reserve(new_size);
for(const_iterator i = begin(); i != end(); ++i)
{
if(i == position)
{
while(n--) new_vec.push_back(*first++);
}
new_vec.push_back(*i);
}
swap(new_vec);
}
else // already have enough space
{
size_type m = iv_finish - position; // # of exising elements to adjust
if(m < n)
{
ctor_copy_backward(position,iv_finish,iv_finish+n); // cp all existing elements to raw storage
while(first != last)
{
if(position < iv_finish) *position = *first; // cp new elements to existing element locations
else new (position) T(*first); // cp remaining new elements to raw storage
++position;
++first;
}
}
else // n <= m
{
ctor_copy_backward(iv_finish-n, iv_finish, iv_finish+n); // cp existing elements to raw storage
copy_backward(position, iv_finish-n, iv_finish); // cp rest of existing elements to existing locations
copy(first,last,position); // cp in new elements to existing locations
}
iv_finish += n;
}
}
// ------------------------------------------------------------------------------------------------
template <class T>
void std::vector<T>::resize(size_type n, T x)
{
size_type sz = size();
if(n < sz)
{
erase(iv_start + n,iv_finish);
}
else if(n > sz)
{
insert(iv_finish,n-sz,x);
}
// else n == size() do nothing
}
#endif
|