source: to-imperative/trunk/libp++/pxx_vector.ih @ 1203

Last change on this file since 1203 was 1203, checked in by sveta, 17 years ago
  • Old version (with decrease and expand).
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
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
10namespace pxx
11{
12
13template <typename type_t>
14Vector<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
31template <typename type_t>
32Vector<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
42template <typename type_t>
43Vector<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
53template <typename type_t>
54Vector<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
63template <typename type_t>
64type_t const* Vector<type_t>::get_data () const
65{
66  return data;
67}
68
69template <typename type_t>
70void Vector<type_t>::decrease (size_t _extra)
71{
72  size_t i;
73  if (_extra > length) {
74    for ( i = 0; i < length; i++)
75      (data + i)-> ~type_t();
76    length = 0;
77  } else {
78    for ( i = length - _extra; i < length; i++)
79      (data + i)-> ~type_t();
80    length -= _extra;
81  }
82}
83
84template <typename type_t>
85void Vector<type_t>::expand (size_t _extra)
86{
87  size_t n;
88  if (capacity - length < _extra) {
89    if ((default_allocator.get_features() & ALLOCATOR_HAS_GET_REAL_SIZE) != 0) {
90      n = default_allocator.get_real_size(sizeof(type_t) * (length + _extra));
91    } else {
92      n = exps_of_two[get_order(sizeof(type_t) * (length + _extra))];
93    }
94    data = static_cast<type_t*>(default_allocator.reallocate(data, n));
95    capacity = n / sizeof(type_t) ;
96  }
97  for (size_t i = 0; i < _extra; i++) {
98    new(data + length + i) type_t();
99  }
100  length += _extra;
101}
102
103template <typename type_t>
104type_t const& Vector<type_t>::operator [] (size_t _index) const
105{
106  if (_index < length) {
107    return data[_index];
108  } else {
109    throw OutOfRange();
110  }
111}
112
113template <typename type_t>
114type_t& Vector<type_t>::operator [] (size_t _index)
115{
116  if (_index < length) {
117    return data[_index];
118  } else {
119    throw OutOfRange();
120  }
121}
122
123template <typename type_t>
124size_t Vector<type_t>::get_length () const
125{
126  return length;
127}
128
129template <typename type_t>
130size_t Vector<type_t>::get_capacity () const
131{
132  return capacity;
133}
134
135}
136
137#endif // __pxx_vector_ih__
Note: See TracBrowser for help on using the repository browser.