aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_KeyValue.c
blob: 27b20f3522548db7fae6bde0f2aa1265838e13c3 (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
/*
 * 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.
 */

/**
 *
 */

#include <config.h>

#include <string.h>

#include "parc_KeyValue.h"
#include "parc_Object.h"

#include <LongBow/runtime.h>

struct parc_key_value {
    PARCObject *key;
    PARCObject *value;
};

static void
_parcKeyValue_Destroy(PARCKeyValue **keyValuePointer)
{
    PARCKeyValue *keyValue = *keyValuePointer;
    parcObject_Release(&keyValue->key);
    if (keyValue->value != NULL) {
        parcObject_Release(&keyValue->value);
    }
}

parcObject_ExtendPARCObject(PARCKeyValue, _parcKeyValue_Destroy, parcKeyValue_Copy, NULL, parcKeyValue_Equals,
                            parcKeyValue_Compare, parcKeyValue_HashCode, NULL);

parcObject_ImplementAcquire(parcKeyValue, PARCKeyValue);

parcObject_ImplementRelease(parcKeyValue, PARCKeyValue);

PARCKeyValue *
parcKeyValue_Create(const PARCObject *key,
                    const PARCObject *value)
{
    assertNotNull(key, "Key may not be null in a KeyValue element");

    PARCKeyValue *keyValue = parcObject_CreateInstance(PARCKeyValue);
    assertNotNull(keyValue, "parcMemory_Allocate(%zu) returned NULL", sizeof(PARCKeyValue));

    keyValue->key = parcObject_Acquire(key);
    keyValue->value = NULL;
    if (value != NULL) {
        keyValue->value = parcObject_Acquire(value);
    }

    return keyValue;
}

PARCKeyValue *
parcKeyValue_Copy(const PARCKeyValue *source)
{
    PARCKeyValue *newKV = parcObject_CreateInstance(PARCKeyValue);
    newKV->key = parcObject_Copy(source->key);
    newKV->value = NULL;
    if (source->value != NULL) {
        newKV->value = parcObject_Copy(source->value);
    }

    return newKV;
}

void
parcKeyValue_SetValue(PARCKeyValue *keyValue, PARCObject *value)
{
    assertNotNull(keyValue, "Not a valid keyValue");
    PARCObject *oldValue = keyValue->value;
    if (value != NULL) {
        keyValue->value = parcObject_Acquire(value);
    } else {
        keyValue->value = NULL;
    }
    if (oldValue != NULL) {
        parcObject_Release(&oldValue);
    }
}

void
parcKeyValue_SetKey(PARCKeyValue *keyValue, PARCObject *key)
{
    assertNotNull(keyValue, "Not a valid keyValue");
    PARCObject *oldKey = keyValue->key;
    keyValue->key = parcObject_Acquire(key);
    parcObject_Release(&oldKey);
}

PARCObject *
parcKeyValue_GetValue(PARCKeyValue *keyValue)
{
    assertNotNull(keyValue, "Not a valid keyValue");
    return keyValue->value;
}

PARCObject *
parcKeyValue_GetKey(PARCKeyValue *keyValue)
{
    assertNotNull(keyValue, "Not a valid keyValue");
    return keyValue->key;
}

bool
parcKeyValue_Equals(const PARCKeyValue *a, const PARCKeyValue *b)
{
    bool result = parcObject_Equals(a->key, b->key);
    if ((a->value == NULL) || (b->value == NULL)) {
        result &= (a->value == b->value); // Only true if both are NULL
    } else {
        result &= parcObject_Equals(a->value, b->value);
    }
    return result;
}

int
parcKeyValue_Compare(const PARCKeyValue *a, const PARCKeyValue *b)
{
    if (a == NULL && b == NULL) {
        return 0;
    }
    if (a != NULL && b == NULL) {
        return 1;
    }
    if (a == NULL && b != NULL) {
        return -1;
    } else {
        return parcObject_Compare(a->key, b->key);
    }
}

PARCHashCode
parcKeyValue_HashCode(const PARCKeyValue *keyValue)
{
    return parcObject_HashCode(keyValue->key);
}

bool
parcKeyValue_EqualKeys(const PARCKeyValue *a, const PARCKeyValue *b)
{
    return parcObject_Equals(a->key, b->key);
}