orx_priority_queue/has_index.rs
1/// A struct which provides an index of type [usize].
2/// Index of the struct can be considered its unchanging id
3/// defined its position in a collection.
4///
5/// For instance consider a tour generation problem for cities in the collection {X, Y, Z}.
6/// Provided that this collection does not change, the cities can be identified by their indices:
7/// X => 0, Y => 1, Z => 2.
8///
9/// In certain algorithms, as well as, in certain priority queue implementations in this crate,
10/// this convention allows to replace a hashmap with an array.
11/// This may be useful in simplifying the algorithms and improving performance when
12/// the elements entering the queue are sampled from a closed and known set, as the cities above.
13pub trait HasIndex: Clone {
14 /// Returns the index of the element.
15 fn index(&self) -> usize;
16}
17
18impl HasIndex for usize {
19 #[inline(always)]
20 fn index(&self) -> usize {
21 *self
22 }
23}
24impl HasIndex for u64 {
25 #[inline(always)]
26 fn index(&self) -> usize {
27 *self as usize
28 }
29}
30impl HasIndex for u32 {
31 #[inline(always)]
32 fn index(&self) -> usize {
33 *self as usize
34 }
35}
36impl HasIndex for u16 {
37 #[inline(always)]
38 fn index(&self) -> usize {
39 *self as usize
40 }
41}
42impl HasIndex for u8 {
43 #[inline(always)]
44 fn index(&self) -> usize {
45 *self as usize
46 }
47}