]> cygwin.com Git - cygwin-apps/setup.git/blob - hash.cc
2003-06-22 Max Bowsher <maxb@ukf.net>
[cygwin-apps/setup.git] / hash.cc
1 /*
2 * Copyright (c) 2000, Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
16 /* Simple hash class for install.cc */
17
18 #if 0
19 static const char *cvsid =
20 "\n%%% $Id$\n";
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "hash.h"
27
28 class hash_internals
29 {
30 public:
31 char **keys;
32 int numkeys;
33 int maxkeys;
34 int prev_index;
35 };
36
37 hash::hash ()
38 {
39 h = new hash_internals;
40 h->numkeys = 0;
41 h->maxkeys = 10;
42 h->keys = (char **) malloc (h->maxkeys * sizeof (char *));
43 h->prev_index = 0;
44 }
45
46 hash::~hash ()
47 {
48 free (h->keys);
49 delete h;
50 }
51
52
53 void
54 hash::add (char const *string)
55 {
56 int i;
57 for (i = 0; i < h->numkeys; i++)
58 if (strcmp (h->keys[i], string) == 0)
59 return;
60 if (h->numkeys >= h->maxkeys)
61 {
62 h->maxkeys += 10;
63 h->keys = (char **) realloc (h->keys, h->maxkeys * sizeof (char *));
64 }
65
66 h->keys[h->numkeys] = new char[strlen(string) + 1];
67 strcpy (h->keys[h->numkeys], string);
68 h->numkeys++;
69 }
70
71
72 int
73 hash::has (char const *string)
74 {
75 int i;
76 for (i = 0; i < h->numkeys; i++)
77 if (strcmp (h->keys[i], string) == 0)
78 return 1;
79 return 0;
80 }
81
82 char *
83 hash::enumerate (char const *prev)
84 {
85 if (prev == 0)
86 h->prev_index = -1;
87 h->prev_index++;
88 if (h->prev_index >= h->numkeys)
89 return 0;
90 return h->keys[h->prev_index];
91 }
92
93 static int
94 rev_len (const void *va, const void *vb)
95 {
96 char *a = *(char **) va;
97 char *b = *(char **) vb;
98 return strcmp (b, a);
99 }
100
101 void
102 hash::reverse_sort ()
103 {
104 qsort (h->keys, h->numkeys, sizeof (h->keys[0]), rev_len);
105 }
This page took 0.039276 seconds and 5 git commands to generate.