29 return compact_map_[result] == sparse_index ? result : -1;
45 if (fwrite(&sparse_size,
sizeof(sparse_size), 1, fp) != 1)
return false;
54 if (fread(&sparse_size,
sizeof(sparse_size), 1, fp) != 1)
return false;
56 ReverseN(&sparse_size,
sizeof(sparse_size));
69 Init(sparse_size,
false);
70 for (
int i = start; i < end; ++i)
82 for (
int i = 0; i < size; ++i)
89 sparse_map_[sparse_index] = mapped ? 0 : -1;
97 for (
int i = 0; i < sparse_map_.
size(); ++i) {
98 if (sparse_map_[i] >= 0) {
99 sparse_map_[i] = compact_size++;
103 for (
int i = 0; i < sparse_map_.
size(); ++i) {
104 if (sparse_map_[i] >= 0) {
113 sparse_map_ = src.sparse_map_;
123 compact_index1 = MasterCompactIndex(compact_index1);
124 compact_index2 = MasterCompactIndex(compact_index2);
126 if (compact_index1 > compact_index2) {
127 int tmp = compact_index1;
128 compact_index1 = compact_index2;
129 compact_index2 = tmp;
130 }
else if (compact_index1 == compact_index2) {
137 sparse_map_[
compact_map_[compact_index2]] = compact_index1;
138 if (compact_index1 >= 0)
155 int compact_size = 0;
156 for (
int i = 0; i < sparse_map_.
size(); ++i) {
157 int compact_index = MasterCompactIndex(sparse_map_[i]);
158 sparse_map_[i] = compact_index;
159 if (compact_index >= compact_size)
160 compact_size = compact_index + 1;
164 for (
int i = 0; i < sparse_map_.
size(); ++i) {
165 if (sparse_map_[i] >= 0) {
177 tmp_compact_map[i] = compact_size;
183 for (
int i = 0; i < sparse_map_.
size(); ++i) {
184 if (sparse_map_[i] >= 0) {
185 sparse_map_[i] = tmp_compact_map[sparse_map_[i]];
197 for (
int i = 0; i < sparse_map_.
size(); ++i) {
198 if (sparse_map_[i] >= 0 &&
compact_map_[sparse_map_[i]] != i) {
200 remaining_pairs.
push_back(sparse_map_[i]);
203 if (!remaining_pairs.
Serialize(fp))
return false;
212 if (!remaining_pairs.
DeSerialize(swap, fp))
return false;
217 for (
int i = 0; i < remaining_pairs.
size(); ++i) {
218 int sparse_index = remaining_pairs[i++];
219 sparse_map_[sparse_index] = remaining_pairs[i];
232 int num_features = sparse.
size();
233 int missed_features = 0;
234 int prev_good_feature = -1;
235 for (
int f = 0; f < num_features; ++f) {
236 int feature = sparse_map_[sparse[f]];
238 if (feature != prev_good_feature) {
240 prev_good_feature = feature;
246 return missed_features;
bool Serialize(FILE *fp) const
void CopyFrom(const IndexMap &src)
void InitAndSetupRange(int sparse_size, int start, int end)
bool Serialize(FILE *fp) const
virtual int SparseSize() const
bool Merge(int compact_index1, int compact_index2)
bool Serialize(FILE *fp) const
void CopyFrom(const IndexMapBiDi &src)
virtual int SparseToCompact(int sparse_index) const
bool DeSerialize(bool swap, FILE *fp)
void Init(int size, bool all_mapped)
GenericVector< inT32 > compact_map_
bool DeSerialize(bool swap, FILE *fp)
int binary_search(const T &target) const
int MapFeatures(const GenericVector< int > &sparse, GenericVector< int > *compact) const
void ReverseN(void *ptr, int num_bytes)
void init_to_size(int size, T t)
void SetMap(int sparse_index, bool mapped)
bool DeSerialize(bool swap, FILE *fp)