Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/collection.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2018 Davide Madrisan <[email protected]>
5 *
6 * A simple dictionary for counting hashable objects (strings)
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "collection.h"
25 #include "logging.h"
26 #include "string-macros.h"
27 #include "xalloc.h"
28
29 #define HASHSIZE 101
30
31 /* hash: form hash value for string s */
32
33 static unsigned
34 hash (const char *s)
35 {
36 unsigned hashval;
37
38 for (hashval = 0; *s != '\0'; s++)
39 hashval = *s + 31 * hashval;
40
41 return hashval % HASHSIZE;
42 }
43
44 /* initialize the hash pointer table */
45
46 hashtable_t *
47 counter_create (void)
48 {
49 hashtable_t *hashtable = xmalloc (sizeof (hashtable_t));
50 hashable_t **table = xnmalloc (HASHSIZE, sizeof (hashable_t *));
51
52 hashtable->capacity = HASHSIZE;
53 hashtable->elements = hashtable->uniq = 0;
54 hashtable->table = table;
55 hashtable->keys = xmalloc (sizeof (*(hashtable->keys)));
56
57 return hashtable;
58 }
59
60 /* lookup: look for a key in hash table */
61
62 hashable_t *
63 counter_lookup (const hashtable_t * hashtable, const char *key)
64 {
65 hashable_t *np;
66
67 dbg ("hashtable lookup for key \"%s\"\n", key);
68 for (np = hashtable->table[hash (key)]; np != NULL; np = np->next)
69 if (STREQ (key, np->key))
70 {
71 dbg ("hashtable lookup: found key \"%s\"\n", key);
72 return np;
73 }
74
75 dbg ("hashtable lookup: key \"%s\" not found\n", key);
76 return NULL;
77 }
78
79 /* Insert the element 'key' into the hash table.
80 * Set count to 'increment' if 'key' was not present in the table or
81 * increment the counter by 'increment' otherwise. */
82
83 hashable_t *
84 counter_put (hashtable_t * hashtable, const char *key, unsigned long increment)
85 {
86 hashable_t *np;
87
88 if ((np = counter_lookup (hashtable, key)) == NULL)
89 { /* not found */
90 dbg ("hashtable: adding a new key \"%s\"\n", key);
91 np = xmalloc (sizeof (*np));
92 np->key = xstrdup (key);
93
94 unsigned int hashval = hash (key);
95 np->count = increment;
96 np->next = hashtable->table[hashval];
97 hashtable->table[hashval] = np;
98
99 unsigned long new_size =
100 sizeof (*(hashtable->keys)) * (hashtable->uniq + 1);
101 hashtable->keys = xrealloc (hashtable->keys, new_size);
102 dbg ("xrealloc'ed hashtable->keys to %lu bytes\n", new_size);
103
104 (hashtable->keys)[hashtable->uniq] = np->key;
105 dbg ("(hashtable->keys)[%u] = \"%s\"\n", hashtable->uniq,
106 (hashtable->keys)[hashtable->uniq]);
107
108 hashtable->uniq++;
109 }
110 else
111 {
112 /* already there */
113 dbg ("hashtable: the key \"%s\" is already present, "
114 "counter incremented: %lu\n", key, np->count);
115 np->count += increment;
116 }
117
118 hashtable->elements++;
119
120 return np;
121 }
122
123 /* Return the number of elements stored in the hash table */
124
125 unsigned int
126 counter_get_elements (const hashtable_t * hashtable)
127 {
128 return hashtable->elements;
129 }
130
131 /* Return the number if unique elements stored in the hash table */
132
133 unsigned int
134 counter_get_unique_elements (const hashtable_t * hashtable)
135 {
136 return hashtable->uniq;
137 }
138
139 /* Return an array containing the pointers to all the keys stored
140 in the hash table */
141
142 char **
143 counter_keys (hashtable_t * hashtable)
144 {
145 return (hashtable->uniq < 1) ? NULL : hashtable->keys;
146 }
147
148 void
149 counter_free (hashtable_t * hashtable)
150 {
151 hashable_t *np, *np2;
152
153 for (int i = 0; i < HASHSIZE; i++)
154 {
155 np = hashtable->table[i];
156 while (np != NULL)
157 {
158 np2 = np;
159 np = np->next;
160 free (np2);
161 }
162 }
163 free (hashtable->table);
164 free (hashtable->keys);
165 free (hashtable);
166 }