a2lfile 3.4.0

read, modify and write a2l files
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
use crate::{A2lObjectName, A2lObjectNameSetter};
use fnv::FnvBuildHasher;
use std::{
    cmp::Ordering,
    collections::HashMap,
    ops::{Index, IndexMut},
};

/// A list of named a2l items
///
/// An ItemList is an ordered collection of items, which additionally allows for
/// fast access to items by their name.
#[derive(Debug, Clone)]
pub struct ItemList<T: A2lObjectName> {
    // storage for items
    items: Vec<T>,
    // mapping from item name to index in the items vector
    map: HashMap<String, usize, FnvBuildHasher>,
}

impl<T: A2lObjectName> ItemList<T> {
    /// create a new ItemList
    pub fn new() -> Self {
        Self {
            items: vec![],
            map: HashMap::default(),
        }
    }

    /// create a new ItemList with a specified initial capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            items: Vec::with_capacity(capacity),
            map: HashMap::with_capacity_and_hasher(capacity, FnvBuildHasher::default()),
        }
    }

    /// push an item into the ItemList
    pub fn push(&mut self, value: T) {
        let index = self.items.len();
        let key = value.get_name().to_string();
        // the ItemList _shouldn't_ contain duplicate keys, but if it does, the map refers to the first one
        // note: this behaviour exists to allow for invalid a2l files which violate the constraint
        // of unique names
        self.map.entry(key).or_insert(index);
        self.items.push(value);
    }

    /// push an item into the ItemList, replacing any existing item with the same name
    pub fn push_unique(&mut self, value: T) {
        let key = value.get_name().to_string();
        if let Some(&index) = self.map.get(&key) {
            self.items[index] = value;
        } else {
            let index = self.items.len();
            self.map.insert(key, index);
            self.items.push(value);
        }
    }

    /// pop an item from the ItemList
    pub fn pop(&mut self) -> Option<T> {
        let item = self.items.pop()?;
        // remove the item from the map
        self.map.remove(item.get_name());
        Some(item)
    }

    /// get an item by key
    pub fn get(&self, key: &str) -> Option<&T> {
        let index = self.map.get(key)?;
        Some(&self.items[*index])
    }

    /// get a mutable reference to an item by key
    pub fn get_mut(&mut self, key: &str) -> Option<&mut T> {
        let index = self.map.get(key)?;
        Some(&mut self.items[*index])
    }

    /// get the first item in the ItemList
    pub fn first(&self) -> Option<&T> {
        self.items.first()
    }

    /// get the last item in the ItemList
    pub fn last(&self) -> Option<&T> {
        self.items.last()
    }

    /// get the index of an item by key
    pub fn index(&self, key: &str) -> Option<usize> {
        self.map.get(key).copied()
    }

    /// Checks if the ItemList contains an item with the given key
    pub fn contains_key(&self, key: &str) -> bool {
        self.map.contains_key(key)
    }

    /// remove an item from the ItemList by key and return it
    pub fn swap_remove(&mut self, key: &str) -> Option<T> {
        let index = self.map.remove(key)?;
        let item = self.items.swap_remove(index);
        // the last item was swapped into the index, so we need to update the map
        self.map
            .insert(self.items[index].get_name().to_string(), index);
        Some(item)
    }

    /// remove an item from the ItemList by index and return it
    pub fn swap_remove_idx(&mut self, index: usize) -> Option<T> {
        if index < self.items.len() {
            let item = self.items.swap_remove(index);
            // remove the item from the map
            self.map.remove(item.get_name());
            // the last item was swapped into the index, so we need to update the map
            self.map
                .insert(self.items[index].get_name().to_string(), index);
            Some(item)
        } else {
            None
        }
    }

    /// Returns an iterator over references to the items in the ItemList
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.items.iter()
    }

    /// Returns an iterator over mutable references to the items in the ItemList
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
        self.items.iter_mut()
    }

    /// Returns an iterator over the keys in the ItemList
    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.map.keys()
    }

    /// Returns the number of items in the ItemList
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Checks if the ItemList is empty
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// extend the ItemList from an iterator
    pub fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = T>,
    {
        let into_iter = iter.into_iter();
        let (low, _high) = into_iter.size_hint();
        self.items.reserve(low);
        self.map.reserve(low);
        for item in into_iter {
            self.push(item);
        }
    }

    /// remove all items from the ItemList
    pub fn clear(&mut self) {
        self.items.clear();
        self.map.clear();
    }

    /// trucate the ItemList to a specified length
    pub fn truncate(&mut self, len: usize) {
        if len < self.items.len() {
            self.items.truncate(len);
            self.map.clear();
            // rebuild the map after truncating
            for (idx, item) in self.items.iter().enumerate() {
                let key = item.get_name().to_string();
                self.map.insert(key, idx);
            }
        }
    }

    /// Selectively remove or retain items from the ItemList based on a predicate function
    pub fn retain<F>(&mut self, mut keep: F)
    where
        F: FnMut(&mut T) -> bool,
    {
        let mut items_new = Vec::with_capacity(self.items.len());
        std::mem::swap(&mut self.items, &mut items_new);
        self.map.clear();
        // re-insert only the items that are kept
        for mut item in items_new {
            if keep(&mut item) {
                let key = item.get_name().to_string();
                self.items.push(item);
                self.map.insert(key, self.items.len() - 1);
            }
        }
    }

    pub fn sort_by<F>(&mut self, mut cmp: F)
    where
        F: FnMut(&T, &T) -> Ordering,
    {
        self.items.sort_by(|a, b| cmp(a, b));
        self.map.clear();
        // rebuild the map after sorting
        for (idx, item) in self.items.iter().enumerate() {
            let key = item.get_name().to_string();
            self.map.insert(key, idx);
        }
    }
}

