about summary refs log tree commit diff
path: root/src/libstd/bitv.rs
blob: 30af05a9c4947bd9479f02edbff1aa6ccc08fff1 (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
590
591
592
593
594
595
596
597
598
599
600
601
/*
Module: bitv

Bitvectors.
*/

export t;
export create;
export union;
export intersect;
export assign;
export clone;
export get;
export equal;
export clear;
export set_all;
export invert;
export difference;
export set;
export is_true;
export is_false;
export to_vec;
export to_str;
export eq_vec;

// FIXME: With recursive object types, we could implement binary methods like
//        union, intersection, and difference. At that point, we could write
//        an optimizing version of this module that produces a different obj
//        for the case where nbits <= 32.

/*
Type: t

The bitvector type.
*/
type t = @{storage: [mutable uint], nbits: uint};


const uint_bits: uint = 32u + (1u << 32u >> 27u);

/*
Function: create

Constructs a bitvector.

Parameters:
nbits - The number of bits in the bitvector
init - If true then the bits are initialized to 1, otherwise 0
*/
fn create(nbits: uint, init: bool) -> t {
    let elt = if init { !0u } else { 0u };
    let storage = vec::init_elt_mut::<uint>(elt, nbits / uint_bits + 1u);
    ret @{storage: storage, nbits: nbits};
}

fn process(v0: t, v1: t, op: block(uint, uint) -> uint) -> bool {
    let len = vec::len(v1.storage);
    assert (vec::len(v0.storage) == len);
    assert (v0.nbits == v1.nbits);
    let changed = false;
    uint::range(0u, len) {|i|
        let w0 = v0.storage[i];
        let w1 = v1.storage[i];
        let w = op(w0, w1);
        if w0 != w { changed = true; v0.storage[i] = w; }
    };
    ret changed;
}


fn lor(w0: uint, w1: uint) -> uint { ret w0 | w1; }

fn union(v0: t, v1: t) -> bool { let sub = lor; ret process(v0, v1, sub); }

fn land(w0: uint, w1: uint) -> uint { ret w0 & w1; }

/*
Function: intersect

Calculates the intersection of two bitvectors

Sets `v0` to the intersection of `v0` and `v1`

Preconditions:

Both bitvectors must be the same length

Returns:

True if `v0` was changed
*/
fn intersect(v0: t, v1: t) -> bool {
    let sub = land;
    ret process(v0, v1, sub);
}

fn right(_w0: uint, w1: uint) -> uint { ret w1; }

/*
Function: assign

Assigns the value of `v1` to `v0`

Preconditions:

Both bitvectors must be the same length

Returns:

True if `v0` was changed
*/
fn assign(v0: t, v1: t) -> bool { let sub = right; ret process(v0, v1, sub); }

/*
Function: clone

Makes a copy of a bitvector
*/
fn clone(v: t) -> t {
    let storage = vec::init_elt_mut::<uint>(0u, v.nbits / uint_bits + 1u);
    let len = vec::len(v.storage);
    uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
    ret @{storage: storage, nbits: v.nbits};
}

/*
Function: get

Retreive the value at index `i`
*/
pure fn get(v: t, i: uint) -> bool {
    assert (i < v.nbits);
    let bits = uint_bits;
    let w = i / bits;
    let b = i % bits;
    let x = 1u & v.storage[w] >> b;
    ret x == 1u;
}

// FIXME: This doesn't account for the actual size of the vectors,
// so it could end up comparing garbage bits
/*
Function: equal

Compares two bitvectors

Preconditions:

Both bitvectors must be the same length

Returns:

True if both bitvectors contain identical elements
*/
fn equal(v0: t, v1: t) -> bool {
    // FIXME: when we can break or return from inside an iterator loop,
    //        we can eliminate this painful while-loop

    let len = vec::len(v1.storage);
    let i = 0u;
    while i < len {
        if v0.storage[i] != v1.storage[i] { ret false; }
        i = i + 1u;
    }
    ret true;
}

/*
Function: clear

Set all bits to 0
*/
fn clear(v: t) {
    uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
}

/*
Function: set_all

Set all bits to 1
*/
fn set_all(v: t) {
    uint::range(0u, v.nbits) {|i| set(v, i, true); };
}

/*
Function: invert

Invert all bits
*/
fn invert(v: t) {
    uint::range(0u, vec::len(v.storage)) {|i|
        v.storage[i] = !v.storage[i];
    };
}

/*
Function: difference

Calculate the difference between two bitvectors

Sets each element of `v0` to the value of that element minus the element
of `v1` at the same index.

Preconditions:

Both bitvectors must be the same length

Returns:

True if `v0` was changed
*/
fn difference(v0: t, v1: t) -> bool {
    invert(v1);
    let b = intersect(v0, v1);
    invert(v1);
    ret b;
}

/*
Function: set

Set the value of a bit at a given index

Preconditions:

`i` must be less than the length of the bitvector
*/
fn set(v: t, i: uint, x: bool) {
    assert (i < v.nbits);
    let bits = uint_bits;
    let w = i / bits;
    let b = i % bits;
    let flag = 1u << b;
    v.storage[w] = if x { v.storage[w] | flag } else { v.storage[w] & !flag };
}


/*
Function: is_true

Returns true if all bits are 1
*/
fn is_true(v: t) -> bool {
    for i: uint in to_vec(v) { if i != 1u { ret false; } }
    ret true;
}


/*
Function: is_false

Returns true if all bits are 0
*/
fn is_false(v: t) -> bool {
    for i: uint in to_vec(v) { if i == 1u { ret false; } }
    ret true;
}

fn init_to_vec(v: t, i: uint) -> uint { ret if get(v, i) { 1u } else { 0u }; }

/*
Function: to_vec

Converts the bitvector to a vector of uint with the same length. Each uint
in the resulting vector has either value 0u or 1u.
*/
fn to_vec(v: t) -> [uint] {
    let sub = bind init_to_vec(v, _);
    ret vec::init_fn::<uint>(sub, v.nbits);
}

/*
Function: to_str

Converts the bitvector to a string. The resulting string has the same
length as the bitvector, and each character is either '0' or '1'.
*/
fn to_str(v: t) -> str {
    let rs = "";
    for i: uint in to_vec(v) { if i == 1u { rs += "1"; } else { rs += "0"; } }
    ret rs;
}

/*
Function: eq_vec

Compare a bitvector to a vector of uint. The uint vector is expected to
only contain the values 0u and 1u.

Preconditions:

Both the bitvector and vector must have the same length
*/
fn eq_vec(v0: t, v1: [uint]) -> bool {
    assert (v0.nbits == vec::len::<uint>(v1));
    let len = v0.nbits;
    let i = 0u;
    while i < len {
        let w0 = get(v0, i);
        let w1 = v1[i];
        if !w0 && w1 != 0u || w0 && w1 == 0u { ret false; }
        i = i + 1u;
    }
    ret true;
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_0_elements() {
        let act;
        let exp;
        act = create(0u, false);
        exp = vec::init_elt::<uint>(0u, 0u);
        assert (eq_vec(act, exp));
    }

    #[test]
    fn test_1_element() {
        let act;
        act = create(1u, false);
        assert (eq_vec(act, [0u]));
        act = create(1u, true);
        assert (eq_vec(act, [1u]));
    }

    #[test]
    fn test_10_elements() {
        let act;
        // all 0

        act = create(10u, false);
        assert (eq_vec(act, [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u]));
        // all 1

        act = create(10u, true);
        assert (eq_vec(act, [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u]));
        // mixed

        act = create(10u, false);
        set(act, 0u, true);
        set(act, 1u, true);
        set(act, 2u, true);
        set(act, 3u, true);
        set(act, 4u, true);
        assert (eq_vec(act, [1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u]));
        // mixed

        act = create(10u, false);
        set(act, 5u, true);
        set(act, 6u, true);
        set(act, 7u, true);
        set(act, 8u, true);
        set(act, 9u, true);
        assert (eq_vec(act, [0u, 0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u]));
        // mixed

        act = create(10u, false);
        set(act, 0u, true);
        set(act, 3u, true);
        set(act, 6u, true);
        set(act, 9u, true);
        assert (eq_vec(act, [1u, 0u, 0u, 1u, 0u, 0u, 1u, 0u, 0u, 1u]));
    }

    #[test]
    fn test_31_elements() {
        let act;
        // all 0

        act = create(31u, false);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u]));
        // all 1

        act = create(31u, true);
        assert (eq_vec(act,
                       [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u]));
        // mixed

        act = create(31u, false);
        set(act, 0u, true);
        set(act, 1u, true);
        set(act, 2u, true);
        set(act, 3u, true);
        set(act, 4u, true);
        set(act, 5u, true);
        set(act, 6u, true);
        set(act, 7u, true);
        assert (eq_vec(act,
                       [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u]));
        // mixed

        act = create(31u, false);
        set(act, 16u, true);
        set(act, 17u, true);
        set(act, 18u, true);
        set(act, 19u, true);
        set(act, 20u, true);
        set(act, 21u, true);
        set(act, 22u, true);
        set(act, 23u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u]));
        // mixed

        act = create(31u, false);
        set(act, 24u, true);
        set(act, 25u, true);
        set(act, 26u, true);
        set(act, 27u, true);
        set(act, 28u, true);
        set(act, 29u, true);
        set(act, 30u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u]));
        // mixed

        act = create(31u, false);
        set(act, 3u, true);
        set(act, 17u, true);
        set(act, 30u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 1u]));
    }

    #[test]
    fn test_32_elements() {
        let act;
        // all 0

        act = create(32u, false);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u]));
        // all 1

        act = create(32u, true);
        assert (eq_vec(act,
                       [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u, 1u]));
        // mixed

        act = create(32u, false);
        set(act, 0u, true);
        set(act, 1u, true);
        set(act, 2u, true);
        set(act, 3u, true);
        set(act, 4u, true);
        set(act, 5u, true);
        set(act, 6u, true);
        set(act, 7u, true);
        assert (eq_vec(act,
                       [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u]));
        // mixed

        act = create(32u, false);
        set(act, 16u, true);
        set(act, 17u, true);
        set(act, 18u, true);
        set(act, 19u, true);
        set(act, 20u, true);
        set(act, 21u, true);
        set(act, 22u, true);
        set(act, 23u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u]));
        // mixed

        act = create(32u, false);
        set(act, 24u, true);
        set(act, 25u, true);
        set(act, 26u, true);
        set(act, 27u, true);
        set(act, 28u, true);
        set(act, 29u, true);
        set(act, 30u, true);
        set(act, 31u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u, 1u]));
        // mixed

        act = create(32u, false);
        set(act, 3u, true);
        set(act, 17u, true);
        set(act, 30u, true);
        set(act, 31u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 1u, 1u]));
    }

    #[test]
    fn test_33_elements() {
        let act;
        // all 0

        act = create(33u, false);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u]));
        // all 1

        act = create(33u, true);
        assert (eq_vec(act,
                       [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u, 1u, 1u]));
        // mixed

        act = create(33u, false);
        set(act, 0u, true);
        set(act, 1u, true);
        set(act, 2u, true);
        set(act, 3u, true);
        set(act, 4u, true);
        set(act, 5u, true);
        set(act, 6u, true);
        set(act, 7u, true);
        assert (eq_vec(act,
                       [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u]));
        // mixed

        act = create(33u, false);
        set(act, 16u, true);
        set(act, 17u, true);
        set(act, 18u, true);
        set(act, 19u, true);
        set(act, 20u, true);
        set(act, 21u, true);
        set(act, 22u, true);
        set(act, 23u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u]));
        // mixed

        act = create(33u, false);
        set(act, 24u, true);
        set(act, 25u, true);
        set(act, 26u, true);
        set(act, 27u, true);
        set(act, 28u, true);
        set(act, 29u, true);
        set(act, 30u, true);
        set(act, 31u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
                        1u, 1u, 1u, 1u, 1u, 1u, 0u]));
        // mixed

        act = create(33u, false);
        set(act, 3u, true);
        set(act, 17u, true);
        set(act, 30u, true);
        set(act, 31u, true);
        set(act, 32u, true);
        assert (eq_vec(act,
                       [0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
                        0u, 0u, 0u, 0u, 1u, 1u, 1u]));
    }

}

//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//