Struct valord_map::ValordMap

source ·
pub struct ValordMap<T, K, V: OrdBy<Target = T>> { /* private fields */ }

Implementations§

source§

impl<T, K, V> ValordMap<T, K, V>
where T: Ord + Clone, K: Hash + Eq, V: OrdBy<Target = T>,

source

pub fn new() -> Self

Examples found in repository?
examples/people_ord_by_age.rs (line 18)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn insert(&mut self, key: K, value: V)

insert into ValordMap

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);

let sorted_pairs: Vec<_> = sorted_map.iter().collect();

println!("{:?}", sorted_pairs);

assert_eq!(sorted_pairs.len(), 3);
assert_eq!(sorted_pairs[0].1, &1);
assert_eq!(sorted_pairs[1].1, &1);
assert_eq!(sorted_pairs[2], (&"tedious", &2));
Run
Examples found in repository?
examples/people_ord_by_age.rs (lines 19-25)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Returns an iterator over the ValordMap. The iterator yields all items from start to end order by value.ord_by().

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);

let mut iter = sorted_map.iter();

assert_eq!(iter.next().unwrap().1, &1);
assert_eq!(iter.next().unwrap().1, &1);
assert_eq!(iter.next().unwrap(), (&"tedious", &2));
Run
Examples found in repository?
examples/people_ord_by_age.rs (line 130)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn rev_iter(&self) -> impl Iterator<Item = (&K, &V)>

Returns an reversesed iterator over the ValordMap. The iterator yields all items from start to end order by value.ord_by().

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);

let mut iter = sorted_map.rev_iter();

assert_eq!(iter.next().unwrap(), (&"tedious", &2));
assert_eq!(iter.next().unwrap().1, &1);
assert_eq!(iter.next().unwrap().1, &1);
Run
source

pub fn iter_mut(&mut self) -> impl Iterator<Item = RefMut<'_, T, K, V>>

Returns an mut iterator over the ValordMap. The iterator yields all items from start to end order by value.ord_by().

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);


let mut iter = sorted_map.iter_mut();

let mut item1 = iter.next().unwrap();
let (k, v) = item1.get_mut_with_key();
assert_eq!(v, &mut 1);
*v = 4;
drop(item1);

assert_eq!(iter.next().unwrap().get_mut_with_key(), (&"tedious", &mut 2));
assert_eq!(iter.next().unwrap().get_mut_with_key(), (&"xuandu", &mut 3));
assert!(iter.next().is_none());
drop(iter);

let max_list = sorted_map.last();
assert_eq!(max_list.len(), 1);
assert_eq!(max_list, vec![(&"qians", &4)]);
Run
Examples found in repository?
examples/people_ord_by_age.rs (line 82)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn rev_iter_mut(&mut self) -> impl Iterator<Item = RefMut<'_, T, K, V>>

Returns an reversesed mut iterator over the ValordMap. The iterator yields all items from start to end order by value.ord_by().

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);


let mut iter = sorted_map.rev_iter_mut();

let mut item1 = iter.next().unwrap();
let (k, v) = item1.get_mut_with_key();
assert_eq!(v, &mut 3);
*v = 0;
drop(item1);

assert_eq!(iter.next().unwrap().get_mut_with_key(), (&"tedious", &mut 2));
assert_eq!(iter.next().unwrap().get_mut_with_key(), (&"qians", &mut 1));
assert!(iter.next().is_none());
drop(iter);

let max_list = sorted_map.first();
assert_eq!(max_list.len(), 1);
assert_eq!(max_list, vec![(&"xuandu", &0)]);
Run
source

pub fn first(&self) -> Vec<(&K, &V)>

Returns the first vector of key-value pairs in the map. The value in this pair is the minimum values in the map.

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);

let min_list = sorted_map.first();

assert_eq!(min_list.len(), 2);
assert!(min_list.iter().all(|(_, v)| **v == 1));
Run
Examples found in repository?
examples/people_ord_by_age.rs (line 55)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn first_mut(&mut self) -> Vec<RefMut<'_, T, K, V>>

