aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_KeyValue.h
blob: a4bc3915dff9d8010233d318ff31628082a6a940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_KeyValue.h
 * @ingroup datastructures
 * @brief A key and value tuple.
 *
 * The `PARCKeyValue` is a simple key-value tuple.
 *
 */
#ifndef libparc_parc_KeyValue_h
#define libparc_parc_KeyValue_h

#include "parc_Object.h"

struct parc_key_value;

/**
 * @typedef `PARCKeyValue`
 * @brief A `PARCKeyValue` is a tuple consisting of a key and a value
 */
typedef struct parc_key_value PARCKeyValue;

/**
 * Create a `PARCKeyValue` Element. The key and value must be PARCObjects.
 *
 * Neither the data nor the key will be copied, but referenced.
 * The key may not be `NULL`.
 *
 * @param [in] key A pointer to the key data
 * @param [in] value A pointer to the value data
 *
 * Example:
 * @code
 * {
 *      ...
 *      PARCKeyValue *kv = parcKeyValue_Create(key, value);
 *      ...
 *      parcKeyValue_Release(&kv);
 * }
 * @endcode
 */
PARCKeyValue *parcKeyValue_Create(const PARCObject *key,
                                  const PARCObject *value);

/**
 * Create a copy of a `PARCKeyValue` Element.
 *
 * The source PARCKeyValue may not be `NULL`.
 *
 * @param [in] keyValue Source PARCKeyValue element
 *
 * Example:
 * @code
 * {
 *      ...
 *      PARCKeyValue *keyValueCopy = parcKeyValue_Copy(sourcKeyValue);
 *      ...
 *      parcKeyValue_Release(&keyValueCopy);
 * }
 * @endcode
 */
PARCKeyValue *parcKeyValue_Copy(const PARCKeyValue *keyValue);

/**
 * Acquire a reference to a `PARCKeyValue` Element.
 *
 * The source PARCKeyValue may not be `NULL`.
 *
 * @param [in] keyValue Source PARCKeyValue element
 *
 * Example:
 * @code
 * {
 *      ...
 *      PARCKeyValue *keyValue = parcKeyValue_Acquire(sourcKeyValue);
 *      ...
 *      parcKeyValue_Release(&keyValue);
 * }
 * @endcode
 */
PARCKeyValue *parcKeyValue_Acquire(const PARCKeyValue *keyValue);

/**
 * Release a `PARCKeyValue`.
 *
 * Releases a reference to a PARCKeyValue element, if it is the last reference then the
 * contained key & value objects are released and the elements memory is freed.
 *
 * @param [in,out] keyValuePointer A pointer to the pointer to the `PARCKeyValue` to be released
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv = parcKeyValue_Create(key, value);
 *      ...
 *      parcKeyValue_Release(&kv);
 * @endcode
 */
void parcKeyValue_Release(PARCKeyValue **keyValuePointer);

/**
 * Set the value for a `PARCKeyValue`.
 * The previous value will be released.
 *
 * @param [in,out] keyValue A pointer to the keyValue
 * @param [in] value A pointer to the new value
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv = parcKeyValue_Create(key, value);
 *      ...
 *      parcKeyValue_SetKey(kv, newKey);
 *      ...
 *      parcKeyValue_Release(&kv);
 * @endcode
 */
void parcKeyValue_SetValue(PARCKeyValue *keyValue, PARCObject *value);

/**
 * Set the key for a `PARCKeyValue`.
 * The previous key will be released.
 *
 * @param [in,out] keyValue A pointer to the `PARCKeyValue`
 * @param [in] key A pointer to the new key
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv = parcKeyValue_Create(key, value);
 *      ...
 *      parcKeyValue_SetValue(kv, newValue);
 *      ...
 *      parcKeyValue_Release(&kv);
 * @endcode
 */
void parcKeyValue_SetKey(PARCKeyValue *keyValue, PARCObject *key);