impl<T: A2lObjectName + A2lObjectNameSetter> ItemList<T> {
    /// Set the name of an item in the ItemList
    pub fn rename_item(&mut self, item_idx: usize, new_name: &str) {
        if item_idx < self.items.len() {
            let old_name = self.items[item_idx].get_name();
            self.map.remove(old_name);
            self.items[item_idx].set_name(new_name.to_string());
            self.map.insert(new_name.to_string(), item_idx);
        }
    }
}

impl<T> Default for ItemList<T>
where
    T: A2lObjectName,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<T: A2lObjectName> Index<usize> for ItemList<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        &self.items[index]
    }
}

impl<T: A2lObjectName> IndexMut<usize> for ItemList<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.items[index]
    }
}

impl<T: A2lObjectName> Index<&str> for ItemList<T> {
    type Output = T;

    fn index(&self, key: &str) -> &Self::Output {
        let index = self.map.get(key).unwrap();
        &self.items[*index]
    }
}

impl<T> FromIterator<T> for ItemList<T>
where
    T: A2lObjectName,
{
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let into_iter = iter.into_iter();
        let (low, _high) = into_iter.size_hint();
        let mut item_list = ItemList::with_capacity(low);
        for item in into_iter {
            item_list.push(item);
        }
        item_list
    }
}

