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
//! Fast Key Value structure for local construction.
use crate::types::{Map, Puff};
use std::collections::HashMap;
use std::hash::Hash;

/// Fast Key Value structure for local construction.
///
/// Puff's `MapBuilder` type uses [std::collections::hash_map::HashMap] under the hood. `HashMap`s are not
/// sharable structures and so need to be wrapped in an Arc to be shared between threads and cheaply copied.
/// You should use a `MapBuilder` and convert it into a `Map` when you are done writing to it.
///
/// Maps can only be constructed from types implementing `IntoMapBuilder`.
///
/// # Examples
///
/// ```
/// use puff_rs::types::MapBuilder;
///
/// let mut builder = MapBuilder::new();
/// builder.insert(42, 0);
/// let map = builder.into_map();
/// ```
///
#[derive(Clone)]
pub struct MapBuilder<K, V>(HashMap<K, V>);

impl<K, T> MapBuilder<K, T>
where
    K: Puff + Hash + Eq,
    T: Puff,
{
    /// Creates an empty `MapBuilder`.
    ///
    /// The hash map is initially created with a capacity of 0, so it will not allocate until it
    /// is first inserted into.
    ///
    /// # Examples
    ///
    /// ```
    /// use puff_rs::types::{Text, MapBuilder};
    /// let mut map: MapBuilder<Text, i32> = MapBuilder::new();
    /// ```
    pub fn new() -> MapBuilder<K, T> {
        Self(HashMap::new())
    }

    /// Creates an empty `MapBuilder` with the specified capacity.
    ///
    /// The hash map will be able to hold at least `capacity` elements without
    /// reallocating. If `capacity` is 0, the hash map will not allocate.
    ///
    /// # Examples
    ///
    /// ```
    /// use puff_rs::types::{Text, MapBuilder};
    /// let mut map: MapBuilder<Text, i32> = MapBuilder::with_capacity(10);
    /// ```
    pub fn with_capacity(capacity: usize) -> MapBuilder<K, T> {
        Self(HashMap::with_capacity(capacity))
    }

    /// Inserts a key-value pair into the `MapBuilder`.
    ///
    /// If the map did not have this key present, [`None`] is returned.
    ///
    /// If the map did have this key present, the value is updated, and the old
    /// value is returned. The key is not updated, though; this matters for
    /// types that can be `==` without being identical. See the [module-level
    /// documentation] for more.
    ///
    /// # Examples
    ///
    /// ```
    /// use puff_rs::types::MapBuilder;
    ///
    /// let mut map = MapBuilder::new();
    /// assert_eq!(map.insert(37, 42), None);
    /// assert_eq!(map.is_empty(), false);
    ///
    /// map.insert(37, 10);
    /// assert_eq!(map.insert(37, 13), Some(10));
    /// ```
    #[inline]
    pub fn insert(&mut self, key: K, item: T) -> Option<T> {
        self.0.insert(key, item)
    }

    /// Returns a reference to the value corresponding to the key.
    ///
    /// # Examples
    ///
    /// ```
    /// use puff_rs::types::MapBuilder;
    ///
    /// let mut map = MapBuilder::new();
    /// map.insert(1, 2);
    /// assert_eq!(map.get(1), Some(2));
    /// ```
    #[inline]
    pub fn get(&self, index: K) -> Option<T> {
        self.0.get(&index).map(|v| v.clone())
    }

    /// Returns true if the map contains no elements.
    ///
    /// # Examples
    ///
    /// ```
    /// use puff_rs::types::MapBuilder;
    /// let map: MapBuilder<usize, usize> = MapBuilder::new();
    /// assert!(map.is_empty())
    /// ```
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the number of elements in the `MapBuilder`.
    ///
    /// # Examples
    ///
    /// ```
    /// use puff_rs::types::MapBuilder;
    ///
    /// let mut map = MapBuilder::new();
    /// assert_eq!(map.len(), 0);
    /// map.insert(1, 2);
    /// assert_eq!(map.len(), 1);
    /// ```
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Convert the `MapBuilder` into a Read-Only `Map` that can be shared between Tasks.
    pub fn into_map(self) -> Map<K, T> {
        Map::from_hash_map(self.0)
    }
}

pub trait IntoMapBuilder<K, V> {
    fn into_map_builder(self) -> MapBuilder<K, V>;
}

impl<T: Into<MapBuilder<K, V>>, K: Puff + Hash + Eq, V: Puff> IntoMapBuilder<K, V> for T {
    fn into_map_builder(self) -> MapBuilder<K, V> {
        self.into()
    }
}

impl<K: Puff + Hash + Eq, V: Puff> Into<MapBuilder<K, V>> for HashMap<K, V> {
    fn into(self) -> MapBuilder<K, V> {
        MapBuilder(self)
    }
}

impl<K: Puff + Hash + Eq, V: Puff> Into<MapBuilder<K, V>> for Vec<(K, V)> {
    fn into(self) -> MapBuilder<K, V> {
        MapBuilder(self.into_iter().collect())
    }
}