static-mapset 0.1.0

Maps and sets based on ordered array or slice
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use core::{
	borrow::Borrow,
	ops::{Bound, Index, RangeBounds}
};

use crate::internal::ordered_map::{self as internal, order};

/// An iterator over the entries of a `OrderedSliceMap`.
///
/// This `struct` is created by the [`iter`] method on [`OrderedSliceMap`]. See its
/// documentation for more.
///
/// [`iter`]: OrderedSliceMap::iter
pub type Iter<'a, K, V> = internal::Iter<'a, K, V>;

/// An iterator over the keys of a `OrderedSliceMap`.
///
/// This `struct` is created by the [`keys`] method on [`OrderedSliceMap`]. See its
/// documentation for more.
///
/// [`keys`]: OrderedSliceMap::keys
pub type Keys<'a, K, V> = internal::Keys<'a, K, V>;

/// An iterator over a sub-range of entries in a `OrderedSliceMap`.
///
/// This `struct` is created by the [`range`] method on [`OrderedSliceMap`]. See its
/// documentation for more.
///
/// [`range`]: OrderedSliceMap::range
pub type Range<'a, K, V> = internal::Iter<'a, K, V>;

/// An iterator over the values of a `OrderedSliceMap`.
///
/// This `struct` is created by the [`values`] method on [`OrderedSliceMap`]. See its
/// documentation for more.
///
/// [`values`]: OrderedSliceMap::values
pub type Values<'a, K, V> = internal::Values<'a, K, V>;

/// A cursor over a `OrderedSliceMap`.
///
/// A `Cursor` is like an iterator, except that it can freely seek back-and-forth.
///
/// Cursors always point to a gap between two elements in the map, and can
/// operate on the two immediately adjacent elements.
///
/// A `Cursor` is created with the [`OrderedSliceMap::lower_bound`] and [`OrderedSliceMap::upper_bound`] methods.
pub type Cursor<'a, K, V> = internal::Cursor<'a, K, V>;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// An ordered map based on an ordered slice.
/// This structures provides a subset of the methods offered by the standard library's
/// `BTreeMap`.
///
/// # Examples
///
/// ```
/// use static_mapset::OrderedSliceMap;
///
/// let movie_reviews_data = [
///     ("Office Space", "Deals with real issues in the workplace."),
///     ("Pulp Fiction", "Masterpiece."),
///     ("The Godfather", "Very enjoyable."),
///     ("The Blues Brothers", "Eye lyked it a lot.")
/// ];
/// let movie_reviews = OrderedSliceMap::new_unchecked(&movie_reviews_data);
///
/// // check for a specific one.
/// if !movie_reviews.contains_key("Les Misérables") {
///     println!("We've got {} reviews, but Les Misérables ain't one.",
///              movie_reviews.len());
/// }
///
/// // look up the values associated with some keys.
/// let to_find = ["Up!", "Office Space"];
/// for movie in &to_find {
///     match movie_reviews.get(movie) {
///        Some(review) => println!("{movie}: {review}"),
///        None => println!("{movie} is unreviewed.")
///     }
/// }
///
/// // Look up the value for a key (will panic if the key is not found).
/// println!("Movie review: {}", movie_reviews[&"Office Space"]);
///
/// // iterate over everything.
/// for (movie, review) in &movie_reviews {
///     println!("{movie}: \"{review}\"");
/// }
/// ```
pub struct OrderedSliceMap<'a, K, V> {
	inner: &'a [(K, V)],
	len: usize
}

impl<'a, K, V> Clone for OrderedSliceMap<'a, K, V> {
	fn clone(&self) -> Self {
		*self
	}
}

impl<'a, K, V> Copy for OrderedSliceMap<'a, K, V> {}

