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
use std::collections::HashMap;
pub use crate::array::Array;
pub trait Algorithm {
fn sort(&self, array: Array);
fn name(&self) -> String;
}
macro_rules! algorithms {
($($id:ident => $type:ident,)+) => {
$(
pub mod $id;
pub use self::$id::$type;
)*
pub fn all() -> HashMap<String, Box<dyn Algorithm + Send>> {
let mut algorithms: HashMap<String, Box<dyn Algorithm + Send>> =
HashMap::new();
$(algorithms.insert(stringify!($id).to_string(), Box::new(self::$id::$type));)*
algorithms
}
};
}
algorithms![
bubble => BubbleSort,
cocktail => CocktailSort,
cycle => CycleSort,
gnome => GnomeSort,
heap => HeapSort,
insertion => InsertionSort,
merge => MergeSort,
quicksort => Quicksort,
selection => SelectionSort,
shellsort => Shellsort,
all => All,
];