Leptonica  1.82.0
Image processing and image analysis suite
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 
50 
51 /*--------------------------------------------------------------------------*
52  * Dnahash creation and destruction *
53  *--------------------------------------------------------------------------*/
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  PROCNAME("l_dnaHashCreate");
77 
78  if (nbuckets <= 0)
79  return (L_DNAHASH *)ERROR_PTR("negative hash size", procName, NULL);
80  lept_isPrime(nbuckets, &is_prime, NULL);
81  if (!is_prime) {
82  findNextLargerPrime(nbuckets, &newsize);
83  nbuckets = newsize;
84  }
85 
86  dahash = (L_DNAHASH *)LEPT_CALLOC(1, sizeof(L_DNAHASH));
87  if ((dahash->dna = (L_DNA **)LEPT_CALLOC(nbuckets, sizeof(L_DNA *)))
88  == NULL) {
89  LEPT_FREE(dahash);
90  return (L_DNAHASH *)ERROR_PTR("dna ptr array not made", procName, NULL);
91  }
92 
93  dahash->nbuckets = nbuckets;
94  dahash->initsize = initsize;
95  return dahash;
96 }
97 
98 
105 void
107 {
108 L_DNAHASH *dahash;
109 l_int32 i;
110 
111  PROCNAME("l_dnaHashDestroy");
112 
113  if (pdahash == NULL) {
114  L_WARNING("ptr address is NULL!\n", procName);
115  return;
116  }
117 
118  if ((dahash = *pdahash) == NULL)
119  return;
120 
121  for (i = 0; i < dahash->nbuckets; i++)
122  l_dnaDestroy(&dahash->dna[i]);
123  LEPT_FREE(dahash->dna);
124  LEPT_FREE(dahash);
125  *pdahash = NULL;
126 }
127 
128 
129 /*--------------------------------------------------------------------------*
130  * Dnahash accessor and modifier *
131  *--------------------------------------------------------------------------*/
140 L_DNA *
142  l_uint64 key,
143  l_int32 copyflag)
144 {
145 l_int32 bucket;
146 L_DNA *da;
147 
148  PROCNAME("l_dnaHashGetDna");
149 
150  if (!dahash)
151  return (L_DNA *)ERROR_PTR("dahash not defined", procName, NULL);
152  bucket = key % dahash->nbuckets;
153  da = dahash->dna[bucket];
154  if (da) {
155  if (copyflag == L_NOCOPY)
156  return da;
157  else if (copyflag == L_COPY)
158  return l_dnaCopy(da);
159  else
160  return l_dnaClone(da);
161  }
162  else
163  return NULL;
164 }
165 
166 
175 l_ok
177  l_uint64 key,
178  l_float64 value)
179 {
180 l_int32 bucket;
181 L_DNA *da;
182 
183  PROCNAME("l_dnaHashAdd");
184 
185  if (!dahash)
186  return ERROR_INT("dahash not defined", procName, 1);
187  bucket = key % dahash->nbuckets;
188  da = dahash->dna[bucket];
189  if (!da) {
190  if ((da = l_dnaCreate(dahash->initsize)) == NULL)
191  return ERROR_INT("da not made", procName, 1);
192  dahash->dna[bucket] = da;
193  }
194  l_dnaAddNumber(da, value);
195  return 0;
196 }
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
Definition: dnabasic.c:180
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
Definition: dnabasic.c:448
L_DNA * l_dnaClone(L_DNA *da)
l_dnaClone()
Definition: dnabasic.c:398
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
Definition: dnabasic.c:332
L_DNA * l_dnaCopy(L_DNA *da)
l_dnaCopy()
Definition: dnabasic.c:369
void l_dnaHashDestroy(L_DNAHASH **pdahash)
l_dnaHashDestroy()
Definition: dnahash.c:106
L_DNAHASH * l_dnaHashCreate(l_int32 nbuckets, l_int32 initsize)
l_dnaHashCreate()
Definition: dnahash.c:69
L_DNA * l_dnaHashGetDna(L_DNAHASH *dahash, l_uint64 key, l_int32 copyflag)
l_dnaHashGetDna()
Definition: dnahash.c:141
l_ok l_dnaHashAdd(L_DNAHASH *dahash, l_uint64 key, l_float64 value)
l_dnaHashAdd()
Definition: dnahash.c:176
@ L_COPY
Definition: pix.h:712
@ L_NOCOPY
Definition: pix.h:710
struct L_Dna ** dna
Definition: array.h:118
l_int32 initsize
Definition: array.h:117
Definition: array.h:95
l_ok lept_isPrime(l_uint64 n, l_int32 *pis_prime, l_uint32 *pfactor)
lept_isPrime()
Definition: utils1.c:896
l_ok findNextLargerPrime(l_int32 start, l_uint32 *pprime)
findNextLargerPrime()
Definition: utils1.c:861