impl<'a, K, V> OrderedSliceMap<'a, K, V> {
	/// Sorts the slice and creates an `OrderedSliceMap`.
	/// Duplicate keys are removed, keeping the last occurrence.
	///
	/// # Complexity
	///
	/// Linearithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMap::new(&mut data);
	/// assert_eq!(map.first_key_value(), Some((&1, &"a")));
	/// ```
	pub fn new(items: &'a mut [(K, V)]) -> Self
	where
		K: Ord
	{
		let len = order(items);
		OrderedSliceMap { inner: items, len }
	}

	/// Creates an `OrderedSliceMap` from an immutable slice without sorting.
	///
	/// The slice must be sorted in ascending order with no duplicate keys.
	///
	/// # Complexity
	///
	/// Constant.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// static MAP: OrderedSliceMap<i32, &str> = OrderedSliceMap::new_unchecked(&[(1, "a"), (2, "b")]);
	/// assert_eq!(MAP.get(&2), Some(&"b"));
	///
	/// // Incorrect key order or duplicates cause undefined behavior:
	/// // let map = OrderedSliceMap::new_unchecked(&[(2, "a"), (1, "b")]);
	/// // let map = OrderedSliceMap::new_unchecked(&[(1, "a"), (1, "b")]);
	/// ```
	pub const fn new_unchecked(ordered_items: &'a [(K, V)]) -> Self {
		OrderedSliceMap {
			inner: ordered_items,
			len: ordered_items.len()
		}
	}

	/// Returns `true` if the map is ordered correctly.
	///
	/// # Complexity
	///
	/// Linear in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let map = OrderedSliceMap::new_unchecked(&[(1, "a"), (2, "b"), (3, "b")]);
	/// assert!(map.is_valid());
	///
	/// let map = OrderedSliceMap::new_unchecked(&[(1, "a"), (2, "b"), (2, "c")]);
	/// assert!(!map.is_valid());
	/// let map = OrderedSliceMap::new_unchecked(&[(1.0, "a"), (2.0, "b"), (f64::NAN, "c")]);
	/// assert!(!map.is_valid());
	/// ```
	pub fn is_valid(&self) -> bool
	where
		K: PartialOrd
	{
		internal::is_valid(&self.inner[..self.len])
	}

	/// Returns `true` if the map contains a value for the specified key.
	///
	/// The key may be any borrowed form of the map's key type, but the ordering
	/// on the borrowed form *must* match the ordering on the key type.
	/// If equal keys are present, this returns `true` if any matching key exists.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let data = [(1, "a")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// assert_eq!(map.contains_key(&1), true);
	/// assert_eq!(map.contains_key(&2), false);
	///
	/// let data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// // f64::NAN is incomparable, so key lookups by NaN never match.
	/// assert_eq!(map.contains_key(&f64::NAN), false);
	/// ```
	pub fn contains_key<Q>(&self, key: &Q) -> bool
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::contains_key(&self.inner[..self.len], key)
	}

	/// Returns the first key-value pair in the map.
	/// The key in this pair is the minimum key in the map.
	///
	/// # Complexity
	///
	/// Constant.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let empty: &[(i32, &str)] = &[];
	/// let map = OrderedSliceMap::new_unchecked(empty);
	/// assert_eq!(map.first_key_value(), None);
	///
	/// let data = [(1, "b"), (2, "a")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// assert_eq!(map.first_key_value(), Some((&1, &"b")));
	/// ```
	pub fn first_key_value(&self) -> Option<(&K, &V)> {
		internal::first_key_value(&self.inner[..self.len])
	}

