Merge in all current libmemcached work, plus restore older, working,
[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/configure.h>
20 #include <libhashkit/types.h>
21 #include <libhashkit/algorithm.h>
22 #include <libhashkit/behavior.h>
23 #include <libhashkit/digest.h>
24 #include <libhashkit/function.h>
25 #include <libhashkit/str_algorithm.h>
26 #include <libhashkit/strerror.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 HASHKIT_API
33 hashkit_st *hashkit_create(hashkit_st *hash);
34
35 HASHKIT_API
36 hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *ptr);
37
38 HASHKIT_API
39 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second);
40
41 HASHKIT_API
42 void hashkit_free(hashkit_st *hash);
43
44 #define hashkit_is_allocated(__object) ((__object)->options.is_allocated)
45 #define hashkit_is_initialized(__object) ((__object)->options.is_initialized)
46
47 #ifdef __cplusplus
48 } // extern "C"
49 #endif
50
51 struct hashkit_st
52 {
53 struct hashkit_function_st {
54 hashkit_hash_fn function;
55 void *context;
56 } base_hash, distribution_hash;
57
58 struct {
59 bool is_base_same_distributed:1;
60 } flags;
61
62 struct {
63 bool is_allocated:1;
64 } options;
65 };
66
67 #ifdef __cplusplus
68
69 #include <string>
70
71 class Hashkit {
72
73 public:
74
75 Hashkit()
76 {
77 hashkit_create(&self);
78 }
79
80 Hashkit(const Hashkit& source)
81 {
82 hashkit_clone(&self, &source.self);
83 }
84
85 Hashkit& operator=(const Hashkit& source)
86 {
87 hashkit_free(&self);
88 hashkit_clone(&self, &source.self);
89
90 return *this;
91 }
92
93 friend bool operator==(const Hashkit &left, const Hashkit &right)
94 {
95 return hashkit_compare(&left.self, &right.self);
96 }
97
98 uint32_t digest(std::string& str)
99 {
100 return hashkit_digest(&self, str.c_str(), str.length());
101 }
102
103 uint32_t digest(const char *key, size_t key_length)
104 {
105 return hashkit_digest(&self, key, key_length);
106 }
107
108 hashkit_return_t set_function(hashkit_hash_algorithm_t hash_algorithm)
109 {
110 return hashkit_set_function(&self, hash_algorithm);
111 }
112
113 hashkit_return_t set_distribution_function(hashkit_hash_algorithm_t hash_algorithm)
114 {
115 return hashkit_set_function(&self, hash_algorithm);
116 }
117
118 ~Hashkit()
119 {
120 hashkit_free(&self);
121 }
122 private:
123
124 hashkit_st self;
125 };
126 #endif
127
128
129 #endif /* HASHKIT_H */