summaryrefslogtreecommitdiffstats
path: root/src/util/t_bimap.pm
blob: 111c24964e2cd66f9d400b51f5d75187f91956b8 (plain)
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
package t_bimap;

use strict;
use vars qw(@ISA);

require t_template;
require t_array;

@ISA=qw(t_template);

my @parms = qw(NAME LEFT RIGHT LEFTCMP RIGHTCMP LEFTPRINT RIGHTPRINT);
my %defaults = ();
my $headertemplate = "/*
 * bidirectional mapping table, add-only
 *
 * Parameters:
 * NAME
 * LEFT, RIGHT - types
 * LEFTCMP, RIGHTCMP - comparison functions
 *
 * Methods:
 * int init() - nonzero is error code, if any possible
 * long size()
 * void foreach(int (*)(LEFT, RIGHT, void*), void*)
 * int add(LEFT, RIGHT) - 0 = success, -1 = allocation failure
 * const <RIGHT> *findleft(<LEFT>) - null iff not found
 * const <LEFT> *findright(<RIGHT>)
 * void destroy() - destroys container, doesn't delete elements
 *
 * initial implementation: flat array of (left,right) pairs
 */

struct <NAME>__pair {
    <LEFT> l;
    <RIGHT> r;
};
";
my $bodytemplate = join "", <DATA>;

sub new { # no args
    my $self = {};
    bless $self;
    $self->init(\@parms, \%defaults, []);
    return $self;
}

sub output {
    my ($self, $fh) = @_;

    my $a = new t_array;
    $a->setparm("NAME", $self->{values}{"NAME"} . "__pairarray");
    $a->setparm("TYPE", "struct " . $self->{values}{"NAME"} . "__pair");

    print $fh "/* start of ", ref($self), " header template */\n";
    print $fh $self->substitute($headertemplate);
    print $fh "/* end of ", ref($self), " header template */\n";
    $a->output($fh);
    print $fh "/* start of ", ref($self), " body template */\n";
    print $fh $self->substitute($bodytemplate);
    print $fh "/* end of ", ref($self), " body template */\n";
}

1;

__DATA__

/* for use in cases where text substitutions may not work, like putting
   "const" before a type that turns out to be "char *"  */
typedef <LEFT> <NAME>__left_t;
typedef <RIGHT> <NAME>__right_t;

typedef struct {
    <NAME>__pairarray a;
    long nextidx;
} <NAME>;

static inline int
<NAME>_init (<NAME> *m)
{
    m->nextidx = 0;
    return <NAME>__pairarray_init (&m->a);
}

static inline long
<NAME>_size (<NAME> *m)
{
    return <NAME>__pairarray_size (&m->a);
}

static inline void
<NAME>_foreach (<NAME> *m, int (*fn)(<LEFT>, <RIGHT>, void *), void *p)
{
    long i, sz;
    sz = m->nextidx;
    for (i = 0; i < sz; i++) {
	struct <NAME>__pair *pair;
	pair = <NAME>__pairarray_getaddr (&m->a, i);
	if ((*fn)(pair->l, pair->r, p) != 0)
	    break;
    }
}

static inline int
<NAME>_add (<NAME> *m, <LEFT> l, <RIGHT> r)
{
    long i, sz;
    struct <NAME>__pair newpair;
    int err;

    sz = m->nextidx;
    /* Make sure we're not duplicating.  */
    for (i = 0; i < sz; i++) {
	struct <NAME>__pair *pair;
	pair = <NAME>__pairarray_getaddr (&m->a, i);
	assert ((*<LEFTCMP>)(l, pair->l) != 0);
	if ((*<LEFTCMP>)(l, pair->l) == 0)
	    abort();
	assert ((*<RIGHTCMP>)(r, pair->r) != 0);
	if ((*<RIGHTCMP>)(r, pair->r) == 0)
	    abort();
    }
    newpair.l = l;
    newpair.r = r;
    if (sz >= LONG_MAX - 1)
	return ENOMEM;
    err = <NAME>__pairarray_grow (&m->a, sz+1);
    if (err)
	return err;
    <NAME>__pairarray_set (&m->a, sz, newpair);
    m->nextidx++;
    return 0;
}

static inline const <NAME>__right_t *
<NAME>_findleft (<NAME> *m, <LEFT> l)
{
    long i, sz;
    sz = <NAME>_size (m);
    for (i = 0; i < sz; i++) {
	struct <NAME>__pair *pair;
	pair = <NAME>__pairarray_getaddr (&m->a, i);
	if ((*<LEFTCMP>)(l, pair->l) == 0)
	    return &pair->r;
    }
    return 0;
}

static inline const <NAME>__left_t *
<NAME>_findright (<NAME> *m, <RIGHT> r)
{
    long i, sz;
    sz = <NAME>_size (m);
    for (i = 0; i < sz; i++) {
	struct <NAME>__pair *pair;
	pair = <NAME>__pairarray_getaddr (&m->a, i);
	if ((*<RIGHTCMP>)(r, pair->r) == 0)
	    return &pair->l;
    }
    return 0;
}

struct <NAME>__printstat {
    FILE *f;
    int comma;
};
static inline int
<NAME>__printone (<LEFT> l, <RIGHT> r, void *p)
{
    struct <NAME>__printstat *ps = p;
    fprintf(ps->f, ps->comma ? ", (" : "(");
    ps->comma = 1;
    (*<LEFTPRINT>)(l, ps->f);
    fprintf(ps->f, ",");
    (*<RIGHTPRINT>)(r, ps->f);
    fprintf(ps->f, ")");
    return 0;
}

static inline void
<NAME>_printmap (<NAME> *m, FILE *f)
{
    struct <NAME>__printstat ps;
    ps.comma = 0;
    ps.f = f;
    fprintf(f, "(");
    <NAME>_foreach (m, <NAME>__printone, &ps);
    fprintf(f, ")");
}

static inline void
<NAME>_destroy (<NAME> *m)
{
    <NAME>__pairarray_destroy (&m->a);
}