	/// Returns a reference to the value corresponding to the key.
	///
	/// The key may be any borrowed form of the map's key type, but the ordering
	/// on the borrowed form *must* match the ordering on the key type.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let data = [(1, "a")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// assert_eq!(map.get(&1), Some(&"a"));
	/// assert_eq!(map.get(&2), None);
	///
	/// let data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// // f64::NAN is incomparable, so key lookups by NaN never match.
	/// assert_eq!(map.get(&f64::NAN), None);
	/// ```
	pub fn get<Q>(&self, key: &Q) -> Option<&V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::get(&self.inner[..self.len], key)
	}

	/// Returns the key-value pair corresponding to the supplied key.
	///
	/// The supplied key may be any borrowed form of the map's key type, but the ordering
	/// on the borrowed form *must* match the ordering on the key type.
	/// If equal keys are present, the first matching entry in key order is returned.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use std::cmp::Ordering;
	/// use static_mapset::OrderedSliceMap;
	///
	/// #[derive(Clone, Copy, Debug)]
	/// struct S {
	///     id: u32,
	/// #   #[allow(unused)] // prevents a "field `name` is never read" error
	///     name: &'static str, // ignored by equality and ordering operations
	/// }
	///
	/// impl PartialEq for S {
	///     fn eq(&self, other: &S) -> bool {
	///         self.id == other.id
	///     }
	/// }
	///
	/// impl Eq for S {}
	///
	/// impl PartialOrd for S {
	///     fn partial_cmp(&self, other: &S) -> Option<Ordering> {
	///         self.id.partial_cmp(&other.id)
	///     }
	/// }
	///
	/// impl Ord for S {
	///     fn cmp(&self, other: &S) -> Ordering {
	///         self.id.cmp(&other.id)
	///     }
	/// }
	///
	/// let j_a = S { id: 1, name: "Jessica" };
	/// let j_b = S { id: 1, name: "Jess" };
	/// let p = S { id: 2, name: "Paul" };
	/// assert_eq!(j_a, j_b);
	///
	/// let data = [(j_a, "Paris")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
	/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
	/// assert_eq!(map.get_key_value(&p), None);
	///
	/// let data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// // f64::NAN is incomparable, so key lookups by NaN never match.
	/// assert_eq!(map.get_key_value(&f64::NAN), None);
	/// ```
	pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::get_key_value(&self.inner[..self.len], key)
	}

	/// Returns `true` if the map contains no elements.
	///
	/// # Complexity
	///
	/// Constant.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let map: OrderedSliceMap<i32, &str> = OrderedSliceMap::new_unchecked(&[]);
	/// assert!(map.is_empty());
	/// let map = OrderedSliceMap::new_unchecked(&[(1, "a")]);
	/// assert!(!map.is_empty());
	/// ```
	pub const fn is_empty(&self) -> bool {
		self.len == 0
	}

	/// Gets an iterator over the entries of the map, sorted by key.
	///
	/// # Complexity
	///
	/// Linear in `self.len()` to iterate all elements.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMap::new(&mut data);
	///
	/// let (first_key, first_value) = map.iter().next().unwrap();
	/// assert_eq!((*first_key, *first_value), (1, "a"));
	/// ```
	pub fn iter(&self) -> Iter<'_, K, V> {
		internal::iter(&self.inner[..self.len])
	}

	/// Gets an iterator over the keys of the map, in sorted order.
	///
	/// # Complexity
	///
	/// Linear in `self.len()` to iterate all elements.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMap::new(&mut data);
	///
	/// let keys: Vec<_> = map.keys().cloned().collect();
	/// assert_eq!(keys, [1, 2]);
	/// ```
	pub fn keys(&self) -> Keys<'_, K, V> {
		internal::keys(&self.inner[..self.len])
	}

	/// Returns the last key-value pair in the map.
	/// The key in this pair is the maximum key in the map.
	///
	/// # Complexity
	///
	/// Constant.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let data = [(1, "b"), (2, "a")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// assert_eq!(map.last_key_value(), Some((&2, &"a")));
	/// ```
	pub fn last_key_value(&self) -> Option<(&K, &V)> {
		internal::last_key_value(&self.inner[..self.len])
	}

	/// Returns the number of elements in the map.
	///
	/// # Complexity
	///
	/// Constant.
	pub const fn len(&self) -> usize {
		self.len
	}

	/// Returns a [`Cursor`] pointing at the gap before the smallest key
	/// greater than the given bound.
	///
	/// Passing `Bound::Included(x)` will return a cursor pointing to the
	/// gap before the smallest key greater than or equal to `x`.
	///
	/// Passing `Bound::Excluded(x)` will return a cursor pointing to the
	/// gap before the smallest key greater than `x`.
	///
	/// Passing `Bound::Unbounded` will return a cursor pointing to the
	/// gap before the smallest key in the map.
	/// With equal keys, `Included` points before the first equal key and `Excluded` points after the last equal key.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()` to find the bounds, and linear in the number of yielded elements to iterate them all.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	/// use std::ops::Bound;
	///
	/// let data = [
	///     (1, "a"),
	///     (2, "b"),
	///     (3, "c"),
	///     (4, "d"),
	/// ];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	///
	/// let cursor = map.lower_bound(Bound::Included(&2));
	/// assert_eq!(cursor.peek_prev(), Some((&1, &"a")));
	/// assert_eq!(cursor.peek_next(), Some((&2, &"b")));
	///
	/// let cursor = map.lower_bound(Bound::Excluded(&2));
	/// assert_eq!(cursor.peek_prev(), Some((&2, &"b")));
	/// assert_eq!(cursor.peek_next(), Some((&3, &"c")));
	///
	/// let cursor = map.lower_bound(Bound::Unbounded);
	/// assert_eq!(cursor.peek_prev(), None);
	/// assert_eq!(cursor.peek_next(), Some((&1, &"a")));
	///
	/// let data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// // f64::NAN is incomparable, so the cursor points to the gap after the last key.
	/// let cursor = map.lower_bound(Bound::Included(&f64::NAN));
	/// assert_eq!(cursor.peek_prev(), Some((&2.0, &"b")));
	/// ```
	pub fn lower_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::lower_bound(&self.inner[..self.len], bound)
	}

	/// Constructs a double-ended iterator over a sub-range of elements in the map.
	/// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will
	/// yield elements from min (inclusive) to max (exclusive).
	/// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
	/// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive
	/// range from 4 to 10.
	///
	/// # Panics
	///
	/// Panics if range `start > end`.
	/// Panics if range `start == end` and both bounds are `Excluded`.
	/// Panics if `start` (or `end`() is incomparable and `end` (or `start`) is bounded.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	/// use std::ops::Bound::Included;
	///
	/// let data = [
	///     (3, "a"),
	///     (5, "b"),
	///     (8, "c"),
	/// ];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// for (&key, &value) in map.range((Included(&4), Included(&8))) {
	///     println!("{key}: {value}");
	/// }
	/// assert_eq!(Some((&5, &"b")), map.range(4..).next());
	/// ```
	pub fn range<Q, R>(&self, range: R) -> Range<'_, K, V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized,
		R: RangeBounds<Q>
	{
		internal::range(&self.inner[..self.len], range)
	}

	/// Returns a [`Cursor`] pointing at the gap after the greatest key
	/// smaller than the given bound.
	///
	/// Passing `Bound::Included(x)` will return a cursor pointing to the
	/// gap after the greatest key smaller than or equal to `x`.
	///
	/// Passing `Bound::Excluded(x)` will return a cursor pointing to the
	/// gap after the greatest key smaller than `x`.
	///
	/// Passing `Bound::Unbounded` will return a cursor pointing to the
	/// gap after the greatest key in the map.
	/// With equal keys, `Excluded` points before the first equal key and `Included` points after the last equal key.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	///
	/// use static_mapset::OrderedSliceMap;
	/// use std::ops::Bound;
	///
	/// let data = [
	///     (1, "a"),
	///     (2, "b"),
	///     (3, "c"),
	///     (4, "d"),
	/// ];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	///
	/// let cursor = map.upper_bound(Bound::Included(&3));
	/// assert_eq!(cursor.peek_prev(), Some((&3, &"c")));
	/// assert_eq!(cursor.peek_next(), Some((&4, &"d")));
	///
	/// let cursor = map.upper_bound(Bound::Excluded(&3));
	/// assert_eq!(cursor.peek_prev(), Some((&2, &"b")));
	/// assert_eq!(cursor.peek_next(), Some((&3, &"c")));
	///
	/// let cursor = map.upper_bound(Bound::Unbounded);
	/// assert_eq!(cursor.peek_prev(), Some((&4, &"d")));
	/// assert_eq!(cursor.peek_next(), None);
	///
	/// let data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMap::new_unchecked(&data);
	/// // f64::NAN is incomparable, so the cursor points to the gap after the last key.
	/// let cursor = map.upper_bound(Bound::Included(&f64::NAN));
	/// assert_eq!(cursor.peek_prev(), Some((&2.0, &"b")));
	/// ```
	pub fn upper_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::upper_bound(&self.inner[..self.len], bound)
	}

	/// Gets an iterator over the values of the map, in order by key.
	///
	/// # Complexity
	///
	/// Linear in `self.len()` to iterate all elements.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMap;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMap::new(&mut data);
	///
	/// let values: Vec<_> = map.values().cloned().collect();
	/// assert_eq!(values, ["a", "b"]);
	/// ```
	pub fn values(&self) -> Values<'_, K, V> {
		internal::values(&self.inner[..self.len])
	}
}

