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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
use core::{
	borrow::Borrow,
	ops::{Bound, Index, RangeBounds}
};

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

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

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

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

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

/// A cursor over a `OrderedSliceMapMut`.
///
/// 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 [`OrderedSliceMapMut::lower_bound`] and [`OrderedSliceMapMut::upper_bound`] methods.
pub type Cursor<'a, K, V> = internal::Cursor<'a, K, V>;

/// A mutable iterator over the entries of a `OrderedSliceMapMut`.
///
/// This `struct` is created by the [`iter_mut`] method on [`OrderedSliceMapMut`]. See its
/// documentation for more.
///
/// [`iter_mut`]: OrderedSliceMapMut::iter_mut
pub type IterMut<'a, K, V> = internal::IterMut<'a, K, V>;

/// A mutable iterator over a sub-range of entries in a `OrderedSliceMapMut`.
///
/// This `struct` is created by the [`range_mut`] method on [`OrderedSliceMapMut`]. See its
/// documentation for more.
///
/// [`range_mut`]: OrderedSliceMapMut::range_mut
pub type RangeMut<'a, K, V> = internal::IterMut<'a, K, V>;

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

/// A cursor over a `OrderedSliceMapMut` with editing operations.
///
/// A `Cursor` is like an iterator, except that it can freely seek back-and-forth, and can
/// safely mutate the map during iteration. This is because the lifetime of its yielded
/// references is tied to its own lifetime, instead of just the underlying map. This means
/// cursors cannot yield multiple elements at once.
///
/// Cursors always point to a gap between two elements in the map, and can
/// operate on the two immediately adjacent elements.
///
/// A `CursorMut` is created with the [`OrderedSliceMapMut::lower_bound_mut`] and [`OrderedSliceMapMut::upper_bound_mut`]
/// methods.
pub type CursorMut<'a, K, V> = internal::CursorMut<'a, K, V>;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// An ordered map based on an ordered mutable slice.
/// This structures provides a subset of the methods offered by the standard library's
/// `BTreeMap`.
///
/// # Examples
///
/// ```
/// use static_mapset::OrderedSliceMapMut;
///
/// let mut movie_review_items = [
///     ("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 = OrderedSliceMapMut::new(&mut movie_review_items);
///
/// // 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.
/// assert_eq!(movie_reviews.get("Office Space"), Some(&"Deals with real issues in the workplace."));
///
/// // iterate over everything.
/// for (movie, review) in &movie_reviews {
///     println!("{movie}: \"{review}\"");
/// }
/// ```
pub struct OrderedSliceMapMut<'a, K, V> {
	inner: &'a mut [(K, V)],
	len: usize
}

