Update Tesseract to version 4.
This commit is contained in:
56 files changed
+10765
-3462
No files matched your search
@@ -20,13 +20,15 @@
|
||||
#ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_
|
||||
#define TESSERACT_CCUTIL_GENERICVECTOR_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "tesscallback.h"
|
||||
#include "errcode.h"
|
||||
#include "helpers.h"
|
||||
#include "ndminx.h"
|
||||
#include "serialis.h"
|
||||
#include "strngs.h"
|
||||
|
||||
// Use PointerVector<T> below in preference to GenericVector<T*>, as that
|
||||
// provides automatic deletion of pointers, [De]Serialize that works, and
|
||||
@@ -34,8 +36,13 @@
|
||||
template <typename T>
|
||||
class GenericVector {
|
||||
public:
|
||||
GenericVector() { this->init(kDefaultVectorSize); }
|
||||
explicit GenericVector(int size) { this->init(size); }
|
||||
GenericVector() {
|
||||
init(kDefaultVectorSize);
|
||||
}
|
||||
GenericVector(int size, const T& init_val) {
|
||||
init(size);
|
||||
init_to_size(size, init_val);
|
||||
}
|
||||
|
||||
// Copy
|
||||
GenericVector(const GenericVector& other) {
|
||||
@@ -45,7 +52,7 @@ class GenericVector {
|
||||
GenericVector<T> &operator+=(const GenericVector& other);
|
||||
GenericVector<T> &operator=(const GenericVector& other);
|
||||
|
||||
virtual ~GenericVector();
|
||||
~GenericVector();
|
||||
|
||||
// Reserve some memory.
|
||||
void reserve(int size);
|
||||
@@ -53,12 +60,27 @@ class GenericVector {
|
||||
void double_the_size();
|
||||
|
||||
// Resizes to size and sets all values to t.
|
||||
void init_to_size(int size, T t);
|
||||
void init_to_size(int size, const T& t);
|
||||
// Resizes to size without any initialization.
|
||||
void resize_no_init(int size) {
|
||||
reserve(size);
|
||||
size_used_ = size;
|
||||
}
|
||||
|
||||
// Return the size used.
|
||||
int size() const {
|
||||
return size_used_;
|
||||
}
|
||||
// Workaround to avoid g++ -Wsign-compare warnings.
|
||||
size_t unsigned_size() const {
|
||||
static_assert(sizeof(size_used_) <= sizeof(size_t),
|
||||
"Wow! sizeof(size_t) < sizeof(int32_t)!!");
|
||||
assert(0 <= size_used_);
|
||||
return static_cast<size_t>(size_used_);
|
||||
}
|
||||
int size_reserved() const {
|
||||
return size_reserved_;
|
||||
}
|
||||
|
||||
int length() const {
|
||||
return size_used_;
|
||||
@@ -73,43 +95,45 @@ class GenericVector {
|
||||
T &get(int index) const;
|
||||
T &back() const;
|
||||
T &operator[](int index) const;
|
||||
// Returns the last object and removes it.
|
||||
T pop_back();
|
||||
|
||||
// Return the index of the T object.
|
||||
// This method NEEDS a compare_callback to be passed to
|
||||
// set_compare_callback.
|
||||
int get_index(T object) const;
|
||||
int get_index(const T& object) const;
|
||||
|
||||
// Return true if T is in the array
|
||||
bool contains(T object) const;
|
||||
bool contains(const T& object) const;
|
||||
|
||||
// Return true if the index is valid
|
||||
T contains_index(int index) const;
|
||||
|
||||
// Push an element in the end of the array
|
||||
int push_back(T object);
|
||||
void operator+=(T t);
|
||||
void operator+=(const T& t);
|
||||
|
||||
// Push an element in the end of the array if the same
|
||||
// element is not already contained in the array.
|
||||
int push_back_new(T object);
|
||||
int push_back_new(const T& object);
|
||||
|
||||
// Push an element in the front of the array
|
||||
// Note: This function is O(n)
|
||||
int push_front(T object);
|
||||
int push_front(const T& object);
|
||||
|
||||
// Set the value at the given index
|
||||
void set(T t, int index);
|
||||
void set(const T& t, int index);
|
||||
|
||||
// Insert t at the given index, push other elements to the right.
|
||||
void insert(T t, int index);
|
||||
void insert(const T& t, int index);
|
||||
|
||||
// Removes an element at the given index and
|
||||
// shifts the remaining elements to the left.
|
||||
virtual void remove(int index);
|
||||
void remove(int index);
|
||||
|
||||
// Truncates the array to the given size by removing the end.
|
||||
// If the current size is less, the array is not expanded.
|
||||
virtual void truncate(int size) {
|
||||
void truncate(int size) {
|
||||
if (size < size_used_)
|
||||
size_used_ = size;
|
||||
}
|
||||
@@ -125,8 +149,8 @@ class GenericVector {
|
||||
// Clear the array, calling the clear 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.
|
||||
virtual void clear();
|
||||
// the callback to nullptr.
|
||||
void clear();
|
||||
|
||||
// Delete objects pointed to by data_[i]
|
||||
void delete_data_pointers();
|
||||
@@ -139,30 +163,41 @@ class GenericVector {
|
||||
// Read/Write the array 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.
|
||||
// If the callbacks are NULL, then the data is simply read/written using
|
||||
// If the callbacks are nullptr, then the data is simply read/written using
|
||||
// fread (and swapping)/fwrite.
|
||||
// Returns false on error or if the callback returns false.
|
||||
// DEPRECATED. Use [De]Serialize[Classes] instead.
|
||||
bool write(FILE* f, TessResultCallback2<bool, FILE*, T const &>* cb) const;
|
||||
bool read(FILE* f, TessResultCallback3<bool, FILE*, T*, bool>* cb, bool swap);
|
||||
bool read(tesseract::TFile* f,
|
||||
TessResultCallback2<bool, tesseract::TFile*, T*>* cb);
|
||||
// Writes a vector of simple types to the given file. Assumes that bitwise
|
||||
// read/write of T will work. Returns false in case of error.
|
||||
virtual bool Serialize(FILE* fp) const;
|
||||
// TODO(rays) Change all callers to use TFile and remove deprecated methods.
|
||||
bool Serialize(FILE* fp) const;
|
||||
bool Serialize(tesseract::TFile* fp) const;
|
||||
// Reads a vector of simple types from the given file. Assumes that bitwise
|
||||
// read/write will work with ReverseN according to sizeof(T).
|
||||
// Returns false in case of error.
|
||||
// If swap is true, assumes a big/little-endian swap is needed.
|
||||
virtual bool DeSerialize(bool swap, FILE* fp);
|
||||
// TFile is assumed to know about swapping.
|
||||
bool DeSerialize(bool swap, FILE* fp);
|
||||
bool DeSerialize(tesseract::TFile* fp);
|
||||
// Skips the deserialization of the vector.
|
||||
static bool SkipDeSerialize(tesseract::TFile* fp);
|
||||
// Writes a vector of classes to the given file. Assumes the existence of
|
||||
// bool T::Serialize(FILE* fp) const that returns false in case of error.
|
||||
// Returns false in case of error.
|
||||
bool SerializeClasses(FILE* fp) const;
|
||||
bool SerializeClasses(tesseract::TFile* fp) const;
|
||||
// Reads a vector of classes from the given file. Assumes the existence of
|
||||
// bool T::Deserialize(bool swap, FILE* fp) that returns false in case of
|
||||
// error. Also needs T::T() and T::T(constT&), as init_to_size is used in
|
||||
// this function. Returns false in case of error.
|
||||
// If swap is true, assumes a big/little-endian swap is needed.
|
||||
bool DeSerializeClasses(bool swap, FILE* fp);
|
||||
bool DeSerializeClasses(tesseract::TFile* fp);
|
||||
// Calls SkipDeSerialize on the elements of the vector.
|
||||
static bool SkipDeSerializeClasses(tesseract::TFile* fp);
|
||||
|
||||
// Allocates a new array of double the current_size, copies over the
|
||||
// information from data to the new location, deletes data and returns
|
||||
@@ -176,6 +211,12 @@ class GenericVector {
|
||||
return data_new;
|
||||
}
|
||||
|
||||
// Reverses the elements of the vector.
|
||||
void reverse() {
|
||||
for (int i = 0; i < size_used_ / 2; ++i)
|
||||
Swap(&data_[i], &data_[size_used_ - 1 - i]);
|
||||
}
|
||||
|
||||
// Sorts the members of this vector using the less than comparator (cmp_lt),
|
||||
// which compares the values. Useful for GenericVectors to primitive types.
|
||||
// Will not work so great for pointers (unless you just want to sort some
|
||||
@@ -209,14 +250,13 @@ class GenericVector {
|
||||
int binary_search(const T& target) const {
|
||||
int bottom = 0;
|
||||
int top = size_used_;
|
||||
do {
|
||||
while (top - bottom > 1) {
|
||||
int middle = (bottom + top) / 2;
|
||||
if (data_[middle] > target)
|
||||
top = middle;
|
||||
else
|
||||
bottom = middle;
|
||||
}
|
||||
while (top - bottom > 1);
|
||||
return bottom;
|
||||
}
|
||||
|
||||
@@ -257,22 +297,56 @@ class GenericVector {
|
||||
|
||||
T dot_product(const GenericVector<T>& other) const {
|
||||
T result = static_cast<T>(0);
|
||||
for (int i = MIN(size_used_, other.size_used_) - 1; i >= 0; --i)
|
||||
for (int i = std::min(size_used_, other.size_used_) - 1; i >= 0; --i)
|
||||
result += data_[i] * other.data_[i];
|
||||
return result;
|
||||
}
|
||||
|
||||
// Returns the index of what would be the target_index_th item in the array
|
||||
// if the members were sorted, without actually sorting. Members are
|
||||
// shuffled around, but it takes O(n) time.
|
||||
// NOTE: uses operator< and operator== on the members.
|
||||
int choose_nth_item(int target_index) {
|
||||
// Make sure target_index is legal.
|
||||
if (target_index < 0)
|
||||
target_index = 0; // ensure legal
|
||||
else if (target_index >= size_used_)
|
||||
target_index = size_used_ - 1;
|
||||
unsigned int seed = 1;
|
||||
return choose_nth_item(target_index, 0, size_used_, &seed);
|
||||
}
|
||||
|
||||
// Swaps the elements with the given indices.
|
||||
void swap(int index1, int index2) {
|
||||
if (index1 != index2) {
|
||||
T tmp = data_[index1];
|
||||
data_[index1] = data_[index2];
|
||||
data_[index2] = tmp;
|
||||
}
|
||||
}
|
||||
// Returns true if all elements of *this are within the given range.
|
||||
// Only uses operator<
|
||||
bool WithinBounds(const T& rangemin, const T& rangemax) const {
|
||||
for (int i = 0; i < size_used_; ++i) {
|
||||
if (data_[i] < rangemin || rangemax < data_[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
// Internal recursive version of choose_nth_item.
|
||||
int choose_nth_item(int target_index, int start, int end, unsigned int* seed);
|
||||
|
||||
// Init the object, allocating size memory.
|
||||
void init(int size);
|
||||
|
||||
// We are assuming that the object generally placed in thie
|
||||
// vector are small enough that for efficiency it makes sence
|
||||
// We are assuming that the object generally placed in the
|
||||
// vector are small enough that for efficiency it makes sense
|
||||
// to start with a larger initial size.
|
||||
static const int kDefaultVectorSize = 4;
|
||||
inT32 size_used_;
|
||||
inT32 size_reserved_;
|
||||
int32_t size_used_;
|
||||
int32_t size_reserved_;
|
||||
T* data_;
|
||||
TessCallback1<T>* clear_cb_;
|
||||
// Mutable because Run method is not const
|
||||
@@ -281,6 +355,62 @@ class GenericVector {
|
||||
|
||||
namespace tesseract {
|
||||
|
||||
// Function to read a GenericVector<char> from a whole file.
|
||||
// Returns false on failure.
|
||||
typedef bool (*FileReader)(const STRING& filename, GenericVector<char>* data);
|
||||
// Function to write a GenericVector<char> to a whole file.
|
||||
// Returns false on failure.
|
||||
typedef bool (*FileWriter)(const GenericVector<char>& data,
|
||||
const STRING& filename);
|
||||
// The default FileReader loads the whole file into the vector of char,
|
||||
// returning false on error.
|
||||
inline bool LoadDataFromFile(const char* filename, GenericVector<char>* data) {
|
||||
bool result = false;
|
||||
FILE* fp = fopen(filename, "rb");
|
||||
if (fp != nullptr) {
|
||||
fseek(fp, 0, SEEK_END);
|
||||
long size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
// Trying to open a directory on Linux sets size to LONG_MAX. Catch it here.
|
||||
if (size > 0 && size < LONG_MAX) {
|
||||
// reserve an extra byte in case caller wants to append a '\0' character
|
||||
data->reserve(size + 1);
|
||||
data->resize_no_init(size);
|
||||
result = static_cast<long>(fread(&(*data)[0], 1, size, fp)) == size;
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool LoadDataFromFile(const STRING& filename,
|
||||
GenericVector<char>* data) {
|
||||
return LoadDataFromFile(filename.string(), data);
|
||||
}
|
||||
|
||||
// The default FileWriter writes the vector of char to the filename file,
|
||||
// returning false on error.
|
||||
inline bool SaveDataToFile(const GenericVector<char>& data,
|
||||
const STRING& filename) {
|
||||
FILE* fp = fopen(filename.string(), "wb");
|
||||
if (fp == nullptr) return false;
|
||||
bool result =
|
||||
static_cast<int>(fwrite(&data[0], 1, data.size(), fp)) == data.size();
|
||||
fclose(fp);
|
||||
return result;
|
||||
}
|
||||
// Reads a file as a vector of STRING.
|
||||
inline bool LoadFileLinesToStrings(const STRING& filename,
|
||||
GenericVector<STRING>* lines) {
|
||||
GenericVector<char> data;
|
||||
if (!LoadDataFromFile(filename.string(), &data)) {
|
||||
return false;
|
||||
}
|
||||
STRING lines_str(&data[0], data.size());
|
||||
lines_str.split('\n', lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool cmp_eq(T const & t1, T const & t2) {
|
||||
return t1 == t2;
|
||||
@@ -309,8 +439,8 @@ int sort_cmp(const void* t1, const void* t2) {
|
||||
// return > 0 if t1 > t2
|
||||
template <typename T>
|
||||
int sort_ptr_cmp(const void* t1, const void* t2) {
|
||||
const T* a = *reinterpret_cast<T * const *>(t1);
|
||||
const T* b = *reinterpret_cast<T * const *>(t2);
|
||||
const T* a = *static_cast<T* const*>(t1);
|
||||
const T* b = *static_cast<T* const*>(t2);
|
||||
if (*a < *b) {
|
||||
return -1;
|
||||
} else if (*b < *a) {
|
||||
@@ -328,14 +458,14 @@ class PointerVector : public GenericVector<T*> {
|
||||
public:
|
||||
PointerVector() : GenericVector<T*>() { }
|
||||
explicit PointerVector(int size) : GenericVector<T*>(size) { }
|
||||
virtual ~PointerVector() {
|
||||
~PointerVector() {
|
||||
// Clear must be called here, even though it is called again by the base,
|
||||
// as the base will call the wrong clear.
|
||||
clear();
|
||||
}
|
||||
// Copy must be deep, as the pointers will be automatically deleted on
|
||||
// destruction.
|
||||
PointerVector(const PointerVector& other) {
|
||||
PointerVector(const PointerVector& other) : GenericVector<T*>(other) {
|
||||
this->init(other.size());
|
||||
this->operator+=(other);
|
||||
}
|
||||
@@ -348,21 +478,23 @@ class PointerVector : public GenericVector<T*> {
|
||||
}
|
||||
|
||||
PointerVector<T>& operator=(const PointerVector& other) {
|
||||
this->truncate(0);
|
||||
this->operator+=(other);
|
||||
if (&other != this) {
|
||||
this->truncate(0);
|
||||
this->operator+=(other);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Removes an element at the given index and
|
||||
// shifts the remaining elements to the left.
|
||||
virtual void remove(int index) {
|
||||
void remove(int index) {
|
||||
delete GenericVector<T*>::data_[index];
|
||||
GenericVector<T*>::remove(index);
|
||||
}
|
||||
|
||||
// Truncates the array to the given size by removing the end.
|
||||
// If the current size is less, the array is not expanded.
|
||||
virtual void truncate(int size) {
|
||||
void truncate(int size) {
|
||||
for (int i = size; i < GenericVector<T*>::size_used_; ++i)
|
||||
delete GenericVector<T*>::data_[i];
|
||||
GenericVector<T*>::truncate(size);
|
||||
@@ -393,52 +525,121 @@ class PointerVector : public GenericVector<T*> {
|
||||
// Clear the array, calling the clear 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.
|
||||
virtual void clear() {
|
||||
// the callback to nullptr.
|
||||
void clear() {
|
||||
GenericVector<T*>::delete_data_pointers();
|
||||
GenericVector<T*>::clear();
|
||||
}
|
||||
|
||||
// Writes a vector of simple types to the given file. Assumes that bitwise
|
||||
// read/write of T will work. Returns false in case of error.
|
||||
virtual bool Serialize(FILE* fp) const {
|
||||
inT32 used = GenericVector<T*>::size_used_;
|
||||
// Writes a vector of (pointers to) classes to the given file. Assumes the
|
||||
// existence of bool T::Serialize(FILE*) const that returns false in case of
|
||||
// error. There is no Serialize for simple types, as you would have a
|
||||
// normal GenericVector of those.
|
||||
// Returns false in case of error.
|
||||
bool Serialize(FILE* fp) const {
|
||||
int32_t used = GenericVector<T*>::size_used_;
|
||||
if (fwrite(&used, sizeof(used), 1, fp) != 1) return false;
|
||||
for (int i = 0; i < used; ++i) {
|
||||
inT8 non_null = GenericVector<T*>::data_[i] != NULL;
|
||||
int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
|
||||
if (fwrite(&non_null, sizeof(non_null), 1, fp) != 1) return false;
|
||||
if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Reads a vector of simple types from the given file. Assumes that bitwise
|
||||
// read/write will work with ReverseN according to sizeof(T).
|
||||
bool Serialize(TFile* fp) const {
|
||||
int32_t used = GenericVector<T*>::size_used_;
|
||||
if (fp->FWrite(&used, sizeof(used), 1) != 1) return false;
|
||||
for (int i = 0; i < used; ++i) {
|
||||
int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
|
||||
if (fp->FWrite(&non_null, sizeof(non_null), 1) != 1) return false;
|
||||
if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Reads a vector of (pointers to) classes to the given file. Assumes the
|
||||
// existence of bool T::DeSerialize(bool, Tfile*) const that returns false in
|
||||
// case of error. There is no Serialize for simple types, as you would have a
|
||||
// normal GenericVector of those.
|
||||
// If swap is true, assumes a big/little-endian swap is needed.
|
||||
// Also needs T::T(), as new T is used in this function.
|
||||
// Returns false in case of error.
|
||||
// If swap is true, assumes a big/little-endian swap is needed.
|
||||
virtual bool DeSerialize(bool swap, FILE* fp) {
|
||||
inT32 reserved;
|
||||
bool DeSerialize(bool swap, FILE* fp) {
|
||||
uint32_t reserved;
|
||||
if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
|
||||
if (swap) Reverse32(&reserved);
|
||||
// Arbitrarily limit the number of elements to protect against bad data.
|
||||
assert(reserved <= UINT16_MAX);
|
||||
if (reserved > UINT16_MAX) {
|
||||
return false;
|
||||
}
|
||||
GenericVector<T*>::reserve(reserved);
|
||||
for (int i = 0; i < reserved; ++i) {
|
||||
inT8 non_null;
|
||||
truncate(0);
|
||||
for (uint32_t i = 0; i < reserved; ++i) {
|
||||
int8_t non_null;
|
||||
if (fread(&non_null, sizeof(non_null), 1, fp) != 1) return false;
|
||||
T* item = NULL;
|
||||
T* item = nullptr;
|
||||
if (non_null) {
|
||||
item = new T;
|
||||
if (!item->DeSerialize(swap, fp)) return false;
|
||||
if (!item->DeSerialize(swap, fp)) {
|
||||
delete item;
|
||||
return false;
|
||||
}
|
||||
this->push_back(item);
|
||||
} else {
|
||||
// Null elements should keep their place in the vector.
|
||||
this->push_back(nullptr);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool DeSerialize(TFile* fp) {
|
||||
int32_t reserved;
|
||||
if (!DeSerializeSize(fp, &reserved)) return false;
|
||||
GenericVector<T*>::reserve(reserved);
|
||||
truncate(0);
|
||||
for (int i = 0; i < reserved; ++i) {
|
||||
if (!DeSerializeElement(fp)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Enables deserialization of a selection of elements. Note that in order to
|
||||
// retain the integrity of the stream, the caller must call some combination
|
||||
// of DeSerializeElement and DeSerializeSkip of the exact number returned in
|
||||
// *size, assuming a true return.
|
||||
static bool DeSerializeSize(TFile* fp, int32_t* size) {
|
||||
return fp->FReadEndian(size, sizeof(*size), 1) == 1;
|
||||
}
|
||||
// Reads and appends to the vector the next element of the serialization.
|
||||
bool DeSerializeElement(TFile* fp) {
|
||||
int8_t non_null;
|
||||
if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) return false;
|
||||
T* item = nullptr;
|
||||
if (non_null) {
|
||||
item = new T;
|
||||
if (!item->DeSerialize(fp)) {
|
||||
delete item;
|
||||
return false;
|
||||
}
|
||||
this->push_back(item);
|
||||
} else {
|
||||
// Null elements should keep their place in the vector.
|
||||
this->push_back(nullptr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Skips the next element of the serialization.
|
||||
static bool DeSerializeSkip(TFile* fp) {
|
||||
int8_t non_null;
|
||||
if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) return false;
|
||||
if (non_null) {
|
||||
if (!T::SkipDeSerialize(fp)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Sorts the items pointed to by the members of this vector using
|
||||
// t::operator<().
|
||||
void sort() {
|
||||
sort(&sort_ptr_cmp<T>);
|
||||
}
|
||||
void sort() { this->GenericVector<T*>::sort(&sort_ptr_cmp<T>); }
|
||||
};
|
||||
|
||||
} // namespace tesseract
|
||||
@@ -460,11 +661,16 @@ class GenericVectorEqEq : public GenericVector<T> {
|
||||
template <typename T>
|
||||
void GenericVector<T>::init(int size) {
|
||||
size_used_ = 0;
|
||||
size_reserved_ = 0;
|
||||
data_ = 0;
|
||||
clear_cb_ = 0;
|
||||
compare_cb_ = 0;
|
||||
reserve(size);
|
||||
if (size <= 0) {
|
||||
data_ = nullptr;
|
||||
size_reserved_ = 0;
|
||||
} else {
|
||||
if (size < kDefaultVectorSize) size = kDefaultVectorSize;
|
||||
data_ = new T[size];
|
||||
size_reserved_ = size;
|
||||
}
|
||||
clear_cb_ = nullptr;
|
||||
compare_cb_ = nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -478,10 +684,11 @@ template <typename T>
|
||||
void GenericVector<T>::reserve(int size) {
|
||||
if (size_reserved_ >= size || size <= 0)
|
||||
return;
|
||||
if (size < kDefaultVectorSize) size = kDefaultVectorSize;
|
||||
T* new_array = new T[size];
|
||||
for (int i = 0; i < size_used_; ++i)
|
||||
new_array[i] = data_[i];
|
||||
if (data_ != NULL) delete[] data_;
|
||||
delete[] data_;
|
||||
data_ = new_array;
|
||||
size_reserved_ = size;
|
||||
}
|
||||
@@ -498,7 +705,7 @@ void GenericVector<T>::double_the_size() {
|
||||
|
||||
// Resizes to size and sets all values to t.
|
||||
template <typename T>
|
||||
void GenericVector<T>::init_to_size(int size, T t) {
|
||||
void GenericVector<T>::init_to_size(int size, const T& t) {
|
||||
reserve(size);
|
||||
size_used_ = size;
|
||||
for (int i = 0; i < size; ++i)
|
||||
@@ -509,25 +716,32 @@ void GenericVector<T>::init_to_size(int size, T t) {
|
||||
// Return the object from an index.
|
||||
template <typename T>
|
||||
T &GenericVector<T>::get(int index) const {
|
||||
ASSERT_HOST(index >= 0 && index < size_used_);
|
||||
assert(index >= 0 && index < size_used_);
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &GenericVector<T>::operator[](int index) const {
|
||||
return data_[index];
|
||||
assert(index >= 0 && index < size_used_);
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &GenericVector<T>::back() const {
|
||||
ASSERT_HOST(size_used_ > 0);
|
||||
assert(size_used_ > 0);
|
||||
return data_[size_used_ - 1];
|
||||
}
|
||||
// Returns the last object and removes it.
|
||||
template <typename T>
|
||||
T GenericVector<T>::pop_back() {
|
||||
assert(size_used_ > 0);
|
||||
return data_[--size_used_];
|
||||
}
|
||||
|
||||
// Return the object from an index.
|
||||
template <typename T>
|
||||
void GenericVector<T>::set(T t, int index) {
|
||||
ASSERT_HOST(index >= 0 && index < size_used_);
|
||||
void GenericVector<T>::set(const T& t, int index) {
|
||||
assert(index >= 0 && index < size_used_);
|
||||
data_[index] = t;
|
||||
}
|
||||
|
||||
@@ -535,8 +749,8 @@ void GenericVector<T>::set(T t, int index) {
|
||||
// space for the new elements and inserts the given element
|
||||
// at the specified index.
|
||||
template <typename T>
|
||||
void GenericVector<T>::insert(T t, int index) {
|
||||
ASSERT_HOST(index >= 0 && index < size_used_);
|
||||
void GenericVector<T>::insert(const T& t, int index) {
|
||||
assert(index >= 0 && index <= size_used_);
|
||||
if (size_reserved_ == size_used_)
|
||||
double_the_size();
|
||||
for (int i = size_used_; i > index; --i) {
|
||||
@@ -550,7 +764,7 @@ void GenericVector<T>::insert(T t, int index) {
|
||||
// shifts the remaining elements to the left.
|
||||
template <typename T>
|
||||
void GenericVector<T>::remove(int index) {
|
||||
ASSERT_HOST(index >= 0 && index < size_used_);
|
||||
assert(index >= 0 && index < size_used_);
|
||||
for (int i = index; i < size_used_ - 1; ++i) {
|
||||
data_[i] = data_[i+1];
|
||||
}
|
||||
@@ -565,9 +779,9 @@ T GenericVector<T>::contains_index(int index) const {
|
||||
|
||||
// Return the index of the T object.
|
||||
template <typename T>
|
||||
int GenericVector<T>::get_index(T object) const {
|
||||
int GenericVector<T>::get_index(const T& object) const {
|
||||
for (int i = 0; i < size_used_; ++i) {
|
||||
ASSERT_HOST(compare_cb_ != NULL);
|
||||
assert(compare_cb_ != nullptr);
|
||||
if (compare_cb_->Run(object, data_[i]))
|
||||
return i;
|
||||
}
|
||||
@@ -576,7 +790,7 @@ int GenericVector<T>::get_index(T object) const {
|
||||
|
||||
// Return true if T is in the array
|
||||
template <typename T>
|
||||
bool GenericVector<T>::contains(T object) const {
|
||||
bool GenericVector<T>::contains(const T& object) const {
|
||||
return get_index(object) != -1;
|
||||
}
|
||||
|
||||
@@ -592,7 +806,7 @@ int GenericVector<T>::push_back(T object) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int GenericVector<T>::push_back_new(T object) {
|
||||
int GenericVector<T>::push_back_new(const T& object) {
|
||||
int index = get_index(object);
|
||||
if (index >= 0)
|
||||
return index;
|
||||
@@ -601,7 +815,7 @@ int GenericVector<T>::push_back_new(T object) {
|
||||
|
||||
// Add an element in the array (front)
|
||||
template <typename T>
|
||||
int GenericVector<T>::push_front(T object) {
|
||||
int GenericVector<T>::push_front(const T& object) {
|
||||
if (size_used_ == size_reserved_)
|
||||
double_the_size();
|
||||
for (int i = size_used_; i > 0; --i)
|
||||
@@ -612,7 +826,7 @@ int GenericVector<T>::push_front(T object) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void GenericVector<T>::operator+=(T t) {
|
||||
void GenericVector<T>::operator+=(const T& t) {
|
||||
push_back(t);
|
||||
}
|
||||
|
||||
@@ -627,8 +841,10 @@ GenericVector<T> &GenericVector<T>::operator+=(const GenericVector& other) {
|
||||
|
||||
template <typename T>
|
||||
GenericVector<T> &GenericVector<T>::operator=(const GenericVector& other) {
|
||||
this->truncate(0);
|
||||
this->operator+=(other);
|
||||
if (&other != this) {
|
||||
this->truncate(0);
|
||||
this->operator+=(other);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -642,38 +858,33 @@ void GenericVector<T>::set_clear_callback(TessCallback1<T>* cb) {
|
||||
// Add a callback to be called to delete the elements when the array took
|
||||
// their ownership.
|
||||
template <typename T>
|
||||
void GenericVector<T>::set_compare_callback(TessResultCallback2<bool, T const &, T const &>* cb) {
|
||||
void GenericVector<T>::set_compare_callback(
|
||||
TessResultCallback2<bool, T const &, T const &>* cb) {
|
||||
compare_cb_ = cb;
|
||||
}
|
||||
|
||||
// Clear the array, calling the callback function if any.
|
||||
template <typename T>
|
||||
void GenericVector<T>::clear() {
|
||||
if (size_reserved_ > 0) {
|
||||
if (clear_cb_ != NULL)
|
||||
for (int i = 0; i < size_used_; ++i)
|
||||
clear_cb_->Run(data_[i]);
|
||||
delete[] data_;
|
||||
data_ = NULL;
|
||||
size_used_ = 0;
|
||||
size_reserved_ = 0;
|
||||
}
|
||||
if (clear_cb_ != NULL) {
|
||||
delete clear_cb_;
|
||||
clear_cb_ = NULL;
|
||||
}
|
||||
if (compare_cb_ != NULL) {
|
||||
delete compare_cb_;
|
||||
compare_cb_ = NULL;
|
||||
if (size_reserved_ > 0 && clear_cb_ != nullptr) {
|
||||
for (int i = 0; i < size_used_; ++i)
|
||||
clear_cb_->Run(data_[i]);
|
||||
}
|
||||
delete[] data_;
|
||||
data_ = nullptr;
|
||||
size_used_ = 0;
|
||||
size_reserved_ = 0;
|
||||
delete clear_cb_;
|
||||
clear_cb_ = nullptr;
|
||||
delete compare_cb_;
|
||||
compare_cb_ = nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void GenericVector<T>::delete_data_pointers() {
|
||||
for (int i = 0; i < size_used_; ++i)
|
||||
if (data_[i]) {
|
||||
delete data_[i];
|
||||
}
|
||||
for (int i = 0; i < size_used_; ++i) {
|
||||
delete data_[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -682,7 +893,7 @@ bool GenericVector<T>::write(
|
||||
FILE* f, TessResultCallback2<bool, FILE*, T const &>* cb) const {
|
||||
if (fwrite(&size_reserved_, sizeof(size_reserved_), 1, f) != 1) return false;
|
||||
if (fwrite(&size_used_, sizeof(size_used_), 1, f) != 1) return false;
|
||||
if (cb != NULL) {
|
||||
if (cb != nullptr) {
|
||||
for (int i = 0; i < size_used_; ++i) {
|
||||
if (!cb->Run(f, data_[i])) {
|
||||
delete cb;
|
||||
@@ -691,35 +902,30 @@ bool GenericVector<T>::write(
|
||||
}
|
||||
delete cb;
|
||||
} else {
|
||||
if (fwrite(data_, sizeof(T), size_used_, f) != size_used_) return false;
|
||||
if (fwrite(data_, sizeof(T), size_used_, f) != unsigned_size())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool GenericVector<T>::read(FILE* f,
|
||||
TessResultCallback3<bool, FILE*, T*, bool>* cb,
|
||||
bool swap) {
|
||||
inT32 reserved;
|
||||
if (fread(&reserved, sizeof(reserved), 1, f) != 1) return false;
|
||||
if (swap) Reverse32(&reserved);
|
||||
bool GenericVector<T>::read(
|
||||
tesseract::TFile* f, TessResultCallback2<bool, tesseract::TFile*, T*>* cb) {
|
||||
int32_t reserved;
|
||||
if (f->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
|
||||
reserve(reserved);
|
||||
if (fread(&size_used_, sizeof(size_used_), 1, f) != 1) return false;
|
||||
if (swap) Reverse32(&size_used_);
|
||||
if (cb != NULL) {
|
||||
if (f->FReadEndian(&size_used_, sizeof(size_used_), 1) != 1) return false;
|
||||
if (cb != nullptr) {
|
||||
for (int i = 0; i < size_used_; ++i) {
|
||||
if (!cb->Run(f, data_ + i, swap)) {
|
||||
if (!cb->Run(f, data_ + i)) {
|
||||
delete cb;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
delete cb;
|
||||
} else {
|
||||
if (fread(data_, sizeof(T), size_used_, f) != size_used_) return false;
|
||||
if (swap) {
|
||||
for (int i = 0; i < size_used_; ++i)
|
||||
ReverseN(&data_[i], sizeof(T));
|
||||
}
|
||||
if (f->FReadEndian(data_, sizeof(T), size_used_) != size_used_)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -729,7 +935,14 @@ bool GenericVector<T>::read(FILE* f,
|
||||
template <typename T>
|
||||
bool GenericVector<T>::Serialize(FILE* fp) const {
|
||||
if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
|
||||
if (fwrite(data_, sizeof(*data_), size_used_, fp) != size_used_) return false;
|
||||
if (fwrite(data_, sizeof(*data_), size_used_, fp) != unsigned_size())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
template <typename T>
|
||||
bool GenericVector<T>::Serialize(tesseract::TFile* fp) const {
|
||||
if (fp->FWrite(&size_used_, sizeof(size_used_), 1) != 1) return false;
|
||||
if (fp->FWrite(data_, sizeof(*data_), size_used_) != size_used_) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -739,18 +952,39 @@ bool GenericVector<T>::Serialize(FILE* fp) const {
|
||||
// If swap is true, assumes a big/little-endian swap is needed.
|
||||
template <typename T>
|
||||
bool GenericVector<T>::DeSerialize(bool swap, FILE* fp) {
|
||||
inT32 reserved;
|
||||
uint32_t reserved;
|
||||
if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
|
||||
if (swap) Reverse32(&reserved);
|
||||
// Arbitrarily limit the number of elements to protect against bad data.
|
||||
assert(reserved <= UINT16_MAX);
|
||||
if (reserved > UINT16_MAX) return false;
|
||||
reserve(reserved);
|
||||
size_used_ = reserved;
|
||||
if (fread(data_, sizeof(T), size_used_, fp) != size_used_) return false;
|
||||
if (fread(data_, sizeof(T), size_used_, fp) != unsigned_size()) return false;
|
||||
if (swap) {
|
||||
for (int i = 0; i < size_used_; ++i)
|
||||
ReverseN(&data_[i], sizeof(data_[i]));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <typename T>
|
||||
bool GenericVector<T>::DeSerialize(tesseract::TFile* fp) {
|
||||
uint32_t reserved;
|
||||
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
|
||||
// Arbitrarily limit the number of elements to protect against bad data.
|
||||
const uint32_t limit = 50000000;
|
||||
assert(reserved <= limit);
|
||||
if (reserved > limit) return false;
|
||||
reserve(reserved);
|
||||
size_used_ = reserved;
|
||||
return fp->FReadEndian(data_, sizeof(T), size_used_) == size_used_;
|
||||
}
|
||||
template <typename T>
|
||||
bool GenericVector<T>::SkipDeSerialize(tesseract::TFile* fp) {
|
||||
uint32_t reserved;
|
||||
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
|
||||
return fp->FRead(nullptr, sizeof(T), reserved) == reserved;
|
||||
}
|
||||
|
||||
// Writes a vector of classes to the given file. Assumes the existence of
|
||||
// bool T::Serialize(FILE* fp) const that returns false in case of error.
|
||||
@@ -763,15 +997,23 @@ bool GenericVector<T>::SerializeClasses(FILE* fp) const {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <typename T>
|
||||
bool GenericVector<T>::SerializeClasses(tesseract::TFile* fp) const {
|
||||
if (fp->FWrite(&size_used_, sizeof(size_used_), 1) != 1) return false;
|
||||
for (int i = 0; i < size_used_; ++i) {
|
||||
if (!data_[i].Serialize(fp)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Reads a vector of classes from the given file. Assumes the existence of
|
||||
// bool T::Deserialize(bool swap, FILE* fp) that returns false in case of
|
||||
// error. Alse needs T::T() and T::T(constT&), as init_to_size is used in
|
||||
// error. Also needs T::T() and T::T(constT&), as init_to_size is used in
|
||||
// this function. Returns false in case of error.
|
||||
// If swap is true, assumes a big/little-endian swap is needed.
|
||||
template <typename T>
|
||||
bool GenericVector<T>::DeSerializeClasses(bool swap, FILE* fp) {
|
||||
uinT32 reserved;
|
||||
int32_t reserved;
|
||||
if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
|
||||
if (swap) Reverse32(&reserved);
|
||||
T empty;
|
||||
@@ -781,6 +1023,26 @@ bool GenericVector<T>::DeSerializeClasses(bool swap, FILE* fp) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <typename T>
|
||||
bool GenericVector<T>::DeSerializeClasses(tesseract::TFile* fp) {
|
||||
int32_t reserved;
|
||||
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
|
||||
T empty;
|
||||
init_to_size(reserved, empty);
|
||||
for (int i = 0; i < reserved; ++i) {
|
||||
if (!data_[i].DeSerialize(fp)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <typename T>
|
||||
bool GenericVector<T>::SkipDeSerializeClasses(tesseract::TFile* fp) {
|
||||
int32_t reserved;
|
||||
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
|
||||
for (int i = 0; i < reserved; ++i) {
|
||||
if (!T::SkipDeSerialize(fp)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// This method clear the current object, then, does a shallow copy of
|
||||
// its argument, and finally invalidates its argument.
|
||||
@@ -792,9 +1054,9 @@ void GenericVector<T>::move(GenericVector<T>* from) {
|
||||
this->size_used_ = from->size_used_;
|
||||
this->compare_cb_ = from->compare_cb_;
|
||||
this->clear_cb_ = from->clear_cb_;
|
||||
from->data_ = NULL;
|
||||
from->clear_cb_ = NULL;
|
||||
from->compare_cb_ = NULL;
|
||||
from->data_ = nullptr;
|
||||
from->clear_cb_ = nullptr;
|
||||
from->compare_cb_ = nullptr;
|
||||
from->size_used_ = 0;
|
||||
from->size_reserved_ = 0;
|
||||
}
|
||||
@@ -804,4 +1066,65 @@ void GenericVector<T>::sort() {
|
||||
sort(&tesseract::sort_cmp<T>);
|
||||
}
|
||||
|
||||
// Internal recursive version of choose_nth_item.
|
||||
// The algorithm used comes from "Algorithms" by Sedgewick:
|
||||
// http://books.google.com/books/about/Algorithms.html?id=idUdqdDXqnAC
|
||||
// The principle is to choose a random pivot, and move everything less than
|
||||
// the pivot to its left, and everything greater than the pivot to the end
|
||||
// of the array, then recurse on the part that contains the desired index, or
|
||||
// just return the answer if it is in the equal section in the middle.
|
||||
// The random pivot guarantees average linear time for the same reason that
|
||||
// n times vector::push_back takes linear time on average.
|
||||
// target_index, start and and end are all indices into the full array.
|
||||
// Seed is a seed for rand_r for thread safety purposes. Its value is
|
||||
// unimportant as the random numbers do not affect the result except
|
||||
// between equal answers.
|
||||
template <typename T>
|
||||
int GenericVector<T>::choose_nth_item(int target_index, int start, int end,
|
||||
unsigned int* seed) {
|
||||
// Number of elements to process.
|
||||
int num_elements = end - start;
|
||||
// Trivial cases.
|
||||
if (num_elements <= 1)
|
||||
return start;
|
||||
if (num_elements == 2) {
|
||||
if (data_[start] < data_[start + 1]) {
|
||||
return target_index > start ? start + 1 : start;
|
||||
} else {
|
||||
return target_index > start ? start : start + 1;
|
||||
}
|
||||
}
|
||||
// Place the pivot at start.
|
||||
#ifndef rand_r // _MSC_VER, ANDROID
|
||||
srand(*seed);
|
||||
#define rand_r(seed) rand()
|
||||
#endif // _MSC_VER
|
||||
int pivot = rand_r(seed) % num_elements + start;
|
||||
swap(pivot, start);
|
||||
// The invariant condition here is that items [start, next_lesser) are less
|
||||
// than the pivot (which is at index next_lesser) and items
|
||||
// [prev_greater, end) are greater than the pivot, with items
|
||||
// [next_lesser, prev_greater) being equal to the pivot.
|
||||
int next_lesser = start;
|
||||
int prev_greater = end;
|
||||
for (int next_sample = start + 1; next_sample < prev_greater;) {
|
||||
if (data_[next_sample] < data_[next_lesser]) {
|
||||
swap(next_lesser++, next_sample++);
|
||||
} else if (data_[next_sample] == data_[next_lesser]) {
|
||||
++next_sample;
|
||||
} else {
|
||||
swap(--prev_greater, next_sample);
|
||||
}
|
||||
}
|
||||
// Now the invariant is set up, we recurse on just the section that contains
|
||||
// the desired index.
|
||||
if (target_index < next_lesser)
|
||||
return choose_nth_item(target_index, start, next_lesser, seed);
|
||||
else if (target_index < prev_greater)
|
||||
return next_lesser; // In equal bracket.
|
||||
else
|
||||
return choose_nth_item(target_index, prev_greater, end, seed);
|
||||
}
|
||||
|
||||
|
||||
#endif // TESSERACT_CCUTIL_GENERICVECTOR_H_
|
||||
Reference in new issue
Block a user