Coverage Report

Created: 2026-06-13 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tesseract/src/ccutil/kdpair.h
Line
Count
Source
1
// Copyright 2012 Google Inc. All Rights Reserved.
2
// Author: rays@google.com (Ray Smith)
3
///////////////////////////////////////////////////////////////////////
4
// File:        kdpair.h
5
// Description: Template pair class like STL pair but geared towards
6
//              the Key+Data design pattern in which some data needs
7
//              to be sorted or kept in a heap sorted on some separate key.
8
// Author:      Ray Smith.
9
//
10
// (C) Copyright 2012, Google Inc.
11
// Licensed under the Apache License, Version 2.0 (the "License");
12
// you may not use this file except in compliance with the License.
13
// You may obtain a copy of the License at
14
// http://www.apache.org/licenses/LICENSE-2.0
15
// Unless required by applicable law or agreed to in writing, software
16
// distributed under the License is distributed on an "AS IS" BASIS,
17
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
// See the License for the specific language governing permissions and
19
// limitations under the License.
20
//
21
///////////////////////////////////////////////////////////////////////
22
23
#ifndef TESSERACT_CCUTIL_KDPAIR_H_
24
#define TESSERACT_CCUTIL_KDPAIR_H_
25
26
#include <vector>
27
28
namespace tesseract {
29
30
// A useful base struct to facilitate the common operation of sorting a vector
31
// of simple or smart-pointer data using a separate key. Similar to STL pair.
32
template <typename Key, typename Data>
33
struct KDPair {
34
0
  KDPair() = default;
Unexecuted instantiation: tesseract::KDPair<float, tesseract::MATRIX_COORD>::KDPair()
Unexecuted instantiation: tesseract::KDPair<double, tesseract::RecodeNode>::KDPair()
35
98.9M
  KDPair(Key k, Data d) : data_(d), key_(k) {}
tesseract::KDPair<float, tesseract::MATRIX_COORD>::KDPair(float, tesseract::MATRIX_COORD)
Line
Count
Source
35
3.58M
  KDPair(Key k, Data d) : data_(d), key_(k) {}
tesseract::KDPair<double, tesseract::RecodeNode>::KDPair(double, tesseract::RecodeNode)
Line
Count
Source
35
23.2M
  KDPair(Key k, Data d) : data_(d), key_(k) {}
tesseract::KDPair<double, tesseract::ICOORD>::KDPair(double, tesseract::ICOORD)
Line
Count
Source
35
41.7M
  KDPair(Key k, Data d) : data_(d), key_(k) {}
tesseract::KDPair<float, int>::KDPair(float, int)
Line
Count
Source
35
27.9M
  KDPair(Key k, Data d) : data_(d), key_(k) {}
Unexecuted instantiation: tesseract::KDPair<int, unsigned long>::KDPair(int, unsigned long)
tesseract::KDPair<float, tesseract::EDGEPT*>::KDPair(float, tesseract::EDGEPT*)
Line
Count
Source
35
2.46M
  KDPair(Key k, Data d) : data_(d), key_(k) {}
36
37
  int operator==(const KDPair<Key, Data> &other) const {
38
    return key_ == other.key_;
39
  }
40
41
98.1M
  Data &data() {
42
98.1M
    return data_;
43
98.1M
  }
tesseract::KDPair<float, int>::data()
Line
Count
Source
41
11.5M
  Data &data() {
42
11.5M
    return data_;
43
11.5M
  }
Unexecuted instantiation: tesseract::KDPair<int, unsigned long>::data()
tesseract::KDPair<float, tesseract::MATRIX_COORD>::data()
Line
Count
Source
41
6.01M
  Data &data() {
42
6.01M
    return data_;
43
6.01M
  }
tesseract::KDPair<double, tesseract::RecodeNode>::data()
Line
Count
Source
41
79.8M
  Data &data() {
42
79.8M
    return data_;
43
79.8M
  }
tesseract::KDPair<double, tesseract::ICOORD>::data()
Line
Count
Source
41
668k
  Data &data() {
42
668k
    return data_;
43
668k
  }
Unexecuted instantiation: tesseract::KDPair<float, tesseract::TEMPCLUSTER*>::data()
44
37.9M
  const Data &data() const {
45
37.9M
    return data_;
46
37.9M
  }
tesseract::KDPair<float, tesseract::EDGEPT*>::data() const
Line
Count
Source
44
2.46M
  const Data &data() const {
45
2.46M
    return data_;
46
2.46M
  }
tesseract::KDPair<float, tesseract::MATRIX_COORD>::data() const
Line
Count
Source
44
1.90M
  const Data &data() const {
45
1.90M
    return data_;
46
1.90M
  }
tesseract::KDPair<double, tesseract::RecodeNode>::data() const
Line
Count
Source
44
33.6M
  const Data &data() const {
45
33.6M
    return data_;
46
33.6M
  }
47
82.3M
  Key &key() {
48
82.3M
    return key_;
49
82.3M
  }
tesseract::KDPair<double, tesseract::RecodeNode>::key()
Line
Count
Source
47
2.61M
  Key &key() {
48
2.61M
    return key_;
49
2.61M
  }
tesseract::KDPair<double, tesseract::ICOORD>::key()
Line
Count
Source
47
79.3M
  Key &key() {
48
79.3M
    return key_;
49
79.3M
  }
Unexecuted instantiation: tesseract::KDPair<float, tesseract::TEMPCLUSTER*>::key()
tesseract::KDPair<float, int>::key()
Line
Count
Source
47
367k
  Key &key() {
48
367k
    return key_;
49
367k
  }
50
757M
  const Key &key() const {
51
757M
    return key_;
52
757M
  }
Unexecuted instantiation: tesseract::KDPair<int, unsigned long>::key() const
tesseract::KDPair<float, tesseract::EDGEPT*>::key() const
Line
Count
Source
50
22.8M
  const Key &key() const {
51
22.8M
    return key_;
52
22.8M
  }
tesseract::KDPair<float, tesseract::MATRIX_COORD>::key() const
Line
Count
Source
50
36.6M
  const Key &key() const {
51
36.6M
    return key_;
52
36.6M
  }
tesseract::KDPair<float, int>::key() const
Line
Count
Source
50
366M
  const Key &key() const {
51
366M
    return key_;
52
366M
  }
tesseract::KDPair<double, tesseract::RecodeNode>::key() const
Line
Count
Source
50
69.9M
  const Key &key() const {
51
69.9M
    return key_;
52
69.9M
  }
tesseract::KDPair<double, tesseract::ICOORD>::key() const
Line
Count
Source
50
261M
  const Key &key() const {
51
261M
    return key_;
52
261M
  }
Unexecuted instantiation: tesseract::KDPair<float, tesseract::TEMPCLUSTER*>::key() const
53
54
  // WARNING! Keep data as the first element! KDPairInc and KDPairDec depend
55
  // on the order of these elements so they can downcast pointers appropriately
56
  // for use by GenericHeap::Reshuffle.
57
  Data data_;
58
  Key key_;
59
};
60
// Specialization of KDPair to provide operator< for sorting in increasing order
61
// and recasting of data pointers for use with DoublePtr.
62
template <typename Key, typename Data>
63
struct KDPairInc : public KDPair<Key, Data> {
64
0
  KDPairInc() = default;
Unexecuted instantiation: tesseract::KDPairInc<float, tesseract::MATRIX_COORD>::KDPairInc()
Unexecuted instantiation: tesseract::KDPairInc<double, tesseract::RecodeNode>::KDPairInc()
65
98.9M
  KDPairInc(Key k, Data d) : KDPair<Key, Data>(k, d) {}
tesseract::KDPairInc<float, int>::KDPairInc(float, int)
Line
Count
Source
65
27.9M
  KDPairInc(Key k, Data d) : KDPair<Key, Data>(k, d) {}
Unexecuted instantiation: tesseract::KDPairInc<int, unsigned long>::KDPairInc(int, unsigned long)
tesseract::KDPairInc<float, tesseract::MATRIX_COORD>::KDPairInc(float, tesseract::MATRIX_COORD)
Line
Count
Source
65
3.58M
  KDPairInc(Key k, Data d) : KDPair<Key, Data>(k, d) {}
tesseract::KDPairInc<double, tesseract::RecodeNode>::KDPairInc(double, tesseract::RecodeNode)
Line
Count
Source
65
23.2M
  KDPairInc(Key k, Data d) : KDPair<Key, Data>(k, d) {}
tesseract::KDPairInc<double, tesseract::ICOORD>::KDPairInc(double, tesseract::ICOORD)
Line
Count
Source
65
41.7M
  KDPairInc(Key k, Data d) : KDPair<Key, Data>(k, d) {}
tesseract::KDPairInc<float, tesseract::EDGEPT*>::KDPairInc(float, tesseract::EDGEPT*)
Line
Count
Source
65
2.46M
  KDPairInc(Key k, Data d) : KDPair<Key, Data>(k, d) {}
66
  // Operator< facilitates sorting in increasing order.
67
282M
  int operator<(const KDPairInc<Key, Data> &other) const {
68
282M
    return this->key() < other.key();
69
282M
  }
Unexecuted instantiation: tesseract::KDPairInc<int, unsigned long>::operator<(tesseract::KDPairInc<int, unsigned long> const&) const
tesseract::KDPairInc<float, tesseract::EDGEPT*>::operator<(tesseract::KDPairInc<float, tesseract::EDGEPT*> const&) const
Line
Count
Source
67
11.4M
  int operator<(const KDPairInc<Key, Data> &other) const {
68
11.4M
    return this->key() < other.key();
69
11.4M
  }
tesseract::KDPairInc<float, tesseract::MATRIX_COORD>::operator<(tesseract::KDPairInc<float, tesseract::MATRIX_COORD> const&) const
Line
Count
Source
67
17.3M
  int operator<(const KDPairInc<Key, Data> &other) const {
68
17.3M
    return this->key() < other.key();
69
17.3M
  }
tesseract::KDPairInc<float, int>::operator<(tesseract::KDPairInc<float, int> const&) const
Line
Count
Source
67
100M
  int operator<(const KDPairInc<Key, Data> &other) const {
68
100M
    return this->key() < other.key();
69
100M
  }
tesseract::KDPairInc<double, tesseract::RecodeNode>::operator<(tesseract::KDPairInc<double, tesseract::RecodeNode> const&) const
Line
Count
Source
67
34.9M
  int operator<(const KDPairInc<Key, Data> &other) const {
68
34.9M
    return this->key() < other.key();
69
34.9M
  }
tesseract::KDPairInc<double, tesseract::ICOORD>::operator<(tesseract::KDPairInc<double, tesseract::ICOORD> const&) const
Line
Count
Source
67
118M
  int operator<(const KDPairInc<Key, Data> &other) const {
68
118M
    return this->key() < other.key();
69
118M
  }
Unexecuted instantiation: tesseract::KDPairInc<float, tesseract::TEMPCLUSTER*>::operator<(tesseract::KDPairInc<float, tesseract::TEMPCLUSTER*> const&) const
70
  // Returns the input Data pointer recast to a KDPairInc pointer.
71
  // Just casts a pointer to the first element to a pointer to the whole struct.
72
  static KDPairInc *RecastDataPointer(Data *data_ptr) {
73
    return reinterpret_cast<KDPairInc *>(data_ptr);
74
  }
75
};
76
// Specialization of KDPair to provide operator< for sorting in decreasing order
77
// and recasting of data pointers for use with DoublePtr.
78
template <typename Key, typename Data>
79
struct KDPairDec : public KDPair<Key, Data> {
80
  KDPairDec() = default;
81
  KDPairDec(Key k, Data d) : KDPair<Key, Data>(k, d) {}
82
  // Operator< facilitates sorting in decreasing order by using operator> on
83
  // the key values.
84
  int operator<(const KDPairDec<Key, Data> &other) const {
85
    return this->key() > other.key();
86
  }
87
  // Returns the input Data pointer recast to a KDPairDec pointer.
88
  // Just casts a pointer to the first element to a pointer to the whole struct.
89
  static KDPairDec *RecastDataPointer(Data *data_ptr) {
90
    return reinterpret_cast<KDPairDec *>(data_ptr);
91
  }
92
};
93
94
// A useful base class to facilitate the common operation of sorting a vector
95
// of owned pointer data using a separate key. This class owns its data pointer,
96
// deleting it when it has finished with it, and providing copy constructor and
97
// operator= that have move semantics so that the data does not get copied and
98
// only a single instance of KDPtrPair holds a specific data pointer.
99
template <typename Key, typename Data>
100
class KDPtrPair {
101
public:
102
89.6M
  KDPtrPair() : data_(nullptr) {}
103
235M
  KDPtrPair(Key k, Data *d) : data_(d), key_(k) {}
104
  // Copy constructor steals the pointer from src and nulls it in src, thereby
105
  // moving the (single) ownership of the data.
106
366M
  KDPtrPair(const KDPtrPair &src) : data_(src.data_), key_(src.key_) {
107
366M
    ((KDPtrPair &)src).data_ = nullptr;
108
366M
  }
109
  // Destructor deletes data, assuming it is the sole owner.
110
690M
  ~KDPtrPair() {
111
690M
    delete this->data_;
112
690M
    this->data_ = nullptr;
113
690M
  }
114
  // Operator= steals the pointer from src and nulls it in src, thereby
115
  // moving the (single) ownership of the data.
116
1.44G
  void operator=(const KDPtrPair &src) {
117
1.44G
    delete this->data_;
118
1.44G
    this->data_ = src.data_;
119
1.44G
    ((KDPtrPair &)src).data_ = nullptr;
120
1.44G
    this->key_ = src.key_;
121
1.44G
  }
122
123
  int operator==(const KDPtrPair<Key, Data> &other) const {
124
    return key_ == other.key_;
125
  }
126
127
  // Accessors.
128
16.1G
  const Key &key() const {
129
16.1G
    return key_;
130
16.1G
  }
131
7.18M
  void set_key(const Key &new_key) {
132
7.18M
    key_ = new_key;
133
7.18M
  }
134
1.70G
  const Data *data() const {
135
1.70G
    return data_;
136
1.70G
  }
137
  // Sets the data pointer, taking ownership of the data.
138
7.18M
  void set_data(Data *new_data) {
139
7.18M
    delete data_;
140
7.18M
    data_ = new_data;
141
7.18M
  }
142
  // Relinquishes ownership of the data pointer (setting it to nullptr).
143
82.4M
  Data *extract_data() {
144
82.4M
    Data *result = data_;
145
82.4M
    data_ = nullptr;
146
82.4M
    return result;
147
82.4M
  }
148
149
private:
150
  // Data members are private to keep deletion of data_ encapsulated.
151
  Data *data_;
152
  Key key_;
153
};
154
// Specialization of KDPtrPair to provide operator< for sorting in increasing
155
// order.
156
template <typename Key, typename Data>
157
struct KDPtrPairInc : public KDPtrPair<Key, Data> {
158
  // Since we are doing non-standard stuff we have to duplicate *all* the
159
  // constructors and operator=.
160
82.4M
  KDPtrPairInc() : KDPtrPair<Key, Data>() {}
161
224M
  KDPtrPairInc(Key k, Data *d) : KDPtrPair<Key, Data>(k, d) {}
162
340M
  KDPtrPairInc(const KDPtrPairInc &src) : KDPtrPair<Key, Data>(src) {}
163
1.32G
  void operator=(const KDPtrPairInc &src) {
164
1.32G
    KDPtrPair<Key, Data>::operator=(src);
165
1.32G
  }
166
  // Operator< facilitates sorting in increasing order.
167
7.78G
  int operator<(const KDPtrPairInc<Key, Data> &other) const {
168
7.78G
    return this->key() < other.key();
169
7.78G
  }
170
};
171
// Specialization of KDPtrPair to provide operator< for sorting in decreasing
172
// order.
173
template <typename Key, typename Data>
174
struct KDPtrPairDec : public KDPtrPair<Key, Data> {
175
  // Since we are doing non-standard stuff we have to duplicate *all* the
176
  // constructors and operator=.
177
7.18M
  KDPtrPairDec() : KDPtrPair<Key, Data>() {}
178
10.8M
  KDPtrPairDec(Key k, Data *d) : KDPtrPair<Key, Data>(k, d) {}
179
25.2M
  KDPtrPairDec(const KDPtrPairDec &src) : KDPtrPair<Key, Data>(src) {}
180
121M
  void operator=(const KDPtrPairDec &src) {
181
121M
    KDPtrPair<Key, Data>::operator=(src);
182
121M
  }
183
  // Operator< facilitates sorting in decreasing order by using operator> on
184
  // the key values.
185
132M
  int operator<(const KDPtrPairDec<Key, Data> &other) const {
186
132M
    return this->key() > other.key();
187
132M
  }
188
};
189
190
// Specialization for a pair of ints in increasing order.
191
using IntKDPair = KDPairInc<int, int>;
192
193
// Vector of IntKDPair.
194
class KDVector : public std::vector<IntKDPair> {
195
  // TODO(rays) Add some code to manipulate a KDVector. For now there
196
  // is nothing and this class is effectively a specialization typedef.
197
};
198
199
} // namespace tesseract
200
201
#endif // TESSERACT_CCUTIL_KDPAIR_H_