impl<'a, K, V> OrderedSliceMapMut<'a, K, V> {
	/// Sorts the slice and creates an `OrderedSliceMapMut`.
	/// Duplicate keys are removed, keeping the last occurrence.
	///
	/// The slice is sorted by key. Duplicate keys are removed, keeping the last occurrence.
	///
	/// # Complexity
	///
	/// Linearithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMapMut;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMapMut::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);
		OrderedSliceMapMut { inner: items, len }
	}

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

	/// Returns `true` if the map is ordered correctly.
	///
	/// # Complexity
	///
	/// Linear in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMapMut;
	///
	/// let mut data = [(1, "a"), (2, "b"), (3, "b")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut data);
	/// assert!(map.is_valid());
	///
	/// let mut data = [(1, "a"), (2, "b"), (2, "c")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut data);
	/// assert!(!map.is_valid());
	/// let mut data = [(1.0, "a"), (2.0, "b"), (f64::NAN, "c")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut data);
	/// 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::OrderedSliceMapMut;
	///
	/// let mut data = [(1, "a")];
	/// let map = OrderedSliceMapMut::new(&mut data);
	/// assert_eq!(map.contains_key(&1), true);
	/// assert_eq!(map.contains_key(&2), false);
	///
	/// let mut data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
	///
	/// let mut empty: [(i32, &str); 0] = [];
	/// let mut map = OrderedSliceMapMut::new(&mut empty);
	/// assert_eq!(map.first_key_value(), None);
	///
	/// let mut data = [(1, "b"), (2, "a")];
	/// map = OrderedSliceMapMut::new(&mut 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::OrderedSliceMapMut;
	///
	/// let mut data = [(1, "a")];
	/// let map = OrderedSliceMapMut::new(&mut data);
	/// assert_eq!(map.get(&1), Some(&"a"));
	/// assert_eq!(map.get(&2), None);
	///
	/// let mut data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut 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. This is
	/// potentially useful:
	/// - for key types where non-identical keys can be considered equal;
	/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
	/// - for getting a reference to a key with the same lifetime as the collection.
	///
	/// 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::OrderedSliceMapMut;
	///
	/// #[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 mut data = [(j_a, "Paris")];
	/// let map = OrderedSliceMapMut::new(&mut 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 mut data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
	///
	/// let mut data: &mut [(i32, &str)] = &mut [];
	/// let map = OrderedSliceMapMut::new_unchecked(data);
	/// assert!(map.is_empty());
	/// let mut data = &mut [(1, "a")];
	/// let map = OrderedSliceMapMut::new_unchecked(data);
	/// 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::OrderedSliceMapMut;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMapMut::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::OrderedSliceMapMut;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMapMut::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::OrderedSliceMapMut;
	///
	/// let mut data = [(1, "b"), (2, "a")];
	/// let map = OrderedSliceMapMut::new(&mut 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::OrderedSliceMapMut;
	/// use std::ops::Bound;
	///
	/// let mut data = [
	///     (1, "a"),
	///     (2, "b"),
	///     (3, "c"),
	///     (4, "d"),
	/// ];
	/// let map = OrderedSliceMapMut::new(&mut 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 mut data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
	/// use std::ops::Bound::Included;
	///
	/// let mut data = [
	///     (3, "a"),
	///     (5, "b"),
	///     (8, "c"),
	/// ];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
	/// use std::ops::Bound;
	///
	/// let mut data = [
	///     (1, "a"),
	///     (2, "b"),
	///     (3, "c"),
	///     (4, "d"),
	/// ];
	/// let map = OrderedSliceMapMut::new(&mut 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 mut data = [(1.0, "a"), (2.0, "b")];
	/// let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
	///
	/// let mut data = [(2, "b"), (1, "a")];
	/// let map = OrderedSliceMapMut::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])
	}

	/// Returns a mutable 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.
	/// If equal keys are present, the first matching entry in key order is returned.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMapMut;
	///
	/// let mut data = [(1, "a")];
	/// let mut map = OrderedSliceMapMut::new(&mut data);
	/// assert_eq!(map.get_mut(&1), Some(&mut "a"));
	/// assert_eq!(map.get_mut(&2), None);
	///
	/// let mut data = [(1.0, "a"), (2.0, "b")];
	/// let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
	/// // f64::NAN is incomparable, so key lookups by NaN never match.
	/// assert_eq!(map.get_mut(&f64::NAN), None);
	/// ```
	pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::get_mut(&mut self.inner[..self.len], key)
	}

	/// Gets a mutable iterator over the entries of the map, sorted by key.
	///
	/// # Complexity
	///
	/// Linear in `self.len()` to iterate all elements.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMapMut;
	///
	/// let mut data = [
	///    ("a", 1),
	///    ("b", 2),
	///    ("c", 3),
	/// ];
	/// let mut map = OrderedSliceMapMut::new(&mut data);
	///
	/// // add 10 to the value if the key isn't "a"
	/// for (key, value) in map.iter_mut() {
	///     if key != &"a" {
	///         *value += 10;
	///     }
	/// }
	///
	/// let values: Vec<_> = map.values().cloned().collect();
	/// assert_eq!(values, [1, 12, 13]);
	/// ```
	pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
		internal::iter_mut(&mut self.inner[..self.len])
	}

	/// Returns a [`CursorMut`] 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.
	///
	/// # 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::OrderedSliceMapMut;
	/// use std::ops::Bound;
	///
	/// let mut data = [
	///     (1, "a"),
	///     (2, "b"),
	///     (3, "c"),
	///     (4, "d"),
	/// ];
	/// let mut map = OrderedSliceMapMut::new(&mut data);
	///
	/// let mut cursor = map.lower_bound_mut(Bound::Included(&2));
	/// assert_eq!(cursor.peek_prev(), Some((&1, &mut "a")));
	/// assert_eq!(cursor.peek_next(), Some((&2, &mut "b")));
	///
	/// let mut cursor = map.lower_bound_mut(Bound::Excluded(&2));
	/// assert_eq!(cursor.peek_prev(), Some((&2, &mut "b")));
	/// assert_eq!(cursor.peek_next(), Some((&3, &mut "c")));
	///
	/// let mut cursor = map.lower_bound_mut(Bound::Unbounded);
	/// assert_eq!(cursor.peek_prev(), None);
	/// assert_eq!(cursor.peek_next(), Some((&1, &mut "a")));
	///
	/// let mut data = [(1.0, "a"), (2.0, "b")];
	/// let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
	/// // f64::NAN is incomparable, so the cursor points to the gap after the last key.
	/// let mut cursor = map.lower_bound_mut(Bound::Included(&f64::NAN));
	/// assert_eq!(cursor.peek_prev(), Some((&2.0, &mut "b")));
	/// ```
	pub fn lower_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::lower_bound_mut(&mut self.inner[..self.len], bound)
	}

	/// Constructs a mutable 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`.
	/// If the range includes equal keys, all matching entries are yielded in key order.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	/// use static_mapset::OrderedSliceMapMut;
	///
	/// let mut data = [("Alice", 0), ("Bob", 0), ("Carol", 0), ("Cheryl", 0)];
	/// let mut map: OrderedSliceMapMut<'_, &str, i32> = OrderedSliceMapMut::new(&mut data);
	/// for (_, balance) in map.range_mut("B".."Cheryl") {
	///     *balance += 100;
	/// }
	/// for (name, balance) in &map {
	///     println!("{name} => {balance}");
	/// }
	/// ```
	pub fn range_mut<Q, R>(&mut self, range: R) -> RangeMut<'_, K, V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized,
		R: RangeBounds<Q>
	{
		internal::range_mut(&mut self.inner[..self.len], range)
	}

	/// Returns a [`CursorMut`] 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.
	///
	/// # Complexity
	///
	/// Logarithmic in `self.len()`.
	///
	/// # Examples
	///
	/// ```
	///
	/// use static_mapset::OrderedSliceMapMut;
	/// use std::ops::Bound;
	///
	/// let mut data = [
	///     (1, "a"),
	///     (2, "b"),
	///     (3, "c"),
	///     (4, "d"),
	/// ];
	/// let mut map = OrderedSliceMapMut::new(&mut data);
	///
	/// let mut cursor = map.upper_bound_mut(Bound::Included(&3));
	/// assert_eq!(cursor.peek_prev(), Some((&3, &mut "c")));
	/// assert_eq!(cursor.peek_next(), Some((&4, &mut "d")));
	///
	/// let mut cursor = map.upper_bound_mut(Bound::Excluded(&3));
	/// assert_eq!(cursor.peek_prev(), Some((&2, &mut "b")));
	/// assert_eq!(cursor.peek_next(), Some((&3, &mut "c")));
	///
	/// let mut cursor = map.upper_bound_mut(Bound::Unbounded);
	/// assert_eq!(cursor.peek_prev(), Some((&4, &mut "d")));
	/// assert_eq!(cursor.peek_next(), None);
	///
	/// let mut data = [(1.0, "a"), (2.0, "b")];
	/// let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
	/// // f64::NAN is incomparable, so the cursor points to the gap after the last key.
	/// let mut cursor = map.upper_bound_mut(Bound::Included(&f64::NAN));
	/// assert_eq!(cursor.peek_prev(), Some((&2.0, &mut "b")));
	/// ```
	pub fn upper_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V>
	where
		K: Borrow<Q>,
		Q: PartialOrd + ?Sized
	{
		internal::upper_bound_mut(&mut self.inner[..self.len], bound)
	}

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

