Leptonica 1.85.0
Image processing and image analysis suite
Loading...
Searching...
No Matches
dnahash.c
Go to the documentation of this file.
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
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 *--------------------------------------------------------------------------*/
69l_dnaHashCreate(l_int32 nbuckets,
70 l_int32 initsize)
71{
72l_int32 is_prime;
73l_uint32 newsize;
74L_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
103void
105{
106L_DNAHASH *dahash;
107l_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 *--------------------------------------------------------------------------*/
136L_DNA *
138 l_uint64 key,
139 l_int32 copyflag)
140{
141l_int32 bucket;
142L_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
169l_ok
171 l_uint64 key,
172 l_float64 value)
173{
174l_int32 bucket;
175L_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}
L_DNAHASH * l_dnaHashCreate(l_int32 nbuckets, l_int32 initsize)
l_dnaHashCreate()
Definition dnahash.c:69
void l_dnaHashDestroy(L_DNAHASH **pdahash)
l_dnaHashDestroy()
Definition dnahash.c:104
L_DNA * l_dnaHashGetDna(L_DNAHASH *dahash, l_uint64 key, l_int32 copyflag)
l_dnaHashGetDna()
Definition dnahash.c:137
l_ok l_dnaHashAdd(L_DNAHASH *dahash, l_uint64 key, l_float64 value)
l_dnaHashAdd()
Definition dnahash.c:170
@ L_COPY
Definition pix.h:505
@ L_NOCOPY
Definition pix.h:503
struct L_Dna ** dna
l_int32 initsize