impl<'a, K, V> IntoIterator for &'a OrderedSliceMap<'a, K, V> {
	type Item = (&'a K, &'a V);
	type IntoIter = Iter<'a, K, V>;

	fn into_iter(self) -> Self::IntoIter {
		self.iter()
	}
}

impl<'a, K, V, Q> Index<&Q> for OrderedSliceMap<'a, K, V>
where
	K: Borrow<Q>,
	Q: Ord
{
	type Output = V;

	fn index(&self, key: &Q) -> &Self::Output {
		self.get(key).expect("no entry found for key")
	}
}

#[cfg(test)]
mod tests {
	use core::ops::Bound;

	use super::*;

	#[test]
	fn test_get() {
		let data = [(1, 'a'), (3, 'c'), (5, 'e')];
		let map = OrderedSliceMap::new_unchecked(&data);
		assert_eq!(map.get(&0), None);
		assert_eq!(map.get(&1), Some(&'a'));
		assert_eq!(map.get(&2), None);
		assert_eq!(map.get(&3), Some(&'c'));
		assert_eq!(map.get(&5), Some(&'e'));
		assert_eq!(map.get(&6), None);
	}

	#[test]
	fn test_contains_key() {
		let data = [(2, 4), (4, 16), (6, 36)];
		let map = OrderedSliceMap::new_unchecked(&data);
		assert!(map.contains_key(&2));
		assert!(map.contains_key(&4));
		assert!(!map.contains_key(&3));
		assert!(!map.contains_key(&7));
	}

