summaryrefslogtreecommitdiff
path: root/dict.c
blob: 4dab1a8947a7589d998a0725419c6fe6902eaa98 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/************************************************

  dict.c -

  $Author: matz $
  $Date: 1994/11/01 08:27:49 $
  created at: Mon Nov 22 18:51:18 JST 1993

  Copyright (C) 1994 Yukihiro Matsumoto

************************************************/

#include "ruby.h"
#include "st.h"

VALUE C_Dict;

static VALUE envtbl;
static ID hash, eq;
VALUE Fgetenv(), Fsetenv();

static VALUE
rb_cmp(a, b)
    VALUE a, b;
{
    return rb_funcall(a, eq, 1, b)?0:1;
}

static VALUE
rb_hash(a, mod)
    VALUE a;
    int mod;
{
    return rb_funcall(a, hash, 0) % mod;
}

#define ASSOC_KEY(a) RARRAY(a)->ptr[0]
#define ASSOC_VAL(a) RARRAY(a)->ptr[1]

VALUE
Fdic_new(class)
    VALUE class;
{
    int i, max;
    NEWOBJ(dic, struct RDict);
    OBJSETUP(dic, class, T_DICT);

    dic->tbl = st_init_table(rb_cmp, rb_hash);

    return (VALUE)dic;
}

static VALUE
Fdic_clone(dic)
    struct RDict *dic;
{
    NEWOBJ(dic2, struct RDict);
    CLONESETUP(dic2, dic);

    dic2->tbl = (st_table*)st_copy(dic->tbl);

    return (VALUE)dic2;
}

static VALUE
Fdic_aref(dic, key)
    struct RDict *dic;
    VALUE key;
{
    VALUE val = Qnil;

    if (!st_lookup(dic->tbl, key, &val)) {
	return Qnil;
    }
    return val;
}

static VALUE
Fdic_indexes(dic, args)
    struct RDict *dic;
    struct RArray *args;
{
    VALUE *p, *pend;
    struct RArray *new;
    int i = 0;

    if (!args || args->len == 1 && TYPE(args->ptr) != T_ARRAY) {
	args = (struct RArray*)rb_to_a(args->ptr[0]);
    }

    new = (struct RArray*)ary_new2(args->len);

    p = args->ptr; pend = p + args->len;
    while (p < pend) {
	new->ptr[i++] = Fdic_aref(dic, *p++);
    }
    new->len = i;
    return (VALUE)new;
}

static VALUE
Fdic_delete(dic, key)
    struct RDict *dic;
    VALUE key;
{
    VALUE val;

    if (st_delete(dic->tbl, &key, &val))
	return val;
    return Qnil;
}

static int
dic_delete_if(key, value)
    VALUE key, value;
{
    if (rb_yield(assoc_new(key, value)))
	return ST_DELETE;
    return ST_CONTINUE;
}

static VALUE
Fdic_delete_if(dic)
    struct RDict *dic;
{
    st_foreach(dic->tbl, dic_delete_if, Qnil);

    return (VALUE)dic;
}

static
dic_clear(key, value)
    VALUE key, value;
{
    return ST_DELETE;
}

static VALUE
Fdic_clear(dic)
    struct RDict *dic;
{
    st_foreach(dic->tbl, dic_clear, Qnil);

    return (VALUE)dic;
}

VALUE
Fdic_aset(dic, key, val)
    struct RDict *dic;
    VALUE key, val;
{
    if (val == Qnil) {
	Fdic_delete(dic, key);
	return Qnil;
    }
    st_insert(dic->tbl, key, val);
    return val;
}

static VALUE
Fdic_length(dic)
    struct RDict *dic;
{
    return INT2FIX(dic->tbl->num_entries);
}

static
dic_each_value(key, value)
    VALUE key, value;
{
    rb_yield(value);
    return ST_CONTINUE;
}

static VALUE
Fdic_each_value(dic)
    struct RDict *dic;
{
    st_foreach(dic->tbl, dic_each_value);
    return (VALUE)dic;
}

static
dic_each_key(key, value)
    VALUE key, value;
{
    rb_yield(key);
    return ST_CONTINUE;
}

static VALUE
Fdic_each_key(dic)
    struct RDict *dic;
{
    st_foreach(dic->tbl, dic_each_key);
    return (VALUE)dic;
}

static
dic_each_pair(key, value)
    VALUE key, value;
{
    rb_yield(assoc_new(key, value));
    return ST_CONTINUE;
}

static VALUE
Fdic_each_pair(dic)
    struct RDict *dic;
{
    st_foreach(dic->tbl, dic_each_pair);
    return (VALUE)dic;
}

static
dic_to_a(key, value, ary)
    VALUE key, value, ary;
{
    Fary_push(ary, assoc_new(key, value));
    return ST_CONTINUE;
}

