]> cygwin.com Git - cygwin-apps/setup.git/blame - hash.cc
2002-05-19 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / hash.cc
CommitLineData
4a83b7b0
DD
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
b24c88b3
RC
18#if 0
19static const char *cvsid =
20 "\n%%% $Id$\n";
21#endif
946fc045 22
4a83b7b0
DD
23#include <stdlib.h>
24#include <string.h>
25
26#include "hash.h"
27
b24c88b3
RC
28class hash_internals
29{
4a83b7b0
DD
30public:
31 char **keys;
32 int numkeys;
33 int maxkeys;
34 int prev_index;
35};
36
37hash::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
46hash::~hash ()
47{
48 free (h->keys);
5e0464a1 49 delete h;
4a83b7b0
DD
50}
51
52
53void
fa0c0d10 54hash::add (char const *string)
4a83b7b0
DD
55{
56 int i;
b24c88b3 57 for (i = 0; i < h->numkeys; i++)
4a83b7b0
DD
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
5e0464a1
RC
66 h->keys[h->numkeys] = new char[strlen(string) + 1];
67 strcpy (h->keys[h->numkeys], string);
b24c88b3 68 h->numkeys++;
4a83b7b0
DD
69}
70
71
72int
fa0c0d10 73hash::has (char const *string)
4a83b7b0
DD
74{
75 int i;
b24c88b3 76 for (i = 0; i < h->numkeys; i++)
4a83b7b0
DD
77 if (strcmp (h->keys[i], string) == 0)
78 return 1;
79 return 0;
80}
81
82char *
fa0c0d10 83hash::enumerate (char const *prev)
4a83b7b0
DD
84{
85 if (prev == 0)
86 h->prev_index = -1;
b24c88b3 87 h->prev_index++;
4a83b7b0
DD
88 if (h->prev_index >= h->numkeys)
89 return 0;
90 return h->keys[h->prev_index];
91}
92
93static int
94rev_len (const void *va, const void *vb)
95{
b24c88b3
RC
96 char *a = *(char **) va;
97 char *b = *(char **) vb;
4a83b7b0
DD
98 return strcmp (b, a);
99}
100
101void
102hash::reverse_sort ()
103{
104 qsort (h->keys, h->numkeys, sizeof (h->keys[0]), rev_len);
105}
This page took 0.037181 seconds and 5 git commands to generate.