BCP_vector_char.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 
4 #include "BCP_vector.hpp"
5 
6 //##############################################################################
7 
8 /* Here only the non-inlined methods are defined */
9 
10 //##############################################################################
11 
13 {
14  const size_t after_pos = finish - position;
15  if (finish != end_of_storage) {
16  memmove(position+1, position, after_pos * sizeof(char));
17  *position = x;
18  ++finish;
19  } else {
20  const size_t len = (2*size() + 0x100);
21  iterator tmp = allocate(len);
22  const size_t until_pos = position - start;
23  memmove(tmp, start, until_pos * sizeof(char));
24  iterator tmp_finish = tmp + until_pos;
25  *tmp_finish++ = x;
26  memmove(tmp_finish, position, after_pos * sizeof(char));
27  deallocate();
28  start = tmp;
29  finish = start + (until_pos + 1 + after_pos);
30  end_of_storage = tmp + len;
31  }
32 }
33 
34 //##############################################################################
35 
36 template<> BCP_vec<char>::BCP_vec(const size_t n, const_reference value) :
37  start(0), finish(0), end_of_storage(0)
38 {
39  if (n > 0) {
40  start = finish = allocate(n);
42  while (finish != end_of_storage)
43  *finish++ = value;
44  }
45 }
46 //------------------------------------------------------------------------------
48  start(0), finish(0), end_of_storage(0)
49 {
50  const size_t len = last - first;
51  if (len > 0) {
52  start = allocate(len);
53  memcpy(start, first, len * sizeof(char));
54  end_of_storage = finish = start + len;
55  }
56 }
57 //------------------------------------------------------------------------------
58 template<> BCP_vec<char>::BCP_vec(const char* x, const size_t num) :
59  start(0), finish(0), end_of_storage(0)
60 {
61  if (num > 0) {
62  start = allocate(num);
63  memcpy(start, x, num * sizeof(char));
64  end_of_storage = finish = start + num;
65  }
66 }
67 
68 //##############################################################################
69 
70 template<> void BCP_vec<char>::reserve(const size_t n)
71 {
72  if (capacity() < n) {
73  iterator tmp = allocate(n);
74  const size_t oldsize = size();
75  if (oldsize > 0)
76  memcpy(tmp, start, oldsize * sizeof(char));
77  deallocate();
78  start = tmp;
79  finish = start + oldsize;
81  }
82 }
83 //------------------------------------------------------------------------------
85 {
86  if (&x != this) {
87  const size_t x_size = x.size();
88  if (x_size > capacity()) {
89  deallocate();
90  start = allocate(x_size);
91  end_of_storage = start + x_size;
92  }
93  if (x_size > 0)
94  memcpy(start, x.start, x_size * sizeof(char));
95  finish = start + x_size;
96  }
97  return *this;
98 }
99 
100 //##############################################################################
101 // these two members serve to copy out entries from a buffer.
102 
103 template<> void BCP_vec<char>::assign(const void* x, const size_t num)
104 {
105  if (num > capacity()){
106  deallocate();
107  start = allocate(num);
108  end_of_storage = start + num;
109  }
110  memcpy(start, x, num * sizeof(char));
111  finish = start + num;
112 }
113 //------------------------------------------------------------------------------
114 template<> void BCP_vec<char>::insert(char* position,
115  const void* first, const size_t n)
116 {
117  if (n == 0) return;
118  if ((size_t) (end_of_storage - finish) >= n) {
119  if (finish != position)
120  memmove(position + n, position, (finish - position) * sizeof(char));
121  memcpy(position, first, n * sizeof(char));
122  finish += n;
123  } else {
124  const size_t new_size = (2*size() + n);
125  iterator tmp = allocate(new_size);
126  const size_t after_pos = finish - position;
127  const size_t until_pos = position - start;
128  memcpy(tmp, start, until_pos * sizeof(char));
129  memcpy(tmp + until_pos, first, n * sizeof(char));
130  memcpy(tmp + (until_pos + n), position, after_pos * sizeof(char));
131  deallocate();
132  start = tmp;
133  finish = start + (until_pos + n + after_pos);
134  end_of_storage = tmp + new_size;
135  }
136 }
137 //------------------------------------------------------------------------------
138 template<> void BCP_vec<char>::insert(iterator position,
139  const_iterator first,
140  const_iterator last)
141 {
142  if (first == last) return;
143  const size_t n = last - first;
144  if ((size_t) (end_of_storage - finish) >= n) {
145  memmove(position + n, position, (finish - position) * sizeof(char));
146  memcpy(position, first, n * sizeof(char));
147  finish += n;
148  } else {
149  const size_t new_size = (2*size() + n);
150  iterator tmp = allocate(new_size);
151  const size_t after_pos = finish - position;
152  const size_t until_pos = position - start;
153  memcpy(tmp, start, until_pos * sizeof(char));
154  memcpy(tmp + until_pos, first, n * sizeof(char));
155  memcpy(tmp + (until_pos + n), position, after_pos * sizeof(char));
156  deallocate();
157  start = tmp;
158  finish = start + (until_pos + n + after_pos);
159  end_of_storage = tmp + new_size;
160  }
161 }
162 //------------------------------------------------------------------------------
163 template<> void BCP_vec<char>::insert(iterator position, const size_t n,
165 {
166  if (n == 0) return;
167  if ((size_t) (end_of_storage - finish) >= n) {
168  memmove(position + n, position, (finish - position) * sizeof(char));
169  finish += n;
170  for (int i = n; i > 0; --i)
171  *position++ = x;
172  } else {
173  const size_t new_size = (2*size() + n);
174  iterator tmp = allocate(new_size);
175  const size_t after_pos = finish - position;
176  const size_t until_pos = position - start;
177  memcpy(tmp, start, until_pos * sizeof(char));
178  memcpy(tmp + (until_pos + n), position, after_pos * sizeof(char));
179  deallocate();
180  start = tmp;
181  finish = start + (until_pos + n + after_pos);
182  end_of_storage = tmp + new_size;
183  position = tmp + until_pos;
184  for (int i = n; i > 0; --i)
185  *position++ = x;
186  }
187 }
188 
189 //##############################################################################
void deallocate()
Destroy the entries in the vector and free the memory allocated for the vector.
void insert_aux(iterator position, const_reference x)
insert x into the given position in the vector.
iterator finish
Iterator pointing to right after the last entry in the vector.
Definition: BCP_vector.hpp:68
const char & const_reference
Definition: BCP_vector.hpp:39
void reserve(const size_t n)
Reallocate the object to make space for n entries.
size_t capacity() const
Return the capacity of the object (space allocated for this many entries).
Definition: BCP_vector.hpp:119
BCP_vec< T > & operator=(const BCP_vec< T > &x)
Copy the contents of x into the object and return a reference the the object itself.
iterator end_of_storage
Iterator pointing to right after the last memory location usable by the vector without reallocation...
Definition: BCP_vector.hpp:71
iterator start
Iterator pointing to the beginning of the memory array where the vector is stored.
Definition: BCP_vector.hpp:66
BCP_vec()
The default constructor initializes the data members as 0 pointers.
void insert(iterator position, const void *first, const size_t num)
Insert num entries starting from memory location first into the vector from position pos...
size_t size() const
Return the current number of entries.
Definition: BCP_vector.hpp:116
iterator allocate(size_t len)
allocate raw, uninitialized memory for len entries.
void fint * n
void assign(const void *x, const size_t num)
Copy num entries of type T starting at the memory location x into the object.
const char * const_iterator
Definition: BCP_vector.hpp:35
void fint fint fint real fint real * x