Returns the first vector of key-value mut pairs in the map. The value in this pair is the minimum values in the map.

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);

let mut min_list = sorted_map.first_mut();
assert_eq!(min_list.len(), 2);
min_list.iter_mut().for_each(|rm| {
    let (_k, v) = rm.get_mut_with_key();
    *v = 0;
});
drop(min_list);

let min_list = sorted_map.first();
assert!(min_list.iter().all(|(_, v)| **v == 0));
Run
source

pub fn last(&self) -> Vec<(&K, &V)>

Returns the last vector of key-value pairs in the map. The value in this pair is the maximum values in the map.

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);

let max_list = sorted_map.last();

assert_eq!(max_list.len(), 1);
assert_eq!(max_list, vec![(&"tedious", &2)]);
Run
Examples found in repository?
examples/people_ord_by_age.rs (line 68)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn last_mut(&mut self) -> Vec<RefMut<'_, T, K, V>>

Returns the last vector of key-value mut pairs in the map. The value in this pair is the minimum values in the map.

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("sheng", 4);

let mut max_list = sorted_map.last_mut();
assert_eq!(max_list.len(), 1);
let (k, v) = max_list[0].get_mut_with_key();
assert_eq!((&k, &v), (&&"sheng", &&mut 4));

*v = 2;
drop(max_list);

let max_list = sorted_map.last();
assert_eq!(max_list.len(), 1);
assert_eq!(max_list, vec![(&"xuandu", &3)]);
Run
source

pub fn range<R>(&self, range: R) -> impl Iterator<Item = (&K, &V)>
where R: RangeBounds<V::Target>,

get range from ValordMap

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("sheng", 3);
sorted_map.insert("xuandu", 4);
sorted_map.insert("xuandu2", 5);
sorted_map.insert("xuandu3", 6);
assert_eq!(sorted_map.range(4..).last().unwrap(), (&"xuandu3", &6));
assert_eq!(
    sorted_map
        .range(4..)
        .filter(|(_, v)| **v != 6)
        .last()
        .unwrap(),
    (&"xuandu2", &5)
);
Run
Examples found in repository?
examples/people_ord_by_age.rs (line 111)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn range_mut<R>( &mut self, range: R ) -> impl Iterator<Item = RefMut<'_, T, K, V>>
where R: RangeBounds<V::Target>,

get range mut from ValordMap

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("sheng", 3);
sorted_map.insert("xuandu", 4);
sorted_map.insert("xuandu2", 5);
sorted_map.insert("xuandu3", 6);

let mut range_iter = sorted_map.range_mut(4..);

let mut item1 = range_iter.next().unwrap();
let (k, v) = item1.get_mut_with_key();
assert_eq!(k, &"xuandu");
assert_eq!(v, &mut 4);
*v += 4;
drop(item1);
drop(range_iter);

assert_eq!(
    sorted_map
        .range(4..)
        .last(),
    Some((&"xuandu", &8))
);
Run
Examples found in repository?
examples/people_ord_by_age.rs (line 116)
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
fn main() {
    let mut peoples = ValordMap::new();
    peoples.insert(
        1,
        People {
            age: 18,
            name: "qians1".to_string(),
        },
    );
    peoples.insert(
        2,
        People {
            age: 19,
            name: "qians2".to_string(),
        },
    );
    peoples.insert(
        3,
        People {
            age: 20,
            name: "qians3".to_string(),
        },
    );
    peoples.insert(
        4,
        People {
            age: 21,
            name: "qians4".to_string(),
        },
    );
    peoples.insert(
        5,
        People {
            age: 22,
            name: "qians5".to_string(),
        },
    );

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 18,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 22,
                name: "qians5".to_string(),
            }
        )
    );

    peoples
        .iter_mut()
        .for_each(|mut people_ref_mut| people_ref_mut.age += 1);

    let youngest = peoples.first();
    assert_eq!(youngest.len(), 1);
    assert_eq!(
        youngest[0],
        (
            &1,
            &People {
                age: 19,
                name: "qians1".to_string(),
            }
        )
    );

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 1);
    assert_eq!(
        oldest[0],
        (
            &5,
            &People {
                age: 23,
                name: "qians5".to_string(),
            }
        )
    );

    let range: Vec<_> = peoples.range(22..).collect();
    assert_eq!(range.len(), 2);
    println!("range: {range:?}");

    let range: Vec<_> = peoples
        .range_mut(22..)
        .map(|mut rm_p| {
            let (k, v) = rm_p.get_mut_with_key();
            v.age = 30;
            (*k, v.name.clone(), v.age)
        })
        .collect();

    println!("range mut: {range:?}");

    let oldest = peoples.last();
    assert_eq!(oldest.len(), 2);
    assert_eq!(oldest[0].1.age, 30,);
    assert_eq!(oldest[1].1.age, 30);
    println!("peoples: {:?}", peoples.iter().collect::<Vec<_>>());
}
source

