1 | #ifndef __pxx_vector_ih__ |
---|
2 | #define __pxx_vector_ih__ |
---|
3 | |
---|
4 | #include "pxx_vector.hh" |
---|
5 | #include "pxx_common.ih" |
---|
6 | #include "pxx_default_allocator.ih" |
---|
7 | |
---|
8 | #include <new> |
---|
9 | |
---|
10 | namespace pxx |
---|
11 | { |
---|
12 | |
---|
13 | template <typename type_t> |
---|
14 | Vector<type_t>::Vector (size_t _length /* = 0 */) : |
---|
15 | length (_length) |
---|
16 | { |
---|
17 | // PMETHOD; |
---|
18 | size_t n; |
---|
19 | if ((default_allocator.get_features() & ALLOCATOR_HAS_GET_REAL_SIZE) != 0) { |
---|
20 | n = default_allocator.get_real_size(sizeof(type_t) * length); |
---|
21 | } else { |
---|
22 | n = exps_of_two[get_order(sizeof(type_t) * length)]; |
---|
23 | } |
---|
24 | data = static_cast<type_t*>(default_allocator.allocate(n)); |
---|
25 | capacity = n / sizeof(type_t) ; |
---|
26 | for (size_t i = 0; i < length; i++) { |
---|
27 | new(data + i) type_t(); |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | template <typename type_t> |
---|
32 | Vector<type_t>::Vector (Vector const& _s) : |
---|
33 | length (_s.length) |
---|
34 | { |
---|
35 | // PMETHOD; |
---|
36 | new(this) Vector(_s.length); |
---|
37 | for (size_t i = 0; i < length; i++) { |
---|
38 | data[i] = _s.data[i]; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | template <typename type_t> |
---|
43 | Vector<type_t>::~Vector () |
---|
44 | { |
---|
45 | // PMETHOD; |
---|
46 | for (size_t i = 0; i < length; i++) { |
---|
47 | // delete (data + i) (data + i); |
---|
48 | (data + i)->~type_t(); |
---|
49 | } |
---|
50 | default_allocator.deallocate(data); |
---|
51 | } |
---|
52 | |
---|
53 | template <typename type_t> |
---|
54 | Vector<type_t>& Vector<type_t>::operator = (Vector const& _s) |
---|
55 | { |
---|
56 | if (this != &_s) { |
---|
57 | this->~Vector(); |
---|
58 | new(this) Vector(_s); |
---|
59 | } |
---|
60 | return self; |
---|
61 | } |
---|
62 | |
---|
63 | template <typename type_t> |
---|
64 | type_t const* Vector<type_t>::get_data () const |
---|
65 | { |
---|
66 | return data; |
---|
67 | } |
---|
68 | |
---|
69 | template <typename type_t> |
---|
70 | void Vector<type_t>::expand (size_t _extra) |
---|
71 | { |
---|
72 | size_t n; |
---|
73 | if (capacity - length < _extra) { |
---|
74 | if ((default_allocator.get_features() & ALLOCATOR_HAS_GET_REAL_SIZE) != 0) { |
---|
75 | n = default_allocator.get_real_size(sizeof(type_t) * (length + _extra)); |
---|
76 | } else { |
---|
77 | n = exps_of_two[get_order(sizeof(type_t) * (length + _extra))]; |
---|
78 | } |
---|
79 | data = static_cast<type_t*>(default_allocator.reallocate(data, n)); |
---|
80 | capacity = n / sizeof(type_t) ; |
---|
81 | } |
---|
82 | for (size_t i = 0; i < _extra; i++) { |
---|
83 | new(data + length + i) type_t(); |
---|
84 | } |
---|
85 | length += _extra; |
---|
86 | } |
---|
87 | |
---|
88 | template <typename type_t> |
---|
89 | type_t const& Vector<type_t>::operator [] (size_t _index) const |
---|
90 | { |
---|
91 | if (_index < length) { |
---|
92 | return data[_index]; |
---|
93 | } else { |
---|
94 | throw OutOfRange(); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | template <typename type_t> |
---|
99 | type_t& Vector<type_t>::operator [] (size_t _index) |
---|
100 | { |
---|
101 | if (_index >= length) expand(_index - length + 1); |
---|
102 | return data[_index]; |
---|
103 | } |
---|
104 | |
---|
105 | template <typename type_t> |
---|
106 | size_t Vector<type_t>::get_length () const |
---|
107 | { |
---|
108 | return length; |
---|
109 | } |
---|
110 | |
---|
111 | template <typename type_t> |
---|
112 | size_t Vector<type_t>::get_capacity () const |
---|
113 | { |
---|
114 | return capacity; |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|
118 | |
---|
119 | #endif // __pxx_vector_ih__ |
---|