File: csound_data_structures.c

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 63,220 kB
  • sloc: ansic: 192,643; cpp: 14,149; javascript: 9,654; objc: 9,181; python: 3,376; java: 3,337; sh: 1,840; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 128
file content (417 lines) | stat: -rw-r--r-- 10,551 bytes parent folder | download | duplicates (3)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 csound_data_structures.c:

 Copyright (C) 2013 Steven Yi

 This file is part of Csound.

 The Csound Library is free software; you can redistribute it
 and/or modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 Csound is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with Csound; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 02110-1301 USA
 */
#include "csoundCore.h"
#include "csound_data_structures.h"

#define HASH_LOAD_FACTOR 0.75

char* cs_hash_table_put_no_key_copy(CSOUND* csound,
    CS_HASH_TABLE* hashTable,
    char* key, void* value);

#ifdef __cplusplus
extern "C" {
#endif

/* FUNCTIONS FOR CONS_CELL */

PUBLIC CONS_CELL* cs_cons(CSOUND* csound, void* val, CONS_CELL* cons) {
    CONS_CELL* cell = csound->Malloc(csound, sizeof(CONS_CELL));
    cell->value = val;
    cell->next = cons;

    return cell;
}

PUBLIC CONS_CELL* cs_cons_append(CONS_CELL* cons1, CONS_CELL* cons2) {
    if (cons1 == NULL) return cons2;
    if (cons2 == NULL) return cons1;

    CONS_CELL* c = cons1;

    while (c->next != NULL) c = c->next;

    c->next = cons2;

    return cons1;
}

PUBLIC int cs_cons_length(CONS_CELL* head) {
    CONS_CELL* current = head;
    int count = 0;
    while (current != NULL) {
      count++;
      current = current->next;
    }
    return count;
}

PUBLIC void cs_cons_free(CSOUND* csound, CONS_CELL* head) {
    CONS_CELL *current, *next;

    if (head == NULL) return;

    current = head;

    while (current != NULL) {
      next = current->next;
      csound->Free(csound, current);
      current = next;
    }
}


PUBLIC void cs_cons_free_complete(CSOUND* csound, CONS_CELL* head) {

    CONS_CELL *current, *next;

    if (head == NULL) return;

    current = head;

    while (current != NULL) {
      next = current->next;
      csound->Free(csound, current->value);
      csound->Free(csound, current);
      current = next;
    }
}

/* FUNCTION FOR HASH SET */

PUBLIC CS_HASH_TABLE* cs_hash_table_create(CSOUND* csound) {
    CS_HASH_TABLE* table =
      (CS_HASH_TABLE*) csound->Calloc(csound, sizeof(CS_HASH_TABLE));
    table->count = 0;
    table->table_size = 8192;
    table->buckets = csound->Calloc(csound, sizeof(CS_HASH_TABLE_ITEM*) * 8192);

    return table;
}

static int cs_hash_table_check_resize(CSOUND* csound, CS_HASH_TABLE* table) {
    if (table->count + 1 > table->table_size * HASH_LOAD_FACTOR) {
        int oldSize = table->table_size;
        int newSize = oldSize * 2;
        CS_HASH_TABLE_ITEM** oldTable = table->buckets;
        CS_HASH_TABLE_ITEM** newTable =
          csound->Calloc(csound, newSize * sizeof(CS_HASH_TABLE_ITEM*));

        table->buckets = newTable;
        table->table_size = newSize;

        for (int i = 0; i < oldSize; i++) {
            CS_HASH_TABLE_ITEM* item = oldTable[i];
            while (item != NULL) {
                cs_hash_table_put_no_key_copy(csound, table, item->key,
                                              item->value);
                item->key = NULL;
                CS_HASH_TABLE_ITEM* next = item->next;
                csound->Free(csound, item);
                item = next;
            }
        }
        return 1;
    }
    return 0;
}

static unsigned int cs_name_hash(CS_HASH_TABLE* table, char *s)
{
    unsigned int h = 0;
    while (*s != '\0') {
      h = (h<<4) ^ *s++;
    }
    return (h % table->table_size);
}

PUBLIC void* cs_hash_table_get(CSOUND* csound,
                               CS_HASH_TABLE* hashTable, char* key) {
    IGN(csound);
    unsigned int index;
    CS_HASH_TABLE_ITEM* item;

    if (key == NULL) {
      return NULL;
    }

    index = cs_name_hash(hashTable, key);
    item = hashTable->buckets[index];
    while (item != NULL) {
      if (strcmp(key, item->key) == 0) {
        return item->value;
      }
      item = item->next;
    }

    return NULL;
}

PUBLIC char* cs_hash_table_get_key(CSOUND* csound,
                                   CS_HASH_TABLE* hashTable, char* key) {
    unsigned int index;
    CS_HASH_TABLE_ITEM* item;
    IGN(csound);

    if (key == NULL) {
      return NULL;
    }

    index = cs_name_hash(hashTable, key);
    item = hashTable->buckets[index];

    while (item != NULL) {
      if (strcmp(key, item->key) == 0) {
        return item->key;
      }
      item = item->next;
    }

    return NULL;
}

/*
 * If item exists, replace.
 * Else, check for resize, then do insert.
*/
char* cs_hash_table_put_no_key_copy(CSOUND* csound,
                                   CS_HASH_TABLE* hashTable,
                                    char* key, void* value) {
    if (key == NULL) {
      return NULL;
    }

    unsigned int index = cs_name_hash(hashTable, key);

    CS_HASH_TABLE_ITEM* item = hashTable->buckets[index];

    while (item != NULL) {
        if (strcmp(key, item->key) == 0) {
            item->value = value;
            return item->key;
        }
        item = item->next;
    }

    int modified = cs_hash_table_check_resize(csound, hashTable);

    if (modified) {
        index = cs_name_hash(hashTable, key);
    }
    CS_HASH_TABLE_ITEM* newItem = csound->Malloc(csound,
                                                   sizeof(CS_HASH_TABLE_ITEM));
    newItem->key = key;
    newItem->value = value;
    newItem->next = NULL;

    item = hashTable->buckets[index];
    hashTable->count++;

    if (item) {
        while (item->next != NULL) {
            item = item->next;
        }

        item->next = newItem;
    } else {
        hashTable->buckets[index] = newItem;
    }

    return key;
}

PUBLIC void cs_hash_table_put(CSOUND* csound,
                              CS_HASH_TABLE* hashTable, char* key, void* value) {
    cs_hash_table_put_no_key_copy(csound, hashTable,
                                  cs_strdup(csound, key), value);
}

PUBLIC char* cs_hash_table_put_key(CSOUND* csound,
                                   CS_HASH_TABLE* hashTable, char* key) {
    return cs_hash_table_put_no_key_copy(csound, hashTable,
                                         cs_strdup(csound, key), NULL);
}

PUBLIC void cs_hash_table_remove(CSOUND* csound,
                                 CS_HASH_TABLE* hashTable, char* key) {
    CS_HASH_TABLE_ITEM *previous, *item;
    unsigned int index;

    if (key == NULL) {
      return;
    }

    index = cs_name_hash(hashTable, key);

    previous = NULL;
    item = hashTable->buckets[index];

    while (item != NULL) {
      if (strcmp(key, item->key) == 0) {
        if (previous == NULL) {
          hashTable->buckets[index] = item->next;
        } else {
          previous->next = item->next;
        }
        csound->Free(csound, item);
        hashTable->count--;
        return;
      }
      previous = item;
      item = item->next;
    }
}

PUBLIC CONS_CELL* cs_hash_table_keys(CSOUND* csound, CS_HASH_TABLE* hashTable) {
    CONS_CELL* head = NULL;

    int i = 0;

    for (i = 0; i < hashTable->table_size; i++) {
      CS_HASH_TABLE_ITEM* item = hashTable->buckets[i];

      while (item != NULL) {
        head = cs_cons(csound, item->key, head);
        item = item->next;
      }
    }
    return head;
}

PUBLIC CONS_CELL* cs_hash_table_values(CSOUND* csound, CS_HASH_TABLE* hashTable) {
    CONS_CELL* head = NULL;

    int i = 0;

    for (i = 0; i < hashTable->table_size; i++) {
      CS_HASH_TABLE_ITEM* item = hashTable->buckets[i];

      while (item != NULL) {
        head = cs_cons(csound, item->value, head);
        item = item->next;
      }
    }
    return head;
}

PUBLIC void cs_hash_table_merge(CSOUND* csound,
                                CS_HASH_TABLE* target, CS_HASH_TABLE* source) {
    // TODO - check if this is the best strategy for merging
    int i = 0;

    for (i = 0; i < source->table_size; i++) {
      CS_HASH_TABLE_ITEM* item = source->buckets[i];

      while (item != NULL) {
        CS_HASH_TABLE_ITEM* next = item->next;
        char* new_key =
          cs_hash_table_put_no_key_copy(csound, target, item->key, item->value);

        if (new_key != item->key) {
          csound->Free(csound, item->key);
        }

        csound->Free(csound, item);
        item = next;
      }
      source->buckets[i] = NULL;
    }

}

PUBLIC void cs_hash_table_free(CSOUND* csound, CS_HASH_TABLE* hashTable) {
    int i;

    for (i = 0; i < hashTable->table_size; i++) {
      CS_HASH_TABLE_ITEM* item = hashTable->buckets[i];

      while(item != NULL) {
        CS_HASH_TABLE_ITEM* next = item->next;
        csound->Free(csound, item->key);
        csound->Free(csound, item);
        item = next;
      }
    }
    csound->Free(csound, hashTable);
}

PUBLIC void cs_hash_table_mfree_complete(CSOUND* csound, CS_HASH_TABLE* hashTable) {

    int i;

    for (i = 0; i < hashTable->table_size; i++) {
      CS_HASH_TABLE_ITEM* item = hashTable->buckets[i];

      while(item != NULL) {
        CS_HASH_TABLE_ITEM* next = item->next;
        csound->Free(csound, item->key);
        csound->Free(csound, item->value);
        csound->Free(csound, item);
        item = next;
      }
    }
    csound->Free(csound, hashTable);
}

PUBLIC void cs_hash_table_free_complete(CSOUND* csound, CS_HASH_TABLE* hashTable) {

    int i;

    for (i = 0; i < hashTable->table_size; i++) {
      CS_HASH_TABLE_ITEM* item = hashTable->buckets[i];

      while(item != NULL) {
        CS_HASH_TABLE_ITEM* next = item->next;
        csound->Free(csound, item->key);

        /* NOTE: This needs to be free, not csound->Free.
           To use mfree on keys, use cs_hash_table_mfree_complete
           TODO: Check if this is even necessary anymore... */
        free(item->value);
        csound->Free(csound, item);
        item = next;
      }
    }
    csound->Free(csound, hashTable);
}

char *cs_inverse_hash_get(CSOUND* csound, CS_HASH_TABLE* hashTable, int n)
{
    int k;
    IGN(csound);
    for (k=0; k<hashTable->table_size;k++) {
      CS_HASH_TABLE_ITEM* item = hashTable->buckets[k];
      while (item) {
        if (n==*(int*)item->value) return item->key;
        item = item->next;
      }
    }
    return "";
}




#ifdef __cplusplus
  extern "C" {
#endif