static VALUE
Fdic_to_a(dic)
    struct RDict *dic;
{
    VALUE ary;

    ary = ary_new();
    st_foreach(dic->tbl, dic_to_a, ary);

    return ary;
}

static
dic_inspect(key, value, str)
    VALUE key, value;
    struct RString *str;
{
    VALUE str2;
    ID inspect = rb_intern("_inspect");

    if (str->len > 1) {
	str_cat(str, ", ", 2);
    }
    str2 = rb_funcall(key, inspect, 0, Qnil);
    str_cat(str, RSTRING(str2)->ptr, RSTRING(str2)->len);
    str_cat(str, "=>", 2);
    str2 = rb_funcall(value, inspect, 0, Qnil);
    str_cat(str, RSTRING(str2)->ptr, RSTRING(str2)->len);

    return ST_CONTINUE;
}

static VALUE
Fdic_inspect(dic)
    struct RDict *dic;
{
    VALUE str;

    str = str_new2("{");
    st_foreach(dic->tbl, dic_inspect, str);
    str_cat(str, "}", 1);

    return str;
}

static VALUE
Fdic_to_s(dic)
    VALUE dic;
{
    VALUE str;

    dic = Fdic_to_a(dic);
    str = Fary_to_s(dic);

    return str;
}

static
dic_keys(key, value, ary)
    VALUE key, value, ary;
{
    Fary_push(ary, key);
    return ST_CONTINUE;
}

static VALUE
Fdic_keys(dic)
    struct RDict *dic;
{
    VALUE ary;

    ary = ary_new();
    st_foreach(dic->tbl, dic_keys, ary);

    return ary;
}

static
dic_values(key, value, ary)
    VALUE key, value, ary;
{
    Fary_push(ary, key);
    return ST_CONTINUE;
}

static VALUE
Fdic_values(dic)
    struct RDict *dic;
{
    VALUE ary;

    ary = ary_new();
    st_foreach(dic->tbl, dic_values, ary);

    return ary;
}

static VALUE
Fdic_has_key(dic, key)
    struct RDict *dic;
    VALUE key;
{
    VALUE val;

    if (st_lookup(dic->tbl, key, &val))
	return TRUE;
    return FALSE;
}

static int
dic_search_value(key, value, data)
    VALUE key, value, *data;
{
    if (rb_funcall(value, eq, 1, data[1])) {
	data[0] = TRUE;
	return ST_STOP;
    }
    return ST_CONTINUE;
}

static VALUE
Fdic_has_value(dic, val)
    struct RDict *dic;
    VALUE val;
{
    VALUE data[2];

    data[0] = FALSE;
    data[1] = val;
    st_foreach(dic->tbl, dic_search_value, data);
    return data[0];
}

struct equal_data {
    int result;
    st_table *tbl;
};

static int
dic_equal(key, val1, data)
    VALUE key, val1;
    struct equal_data *data;
{
    VALUE val2;

    if (!st_lookup(data->tbl, key, &val2)) {
	data->result = FALSE;
	return ST_STOP;
    }
    if (!rb_funcall(val1, eq, 1, val2)) {
	data->result = FALSE;
	return ST_STOP;
    }
    return ST_CONTINUE;
}

static VALUE
Fdic_equal(dic1, dic2)
    struct RDict *dic1, *dic2;
{
    struct equal_data data;

    if (TYPE(dic2) != T_DICT) return FALSE;
    if (dic1->tbl->num_entries != dic2->tbl->num_entries)
	return FALSE;

    data.tbl = dic2->tbl;
    data.result = TRUE;
    st_foreach(dic1->tbl, dic_equal, &data);

    return data.result;
}

static int
dic_hash(key, val, data)
    VALUE key, val;
    int *data;
{
    *data ^= rb_funcall(key, hash, 0);
    *data ^= rb_funcall(val, hash, 0);
    return ST_CONTINUE;
}

static VALUE
Fdic_hash(dic)
    struct RDict *dic;
{
    int h;

    st_foreach(dic->tbl, dic_hash, &h);
    return INT2FIX(h);
}

char *strchr();
extern VALUE rb_readonly_hook();

extern char **environ;

static VALUE
Fenv_each(dic)
    VALUE dic;
{
    char **env;

    env = environ;
    while (*env) {
	VALUE var, val;
	char *s = strchr(*env, '=');

	var = str_new(*env, s-*env);
	val = str_new2(s+1);
	rb_yield(assoc_new(var, val));
	env++;
    }
    return dic;
}

static VALUE
Fenv_delete(obj, name)
    VALUE obj;
    struct RString *name;
{
    int i, len;
    char *nam, *val = Qnil;

