pub struct FloorQuotientIndex {
num: usize,
sqrt: usize,
pivot: usize,
}Expand description
sorted({ floor(n/k) | k in [1, n] })
Fields§
§num: usize§sqrt: usize§pivot: usizeImplementations§
Source§impl FloorQuotientIndex
impl FloorQuotientIndex
Sourcepub fn values(
&self,
) -> impl ExactSizeIterator<Item = usize> + FusedIterator + '_
pub fn values( &self, ) -> impl ExactSizeIterator<Item = usize> + FusedIterator + '_
pub fn key_values( &self, ) -> impl ExactSizeIterator<Item = (RangeInclusive<usize>, usize)> + FusedIterator + '_
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Examples found in repository?
crates/competitive/src/algorithm/quotient_index.rs (line 22)
21 pub fn values(&self) -> impl ExactSizeIterator<Item = usize> + FusedIterator + '_ {
22 let len = self.len();
23 (0..len).map(move |index| unsafe { self.index_to_value_unchecked(index) })
24 }
25
26 pub fn key_values(
27 &self,
28 ) -> impl ExactSizeIterator<Item = (RangeInclusive<usize>, usize)> + FusedIterator + '_ {
29 let len = self.len();
30 (0..len).map(move |index| unsafe {
31 (
32 self.index_to_key_unchecked(index),
33 self.index_to_value_unchecked(index),
34 )
35 })
36 }
37
38 #[allow(clippy::len_without_is_empty)]
39 pub fn len(&self) -> usize {
40 2 * self.sqrt - (self.pivot == self.sqrt) as usize
41 }
42
43 pub fn contains_key(&self, key: usize) -> bool {
44 (1..=self.num).contains(&key)
45 }
46
47 pub fn contains_value(&self, value: usize) -> bool {
48 if value == 0 || value > self.num {
49 return false;
50 }
51 if value == usize::MAX {
52 return self.num == usize::MAX;
53 }
54 self.num / value > self.num / (value + 1)
55 }
56
57 pub fn index_to_key(&self, index: usize) -> RangeInclusive<usize> {
58 assert!(index < self.len(), "index out of bounds");
59 unsafe { self.index_to_key_unchecked(index) }
60 }
61
62 pub fn index_to_value(&self, index: usize) -> usize {
63 assert!(index < self.len(), "index out of bounds");
64 unsafe { self.index_to_value_unchecked(index) }
65 }
66
67 pub fn value_to_index(&self, value: usize) -> usize {
68 assert!(self.contains_value(value), "value is not present");
69 unsafe { self.value_to_index_unchecked(value) }
70 }
71
72 pub fn value_to_key(&self, value: usize) -> RangeInclusive<usize> {
73 assert!(self.contains_value(value), "value is not present");
74 unsafe { self.value_to_key_unchecked(value) }
75 }
76
77 pub fn key_to_index(&self, key: usize) -> usize {
78 assert!(self.contains_key(key), "key out of bounds");
79 let value = self.key_to_value(key);
80 unsafe { self.value_to_index_unchecked(value) }
81 }
82
83 pub fn key_to_value(&self, key: usize) -> usize {
84 assert!(self.contains_key(key), "key out of bounds");
85 unsafe { self.key_to_value_unchecked(key) }
86 }
87
88 /// # Safety
89 /// `index` must satisfy `index < self.len()`.
90 pub unsafe fn index_to_key_unchecked(&self, index: usize) -> RangeInclusive<usize> {
91 if index < self.sqrt {
92 self.num / (index + 2) + 1..=self.num / (index + 1)
93 } else {
94 let key = self.len() - index;
95 key..=key
96 }
97 }
98
99 /// # Safety
100 /// `index` must satisfy `index < self.len()`.
101 pub unsafe fn index_to_value_unchecked(&self, index: usize) -> usize {
102 if index < self.sqrt {
103 index + 1
104 } else {
105 self.num / (self.len() - index)
106 }
107 }
108
109 /// # Safety
110 /// `value` must satisfy `self.contains_value(value)`.
111 pub unsafe fn value_to_index_unchecked(&self, value: usize) -> usize {
112 if value <= self.sqrt {
113 value - 1
114 } else {
115 self.len() - self.num / value
116 }
117 }
118
119 /// # Safety
120 /// `value` must satisfy `self.contains_value(value)`.
121 pub unsafe fn value_to_key_unchecked(&self, value: usize) -> RangeInclusive<usize> {
122 if value == usize::MAX {
123 return 1..=1;
124 }
125 let start = (self.num / (value + 1)) + 1;
126 let end = self.num / value;
127 start..=end
128 }
129
130 /// # Safety
131 /// `key` must satisfy `1 <= key && key <= self.num`.
132 pub unsafe fn key_to_index_unchecked(&self, key: usize) -> usize {
133 let len = self.len();
134 if key <= len - self.sqrt {
135 len - key
136 } else {
137 self.num / key - 1
138 }
139 }More examples
Sourcepub fn contains_key(&self, key: usize) -> bool
pub fn contains_key(&self, key: usize) -> bool
Sourcepub fn contains_value(&self, value: usize) -> bool
pub fn contains_value(&self, value: usize) -> bool
pub fn index_to_key(&self, index: usize) -> RangeInclusive<usize>
pub fn index_to_value(&self, index: usize) -> usize
pub fn value_to_index(&self, value: usize) -> usize
pub fn value_to_key(&self, value: usize) -> RangeInclusive<usize>
pub fn key_to_index(&self, key: usize) -> usize
Sourcepub fn key_to_value(&self, key: usize) -> usize
pub fn key_to_value(&self, key: usize) -> usize
Sourcepub unsafe fn index_to_key_unchecked(
&self,
index: usize,
) -> RangeInclusive<usize>
pub unsafe fn index_to_key_unchecked( &self, index: usize, ) -> RangeInclusive<usize>
§Safety
index must satisfy index < self.len().
Examples found in repository?
crates/competitive/src/algorithm/quotient_index.rs (line 32)
26 pub fn key_values(
27 &self,
28 ) -> impl ExactSizeIterator<Item = (RangeInclusive<usize>, usize)> + FusedIterator + '_ {
29 let len = self.len();
30 (0..len).map(move |index| unsafe {
31 (
32 self.index_to_key_unchecked(index),
33 self.index_to_value_unchecked(index),
34 )
35 })
36 }
37
38 #[allow(clippy::len_without_is_empty)]
39 pub fn len(&self) -> usize {
40 2 * self.sqrt - (self.pivot == self.sqrt) as usize
41 }
42
43 pub fn contains_key(&self, key: usize) -> bool {
44 (1..=self.num).contains(&key)
45 }
46
47 pub fn contains_value(&self, value: usize) -> bool {
48 if value == 0 || value > self.num {
49 return false;
50 }
51 if value == usize::MAX {
52 return self.num == usize::MAX;
53 }
54 self.num / value > self.num / (value + 1)
55 }
56
57 pub fn index_to_key(&self, index: usize) -> RangeInclusive<usize> {
58 assert!(index < self.len(), "index out of bounds");
59 unsafe { self.index_to_key_unchecked(index) }
60 }Sourcepub unsafe fn index_to_value_unchecked(&self, index: usize) -> usize
pub unsafe fn index_to_value_unchecked(&self, index: usize) -> usize
§Safety
index must satisfy index < self.len().
Examples found in repository?
crates/competitive/src/algorithm/quotient_index.rs (line 23)
21 pub fn values(&self) -> impl ExactSizeIterator<Item = usize> + FusedIterator + '_ {
22 let len = self.len();
23 (0..len).map(move |index| unsafe { self.index_to_value_unchecked(index) })
24 }
25
26 pub fn key_values(
27 &self,
28 ) -> impl ExactSizeIterator<Item = (RangeInclusive<usize>, usize)> + FusedIterator + '_ {
29 let len = self.len();
30 (0..len).map(move |index| unsafe {
31 (
32 self.index_to_key_unchecked(index),
33 self.index_to_value_unchecked(index),
34 )
35 })
36 }
37
38 #[allow(clippy::len_without_is_empty)]
39 pub fn len(&self) -> usize {
40 2 * self.sqrt - (self.pivot == self.sqrt) as usize
41 }
42
43 pub fn contains_key(&self, key: usize) -> bool {
44 (1..=self.num).contains(&key)
45 }
46
47 pub fn contains_value(&self, value: usize) -> bool {
48 if value == 0 || value > self.num {
49 return false;
50 }
51 if value == usize::MAX {
52 return self.num == usize::MAX;
53 }
54 self.num / value > self.num / (value + 1)
55 }
56
57 pub fn index_to_key(&self, index: usize) -> RangeInclusive<usize> {
58 assert!(index < self.len(), "index out of bounds");
59 unsafe { self.index_to_key_unchecked(index) }
60 }
61
62 pub fn index_to_value(&self, index: usize) -> usize {
63 assert!(index < self.len(), "index out of bounds");
64 unsafe { self.index_to_value_unchecked(index) }
65 }Sourcepub unsafe fn value_to_index_unchecked(&self, value: usize) -> usize
pub unsafe fn value_to_index_unchecked(&self, value: usize) -> usize
§Safety
value must satisfy self.contains_value(value).
Examples found in repository?
crates/competitive/src/algorithm/quotient_index.rs (line 69)
67 pub fn value_to_index(&self, value: usize) -> usize {
68 assert!(self.contains_value(value), "value is not present");
69 unsafe { self.value_to_index_unchecked(value) }
70 }
71
72 pub fn value_to_key(&self, value: usize) -> RangeInclusive<usize> {
73 assert!(self.contains_value(value), "value is not present");
74 unsafe { self.value_to_key_unchecked(value) }
75 }
76
77 pub fn key_to_index(&self, key: usize) -> usize {
78 assert!(self.contains_key(key), "key out of bounds");
79 let value = self.key_to_value(key);
80 unsafe { self.value_to_index_unchecked(value) }
81 }Sourcepub unsafe fn value_to_key_unchecked(
&self,
value: usize,
) -> RangeInclusive<usize>
pub unsafe fn value_to_key_unchecked( &self, value: usize, ) -> RangeInclusive<usize>
§Safety
value must satisfy self.contains_value(value).
Sourcepub unsafe fn key_to_index_unchecked(&self, key: usize) -> usize
pub unsafe fn key_to_index_unchecked(&self, key: usize) -> usize
§Safety
key must satisfy 1 <= key && key <= self.num.
Sourcepub unsafe fn key_to_value_unchecked(&self, key: usize) -> usize
pub unsafe fn key_to_value_unchecked(&self, key: usize) -> usize
§Safety
key must satisfy 1 <= key && key <= self.num.
Auto Trait Implementations§
impl Freeze for FloorQuotientIndex
impl RefUnwindSafe for FloorQuotientIndex
impl Send for FloorQuotientIndex
impl Sync for FloorQuotientIndex
impl Unpin for FloorQuotientIndex
impl UnwindSafe for FloorQuotientIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more