aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_BufferDictionary.c
blob: 56ed800a346bf336fe58a6e8bc977684cca7c2c3 (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
/*
 * 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.
 */

/**
 * The dictionary is implemented with the parc_HashCodeTable backend.  This implementation
 * is inefficient for additions with duplicate keys, because the semantics of parc_HashCodeTable
 * are not the same as parc_BufferDictionary in returning values for Put and Remove.
 *
 */
#include <config.h>

#include <LongBow/runtime.h>

#include <parc/algol/parc_BufferDictionary.h>

#include <parc/algol/parc_Object.h>
#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_HashCodeTable.h>

struct parc_buffer_dictionary {
    PARCHashCodeTable *hashtable;
};

// Wrapper to go from void * to PARCBuffer *
static bool
_parcBufferEquals(const void *a, const void *b)
{
    return parcBuffer_Equals((const PARCBuffer *) a, (const PARCBuffer *) b);
}

// Wrapper to go from void * to PARCBuffer *
static PARCHashCode
_parcBufferHashCode(const void *a)
{
    return parcBuffer_HashCode((const PARCBuffer *) a);
}

// Wrapper to go from void ** to PARCBuffer **
static void
_parcBufferRelease(void **bufferVoidPtr)
{
    parcBuffer_Release((PARCBuffer **) bufferVoidPtr);
}

/*
 * Initialise a parcBufferDictionary instance.
 * @return The same pointer as `result`.
 */
static PARCBufferDictionary *
_init(PARCBufferDictionary *result)
{
    result->hashtable = parcHashCodeTable_Create(_parcBufferEquals, _parcBufferHashCode, _parcBufferRelease, _parcBufferRelease);
    return result;
}

/**
 * Cleans up the internal memory of a PARCBufferDictionary
 *
 * @param [in] dictionaryPtr Double pointer to the dictionary to finalize
 */
static void
_destroy(PARCBufferDictionary **dictionaryPtr)
{
    assertNotNull(dictionaryPtr, "Double pointer dictionaryPtr must be non-NULL");
    assertNotNull(*dictionaryPtr, "Double pointer dictionaryPtr must dereference to non-NULL");

    PARCBufferDictionary *dict = *dictionaryPtr;

    parcHashCodeTable_Destroy(&dict->hashtable);
}


parcObject_ExtendPARCObject(PARCBufferDictionary, _destroy, NULL, NULL, NULL, NULL, NULL, NULL);

PARCBufferDictionary *
parcBufferDictionary_Create(void)
{
    PARCBufferDictionary *result = parcObject_CreateInstance(PARCBufferDictionary);
    if (result != NULL) {
        return _init(result);
    }

    return NULL;
}

parcObject_ImplementAcquire(parcBufferDictionary, PARCBufferDictionary);

parcObject_ImplementRelease(parcBufferDictionary, PARCBufferDictionary);

PARCBuffer *
parcBufferDictionary_Put(PARCBufferDictionary *dictionary, PARCBuffer *key, PARCBuffer *value)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-NULL");
    assertNotNull(key, "Parameter key must be non-NULL");
    assertNotNull(value, "Parameter value must be non-NULL");

    PARCBuffer *oldValue = NULL;

    // We use reference counted copyies of the supplied parameters
    PARCBuffer *key_copy = parcBuffer_Acquire(key);
    PARCBuffer *value_copy = parcBuffer_Acquire(value);

    if (!parcHashCodeTable_Add(dictionary->hashtable, key_copy, value_copy)) {
        // parcHashCodeTable_Del will free the referece, to make a copy of it
        PARCBuffer *original = (PARCBuffer *) parcHashCodeTable_Get(dictionary->hashtable, key_copy);
        oldValue = parcBuffer_Acquire(original);
        parcHashCodeTable_Del(dictionary->hashtable, key_copy);
        parcHashCodeTable_Add(dictionary->hashtable, key_copy, value_copy);
    }

    return oldValue;
}

PARCBuffer *
parcBufferDictionary_Get(PARCBufferDictionary *dictionary, PARCBuffer *key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-NULL");
    assertNotNull(key, "Parameter key must be non-NULL");

    return parcHashCodeTable_Get(dictionary->hashtable, key);
}

PARCBuffer *
parcBufferDictionary_Remove(PARCBufferDictionary *dictionary, PARCBuffer *key)
{
    assertNotNull(dictionary, "Parameter dictionary must be non-NULL");
    assertNotNull(key, "Parameter key must be non-NULL");

    // parcHashCodeTable_Del will free the referece, to make a copy of it
    PARCBuffer *original = (PARCBuffer *) parcHashCodeTable_Get(dictionary->hashtable, key);
    PARCBuffer *oldValue = parcBuffer_Acquire(original);
    parcHashCodeTable_Del(dictionary->hashtable, key);
    return oldValue;
}