aboutsummaryrefslogtreecommitdiff
path: root/ccutil/unicity_table.h
blob: f05b6f6abb95ce7b168b36e588e0fc248306e627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
///////////////////////////////////////////////////////////////////////
// File:        UnicityTable.h
// Description: a class to uniquify objects, manipulating them using integers
// ids.
// Author:      Samuel Charron
//
// (C) Copyright 2006, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////

#ifndef TESSERACT_CCUTIL_UNICITY_TABLE_H_
#define TESSERACT_CCUTIL_UNICITY_TABLE_H_

#include "callback.h"
#include "errcode.h"
#include "genericvector.h"

// A class to uniquify objects, manipulating them using integers ids.
// T requirements:
//   operator= to add an element
//   default-constructible: allocating the internal table will call the default
//     constructor.
template <typename T>
class UnicityTable {
 public:
  UnicityTable();
  // Clear the structures and deallocate internal structures.
  ~UnicityTable();

  // Reserve some memory. If there is size or more elements, the table will
  // then allocate size * 2 elements.
  void reserve(int size);

  // Return the size used.
  int size() const;

  // Return the object from an id.
  T get(int id) const;

  // Return the id of the T object.
  // This method NEEDS a compare_callback to be passed to
  // set_compare_callback.
  int get_id(T object) const;

  // Return true if T is in the table
  bool contains(T object) const;

  // Return true if the id is valid
  T contains_id(int id) const;

  // Add an element in the table
  int push_back(T object);

  // Add a callback to be called to delete the elements when the table took
  // their ownership.
  void set_clear_callback(Callback1<T>* cb);

  // Add a callback to be called to compare the elements when needed (contains,
  // get_id, ...)
  void set_compare_callback(ResultCallback2<bool, T const &, T const &>* cb);

  // Clear the table, calling the callback function if any.
  // All the owned Callbacks are also deleted.
  // If you don't want the Callbacks to be deleted, before calling clear, set
  // the callback to NULL.
  void clear();

  // This method clear the current object, then, does a shallow copy of
  // its argument, and finally invalidate its argument.
  void move(UnicityTable<T>* from);

  // Read/Write the table to a file. This does _NOT_ read/write the callbacks.
  // The Callback given must be permanent since they will be called more than
  // once. The given callback will be deleted at the end.
  void write(FILE* f, Callback2<FILE*, T const &>* cb);
  // swap is used to switch the endianness.
  void read(FILE* f, Callback3<FILE*, T*, bool>* cb, bool swap);

 private:
  GenericVector<T> table_;
  // Mutable because Run method is not const
  mutable ResultCallback2<bool, T const &, T const &>* compare_cb_;
};

template <typename T>
class UnicityTableEqEq : public UnicityTable<T> {
 public:
  UnicityTableEqEq() {
    UnicityTable<T>::set_compare_callback(
        NewPermanentCallback(tesseract::cmp_eq<T>));
  }
};

template <typename T>
UnicityTable<T>::UnicityTable() :
  compare_cb_(0) {
}


template <typename T>
UnicityTable<T>::~UnicityTable() {
  clear();
}

template <typename T>
int UnicityTable<T>::size() const{
  return table_.size();
}

// Reserve some memory. If there is size or more elements, the table will
// then allocate size * 2 elements.
template <typename T>
void UnicityTable<T>::reserve(int size) {
  table_.reserve(size);
}

// Return the object from an id.
template <typename T>
T UnicityTable<T>::get(int id) const {
  return table_.get(id);
}

// Return true if the id is valid
template <typename T>
T UnicityTable<T>::contains_id(int id) const {
  return table_.contains_index(id);
}

// Return the id of the T object.
template <typename T>
int UnicityTable<T>::get_id(T object) const {
  return table_.get_index(object);
}

// Return true if T is in the table
template <typename T>
bool UnicityTable<T>::contains(T object) const {
  return get_id(object) != -1;
}

// Add an element in the table
template <typename T>
int UnicityTable<T>::push_back(T object) {
  int idx = get_id(object);
  if (idx == -1) {
    idx = table_.push_back(object);
  }
  return idx;
}

// Add a callback to be called to delete the elements when the table took
// their ownership.
template <typename T>
void UnicityTable<T>::set_clear_callback(Callback1<T>* cb) {
  table_.set_clear_callback(cb);
}

// Add a callback to be called to delete the elements when the table took
// their ownership.
template <typename T>
void UnicityTable<T>::set_compare_callback(ResultCallback2<bool, T const &, T const &>* cb) {
  table_.set_compare_callback(cb);
  compare_cb_ = cb;
}

// Clear the table, calling the callback function if any.
template <typename T>
void UnicityTable<T>::clear() {
  table_.clear();
}

template <typename T>
void UnicityTable<T>::write(FILE* f, Callback2<FILE*, T const &>* cb) {
  table_.write(f, cb);
}

template <typename T>
void UnicityTable<T>::read(FILE* f, Callback3<FILE*, T*, bool>* cb, bool swap) {
  table_.read(f, cb, swap);
}

// This method clear the current object, then, does a shallow copy of
// its argument, and finally invalidate its argument.
template <typename T>
void UnicityTable<T>::move(UnicityTable<T>* from) {
  table_.move(&from->table_);
}

#endif  // TESSERACT_CCUTIL_UNICITY_TABLE_H_