Skip to content

Commit 8fab8d3

Browse files
committed
2 parents eab4fc0 + 663589c commit 8fab8d3

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

.github/workflows/rust.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: Build
20+
run: cargo build --verbose
21+
- name: Run tests
22+
run: cargo test --verbose

rust_puzzle/src/utilities.rs

+93-1
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,96 @@ impl<'a> USizeSetIter<'a> {
9191

9292
const U64_BIT_SIZE: usize = mem::size_of::<u64>() * 8;
9393

94-
// line 113
94+
impl<'a> Iterator for USizeSetIter<'a> {
95+
type Item = usize;
96+
97+
fn next(&mut self) -> Option<usize> {
98+
loop {
99+
if let Some(bit_index) = self.current.next() {
100+
return Some(self.offset + bit_index);
101+
}
102+
103+
if let Some(&next_content) = self.content.next() {
104+
self.current = BitIterator::new(next_content);
105+
self.offset += U64_BIT_SIZE;
106+
}
107+
else {
108+
return None;
109+
}
110+
}
111+
}
112+
}
113+
114+
impl USizeSet {
115+
pub fn new(lower: usize, upper: usize) -> USizeSetResult<USizeSet> {
116+
if lower > upper {
117+
Err(USizeSetError::InvalidBounds)
118+
}
119+
else {
120+
let required_words = (upper - lower + 64) >> 6;
121+
Ok(USizeSet {
122+
lower, upper, len: 0, content: vec![0u64; required_words]
123+
})
124+
}
125+
}
126+
127+
pub fn singleton(lower: usize, upper: usize, content: usize) -> USizeSetResult<USizeSet> {
128+
let mut result = USizeSet::new(lower, upper)?;
129+
result.insert(content)?;
130+
Ok(result)
131+
}
132+
133+
pub fn range(lower: usize, upper: usize) -> USizeSetResult<USizeSet> {
134+
if lower > upper {
135+
Err(USizeSetError::InvalidBounds)
136+
}
137+
else {
138+
let mut content = Vec::new();
139+
let ones = upper - lower + 1;
140+
let ones_words = ones / U64_BIT_SIZE;
141+
142+
for _ in 0..ones_words {
143+
content.push(!0);
144+
}
145+
146+
let remaining_ones = ones - (ones_words << 6);
147+
148+
if remaining_ones > 0 {
149+
content.push((1 << remaining_ones) - 1);
150+
}
151+
152+
Ok(USizeSet {
153+
lower, upper, len:ones, content
154+
})
155+
}
156+
}
157+
158+
fn compute_index(&self, number: usize) -> USizeSetResult<(usize, u64)> {
159+
if number < self.lower || number > self.upper {
160+
Err(USizeSetError::OutOfBounds)
161+
}
162+
else {
163+
let index = number - self.lower;
164+
let word_index = index >> 6;
165+
let sub_word_index = index & 63;
166+
let mask = 1u64 << sub_word_index;
167+
Ok((word_index, mask))
168+
}
169+
}
170+
171+
pub fn lower(&self) -> usize {
172+
self.lower
173+
}
174+
175+
pub fn upper(&self) -> usize {
176+
self.upper
177+
}
178+
179+
pub fn min(&self) -> Option<usize> {
180+
for (index, &content) in self.content.iter().enumerate() {
181+
let trailing_zeros = content.trailing_zeros() as usize;
182+
183+
//line 266
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)