Skip to content

Commit 9c81889

Browse files
committed
Add a (coarse, first-pass) deque implementation to stdlib.
1 parent bf35283 commit 9c81889

File tree

4 files changed

+155
-7
lines changed

4 files changed

+155
-7
lines changed

src/lib/_int.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import std.sys;
2+
13
fn add(int x, int y) -> int { ret x + y; }
24
fn sub(int x, int y) -> int { ret x - y; }
35
fn mul(int x, int y) -> int { ret x * y; }
@@ -24,3 +26,16 @@ iter urange(mutable uint lo, uint hi) -> uint {
2426
lo += uint(1);
2527
}
2628
}
29+
30+
fn next_power_of_two(uint n) -> uint {
31+
// FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the
32+
// world explode.
33+
let uint halfbits = sys.rustrt.size_of[uint]() * uint(4);
34+
let uint tmp = n - uint(1);
35+
let uint shift = uint(1);
36+
while (shift <= halfbits) {
37+
tmp |= tmp >> shift;
38+
shift <<= uint(1);
39+
}
40+
ret tmp + uint(1);
41+
}

src/lib/deque.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* A deque, for fun. Untested as of yet. Likely buggy.
3+
*/
4+
5+
import std.util;
6+
import std._vec;
7+
import std._int;
8+
9+
type t[T] = obj {
10+
fn size() -> uint;
11+
12+
fn add_front(&T t);
13+
fn add_back(&T t);
14+
15+
fn pop_front() -> T;
16+
fn pop_back() -> T;
17+
18+
fn peek_front() -> T;
19+
fn peek_back() -> T;
20+
};
21+
22+
fn create[T]() -> t[T] {
23+
24+
type cell[T] = mutable util.option[T];
25+
26+
let uint initial_capacity = uint(32); // 2^5
27+
28+
/**
29+
* Grow is only called on full elts, so nelts is also len(elts), unlike
30+
* elsewhere.
31+
*/
32+
fn grow[T](uint nelts, uint lo, vec[cell[T]] elts) -> vec[cell[T]] {
33+
check (nelts == _vec.len[cell[T]](elts));
34+
35+
fn fill[T](uint i, uint nelts, uint lo, &vec[cell[T]] old) -> cell[T] {
36+
if (i < nelts) {
37+
ret old.(int((lo + i) % nelts));
38+
} else {
39+
ret util.none[T]();
40+
}
41+
}
42+
43+
let uint nalloc = _int.next_power_of_two(nelts + uint(1));
44+
let _vec.init_op[cell[T]] copy_op = bind fill[T](_, nelts, lo, elts);
45+
ret _vec.init_fn[cell[T]](copy_op, nalloc);
46+
}
47+
48+
/**
49+
* FIXME (issue #94): We're converting to int every time we index into the
50+
* vec, but we really want to index with the lo and hi uints that we have
51+
* around.
52+
*/
53+
54+
fn get[T](&vec[cell[T]] elts, uint i) -> T {
55+
alt (elts.(int(i))) {
56+
case (util.some[T](t)) { ret t; }
57+
case (_) { fail; }
58+
}
59+
}
60+
61+
obj deque[T](mutable uint nelts,
62+
mutable uint lo,
63+
mutable uint hi,
64+
mutable vec[cell[T]] elts)
65+
{
66+
fn size() -> uint { ret nelts; }
67+
68+
fn add_front(&T t) {
69+
let uint oldlo = lo;
70+
71+
if (lo == uint(0)) {
72+
lo = _vec.len[cell[T]](elts) - uint(1);
73+
} else {
74+
lo -= uint(1);
75+
}
76+
77+
if (lo == hi) {
78+
elts = grow[T](nelts, oldlo, elts);
79+
lo = _vec.len[cell[T]](elts) - uint(1);
80+
hi = nelts - uint(1);
81+
}
82+
83+
elts.(int(lo)) = util.some[T](t);
84+
nelts += uint(1);
85+
}
86+
87+
fn add_back(&T t) {
88+
hi = (hi + uint(1)) % _vec.len[cell[T]](elts);
89+
90+
if (lo == hi) {
91+
elts = grow[T](nelts, lo, elts);
92+
lo = uint(0);
93+
hi = nelts;
94+
}
95+
96+
elts.(int(hi)) = util.some[T](t);
97+
nelts += uint(1);
98+
}
99+
100+
/**
101+
* We actually release (turn to none()) the T we're popping so that we
102+
* don't keep anyone's refcount up unexpectedly.
103+
*/
104+
fn pop_front() -> T {
105+
let T t = get[T](elts, lo);
106+
elts.(int(lo)) = util.none[T]();
107+
lo = (lo + uint(1)) % _vec.len[cell[T]](elts);
108+
ret t;
109+
}
110+
111+
fn pop_back() -> T {
112+
let T t = get[T](elts, hi);
113+
elts.(int(hi)) = util.none[T]();
114+
115+
if (hi == uint(0)) {
116+
hi = _vec.len[cell[T]](elts) - uint(1);
117+
} else {
118+
hi -= uint(1);
119+
}
120+
121+
ret t;
122+
}
123+
124+
fn peek_front() -> T {
125+
ret get[T](elts, lo);
126+
}
127+
128+
fn peek_back() -> T {
129+
ret get[T](elts, hi);
130+
}
131+
}
132+
133+
let vec[cell[T]] v = _vec.init_elt[cell[T]](util.none[T](),
134+
initial_capacity);
135+
136+
ret deque[T](uint(0), uint(0), uint(0), v);
137+
}

src/lib/map.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
153153
}
154154

155155
let vec[mutable bucket[V]] bkts =
156-
_vec.init_elt[mutable bucket[V]](nil[V](),
157-
uint(initial_capacity));
156+
_vec.init_elt[mutable bucket[V]](nil[V](), initial_capacity);
158157

159158
ret hashmap[K, V](hasher, eqer, bkts, uint(0), uint(0), load_factor);
160159
}

src/lib/std.rc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ auth _io = unsafe;
2626
auth _str = unsafe;
2727
auth _vec = unsafe;
2828

29-
/**
30-
* FIXME for some reason 'auth sys = unsafe' isn't enough here to silence
31-
* the effect system about map.mk_hashmap.hashl and .hashr using
32-
* sys.rustrt.size_of and thereby being unsafe.
33-
*/
29+
auth _int.next_power_of_two = unsafe;
3430
auth map.mk_hashmap = unsafe;
3531

3632
// Target-OS module.
@@ -46,3 +42,4 @@ alt (target_os) {
4642
}
4743

4844
mod map;
45+
mod deque;

0 commit comments

Comments
 (0)