34 return compact_map_[result] == sparse_index ? result : -1;
58 ReverseN(&sparse_size,
sizeof(sparse_size));
60 if (sparse_size > UINT16_MAX)
return false;
76 Init(sparse_size,
false);
77 for (
int i = start; i < end; ++i)
89 for (
int i = 0; i < size; ++i)
96 sparse_map_[sparse_index] = mapped ? 0 : -1;
103 int compact_size = 0;
104 for (
int i = 0; i < sparse_map_.
size(); ++i) {
105 if (sparse_map_[i] >= 0) {
106 sparse_map_[i] = compact_size++;
110 for (
int i = 0; i < sparse_map_.
size(); ++i) {
111 if (sparse_map_[i] >= 0) {
120 sparse_map_ = src.sparse_map_;
130 compact_index1 = MasterCompactIndex(compact_index1);
131 compact_index2 = MasterCompactIndex(compact_index2);
133 if (compact_index1 > compact_index2) {
134 int tmp = compact_index1;
135 compact_index1 = compact_index2;
136 compact_index2 = tmp;
137 }
else if (compact_index1 == compact_index2) {
144 sparse_map_[
compact_map_[compact_index2]] = compact_index1;
145 if (compact_index1 >= 0)
162 int compact_size = 0;
163 for (
int i = 0; i < sparse_map_.
size(); ++i) {
164 int compact_index = MasterCompactIndex(sparse_map_[i]);
165 sparse_map_[i] = compact_index;
166 if (compact_index >= compact_size)
167 compact_size = compact_index + 1;
171 for (
int i = 0; i < sparse_map_.
size(); ++i) {
172 if (sparse_map_[i] >= 0) {
184 tmp_compact_map[i] = compact_size;
190 for (
int i = 0; i < sparse_map_.
size(); ++i) {
191 if (sparse_map_[i] >= 0) {
192 sparse_map_[i] = tmp_compact_map[sparse_map_[i]];
204 for (
int i = 0; i < sparse_map_.
size(); ++i) {
205 if (sparse_map_[i] >= 0 &&
compact_map_[sparse_map_[i]] != i) {
207 remaining_pairs.
push_back(sparse_map_[i]);
210 if (!remaining_pairs.
Serialize(fp))
return false;
219 if (!remaining_pairs.
DeSerialize(swap, fp))
return false;
224 for (
int i = 0; i < remaining_pairs.
size(); ++i) {
225 int sparse_index = remaining_pairs[i++];
226 sparse_map_[sparse_index] = remaining_pairs[i];
239 int num_features = sparse.
size();
240 int missed_features = 0;
241 int prev_good_feature = -1;
242 for (
int f = 0; f < num_features; ++f) {
243 int feature = sparse_map_[sparse[f]];
245 if (feature != prev_good_feature) {
247 prev_good_feature = feature;
253 return missed_features;
void SetMap(int sparse_index, bool mapped)
void CopyFrom(const IndexMapBiDi &src)
GenericVector< int32_t > compact_map_
bool Serialize(FILE *fp) const
bool DeSerialize(bool swap, FILE *fp)
bool Serialize(FILE *fp, const char *data, size_t n)
virtual int SparseSize() const
bool Serialize(FILE *fp) const
void ReverseN(void *ptr, int num_bytes)
int MapFeatures(const GenericVector< int > &sparse, GenericVector< int > *compact) const
bool Merge(int compact_index1, int compact_index2)
void init_to_size(int size, const T &t)
bool DeSerialize(bool swap, FILE *fp)
void InitAndSetupRange(int sparse_size, int start, int end)
bool Serialize(FILE *fp) const
virtual int SparseToCompact(int sparse_index) const
int binary_search(const T &target) const
void CopyFrom(const IndexMap &src)
bool DeSerialize(FILE *fp, char *data, size_t n)
bool DeSerialize(bool swap, FILE *fp)
void Init(int size, bool all_mapped)