coin-Bcp
BCP_vector_char.hpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 
4 #include <cstring>
5 
6 //##############################################################################
7 
8 /* The methods that are commented out are sort of generic methods that do not
9  need specialization. Their implementation is in BCP_vector_general.hpp */
10 
11 //##############################################################################
12 
13 template<> inline void BCP_vec<char>::destroy(iterator pos)
14 {
15 }
16 //------------------------------------------------------------------------------
17 template<> inline void BCP_vec<char>::destroy_range(iterator first, iterator last)
18 {
19 }
20 //------------------------------------------------------------------------------
21 template<> inline void BCP_vec<char>::construct(iterator pos)
22 {
23  *pos = 0;
24 }
25 //------------------------------------------------------------------------------
26 template<> inline void BCP_vec<char>::construct(iterator pos, const_reference x)
27 {
28  *pos = x;
29 }
30 
31 //##############################################################################
32 
33 // template<> inline void BCP_vec<char>::allocate(size_t len)
34 //------------------------------------------------------------------------------
35 template<> inline void BCP_vec<char>::deallocate() {
36  if (start) {
37  ::operator delete(start);
38  }
39 }
40 //------------------------------------------------------------------------------
41 template<> void BCP_vec<char>::insert_aux(iterator position, const_reference x);
42 
43 //##############################################################################
44 
45 // template<> void BCP_vec<char>::BCP_vec();
46 //------------------------------------------------------------------------------
47 // template<> void BCP_vec<char>::BCP_vec(const BCP_vec<char>& x);
48 //------------------------------------------------------------------------------
49 template<> BCP_vec<char>::BCP_vec(const size_t n, const_reference value);
50 //------------------------------------------------------------------------------
51 template<> BCP_vec<char>::BCP_vec(const_iterator first, const_iterator last);
52 //------------------------------------------------------------------------------
53 template<> BCP_vec<char>::BCP_vec(const char* x, const size_t num);
54 
55 //##############################################################################
56 
57 template<> void BCP_vec<char>::reserve(const size_t n);
58 //------------------------------------------------------------------------------
59 // template<> inline void BCP_vec<char>::swap(BCP_vec<char>& x);
60 //------------------------------------------------------------------------------
62 
63 //##############################################################################
64 // these two members serve to copy out entries from a buffer.
65 
66 template<> void BCP_vec<char>::assign(const void* x, const size_t num);
67 //------------------------------------------------------------------------------
68 template<> void BCP_vec<char>::insert(char* position,
69  const void* first, const size_t n);
70 //------------------------------------------------------------------------------
71 template<> void BCP_vec<char>::insert(iterator position,
72  const_iterator first,
73  const_iterator last);
74 //------------------------------------------------------------------------------
75 template<> void BCP_vec<char>::insert(iterator position, const size_t n,
76  const_reference x);
77 //------------------------------------------------------------------------------
78 template<> inline BCP_vec<char>::iterator
80 {
81  const size_t n = position - start;
82  if (finish != end_of_storage && position == finish) {
83  *finish++ = x;
84  } else
85  insert_aux(position, x);
86  return start + n;
87 }
88 
89 //##############################################################################
90 
91 template<> inline void BCP_vec<char>::push_back(const_reference x)
92 {
93  if (finish != end_of_storage)
94  *finish++ = x;
95  else
96  insert_aux(finish, x);
97 }
98 //------------------------------------------------------------------------------
100 {
101  *finish++ = x;
102 }
103 //------------------------------------------------------------------------------
104 template<> inline void BCP_vec<char>::pop_back()
105 {
106  --finish;
107 }
108 //------------------------------------------------------------------------------
109 // template<> inline void BCP_vec<char>::clear();
110 //------------------------------------------------------------------------------
111 template<> inline void
113  const BCP_vec<char>& values)
114 {
115  if (positions.size() == 0)
116  return;
117  const_iterator val = values.begin();
118  BCP_vec<int>::const_iterator pos = positions.begin();
119  const BCP_vec<int>::const_iterator lastpos = positions.end();
120  while (pos != lastpos)
121  operator[](*pos++) = *val++;
122 }
123 //------------------------------------------------------------------------------
124 template<> inline void BCP_vec<char>::update(const BCP_vec<int>& positions,
125  const BCP_vec<char>& values)
126 {
127  if (positions.size() != values.size())
128  throw BCP_fatal_error("BCP_vec::update() called with unequal sizes.\n");
129  BCP_vec_sanity_check(positions.begin(), positions.end(), size());
130  unchecked_update(positions, values);
131 }
132 
133 //##############################################################################
134 
135 template<> inline void BCP_vec<char>::keep(iterator pos)
136 {
137  *start = *pos;
138  finish = start + 1;
139 }
140 //------------------------------------------------------------------------------
141 template<> inline void BCP_vec<char>::keep(iterator first, iterator last)
142 {
143  const size_t len = last - first;
144  std::memmove(start, first, len * sizeof(char));
145  finish = start + len;
146 }
147 //------------------------------------------------------------------------------
148 // template<> inline void
149 // BCP_vec<char>::unchecked_keep_by_index(BCP_vec<int>::const_iterator firstpos,
150 // BCP_vec<int>::const_iterator lastpos);
151 //------------------------------------------------------------------------------
152 // template<> inline void
153 // BCP_vec<char>::keep_by_index(BCP_vec<int>::const_iterator firstpos,
154 // BCP_vec<int>::const_iterator lastpos);
155 //------------------------------------------------------------------------------
156 // template<> inline void
157 // BCP_vec<char>::keep_by_index(const BCP_vec<int>& positions);
158 //------------------------------------------------------------------------------
159 // template<> inline void
160 // BCP_vec<char>::unchecked_keep_by_index(const BCP_vec<int>& positions);
161 
162 //##############################################################################
163 
164 template<> inline void BCP_vec<char>::erase(iterator position)
165 {
166  if (position + 1 != finish)
167  std::memmove(position, position + 1, ((finish-position) - 1) * sizeof(char));
168  --finish;
169 }
170 //------------------------------------------------------------------------------
171 template<> inline void BCP_vec<char>::erase(iterator first, iterator last)
172 {
173  if (first != last && last != finish)
174  std::memmove(first, last, (finish - last) * sizeof(char));
175  finish -= (last - first);
176 }
177 //------------------------------------------------------------------------------
178 // template <class T> inline void
179 // BCP_vec<T>::erase_by_index(const BCP_vec<int>& positions);
180 //------------------------------------------------------------------------------
181 // template <class T> inline void
182 // BCP_vec<T>::unchecked_erase_by_index(const BCP_vec<int>& positions);
183 //------------------------------------------------------------------------------
184 // template <class T> inline void
185 // BCP_vec<T>::erase_by_index(BCP_vec<int>::const_iterator firstpos,
186 // BCP_vec<int>::const_iterator lastpos);
187 //------------------------------------------------------------------------------
188 // template <class T> void
189 // BCP_vec<T>::unchecked_erase_by_index(BCP_vec<int>::const_iterator firstpos,
190 // BCP_vec<int>::const_iterator lastpos);
191 
192 //#############################################################################
193 
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
reference operator[](const size_t i)
Return a reference to the i-th entry.
Definition: BCP_vector.hpp:124
const char & const_reference
Definition: BCP_vector.hpp:39
iterator begin()
Return an iterator to the beginning of the object.
Definition: BCP_vector.hpp:99
void unchecked_update(const BCP_vec< int > &positions, const BCP_vec< T > &values)
Same as the previous method but without sanity checks.
void reserve(const size_t n)
Reallocate the object to make space for n entries.
void push_back(const_reference x)
Append x to the end of the vector.
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.
void construct(iterator pos)
void erase(iterator pos)
Erase the entry pointed to by pos.
iterator end_of_storage
Iterator pointing to right after the last memory location usable by the vector without reallocation...
Definition: BCP_vector.hpp:71
void pop_back()
Delete the last entry.
iterator start
Iterator pointing to the beginning of the memory array where the vector is stored.
Definition: BCP_vector.hpp:66
void update(const BCP_vec< int > &positions, const BCP_vec< T > &values)
Update those entries listed in positions to the given values.
void destroy_range(iterator first, iterator last)
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...
void BCP_vec_sanity_check(BCP_vec< int >::const_iterator firstpos, BCP_vec< int >::const_iterator lastpos, const int maxsize)
A helper function to test whether a set positions is sane for a vector.
Currently there isn&#39;t any error handling in BCP.
Definition: BCP_error.hpp:20
size_t size() const
Return the current number of entries.
Definition: BCP_vector.hpp:116
iterator end()
Return an iterator to the end of the object.
Definition: BCP_vector.hpp:104
void unchecked_push_back(const_reference x)
Append x to the end of the vector.
void keep(iterator pos)
Keep only the entry pointed to by pos.
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 destroy(iterator pos)