taskrambler  0.1.9
Web server and task management solution.
hash/get.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdio.h>
24 
25 #include <search.h>
26 #include <sys/types.h>
27 
28 #include "hash.h"
29 #include "tree.h"
30 #include "utils/hash.h"
31 
32 static
33 inline
34 int
35 hashGetComp(const void * a, const void * b)
36 {
37  unsigned long hash_a = hashableGetHash((void*)a);
38 
39  if (hash_a < *(const unsigned long*)b) {
40  return -1;
41  }
42 
43  if (hash_a > *(const unsigned long*)b) {
44  return 1;
45  }
46 
47  return 0;
48 }
49 
50 void *
51 hashGet(Hash this, const char * search, size_t nsearch)
52 {
53  unsigned long hash = sdbm((const unsigned char *)search, nsearch);
54  void * found = treeFind(this->root, &hash, hashGetComp);
55 
56  return found;
57 }
58 
59 void *
60 hashGetByVal(Hash this, unsigned long hash)
61 {
62  void * found = treeFind(this->root, &hash, hashGetComp);
63 
64  return found;
65 }
66 // vim: set ts=4 sw=4:
unsigned long sdbm(const unsigned char *, size_t)
Definition: utils/hash.c:45
unsigned long hashableGetHash(void *)
Definition: hashable.c:36
static int hashGetComp(const void *a, const void *b)
Definition: hash/get.c:35
void * hashGetByVal(Hash this, unsigned long hash)
Definition: hash/get.c:60
void * treeFind(Tree, const void *, TreeComp)
Definition: find.c:26
void * hashGet(Hash this, const char *search, size_t nsearch)
Definition: hash/get.c:51