pub fn get(&self, key: &K) -> Option<&V>

Get the ref value by given key, or return None if not found

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("key1", 1);
sorted_map.insert("key2", 2);
sorted_map.insert("key3", 3);

let mut val1 = sorted_map.get(&"key2");
let mut val2 = sorted_map.get(&"key4");
assert_eq!(val1.unwrap(), &2);
assert_eq!(val2, None);
Run
source

pub fn get_mut<'a>(&'a mut self, key: &K) -> Option<RefMut<'a, T, K, V>>

Get the ref mut value by given key, or return None if not found

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("key1", 1);
sorted_map.insert("key2", 2);
sorted_map.insert("key3", 3);

let mut val = sorted_map.get_mut(&"key2").unwrap();
*val = 4;
drop(val);
assert_eq!(sorted_map.get(&"key2").unwrap(), &4);
assert_eq!(sorted_map.last(), vec![(&"key2", &4)]);
Run
source

pub fn modify<F>(&mut self, key: &K, op: F) -> bool
where F: Fn(&mut V),

Modify value in map, if exist return true, else return false

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);

assert!(sorted_map.modify(&"qians", |v| *v = 2));
assert_eq!(sorted_map.iter().next().unwrap(), (&"qians", &2));
Run
source

pub fn remove(&mut self, key: &K) -> Option<V>

remove from ValordMap

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert(1, "a");
sorted_map.insert(2, "b");

let removed_value = sorted_map.remove(&1);
assert_eq!(removed_value, Some("a"));
assert_eq!(sorted_map.get(&1), None);
Run
source

pub fn remove_entry<'a>(&'a mut self, key: &'a K) -> Option<(&K, V)>

Removes a key from the map, returning the stored key and value if the key was previously in the map.

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert(1, "a");
sorted_map.insert(2, "b");

let removed_entry = sorted_map.remove_entry(&1);
assert_eq!(removed_entry, Some((&1, "a")));
assert_eq!(sorted_map.get(&1), None);
Run
source

pub fn len(&self) -> usize

Return the number of key-value pairs in the map.

§Example
use valord_map::ValordMap;

let mut sorted_map = ValordMap::new();
sorted_map.insert(1, "a");
sorted_map.insert(2, "b");
sorted_map.insert(3, "c");
sorted_map.insert(2, "d");

assert_eq!(sorted_map.len(), 3);

let removed_value = sorted_map.remove(&1);
assert_eq!(removed_value, Some("a"));
assert_eq!(sorted_map.len(), 2);
Run
source

pub fn is_empty(&self) -> bool

Trait Implementations§

source§

impl<T, K, V> Default for ValordMap<T, K, V>
where T: Ord + Clone, K: Hash + Eq, V: OrdBy<Target = T>,

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, K, V> Freeze for ValordMap<T, K, V>

§

impl<T, K, V> RefUnwindSafe for ValordMap<T, K, V>

§

impl<T, K, V> Send for ValordMap<T, K, V>
where T: Send, K: Send, V: Send,

§

impl<T, K, V> Sync for ValordMap<T, K, V>
where T: Sync, K: Sync, V: Sync,

§

impl<T, K, V> Unpin for ValordMap<T, K, V>
where K: Unpin, V: Unpin,

§

impl<T, K, V> UnwindSafe for ValordMap<T, K, V>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.