impl<'a, K, V> IntoIterator for &'a OrderedSliceMapMut<'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> IntoIterator for &'a mut OrderedSliceMapMut<'a, K, V> {
	type Item = (&'a K, &'a mut V);
	type IntoIter = IterMut<'a, K, V>;

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

impl<'a, K, V, Q> Index<&Q> for OrderedSliceMapMut<'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 mut data = [(1, 'a'), (3, 'c'), (5, 'e')];
		let map = OrderedSliceMapMut::new_unchecked(&mut data);
		assert_eq!(map.get(&0), None);
		assert_eq!(map.get(&1), Some(&'a'));
		assert_eq!(map.get(&3), Some(&'c'));
		assert_eq!(map.get(&5), Some(&'e'));
		assert_eq!(map.get(&6), None);
	}

	#[test]
	fn test_get_mut() {
		let mut data = [(1, 10), (2, 20), (3, 30)];
		let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
		if let Some(v) = map.get_mut(&2) {
			*v = 200;
		}
		assert_eq!(map.get(&2), Some(&200));
		assert_eq!(map.get(&1), Some(&10));
	}

	#[test]
	fn test_iter_mut() {
		let mut data = [(1, 1), (2, 4), (3, 9)];
		let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
		for (_, v) in map.iter_mut() {
			*v *= 2;
		}
		assert_eq!(map.get(&1), Some(&2));
		assert_eq!(map.get(&2), Some(&8));
		assert_eq!(map.get(&3), Some(&18));
	}

	#[test]
	fn test_lower_bound() {
		let mut data = [(1, 1), (3, 9), (5, 25)];
		let map = OrderedSliceMapMut::new_unchecked(&mut 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_values_mut() {
		let mut data = [(1, 1), (2, 2), (3, 3)];
		let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
		for v in map.values_mut() {
			*v += 10;
		}
		assert_eq!(map.get(&1), Some(&11));
		assert_eq!(map.get(&2), Some(&12));
		assert_eq!(map.get(&3), Some(&13));
	}

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