Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
2357c443
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2357c443
编写于
7月 20, 2014
作者:
J
Jonas Hietala
提交者:
Alex Crichton
7月 21, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplify and cleanup bitv examples.
上级
2957644e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
120 addition
and
74 deletion
+120
-74
src/libcollections/bitv.rs
src/libcollections/bitv.rs
+120
-74
未找到文件。
src/libcollections/bitv.rs
浏览文件 @
2357c443
...
...
@@ -98,7 +98,7 @@ enum BitvVariant { Big(BigBitv), Small(SmallBitv) }
/// # Example
///
/// ```rust
/// use collections::
bitv::
Bitv;
/// use collections::Bitv;
///
/// let mut bv = Bitv::with_capacity(10, false);
///
...
...
@@ -249,9 +249,9 @@ pub fn with_capacity(nbits: uint, init: bool) -> Bitv {
/// # Example
///
/// ```
/// use std::collections::
B
itv;
/// use std::collections::
b
itv;
///
/// let bv
: Bitv = [false, true].iter().map(|n| *n).collect(
);
/// let bv
= bitv::from_bytes([0b01100000]
);
/// assert_eq!(bv.get(0), false);
/// assert_eq!(bv.get(1), true);
///
...
...
@@ -297,11 +297,15 @@ pub fn set(&mut self, i: uint, x: bool) {
/// # Example
///
/// ```
/// use std::collections::
B
itv;
/// use std::collections::
b
itv;
///
/// let mut bv: Bitv = [false, true, false].iter().map(|n| *n).collect();
/// let before = 0b01100000;
/// let after = 0b11111111;
///
/// let mut bv = bitv::from_bytes([before]);
/// bv.set_all();
/// assert!(bv.eq_vec([true, true, true]));
/// assert_eq!(bv, bitv::from_bytes([after]));
/// ```
#[inline]
pub
fn
set_all
(
&
mut
self
)
{
for
w
in
self
.storage
.mut_iter
()
{
*
w
=
!
0u
;
}
...
...
@@ -312,11 +316,15 @@ pub fn set_all(&mut self) {
/// # Example
///
/// ```
/// use std::collections::Bitv;
/// use std::collections::bitv;
///
/// let before = 0b01100000;
/// let after = 0b10011111;
///
/// let mut bv
: Bitv = [false, true, false].iter().map(|n| *n).collect(
);
/// let mut bv
= bitv::from_bytes([before]
);
/// bv.negate();
/// assert!(bv.eq_vec([true, false, true]));
/// assert_eq!(bv, bitv::from_bytes([after]));
/// ```
#[inline]
pub
fn
negate
(
&
mut
self
)
{
for
w
in
self
.storage
.mut_iter
()
{
*
w
=
!*
w
;
}
...
...
@@ -334,14 +342,17 @@ pub fn negate(&mut self) {
/// # Example
///
/// ```
/// use std::collections::Bitv;
/// use std::collections::bitv;
///
/// let a = 0b01100100;
/// let b = 0b01011010;
/// let res = 0b01111110;
///
/// let mut bv1: Bitv = [false, true, true, false].iter().map(|n| *n).collect();
/// let bv2: Bitv = [false, true, false, true].iter().map(|n| *n).collect();
/// let res: Bitv = [false, true, true, true].iter().map(|n| *n).collect();
/// let mut a = bitv::from_bytes([a]);
/// let b = bitv::from_bytes([b]);
///
/// assert!(
bv1.union(&bv2
));
/// assert_eq!(
bv1, res
);
/// assert!(
a.union(&b
));
/// assert_eq!(
a, bitv::from_bytes([res])
);
/// ```
#[inline]
pub
fn
union
(
&
mut
self
,
other
:
&
Bitv
)
->
bool
{
...
...
@@ -360,14 +371,17 @@ pub fn union(&mut self, other: &Bitv) -> bool {
/// # Example
///
/// ```
/// use std::collections::
B
itv;
/// use std::collections::
b
itv;
///
/// let
mut bv1: Bitv = [false, true, true, false].iter().map(|n| *n).collect()
;
/// let b
v2: Bitv = [false, true, false, true].iter().map(|n| *n).collect()
;
/// let res
: Bitv = [false, true, false, false].iter().map(|n| *n).collect()
;
/// let
a = 0b01100100
;
/// let b
= 0b01011010
;
/// let res
= 0b01000000
;
///
/// assert!(bv1.intersect(&bv2));
/// assert_eq!(bv1, res);
/// let mut a = bitv::from_bytes([a]);
/// let b = bitv::from_bytes([b]);
///
/// assert!(a.intersect(&b));
/// assert_eq!(a, bitv::from_bytes([res]));
/// ```
#[inline]
pub
fn
intersect
(
&
mut
self
,
other
:
&
Bitv
)
->
bool
{
...
...
@@ -387,14 +401,24 @@ pub fn intersect(&mut self, other: &Bitv) -> bool {
/// # Example
///
/// ```
/// use std::collections::Bitv;
/// use std::collections::bitv;
///
/// let a = 0b01100100;
/// let b = 0b01011010;
/// let a_b = 0b00100100; // a - b
/// let b_a = 0b00011010; // b - a
///
/// let mut bva = bitv::from_bytes([a]);
/// let bvb = bitv::from_bytes([b]);
///
/// let mut bv1: Bitv = [false, true, true, false].iter().map(|n| *n).collect();
/// let bv2: Bitv = [false, true, false, true].iter().map(|n| *n).collect();
/// let res: Bitv = [false, false, true, false].iter().map(|n| *n).collect();
/// assert!(bva.difference(&bvb));
/// assert_eq!(bva, bitv::from_bytes([a_b]));
///
/// assert!(bv1.difference(&bv2));
/// assert_eq!(bv1, res);
/// let bva = bitv::from_bytes([a]);
/// let mut bvb = bitv::from_bytes([b]);
///
/// assert!(bvb.difference(&bva));
/// assert_eq!(bvb, bitv::from_bytes([b_a]));
/// ```
#[inline]
pub
fn
difference
(
&
mut
self
,
other
:
&
Bitv
)
->
bool
{
...
...
@@ -406,7 +430,7 @@ pub fn difference(&mut self, other: &Bitv) -> bool {
/// # Example
///
/// ```
/// use std::collections::
bitv::
Bitv;
/// use std::collections::Bitv;
///
/// let mut bv = Bitv::with_capacity(5, true);
/// assert_eq!(bv.all(), true);
...
...
@@ -429,16 +453,10 @@ pub fn all(&self) -> bool {
/// # Example
///
/// ```
/// use std::collections::bitv::Bitv;
///
/// let mut bv = Bitv::with_capacity(10, false);
/// bv.set(1, true);
/// bv.set(2, true);
/// bv.set(3, true);
/// bv.set(5, true);
/// bv.set(8, true);
/// use std::collections::bitv;
///
/// assert_eq!(bv.iter().filter(|x| *x).count(), 5);
/// let bv = bitv::from_bytes([0b01110100, 0b10010010]);
/// assert_eq!(bv.iter().filter(|x| *x).count(), 7);
/// ```
#[inline]
pub
fn
iter
<
'a
>
(
&
'a
self
)
->
Bits
<
'a
>
{
...
...
@@ -450,7 +468,7 @@ pub fn iter<'a>(&'a self) -> Bits<'a> {
/// # Example
///
/// ```
/// use std::collections::
bitv::
Bitv;
/// use std::collections::Bitv;
///
/// let mut bv = Bitv::with_capacity(10, false);
/// assert_eq!(bv.none(), true);
...
...
@@ -467,7 +485,7 @@ pub fn none(&self) -> bool {
/// # Example
///
/// ```
/// use std::collections::
bitv::
Bitv;
/// use std::collections::Bitv;
///
/// let mut bv = Bitv::with_capacity(10, false);
/// assert_eq!(bv.any(), false);
...
...
@@ -488,7 +506,7 @@ pub fn any(&self) -> bool {
/// # Example
///
/// ```
/// use std::collections::
bitv::
Bitv;
/// use std::collections::Bitv;
///
/// let mut bv = Bitv::with_capacity(3, true);
/// bv.set(1, false);
...
...
@@ -530,10 +548,11 @@ fn bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 {
/// # Example
///
/// ```
/// use std::collections::bitv
::Bitv
;
/// use std::collections::bitv;
///
/// let bv: Bitv = [true, false, true].iter().map(|n| *n).collect();
/// assert_eq!(bv.to_bools(), vec!(true, false, true));
/// let bv = bitv::from_bytes([0b10100000]);
/// assert_eq!(bv.to_bools(), vec!(true, false, true, false,
/// false, false, false, false));
/// ```
pub
fn
to_bools
(
&
self
)
->
Vec
<
bool
>
{
Vec
::
from_fn
(
self
.nbits
,
|
i
|
self
.get
(
i
))
...
...
@@ -548,11 +567,12 @@ pub fn to_bools(&self) -> Vec<bool> {
/// # Example
///
/// ```
/// use std::collections::
B
itv;
/// use std::collections::
b
itv;
///
/// let bv
: Bitv = [false, true, true].iter().map(|n| *n).collect(
);
/// let bv
= bitv::from_bytes([0b10100000]
);
///
/// assert!(bv.eq_vec([false, true, true]));
/// assert!(bv.eq_vec([true, false, true, false,
/// false, false, false, false]));
/// ```
pub
fn
eq_vec
(
&
self
,
v
:
&
[
bool
])
->
bool
{
assert_eq!
(
self
.nbits
,
v
.len
());
...
...
@@ -572,9 +592,9 @@ pub fn eq_vec(&self, v: &[bool]) -> bool {
/// # Example
///
/// ```
/// use std::collections::bitv
::Bitv
;
/// use std::collections::bitv;
///
/// let mut bv
: Bitv = [false, true, true, false].iter().map(|n| *n).collect(
);
/// let mut bv
= bitv::from_bytes([0b01001011]
);
/// bv.truncate(2);
/// assert!(bv.eq_vec([false, true]));
/// ```
...
...
@@ -595,7 +615,7 @@ pub fn truncate(&mut self, len: uint) {
/// # Example
///
/// ```
/// use std::collections::
bitv::
Bitv;
/// use std::collections::Bitv;
///
/// let mut bv = Bitv::with_capacity(3, false);
/// bv.reserve(10);
...
...
@@ -616,7 +636,7 @@ pub fn reserve(&mut self, size: uint) {
/// # Example
///
/// ```
/// use std::collections::
bitv::
Bitv;
/// use std::collections::Bitv;
///
/// let mut bv = Bitv::new();
/// bv.reserve(10);
...
...
@@ -632,11 +652,12 @@ pub fn capacity(&self) -> uint {
/// # Example
///
/// ```
/// use std::collections::bitv
::Bitv
;
/// use std::collections::bitv;
///
/// let mut bv
: Bitv = [false, true, true, false].iter().map(|n| *n).collect(
);
/// let mut bv
= bitv::from_bytes([0b01001011]
);
/// bv.grow(2, true);
/// assert!(bv.eq_vec([false, true, true, false, true, true]));
/// assert_eq!(bv.len(), 10);
/// assert_eq!(bv.to_bytes(), vec!(0b01001011, 0b11000000));
/// ```
pub
fn
grow
(
&
mut
self
,
n
:
uint
,
value
:
bool
)
{
let
new_nbits
=
self
.nbits
+
n
;
...
...
@@ -676,11 +697,13 @@ pub fn grow(&mut self, n: uint, value: bool) {
/// # Example
///
/// ```
/// use std::collections::bitv
::Bitv
;
/// use std::collections::bitv;
///
/// let mut bv: Bitv = [false, true, true, false].iter().map(|n| *n).collect();
/// let mut bv = bitv::from_bytes([0b01001001]);
/// assert_eq!(bv.pop(), true);
/// assert_eq!(bv.pop(), false);
/// assert!(bv.eq_vec([false, true, true]));
/// assert_eq!(bv.len(), 6);
/// assert_eq!(bv.to_bytes(), vec!(0b01001000));
/// ```
pub
fn
pop
(
&
mut
self
)
->
bool
{
let
ret
=
self
.get
(
self
.nbits
-
1
);
...
...
@@ -697,12 +720,12 @@ pub fn pop(&mut self) -> bool {
/// # Example
///
/// ```
/// use std::collections::
bitv::
Bitv;
/// use std::collections::Bitv;
///
/// let mut bv
: Bitv = [false, true].iter().map(|n| *n).collect
();
/// let mut bv
= Bitv::new
();
/// bv.push(true);
/// bv.push(false);
/// assert!(bv.eq_vec([
false, true,
true, false]));
/// assert!(bv.eq_vec([true, false]));
/// ```
pub
fn
push
(
&
mut
self
,
elem
:
bool
)
{
let
insert_pos
=
self
.nbits
;
...
...
@@ -974,9 +997,9 @@ pub fn with_capacity(nbits: uint) -> BitvSet {
/// # Example
///
/// ```
/// use std::collections::{
B
itv, BitvSet};
/// use std::collections::{
b
itv, BitvSet};
///
/// let bv
: Bitv = [false, true, true, false].iter().map(|n| *n).collect(
);
/// let bv
= bitv::from_bytes([0b01100000]
);
/// let s = BitvSet::from_bitv(bv);
///
/// // Print 1, 2 in arbitrary order
...
...
@@ -1279,11 +1302,15 @@ pub fn symmetric_difference<'a>(&'a self, other: &'a BitvSet) -> TwoBitPositions
/// use std::collections::BitvSet;
/// use std::collections::bitv;
///
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
/// let a = 0b01101000;
/// let b = 0b10100000;
/// let res = 0b11101000;
///
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([a]));
/// let b = BitvSet::from_bitv(bitv::from_bytes([b]));
///
/// a.union_with(&b);
/// assert_eq!(a.unwrap(), bitv::from_bytes([
0b11101000
]));
/// assert_eq!(a.unwrap(), bitv::from_bytes([
res
]));
/// ```
#[inline]
pub
fn
union_with
(
&
mut
self
,
other
:
&
BitvSet
)
{
...
...
@@ -1298,11 +1325,15 @@ pub fn union_with(&mut self, other: &BitvSet) {
/// use std::collections::BitvSet;
/// use std::collections::bitv;
///
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
/// let a = 0b01101000;
/// let b = 0b10100000;
/// let res = 0b00100000;
///
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([a]));
/// let b = BitvSet::from_bitv(bitv::from_bytes([b]));
///
/// a.intersect_with(&b);
/// assert_eq!(a.unwrap(), bitv::from_bytes([
0b00100000
]));
/// assert_eq!(a.unwrap(), bitv::from_bytes([
res
]));
/// ```
#[inline]
pub
fn
intersect_with
(
&
mut
self
,
other
:
&
BitvSet
)
{
...
...
@@ -1317,11 +1348,22 @@ pub fn intersect_with(&mut self, other: &BitvSet) {
/// use std::collections::BitvSet;
/// use std::collections::bitv;
///
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
/// let a = 0b01101000;
/// let b = 0b10100000;
/// let a_b = 0b01001000; // a - b
/// let b_a = 0b10000000; // b - a
///
/// let mut bva = BitvSet::from_bitv(bitv::from_bytes([a]));
/// let bvb = BitvSet::from_bitv(bitv::from_bytes([b]));
///
/// bva.difference_with(&bvb);
/// assert_eq!(bva.unwrap(), bitv::from_bytes([a_b]));
///
/// a.difference_with(&b);
/// assert_eq!(a.unwrap(), bitv::from_bytes([0b01001000]));
/// let bva = BitvSet::from_bitv(bitv::from_bytes([a]));
/// let mut bvb = BitvSet::from_bitv(bitv::from_bytes([b]));
///
/// bvb.difference_with(&bva);
/// assert_eq!(bvb.unwrap(), bitv::from_bytes([b_a]));
/// ```
#[inline]
pub
fn
difference_with
(
&
mut
self
,
other
:
&
BitvSet
)
{
...
...
@@ -1336,11 +1378,15 @@ pub fn difference_with(&mut self, other: &BitvSet) {
/// use std::collections::BitvSet;
/// use std::collections::bitv;
///
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
/// let a = 0b01101000;
/// let b = 0b10100000;
/// let res = 0b11001000;
///
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([a]));
/// let b = BitvSet::from_bitv(bitv::from_bytes([b]));
///
/// a.symmetric_difference_with(&b);
/// assert_eq!(a.unwrap(), bitv::from_bytes([
0b11001000
]));
/// assert_eq!(a.unwrap(), bitv::from_bytes([
res
]));
/// ```
#[inline]
pub
fn
symmetric_difference_with
(
&
mut
self
,
other
:
&
BitvSet
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录