comparison mupdf-source/thirdparty/leptonica/src/dnahash.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
27 /*!
28 * \file dnahash.c
29 * <pre>
30 * Dnahash creation, destruction
31 * L_DNAHASH *l_dnaHashCreate()
32 * void l_dnaHashDestroy()
33 *
34 * Dnahash accessor and modifier
35 * L_DNA *l_dnaHashGetDna()
36 * l_int32 l_dnaHashAdd()
37 *
38 * (1) The DnaHash is an array of Dna. It is a simple method used for
39 * fast lookup of templates in the jbig2 classifier (jbclass.c).
40 * </pre>
41 */
42
43
44 #ifdef HAVE_CONFIG_H
45 #include <config_auto.h>
46 #endif /* HAVE_CONFIG_H */
47
48 #include "allheaders.h"
49 #include "array_internal.h"
50
51 /*--------------------------------------------------------------------------*
52 * Dnahash creation and destruction *
53 *--------------------------------------------------------------------------*/
54 /*!
55 * \brief l_dnaHashCreate()
56 *
57 * \param[in] nbuckets the number of buckets in the hash table,
58 * which should be prime.
59 * \param[in] initsize initial size of each allocated dna; 0 for default
60 * \return ptr to new dnahash, or NULL on error
61 *
62 * <pre>
63 * Notes:
64 * (1) If %nbuckets is not prime, use the next largest prime.
65 * (2) In use, stored dna are created by l_dnaHashAdd().
66 * </pre>
67 */
68 L_DNAHASH *
69 l_dnaHashCreate(l_int32 nbuckets,
70 l_int32 initsize)
71 {
72 l_int32 is_prime;
73 l_uint32 newsize;
74 L_DNAHASH *dahash;
75
76 if (nbuckets <= 0)
77 return (L_DNAHASH *)ERROR_PTR("negative hash size", __func__, NULL);
78 lept_isPrime(nbuckets, &is_prime, NULL);
79 if (!is_prime) {
80 findNextLargerPrime(nbuckets, &newsize);
81 nbuckets = newsize;
82 }
83
84 dahash = (L_DNAHASH *)LEPT_CALLOC(1, sizeof(L_DNAHASH));
85 if ((dahash->dna = (L_DNA **)LEPT_CALLOC(nbuckets, sizeof(L_DNA *)))
86 == NULL) {
87 LEPT_FREE(dahash);
88 return (L_DNAHASH *)ERROR_PTR("dna ptr array not made", __func__, NULL);
89 }
90
91 dahash->nbuckets = nbuckets;
92 dahash->initsize = initsize;
93 return dahash;
94 }
95
96
97 /*!
98 * \brief l_dnaHashDestroy()
99 *
100 * \param[in,out] pdahash will be set to null before returning
101 * \return void
102 */
103 void
104 l_dnaHashDestroy(L_DNAHASH **pdahash)
105 {
106 L_DNAHASH *dahash;
107 l_int32 i;
108
109 if (pdahash == NULL) {
110 L_WARNING("ptr address is NULL!\n", __func__);
111 return;
112 }
113
114 if ((dahash = *pdahash) == NULL)
115 return;
116
117 for (i = 0; i < dahash->nbuckets; i++)
118 l_dnaDestroy(&dahash->dna[i]);
119 LEPT_FREE(dahash->dna);
120 LEPT_FREE(dahash);
121 *pdahash = NULL;
122 }
123
124
125 /*--------------------------------------------------------------------------*
126 * Dnahash accessor and modifier *
127 *--------------------------------------------------------------------------*/
128 /*!
129 * \brief l_dnaHashGetDna()
130 *
131 * \param[in] dahash
132 * \param[in] key key to be hashed into a bucket number
133 * \param[in] copyflag L_NOCOPY, L_COPY, L_CLONE
134 * \return ptr to dna
135 */
136 L_DNA *
137 l_dnaHashGetDna(L_DNAHASH *dahash,
138 l_uint64 key,
139 l_int32 copyflag)
140 {
141 l_int32 bucket;
142 L_DNA *da;
143
144 if (!dahash)
145 return (L_DNA *)ERROR_PTR("dahash not defined", __func__, NULL);
146 bucket = key % dahash->nbuckets;
147 da = dahash->dna[bucket];
148 if (da) {
149 if (copyflag == L_NOCOPY)
150 return da;
151 else if (copyflag == L_COPY)
152 return l_dnaCopy(da);
153 else
154 return l_dnaClone(da);
155 }
156 else
157 return NULL;
158 }
159
160
161 /*!
162 * \brief l_dnaHashAdd()
163 *
164 * \param[in] dahash
165 * \param[in] key key to be hashed into a bucket number
166 * \param[in] value float value to be appended to the specific dna
167 * \return 0 if OK; 1 on error
168 */
169 l_ok
170 l_dnaHashAdd(L_DNAHASH *dahash,
171 l_uint64 key,
172 l_float64 value)
173 {
174 l_int32 bucket;
175 L_DNA *da;
176
177 if (!dahash)
178 return ERROR_INT("dahash not defined", __func__, 1);
179 bucket = key % dahash->nbuckets;
180 da = dahash->dna[bucket];
181 if (!da) {
182 if ((da = l_dnaCreate(dahash->initsize)) == NULL)
183 return ERROR_INT("da not made", __func__, 1);
184 dahash->dna[bucket] = da;
185 }
186 l_dnaAddNumber(da, value);
187 return 0;
188 }