fe6c148d97f1a1ef4e1909922f605d9646f86503
[m6w6/libmemcached] / libhashkit / hashkit.h
1 /* HashKit
2 * Copyright (C) 2009-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #ifndef HASHKIT_H
10 #define HASHKIT_H
11
12
13 #if !defined(__cplusplus)
14 # include <stdbool.h>
15 #endif
16 #include <inttypes.h>
17 #include <sys/types.h>
18 #include <libhashkit/visibility.h>
19 #include <libhashkit/types.h>
20 #include <libhashkit/algorithm.h>
21 #include <libhashkit/behavior.h>
22 #include <libhashkit/digest.h>
23 #include <libhashkit/function.h>
24 #include <libhashkit/strerror.h>
25
26 #ifdef __cplusplus
27
28 #include <string>
29
30 extern "C" {
31 #endif
32
33 HASHKIT_API
34 hashkit_st *hashkit_create(hashkit_st *hash);
35
36 HASHKIT_API
37 hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *ptr);
38
39 HASHKIT_API
40 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second);
41
42 HASHKIT_API
43 void hashkit_free(hashkit_st *hash);
44
45 #define hashkit_is_allocated(__object) ((__object)->options.is_allocated)
46 #define hashkit_is_initialized(__object) ((__object)->options.is_initialized)
47
48 struct hashkit_st
49 {
50 struct hashkit_function_st {
51 hashkit_hash_fn function;
52 void *context;
53 } base_hash, distribution_hash;
54
55 struct {
56 bool is_base_same_distributed:1;
57 } flags;
58
59 struct {
60 bool is_allocated:1;
61 } options;
62
63 #ifdef __cplusplus
64
65 hashkit_st() :
66 base_hash(),
67 distribution_hash(),
68 flags(),
69 options()
70 {
71 hashkit_create(this);
72 }
73
74 hashkit_st(const hashkit_st& source) :
75 base_hash(),
76 distribution_hash(),
77 flags(),
78 options()
79 {
80 hashkit_clone(this, &source);
81 }
82
83 hashkit_st& operator=(const hashkit_st& source)
84 {
85 hashkit_clone(this, &source);
86
87 return *this;
88 }
89
90 friend bool operator==(const hashkit_st &left, const hashkit_st &right)
91 {
92 return hashkit_compare(&left, &right);
93 }
94
95 uint32_t digest(std::string& str)
96 {
97 return hashkit_digest(this, str.c_str(), str.length());
98 }
99
100 uint32_t digest(const char *key, size_t key_length)
101 {
102 return hashkit_digest(this, key, key_length);
103 }
104
105 hashkit_return_t set_function(hashkit_hash_algorithm_t hash_algorithm)
106 {
107 return hashkit_set_function(this, hash_algorithm);
108 }
109
110 hashkit_return_t set_distribution_function(hashkit_hash_algorithm_t hash_algorithm)
111 {
112 return hashkit_set_function(this, hash_algorithm);
113 }
114
115 ~hashkit_st()
116 {
117 hashkit_free(this);
118 }
119 #endif
120 };
121
122 #ifdef __cplusplus
123 } // extern "C"
124 #endif
125
126
127 #endif /* HASHKIT_H */