1 /**********************************************************************
2  * File:        sortflts.cpp  (Formerly sfloats.c)
3  * Description: Code to maintain a sorted list of floats.
4  * Author:      Ray Smith
5  *
6  * (C) Copyright 1993, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #include "sortflts.h"
20 
21 namespace tesseract {
22 
23 /**
24  * @name SORTED_FLOATS::add
25  *
26  * Add a new entry to the sorted list of floats.
27  */
add(float value,int32_t key)28 void SORTED_FLOATS::add( // add new entry
29     float value, int32_t key) {
30   auto *new_float = new SORTED_FLOAT(value, key);
31 
32   if (list.empty()) {
33     it.add_after_stay_put(new_float);
34   } else {
35     it.move_to_first();
36     while (!it.at_last() && it.data()->entry < value) {
37       it.forward();
38     }
39     if (it.data()->entry < value) {
40       it.add_after_stay_put(new_float);
41     } else {
42       it.add_before_stay_put(new_float);
43     }
44   }
45 }
46 
47 /**
48  * @name SORTED_FLOATS::remove
49  *
50  * Remove an entry from the sorted list of floats.
51  */
52 
remove(int32_t key)53 void SORTED_FLOATS::remove( // remove the entry
54     int32_t key) {
55   if (!list.empty()) {
56     for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
57       if (it.data()->address == key) {
58         delete it.extract();
59         return;
60       }
61     }
62   }
63 }
64 
65 /**
66  * @name SORTED_FLOATS::operator[]
67  *
68  * Return the floating point value of the given index into the list.
69  */
70 
operator [](int32_t index)71 float SORTED_FLOATS::operator[]( // get an entry
72     int32_t index                // to list
73 ) {
74   it.move_to_first();
75   return it.data_relative(index)->entry;
76 }
77 
78 } // namespace tesseract
79