/**
 * Get the value pointer for the `PARCKeyValue`. No extra reference is created.
 *
 * @param [in] keyValue A pointer to the `PARCKeyValue`
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv = parcKeyValue_Create(key, value);
 *      ...
 *      PARCObject *value = parcKeyValue_GetValue(kv);
 *      ...
 *      parcKeyValue_Release(&kv);
 * @endcode
 */
PARCObject *parcKeyValue_GetValue(PARCKeyValue *keyValue);

/**
 * Get the key pointer for the `PARCKeyValue`. No extra reference is created.
 *
 * @param [in] keyValue A pointer to the `PARCKeyValue`
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv = parcKeyValue_Create(key, value);
 *      ...
 *      PARCObject *key = parcKeyValue_GetKey(kv);
 *      ...
 *      parcKeyValue_Release(&kv);
 * @endcode
 */
PARCObject *parcKeyValue_GetKey(PARCKeyValue *keyValue);

/**
 * Check for Key equality.  Return true if equal.
 *
 * @param [in] keyValue1 A pointer to the first `PARCKeyValue`
 * @param [in] keyValue2 A pointer to the second `PARCKeyValue`
 * @return true if the keyValues have the same key
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv1 = parcKeyValue_Create(key1, value);
 *      PARCKeyValue *kv2 = parcKeyValue_Create(key2, value);
 *      ...
 *      if (parcKeyValue_EqualKeys(kv1, kv2)) {
 *         ...
 *      }
 *      ...
 *      parcKeyValue_Release(&kv1);
 *      parcKeyValue_Release(&kv2);
 * @endcode
 */
bool parcKeyValue_EqualKeys(const PARCKeyValue *keyValue1, const PARCKeyValue *keyValue2);

/**
 * Check for element equality.  Return true if both key & value are equal.
 *
 * @param [in] keyValue1 A pointer to the first `PARCKeyValue`
 * @param [in] keyValue2 A pointer to the second `PARCKeyValue`
 * @return true if the keys & values both have the same value
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv1 = parcKeyValue_Create(key1, value);
 *      PARCKeyValue *kv2 = parcKeyValue_Create(key2, value);
 *      ...
 *      if (parcKeyValue_Equals(kv1, kv2)) {
 *         ...
 *      }
 *      ...
 *      parcKeyValue_Release(&kv1);
 *      parcKeyValue_Release(&kv2);
 * @endcode
 */
bool parcKeyValue_Equals(const PARCKeyValue *keyValue1, const PARCKeyValue *keyValue2);

/**
 * Compare PARCKeyValue elements.  Return an int result based on key compare only.
 *
 * @param [in] keyValue1 A pointer to the first `PARCKeyValue`
 * @param [in] keyValue2 A pointer to the second `PARCKeyValue`
 * @return parcObject_Compare(keyValue1->key, keyValue2->key)
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *kv1 = parcKeyValue_Create(key1, value);
 *      PARCKeyValue *kv2 = parcKeyValue_Create(key2, value);
 *      ...
 *      if (parcKeyValue_Compare(kv1, kv2) > 0) {
 *         ...
 *      }
 *      ...
 *      parcKeyValue_Release(&kv1);
 *      parcKeyValue_Release(&kv2);
 * @endcode
 */
int parcKeyValue_Compare(const PARCKeyValue *keyValue1, const PARCKeyValue *keyValue2);

/**
 * Return the HashCode of the PARCKeyValue key.
 *
 * @param [in] keyValue A pointer to the first `PARCKeyValue`
 * @return parcObject_HashCode(keyValue->key);
 *
 * Example:
 * @code
 *      ...
 *      PARCKeyValue *keyValue = parcKeyValue_Create(key1, value);
 *      ...
 *      PARCHashCode *hashCode = parcKeyValue_HashCode(keyValue);
 *      ...
 *      parcKeyValue_Release(&keyValue);
 * @endcode
 */
PARCHashCode parcKeyValue_HashCode(const PARCKeyValue *keyValue);
#endif // libparc_parc_KeyValue_h