Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fp-succinct-trees-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Mehren
fp-succinct-trees-1
Commits
d87f1688
Unverified
Commit
d87f1688
authored
7 years ago
by
Daniel Rose
Browse files
Options
Downloads
Patches
Plain Diff
[min_max] fixed select_1 method
parent
45326b9d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/datastructures/min_max.rs
+34
-13
34 additions, 13 deletions
src/datastructures/min_max.rs
with
34 additions
and
13 deletions
src/datastructures/min_max.rs
+
34
−
13
View file @
d87f1688
...
...
@@ -173,7 +173,7 @@ impl MinMax {
fn
is_leaf
(
&
self
,
index
:
usize
)
->
bool
{
if
index
>=
self
.heap
.len
()
/
2
{
return
true
return
true
;
}
false
}
...
...
@@ -452,22 +452,35 @@ impl MinMax {
pub
fn
select_1
(
&
self
,
rank
:
u64
)
->
Result
<
u64
,
NodeError
>
{
let
index
=
self
.select_1_recursive
(
rank
as
i64
,
0
)
as
u64
;
if
index
>=
self
.bits_len
{
return
Err
(
NodeError
::
NotANodeError
)
return
Err
(
NodeError
::
NotANodeError
)
;
}
Ok
(
index
)
}
// TODO: not working correctly
fn
select_1_recursive
(
&
self
,
bits_index
:
i64
,
heap_index
:
usize
)
->
i64
{
fn
select_1_recursive
(
&
self
,
rank
:
i64
,
heap_index
:
usize
)
->
i64
{
if
self
.is_leaf
(
heap_index
)
{
return
bits_index
}
else
if
self
.ones_for_node
(
self
.left_child
(
heap_index
))
>=
bits_index
{
return
self
.select_1_recursive
(
bits_index
,
self
.left_child
(
heap_index
))
// recursion termination: return index of kth "1" in block for k = rank
let
block_no
=
(
heap_index
-
self
.heap
.len
()
/
2
)
as
i64
;
let
begin_of_block
=
block_no
*
self
.block_size
as
i64
;
let
end_of_block
=
begin_of_block
+
self
.block_size
as
i64
;
let
mut
remaining_rank
=
rank
;
let
mut
index
=
begin_of_block
;
for
k
in
begin_of_block
..
end_of_block
{
if
self
.bits
[
k
as
u64
]
&&
remaining_rank
>
0
{
remaining_rank
-=
1
;
index
=
k
;
}
}
return
index
;
}
else
if
self
.ones_for_node
(
self
.left_child
(
heap_index
))
>=
rank
{
// case: the sought index belongs to left child: recursive call for lc with rank
return
self
.select_1_recursive
(
rank
,
self
.left_child
(
heap_index
));
}
else
{
// case the sought index belongs to right child: recursive call
// for rc with rank - 1s belonging to left child
self
.select_1_recursive
(
heap_index
as
i64
-
self
.ones_for_node
(
self
.left_child
(
heap_index
)),
self
.bits_for_node
(
self
.left_child
(
heap_index
))
as
usize
+
self
.right_child
(
heap_index
)
rank
as
i64
-
self
.ones_for_node
(
self
.left_child
(
heap_index
)),
self
.right_child
(
heap_index
),
)
}
}
...
...
@@ -698,15 +711,23 @@ mod tests {
let
bits
=
bit_vec!
[
true
,
true
,
false
,
false
];
let
min_max
=
MinMax
::
new
(
bits
,
1
);
assert_eq!
(
min_max
.ones_for_node
(
0
),
2
);
assert_eq!
(
min_max
.ones_for_node
(
1
),
2
);
assert_eq!
(
min_max
.ones_for_node
(
6
),
0
);
}
#[test]
#[ignore]
fn
test_select_1
()
{
let
bits
=
bit_vec!
[
true
,
true
,
false
,
false
];
let
min_max
=
MinMax
::
new
(
bits
,
1
);
assert_eq!
(
min_max
.select_1
(
2
)
.unwrap
(),
1
);
let
bits
=
bit_vec!
[
true
,
true
,
true
,
false
,
true
,
false
,
true
,
true
,
false
,
false
,
false
,
true
,
false
,
true
,
true
,
true
,
false
,
true
,
false
,
false
,
false
,
false
];
let
min_max
=
MinMax
::
new
(
bits
,
4
);
assert_eq!
(
min_max
.select_1
(
4
)
.unwrap
(),
4
);
assert_eq!
(
min_max
.select_1
(
10
)
.unwrap
(),
15
);
assert_eq!
(
min_max
.select_1
(
11
)
.unwrap
(),
17
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment