emap/next_key.rs
1// Copyright (c) 2023 Yegor Bugayenko
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included
11// in all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::Map;
22
23impl<V: Clone> Map<V> {
24 /// Get the next key available for insertion.
25 ///
26 /// # Panics
27 ///
28 /// If no more keys left.
29 #[inline]
30 #[must_use]
31 pub fn next_key(&self) -> usize {
32 self.next_key_gte(0)
33 }
34
35 /// Get the next key available for insertion, which is "greater or equal"
36 /// than the number provided.
37 ///
38 /// # Panics
39 ///
40 /// If no more keys left.
41 ///
42 /// It may also panic in "debug" mode if the Map is not initialized.
43 #[inline]
44 #[must_use]
45 pub fn next_key_gte(&self, k: usize) -> usize {
46 #[cfg(debug_assertions)]
47 assert!(
48 self.initialized,
49 "Can't do next_key_gte() on non-initialized Map"
50 );
51 let mut i = k;
52 loop {
53 if i == self.max {
54 break;
55 }
56 if i > self.max {
57 return i;
58 }
59 if self.get(i).is_none() {
60 return i;
61 }
62 i += 1;
63 }
64 assert_ne!(self.max, self.layout.size(), "No more keys available left");
65 self.max
66 }
67}
68
69#[test]
70fn get_next_key_empty_map() {
71 let m: Map<&str> = Map::with_capacity_none(16);
72 assert_eq!(0, m.next_key());
73}
74
75#[test]
76fn get_next_in_the_middle() {
77 let mut m: Map<u32> = Map::with_capacity_none(16);
78 m.insert(0, 42);
79 m.insert(1, 42);
80 m.remove(1);
81 m.insert(2, 42);
82 assert_eq!(1, m.next_key());
83}
84
85#[test]
86fn get_next_over() {
87 let mut m: Map<u32> = Map::with_capacity_none(16);
88 m.insert(2, 42);
89 assert_eq!(5, m.next_key_gte(5));
90}
91
92#[test]
93fn reset_next_key_on_clear() {
94 let mut m: Map<u32> = Map::with_capacity_none(16);
95 m.insert(0, 42);
96 assert_eq!(1, m.next_key());
97 m.clear();
98 assert_eq!(0, m.next_key());
99}
100
101#[test]
102#[should_panic]
103fn panics_on_end_of_keys() {
104 let mut m: Map<u32> = Map::with_capacity_none(1);
105 m.insert(0, 42);
106 assert_ne!(1, m.next_key());
107}