Skip to content

Commit b0fe0a9

Browse files
committed
multi_set
1 parent 4ff9f26 commit b0fe0a9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod bounds;
22
pub mod cumsum;
33
pub mod euclidean;
4+
pub mod multi_set;
45
pub mod treap;
56
pub mod trie;
67
pub mod union_find;

src/multi_set.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::collections::BTreeSet;
2+
3+
const INF: usize = 1 << 60;
4+
#[derive(Debug)]
5+
pub struct MultiSet<T>
6+
where
7+
T: Ord,
8+
{
9+
set: BTreeSet<(T, usize)>,
10+
index: usize,
11+
}
12+
13+
impl<T> MultiSet<T>
14+
where
15+
T: Ord + std::fmt::Debug,
16+
{
17+
pub fn new() -> Self {
18+
Self {
19+
set: BTreeSet::new(),
20+
index: 0,
21+
}
22+
}
23+
24+
pub fn lower_bound(&self, x: T) -> Option<usize> {
25+
let cnt = self.set.range(..(x, INF)).count();
26+
if cnt == self.set.len() {
27+
None
28+
} else {
29+
Some(cnt)
30+
}
31+
}
32+
33+
pub fn lower_kth(&self, x: T, k: usize) -> Option<&(T, usize)> {
34+
self.set.range(..=(x, INF)).rev().nth(k)
35+
}
36+
37+
pub fn higer_kth(&self, x: T, k: usize) -> Option<&(T, usize)> {
38+
self.set.range((x, 0)..).nth(k)
39+
}
40+
41+
pub fn upper_bound(&self, x: T) -> Option<usize> {
42+
let cnt = self.set.range(..=(x, INF)).count();
43+
if cnt == self.set.len() {
44+
None
45+
} else {
46+
Some(cnt)
47+
}
48+
}
49+
50+
pub fn insert(&mut self, x: T) {
51+
self.set.insert((x, self.index));
52+
self.index += 1;
53+
}
54+
55+
pub fn is_empty(&self) -> bool {
56+
self.set.is_empty()
57+
}
58+
}

0 commit comments

Comments
 (0)