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

Last change on this file since 1191 was 1191, checked in by pooh, 17 years ago
  • Added missing deallocate().
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 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>::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
88template <typename type_t>
89type_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
98template <typename type_t>
99type_t& Vector<type_t>::operator [] (size_t _index)
100{
101  if (_index >= length) expand(_index - length + 1);
102  return data[_index];
103}
104
105template <typename type_t>
106size_t Vector<type_t>::get_length () const
107{
108  return length;
109}
110
111template <typename type_t>
112size_t Vector<type_t>::get_capacity () const
113{
114  return capacity;
115}
116
117}
118
119#endif // __pxx_vector_ih__
Note: See TracBrowser for help on using the repository browser.