impl<T> IntoIterator for ItemList<T>
where
    T: A2lObjectName,
{
    type Item = T;
    type IntoIter = std::vec::IntoIter<T>;

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

impl<'a, T> IntoIterator for &'a ItemList<T>
where
    T: A2lObjectName,
{
    type Item = &'a T;
    type IntoIter = std::slice::Iter<'a, T>;

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

impl<'a, T> IntoIterator for &'a mut ItemList<T>
where
    T: A2lObjectName,
{
    type Item = &'a mut T;
    type IntoIter = std::slice::IterMut<'a, T>;

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

impl<T> PartialEq for ItemList<T>
where
    T: A2lObjectName + PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.items == other.items
        // no need to compare the maps, as they are derived from the items
    }
}

#[macro_export]
macro_rules! itemlist {
    () => (
        $crate::ItemList::new()
    );
    ($($x:expr),+ $(,)?) => (
        {
            const CAP: usize = <[()]>::len(&[$({ stringify!($x); }),*]);
            let mut itemlist = $crate::ItemList::with_capacity(CAP);
            $(
                itemlist.push($x);
            )*
            itemlist
        }
    );
}

#[cfg(test)]
mod test {
    use super::*;

    #[derive(Debug, Clone, PartialEq)]
    struct TestItem {
        name: String,
    }

    impl TestItem {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
            }
        }
    }

    impl A2lObjectName for TestItem {
        fn get_name(&self) -> &str {
            &self.name
        }
    }

    impl A2lObjectNameSetter for TestItem {
        fn set_name(&mut self, name: String) {
            self.name = name;
        }
    }

    #[test]
    fn test_itemlist() {
        let mut itemlist = ItemList::new();
        assert!(itemlist.is_empty());
        assert_eq!(itemlist.len(), 0);
        assert_eq!(itemlist.first(), None);

        itemlist.push(TestItem::new("item1"));
        itemlist.push(TestItem::new("item2"));
        assert_eq!(itemlist.len(), 2);
        verify_itemlist(&itemlist);
        assert_eq!(itemlist.first().unwrap().get_name(), "item1");
        assert_eq!(itemlist.last().unwrap().get_name(), "item2");

        assert_eq!(itemlist[1].get_name(), "item2");
        assert_eq!(itemlist["item2"].get_name(), "item2");

        itemlist.push(TestItem::new("item3"));
        itemlist.push(TestItem::new("item4"));
        itemlist.swap_remove("item2").unwrap();
        verify_itemlist(&itemlist);
        assert_eq!(itemlist.len(), 3);
        assert_eq!(
            itemlist,
            itemlist![
                TestItem::new("item1"),
                TestItem::new("item4"),
                TestItem::new("item3")
            ]
        );

        itemlist.push(TestItem::new("item5"));
        itemlist.swap_remove_idx(1).unwrap();
        verify_itemlist(&itemlist);
        assert_eq!(itemlist.len(), 3);
        assert_eq!(
            itemlist,
            itemlist![
                TestItem::new("item1"),
                TestItem::new("item5"),
                TestItem::new("item3")
            ]
        );
        assert_eq!(itemlist.swap_remove_idx(99), None);

        itemlist.retain(|item| item.get_name() != "item1");
        verify_itemlist(&itemlist);
        assert_eq!(itemlist.len(), 2);
        assert_eq!(
            itemlist,
            itemlist![TestItem::new("item5"), TestItem::new("item3")]
        );

        let item = itemlist.pop().unwrap();
        assert_eq!(item.get_name(), "item3");

        itemlist.push(TestItem::new("item6"));
        itemlist.push(TestItem::new("item7"));
        assert_eq!(itemlist.len(), 3);
        itemlist.truncate(99);
        assert_eq!(itemlist.len(), 3);
        itemlist.truncate(2);
        verify_itemlist(&itemlist);
        assert_eq!(itemlist.len(), 2);
        assert_eq!(
            itemlist,
            itemlist![TestItem::new("item5"), TestItem::new("item6")]
        );

        itemlist.rename_item(1, "item8");
        verify_itemlist(&itemlist);
        assert_eq!(itemlist.len(), 2);
        assert_eq!(
            itemlist,
            itemlist![TestItem::new("item5"), TestItem::new("item8")]
        );
    }

    fn verify_itemlist(itemlist: &ItemList<TestItem>) {
        assert_eq!(itemlist.len(), itemlist.items.len());
        assert_eq!(itemlist.map.len(), itemlist.items.len());
        for (idx, item) in itemlist.iter().enumerate() {
            assert_eq!(idx, itemlist.index(item.get_name()).unwrap());
            assert_eq!(item.get_name(), itemlist[idx].get_name());
        }
    }
}