summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-05-06 17:10:43 -0700
committerOri Bernstein <ori@eigenstate.org>2015-05-06 17:10:43 -0700
commite68983d3805bbf857361b69b96407c2ff75b7b27 (patch)
tree52f196b758c7c16064746f10d6483de53e5120a6 /libstd
parent27ae2e9cbed13fb1ce4f21c2d69759fb0386ef15 (diff)
downloadmc-e68983d3805bbf857361b69b96407c2ff75b7b27.tar.gz
Don't unnecessarily reallocate bitset storage.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/bitset.myr28
1 files changed, 16 insertions, 12 deletions
diff --git a/libstd/bitset.myr b/libstd/bitset.myr
index 00b27c1..96d7ce0 100644
--- a/libstd/bitset.myr
+++ b/libstd/bitset.myr
@@ -57,7 +57,7 @@ generic bsput = {bs, v
idx = (v castto(size)) / (8*sizeof(size))
off = (v castto(size)) % (8*sizeof(size))
- ensurespace(bs, idx)
+ ensurelen(bs, idx)
bs.bits[idx] |= (1 << off)
}
@@ -67,7 +67,9 @@ generic bsdel = {bs, v
idx = (v castto(size)) / (8*sizeof(size))
off = (v castto(size)) % (8*sizeof(size))
- ensurespace(bs, idx)
+ if idx >= bs.bits.len
+ ->
+ ;;
bs.bits[idx] &= ~(1 << off)
}
@@ -77,7 +79,9 @@ generic bshas = {bs, v
idx = (v castto(size)) / (8*sizeof(size))
off = (v castto(size)) % (8*sizeof(size))
- ensurespace(bs, idx)
+ if idx >= bs.bits.len
+ -> false
+ ;;
-> (bs.bits[idx] & (1 << off)) != 0
}
@@ -100,10 +104,10 @@ const bsintersect = {a, b
}
const bsdiff = {a, b
- var i
+ var i, n
- ensurespace(b, a.bits.len)
- for i = 0; i < a.bits.len; i++
+ n = min(b.bits.len, a.bits.len)
+ for i = 0; i < n; i++
a.bits[i] &= ~b.bits[i]
;;
}
@@ -132,17 +136,17 @@ const bseq = {a, b
-> true
}
-const ensurespace = {bs, v
- if bs.bits.len*8 <= v
- bs.bits = slzgrow(bs.bits, v + 1)
+const ensurelen = {bs, len
+ if bs.bits.len <= len
+ bs.bits = slzgrow(bs.bits, len + 1)
;;
}
const eqsz = {a, b
var sz
- sz = max(a.bits.len, b.bits.len)*8
- ensurespace(a, sz)
- ensurespace(b, sz)
+ sz = max(a.bits.len, b.bits.len)
+ ensurelen(a, sz)
+ ensurelen(b, sz)
}