    Check_Type(name, T_STRING);
    nam = name->ptr;
    len = strlen(nam);
    for(i=0; environ[i]; i++) {
	if (strncmp(environ[i], nam, len) == 0 && environ[i][len] == '=') {
	    val = environ[i]+len+1;
	    break;
	}
    }
    while (environ[i]) {
	environ[i] = environ[i+1];
	i++;
    }
    if (val) {
	return str_new2(val);
    }
    return Qnil;
}

VALUE
Fgetenv(obj, name)
    VALUE obj;
    struct RString *name;
{
    extern char *getenv();
    char *env;

    Check_Type(name, T_STRING);

    if (strlen(name->ptr) != name->len)
	Fail("Bad environment name");

    env = getenv(name->ptr);
    if (env) {
	return str_new2(env);
    }
    return Qnil;
}

VALUE
Fsetenv(obj, name, value)
    VALUE obj;
    struct RString *name, *value;
{
    Check_Type(name, T_STRING);
    if (value == Qnil) {
	Fenv_delete(obj, name);
	return Qnil;
    }

    Check_Type(value, T_STRING);

    if (strlen(name->ptr) != name->len)
	Fail("Bad environment name");
    if (strlen(value->ptr) != value->len)
	Fail("Bad environment value");

#ifdef HAVE_SETENV
    if (setenv(name->ptr, value->ptr, 1) == 0) return TRUE;
#else
#ifdef HAVE_PUTENV
    {
	char *str;
	int len;

	str = ALLOC_N(char, name->len + value->len + 2);
	sprintf("%s=%s", name->ptr, value->ptr);
	if (putenv(str) == 0) return TRUE;
    }
#else
    Fail("setenv is not supported on this system");
#endif
#endif

    Fail("setenv failed");
    return FALSE;		/* not reached */
}

static VALUE
Fenv_to_s()
{
    return str_new2("$ENV");
}

Init_Dict()
{
    extern VALUE C_Kernel;
    extern VALUE M_Enumerable;

    hash = rb_intern("hash");
    eq   = rb_intern("==");

    C_Dict = rb_define_class("Dict", C_Object);
    rb_name_class(C_Dict, rb_intern("Hash")); /* alias */

    rb_include_module(C_Dict, M_Enumerable);

    rb_define_single_method(C_Dict, "new", Fdic_new, 0);

    rb_define_method(C_Dict,"clone",  Fdic_clone, 0);

    rb_define_method(C_Dict,"to_a",  Fdic_to_a, 0);
    rb_define_method(C_Dict,"to_s",  Fdic_to_s, 0);
    rb_define_method(C_Dict,"_inspect",  Fdic_inspect, 0);

    rb_define_method(C_Dict,"==",  Fdic_equal, 1);
    rb_define_method(C_Dict,"hash",  Fdic_hash, 0);
    rb_define_method(C_Dict,"[]",  Fdic_aref, 1);
    rb_define_method(C_Dict,"[]=", Fdic_aset, 2);
    rb_define_method(C_Dict,"indexes",  Fdic_indexes, -2);
    rb_define_method(C_Dict,"length", Fdic_length, 0);
    rb_define_alias(C_Dict,  "size", "length");
    rb_define_method(C_Dict,"each", Fdic_each_pair, 0);
    rb_define_method(C_Dict,"each_value", Fdic_each_value, 0);
    rb_define_method(C_Dict,"each_key", Fdic_each_key, 0);
    rb_define_method(C_Dict,"each_pair", Fdic_each_pair, 0);

    rb_define_method(C_Dict,"keys", Fdic_keys, 0);
    rb_define_method(C_Dict,"values", Fdic_values, 0);

    rb_define_method(C_Dict,"delete", Fdic_delete, 1);
    rb_define_method(C_Dict,"delete_if", Fdic_delete_if, 0);
    rb_define_method(C_Dict,"clear", Fdic_clear, 0);

    rb_define_method(C_Dict,"includes", Fdic_has_key, 1);
    rb_define_method(C_Dict,"has_key", Fdic_has_key, 1);
    rb_define_method(C_Dict,"has_value", Fdic_has_value, 1);


    envtbl = obj_alloc(C_Object);
    rb_define_single_method(envtbl,"[]", Fgetenv, 1);
    rb_define_single_method(envtbl,"[]=", Fsetenv, 2);
    rb_define_single_method(envtbl,"each", Fenv_each, 0);
    rb_define_single_method(envtbl,"delete", Fenv_delete, 1);
    rb_define_single_method(envtbl,"to_s", Fenv_to_s, 0);
    rb_include_module(CLASS_OF(envtbl), M_Enumerable);

    rb_define_variable("$ENV", &envtbl, Qnil, rb_readonly_hook);

    rb_define_private_method(C_Kernel, "getenv", Fgetenv, 1);
    rb_define_private_method(C_Kernel, "setenv", Fsetenv, 2);
}