	#[test]
	fn test_first_last() {
		let data = [(1, 1), (2, 4), (3, 9)];
		let map = OrderedSliceMap::new_unchecked(&data);
		assert_eq!(map.first_key_value(), Some((&1, &1)));
		assert_eq!(map.last_key_value(), Some((&3, &9)));

		let empty: &[(i32, i32)] = &[];
		let empty_map = OrderedSliceMap::new_unchecked(empty);
		assert_eq!(empty_map.first_key_value(), None);
		assert_eq!(empty_map.last_key_value(), None);
	}

	#[test]
	fn test_iter() {
		let data = [(1, 'a'), (2, 'b'), (3, 'c')];
		let map = OrderedSliceMap::new_unchecked(&data);
		let collected: Vec<_> = map.iter().collect();
		assert_eq!(collected, vec![(&1, &'a'), (&2, &'b'), (&3, &'c')]);
	}

	#[test]
	fn test_into_iter_for_ref() {
		let data = [(10, 100), (20, 200)];
		let map = OrderedSliceMap::new_unchecked(&data);
		let collected: Vec<_> = (&map).into_iter().collect();
		assert_eq!(collected, vec![(&10, &100), (&20, &200)]);
	}

	#[test]
	fn test_index() {
		let data = [(5, 50), (10, 100)];
		let map = OrderedSliceMap::new_unchecked(&data);
		assert_eq!(map[&5], 50);
		assert_eq!(map[&10], 100);
	}

	#[test]
	fn test_lower_bound() {
		let data = [(1, 1), (3, 9), (5, 25)];
		let map = OrderedSliceMap::new_unchecked(&data);
		assert_eq!(map.lower_bound(Bound::Included(&2)).next(), Some((&3, &9)));
		assert_eq!(map.lower_bound(Bound::Excluded(&3)).next(), Some((&5, &25)));
		assert_eq!(map.lower_bound(Bound::Unbounded).next(), Some((&1, &1)));
		assert_eq!(map.lower_bound(Bound::Included(&6)).next(), None);
	}

	#[test]
	fn test_upper_bound() {
		let data = [(1, 1), (3, 9), (5, 25)];
		let map = OrderedSliceMap::new_unchecked(&data);
		assert_eq!(map.upper_bound(Bound::Included(&3)).next(), Some((&5, &25)));
		assert_eq!(map.upper_bound(Bound::Excluded(&3)).next(), Some((&3, &9)));
		assert_eq!(map.upper_bound(Bound::Unbounded).next(), None);
	}

	#[test]
	fn test_is_valid() {
		let sorted = [(1, 1), (2, 4), (3, 9)];
		assert!(OrderedSliceMap::new_unchecked(&sorted).keys().is_sorted());
		let unsorted = [(3, 9), (1, 1)];
		assert!(!OrderedSliceMap::new_unchecked(&unsorted).keys().is_sorted());
	}
}