eset.c 2.15 KB
Newer Older
Götz Lindenmaier's avatar
Götz Lindenmaier committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
Götz Lindenmaier's avatar
Götz Lindenmaier committed
18
 */
Götz Lindenmaier's avatar
Götz Lindenmaier committed
19

20
21
22
23
24
25
/**
 * @file
 * @brief       A pointer hash-set (WARNING: deprecated!)
 * @author      Hubert Schmid
 * @date        09.06.2002
 */
Matthias Braun's avatar
Matthias Braun committed
26
#include "config.h"
27

Götz Lindenmaier's avatar
Götz Lindenmaier committed
28
29
#include "eset.h"
#include "set.h"
30
#include "hashptr.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
31
32
33
34
35
36

struct eset {
  int dummy; /* dummy entry */
};


37
#define INITIAL_SLOTS 64
Götz Lindenmaier's avatar
Götz Lindenmaier committed
38

39
40
static int pcmp(const void *p1, const void *p2, size_t size)
{
41
42
  const void **q1 = (const void **)p1;
  const void **q2 = (const void **)p2;
Matthias Braun's avatar
Matthias Braun committed
43
  (void) size;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
44

45
  return *q1 != *q2;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
46
47
48
}


49
50
eset * eset_create(void)
{
51
  return (eset *) new_set(pcmp, INITIAL_SLOTS);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
52
53
54
}


55
56
eset * eset_copy(eset *source)
{
Götz Lindenmaier's avatar
Götz Lindenmaier committed
57
58
59
60
61
62
63
64
65
  eset * ret = eset_create();
  void * p;
  for (p = eset_first(source); p; p = eset_next(source)) {
    eset_insert(ret, p);
  }
  return ret;
}


66
67
void eset_destroy(eset *s)
{
68
  del_set((set *)s);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
69
70
}

Michael Beck's avatar
Michael Beck committed
71
/* Returns the number of elements in the set. */
72
size_t eset_count(eset *s)
73
{
Michael Beck's avatar
Michael Beck committed
74
75
  return set_count((set *)s);
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
76

77
78
void eset_insert(eset *s, void *p)
{
Götz Lindenmaier's avatar
Götz Lindenmaier committed
79
  if (!eset_contains(s, p)) {
80
    set_insert((set *)s, &p, sizeof(p), hash_ptr(p));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
81
82
83
84
  }
}


85
86
int eset_contains(eset *s, void *p)
{
87
  return set_find((set *)s, &p, sizeof(p), hash_ptr(p)) != NULL;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
88
89
90
}


91
92
void * eset_first(eset *s)
{
Götz Lindenmaier's avatar
Götz Lindenmaier committed
93
  void * p = set_first((set *) s);
94
  return p == NULL ? NULL : *((void **)p);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
95
96
97
}


98
99
void * eset_next(eset *s)
{
100
101
  void *p = set_next((set *) s);
  return p == NULL ? NULL : *((void **)p);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
102
103
104
}


105
106
void eset_insert_all(eset *target, eset *source)
{
107
  void *p;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
108
109
110
111
  for (p = eset_first(source); p; p = eset_next(source)) {
    eset_insert(target, p);
  }
}