Bcp 1.4.4
Loading...
Searching...
No Matches
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
20template <class T> class BCP_vec_change {
21 private:
23 BCP_storage_t _storage;
26 BCP_vec<int> _del_pos;
30 BCP_vec<int> _change_pos;
34 BCP_vec<T> _values;
35
36 public:
43 BCP_vec_change(const BCP_storage_t st) : _storage(st) {}
44
47 BCP_vec_change(const T* first, const T* last) :
48 _storage(BCP_Storage_Explicit) {
49 _values.append(first, last);
50 }
51
60 const BCP_vec_change<T>& new_vec,
61 const BCP_vec<int>& del_pos) :
62 _storage(BCP_Storage_WrtParent), _del_pos(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]) {
75 _change_pos.push_back(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 \
81old_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]) {
84 _change_pos.push_back(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) :
103 _storage(BCP_Storage_WrtParent), _del_pos(del_pos) {
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) {
116 _change_pos.push_back(j);
117 }
118 ++j;
119 }
120 if (old_size - i > new_size - j)
121 throw BCP_fatal_error("BCP_vec_change::BCP_vec_change() : \n \
122old_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) {
125 _change_pos.push_back(j);
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
153 if (_storage != BCP_Storage_Explicit)
154 throw BCP_fatal_error("\
155BCP_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()) {
178 _storage = BCP_Storage_Explicit;
179 _del_pos.clear();
180 _change_pos.clear();
181 _values = change._values;
182 return;
183
185 _storage = BCP_Storage_NoData;
186 _del_pos.clear();
187 _change_pos.clear();
188 _values.clear();
189 return;
190
191 default: // must be BCP_Storage_WrtParent:
192 if (_storage != BCP_Storage_Explicit)
193 throw BCP_fatal_error("\
194trying 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;
210 buf.pack(st).pack(_del_pos).pack(_change_pos).pack(_values);
211 }
213 void unpack(BCP_buffer& buf) {
214 _del_pos.clear();
215 _change_pos.clear();
216 _values.clear();
217 int st;
218 buf.unpack(st).unpack(_del_pos).unpack(_change_pos).unpack(_values);
219 _storage = static_cast<BCP_storage_t>(st);
220 }
222};
223
224#endif
BCP_storage_t
This enumerative constant describes how to store certain data for a search tree node.
Definition BCP_enum.hpp:84
@ BCP_Storage_NoData
No data is stored.
Definition BCP_enum.hpp:86
@ BCP_Storage_Explicit
The data stored is an explicit listing of values.
Definition BCP_enum.hpp:88
@ BCP_Storage_WrtParent
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
T CoinAbs(const T value)
This class describes the message buffer used for all processes of BCP.
BCP_buffer & pack(const T &value)
Pack a single object of type T.
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Currently there isn't any error handling in BCP.
Definition BCP_error.hpp:20
This class stores a vector explicitly or relatively to another vector.
void unpack(BCP_buffer &buf)
Unpack the data from a buffer.
BCP_vec_change(const BCP_storage_t st)
Construct a vector change that's empty and is of the given storage.
void update(const BCP_vec_change< T > &change)
Update the current vector with the argument vector.
BCP_storage_t storage() const
Return the storage type of the vector.
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.
BCP_vec_change(const T *first, const T *last)
Construct an explicit description describing the vector bounded by the two iterators.
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, const double etol)
Construct a relative description.
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_change(BCP_buffer &buf)
Construct the object by unpacking it from a buffer.
int storage_size() const
Return how much memory it'll take to pack this info.
~BCP_vec_change()
The destructor need not do anything.
The class BCP_vec serves the same purpose as the vector class in the standard template library.
void clear()
Delete every entry.
void push_back(const_reference x)
Append x to the end of the vector.
void append(const BCP_vec< T > &x)
Append the entries in x to the end of the vector.
void unchecked_push_back(const_reference x)
Append x to the end of the vector.
void erase_by_index(const BCP_vec< int > &positions)
Erase the entries indexed by indices.
iterator end()
Return an iterator to the end of the object.
size_t size() const
Return the current number of entries.
iterator entry(const int i)
Return an iterator to the i-th entry.
void reserve(const size_t n)
Reallocate the object to make space for n entries.