/src/tesseract/src/cutil/oldlist.h
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * File: oldlist.h (Formerly list.h) |
4 | | * Description: List processing procedures declarations. |
5 | | * Author: Mark Seaman, SW Productivity |
6 | | * |
7 | | * (c) Copyright 1987, Hewlett-Packard Company. |
8 | | ** Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | ** you may not use this file except in compliance with the License. |
10 | | ** You may obtain a copy of the License at |
11 | | ** http://www.apache.org/licenses/LICENSE-2.0 |
12 | | ** Unless required by applicable law or agreed to in writing, software |
13 | | ** distributed under the License is distributed on an "AS IS" BASIS, |
14 | | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | ** See the License for the specific language governing permissions and |
16 | | ** limitations under the License. |
17 | | * |
18 | | ****************************************************************************** |
19 | | * |
20 | | * This file contains the interface for a set of general purpose list |
21 | | * manipulation routines. For the implementation of these routines see |
22 | | * the file "list.c". |
23 | | * |
24 | | ****************************************************************************** |
25 | | * |
26 | | * INDEX |
27 | | * ======= |
28 | | * |
29 | | * BASICS: |
30 | | * ------- |
31 | | * first_node - Macro to return the first list node (not the cell). |
32 | | * list_rest - Macro the return the second list cell |
33 | | * pop - Destroy one list cell |
34 | | * push - Create one list cell and set the node and next fields |
35 | | * |
36 | | * ITERATION: |
37 | | * ----------------- |
38 | | * iterate - Macro to create a for loop to visit each cell. |
39 | | * |
40 | | * LIST CELL COUNTS: |
41 | | * ----------------- |
42 | | * count - Returns the number of list cells in the list. |
43 | | * last - Returns the last list cell. |
44 | | * |
45 | | * TRANSFORMS: (Note: These functions all modify the input list.) |
46 | | * ---------- |
47 | | * delete_d - Removes the requested elements from the list. |
48 | | * push_last - Add a new element onto the end of a list. |
49 | | * |
50 | | * SETS: |
51 | | * ----- |
52 | | * search - Return the pointer to the list cell whose node matches. |
53 | | * |
54 | | * CELL OPERATIONS: |
55 | | * ----------------- |
56 | | * destroy - Return all list cells in a list. |
57 | | * destroy_nodes - Apply a function to each list cell and destroy the list. |
58 | | * set_rest - Assign the next field in a list cell. |
59 | | * |
60 | | ***********************************************************************/ |
61 | | |
62 | | #ifndef LIST_H |
63 | | #define LIST_H |
64 | | |
65 | | #include <tesseract/export.h> |
66 | | |
67 | | #include <cstddef> // for size_t |
68 | | |
69 | | namespace tesseract { |
70 | | |
71 | | /*---------------------------------------------------------------------- |
72 | | T y p e s |
73 | | ----------------------------------------------------------------------*/ |
74 | | |
75 | 3.52M | #define NIL_LIST static_cast<LIST>(nullptr) |
76 | | |
77 | | using int_compare = int (*)(void *, void *); |
78 | | using void_dest = void (*)(void *); |
79 | | |
80 | | /*---------------------------------------------------------------------- |
81 | | M a c r o s |
82 | | ----------------------------------------------------------------------*/ |
83 | | |
84 | | /********************************************************************** |
85 | | * i t e r a t e |
86 | | * |
87 | | * Visit each node in the list. Replace the old list with the list |
88 | | * minus the head. Continue until the list is NIL_LIST. |
89 | | **********************************************************************/ |
90 | | |
91 | 407M | #define iterate(l) for (; (l) != nullptr; (l) = (l)->list_rest()) |
92 | | |
93 | | /********************************************************************** |
94 | | * s e t r e s t |
95 | | * |
96 | | * Change the "next" field of a list element to point to a desired place. |
97 | | * |
98 | | * #define set_rest(l,node) l->next = node; |
99 | | **********************************************************************/ |
100 | | |
101 | 57.1k | #define set_rest(l, cell) ((l)->next = (cell)) |
102 | | |
103 | | struct list_rec { |
104 | | list_rec *node; |
105 | | list_rec *next; |
106 | | |
107 | 203M | list_rec *first_node() { |
108 | 203M | return node; |
109 | 203M | } |
110 | | |
111 | 203M | list_rec *list_rest() { |
112 | 203M | return next; |
113 | 203M | } |
114 | | |
115 | | //******************************************************************** |
116 | | // Recursively count the elements in a list. Return the count. |
117 | | //******************************************************************** |
118 | 0 | size_t size() { |
119 | 0 | auto var_list = this; |
120 | 0 | size_t n = 0; |
121 | 0 | iterate(var_list) n++; |
122 | 0 | return n; |
123 | 0 | } |
124 | | }; |
125 | | using LIST = list_rec *; |
126 | | |
127 | | /*---------------------------------------------------------------------- |
128 | | Public Function Prototypes |
129 | | ----------------------------------------------------------------------*/ |
130 | | |
131 | | LIST delete_d(LIST list, void *key, int_compare is_equal); |
132 | | |
133 | | TESS_API |
134 | | LIST destroy(LIST list); |
135 | | |
136 | | void destroy_nodes(LIST list, void_dest destructor); |
137 | | |
138 | | LIST last(LIST var_list); |
139 | | |
140 | | LIST pop(LIST list); |
141 | | |
142 | | TESS_API |
143 | | LIST push(LIST list, void *element); |
144 | | |
145 | | TESS_API |
146 | | LIST push_last(LIST list, void *item); |
147 | | |
148 | | LIST search(LIST list, void *key, int_compare is_equal); |
149 | | |
150 | | } // namespace tesseract |
151 | | |
152 | | #endif |