Leptonica  1.83.1
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 #include "array_internal.h"
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  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 
103 void
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  *--------------------------------------------------------------------------*/
136 L_DNA *
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 
169 l_ok
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 }
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
Definition: dnabasic.c:179
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
Definition: dnabasic.c:430
L_DNA * l_dnaClone(L_DNA *da)
l_dnaClone()
Definition: dnabasic.c:384
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
Definition: dnabasic.c:323
L_DNA * l_dnaCopy(L_DNA *da)
l_dnaCopy()
Definition: dnabasic.c:357
void l_dnaHashDestroy(L_DNAHASH **pdahash)
l_dnaHashDestroy()
Definition: dnahash.c:104
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: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
l_ok lept_isPrime(l_uint64 n, l_int32 *pis_prime, l_uint32 *pfactor)
lept_isPrime()
Definition: utils1.c:876
l_ok findNextLargerPrime(l_int32 start, l_uint32 *pprime)
findNextLargerPrime()
Definition: utils1.c:843