coin-Bcp
BCP_vector_change.hpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #ifndef _BCP_VECTOR_CHANGE_H
4 #define _BCP_VECTOR_CHANGE_H
5 
6 // This file is fully docified.
7 
9 #include "BCP_enum.hpp"
10 #include "BCP_vector.hpp"
11 #include "BCP_buffer.hpp"
12 
20 template <class T> class BCP_vec_change {
21  private:
35 
36  public:
44 
47  BCP_vec_change(const T* first, const T* last) :
49  _values.append(first, last);
50  }
51 
60  const BCP_vec_change<T>& new_vec,
61  const BCP_vec<int>& del_pos) :
63  const BCP_vec<T>& ov = old_vec.explicit_vector();
64  const BCP_vec<T>& nv = new_vec.explicit_vector();
65  const int new_size = nv.size();
66  const int old_size = ov.size();
67  const int del_size = del_pos.size();
68  int i, j, k; // runs on old_vec/new_vec/del_pos
69  for (i = 0, j = 0, k = 0; i < old_size && k < del_size; ++i) {
70  if (del_pos[k] == i) {
71  ++k;
72  continue;
73  }
74  if (ov[i] != nv[j]) {
76  }
77  ++j;
78  }
79  if (old_size - i > new_size - j)
80  throw BCP_fatal_error("BCP_vec_change::BCP_vec_change() : \n \
81 old_vec has entries not in new_vec but not listed in del_pos.\n");
82  for ( ; i < old_size; ++i, ++j) {
83  if (ov[i] != nv[j]) {
85  }
86  }
87  const int change_size = _change_pos.size();
88  _values.reserve(change_size + new_size - j);
89  for (i = 0; i < change_size; ++i) {
90  _values.unchecked_push_back(nv[_change_pos[i]]);
91  }
92  _values.append(nv.entry(j), nv.end());
93  }
94 
101  const BCP_vec_change<T>& new_vec,
102  const BCP_vec<int>& del_pos, const double etol) :
104  const BCP_vec<T>& ov = old_vec.explicit_vector();
105  const BCP_vec<T>& nv = new_vec.explicit_vector();
106  const int new_size = nv.size();
107  const int old_size = ov.size();
108  const int del_size = del_pos.size();
109  int i, j, k; // runs on old_vec/new_vec/del_pos
110  for (i = 0, j = 0, k = 0; i < old_size && k < del_size; ++i) {
111  if (del_pos[k] == i) {
112  ++k;
113  continue;
114  }
115  if (CoinAbs(ov[i] - nv[j]) > etol) {
117  }
118  ++j;
119  }
120  if (old_size - i > new_size - j)
121  throw BCP_fatal_error("BCP_vec_change::BCP_vec_change() : \n \
122 old_vec has entries not in new_vec but not listed in del_pos.\n");
123  for ( ; i < old_size; ++i, ++j) {
124  if (CoinAbs(ov[i] - nv[j]) > etol) {
126  }
127  }
128  const int change_size = _change_pos.size();
129  _values.reserve(change_size + new_size - j);
130  for (i = 0; i < change_size; ++i) {
131  _values.unchecked_push_back(nv[_change_pos[i]]);
132  }
133  _values.append(nv.entry(j), nv.end());
134  }
135 
138  unpack(buf);
139  }
140 
148  BCP_storage_t storage() const { return _storage; }
149 
152  const BCP_vec<T>& explicit_vector() const {
154  throw BCP_fatal_error("\
155 BCP_vec_change::explicit_vector() : non-explicit storage!\n");
156  return _values;
157  }
158 
161  int storage_size() const {
162  return (_del_pos.size() + _change_pos.size()) * sizeof(int) +
163  _values.size() * sizeof(T);
164  }
165 
175  void update(const BCP_vec_change<T>& change) {
176  switch (change.storage()) {
179  _del_pos.clear();
180  _change_pos.clear();
181  _values = change._values;
182  return;
183 
184  case BCP_Storage_NoData:
186  _del_pos.clear();
187  _change_pos.clear();
188  _values.clear();
189  return;
190 
191  default: // must be BCP_Storage_WrtParent:
193  throw BCP_fatal_error("\
194 trying to update a non-explicit storage with another non-explicit!\n");
195  _values.erase_by_index(change._del_pos);
196  const int ch_size = change._change_pos.size();
197  for (int i = 0; i < ch_size; ++i) {
198  _values[change._change_pos[i]] = change._values[i];
199  }
200  _values.append(change._values.entry(ch_size), change._values.end());
201  }
202  }
208  void pack(BCP_buffer& buf) const {
209  const int st = _storage;
211  }
213  void unpack(BCP_buffer& buf) {
214  _del_pos.clear();
215  _change_pos.clear();
216  _values.clear();
217  int st;
219  _storage = static_cast<BCP_storage_t>(st);
220  }
222 };
223 
224 #endif
BCP_buffer & pack(const T &value)
Pack a single object of type T.
Definition: BCP_buffer.hpp:177
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Definition: BCP_buffer.hpp:186
void clear()
Delete every entry.
BCP_vec_change(const T *first, const T *last)
Construct an explicit description describing the vector bounded by the two iterators.
int storage_size() const
Return how much memory it&#39;ll take to pack this info.
const BCP_vec< T > & explicit_vector() const
Return a const reference to the vector if it is explicitly stored, otherwise throw an exception...
BCP_vec< int > _del_pos
Empty in case of explicit storage.
No data is stored.
Definition: BCP_enum.hpp:86
BCP_storage_t _storage
The storage type of the the vector.
~BCP_vec_change()
The destructor need not do anything.
void unpack(BCP_buffer &buf)
Unpack the data from a buffer.
void push_back(const_reference x)
Append x to the end of the vector.
T CoinAbs(const T value)
Return the absolute value of the argument.
The data stored is an explicit listing of values.
Definition: BCP_enum.hpp:88
This class stores a vector explicitly or relatively to another vector.
BCP_vec_change(BCP_buffer &buf)
Construct the object by unpacking it from a buffer.
BCP_vec_change(const BCP_storage_t st)
Construct a vector change that&#39;s empty and is of the given storage.
The data stored is with respect to the same kind of data in the parent of the search tree node...
Definition: BCP_enum.hpp:91
BCP_storage_t storage() const
Return the storage type of the vector.
void update(const BCP_vec_change< T > &change)
Update the current vector with the argument vector.
BCP_vec< T > _values
In case of explicit storage these are the values in the vector.
Currently there isn&#39;t any error handling in BCP.
Definition: BCP_error.hpp:20
BCP_vec< int > _change_pos
Empty in case of explicit storage.
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
BCP_vec_change(const BCP_vec_change< T > &old_vec, const BCP_vec_change< T > &new_vec, const BCP_vec< int > &del_pos, const double etol)
Construct a relative description.
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
BCP_storage_t
This enumerative constant describes how to store certain data for a search tree node.
Definition: BCP_enum.hpp:84
iterator entry(const int i)
Return an iterator to the i-th entry.
Definition: BCP_vector.hpp:109
void pack(BCP_buffer &buf) const
Pack the data into a buffer.
BCP_vec_change(const BCP_vec_change< T > &old_vec, const BCP_vec_change< T > &new_vec, const BCP_vec< int > &del_pos)
Construct a relative description.