aboutsummaryrefslogtreecommitdiff
path: root/docs/notes/iterator_safety.txt
blob: 6ca10b5886f5a622f8b51b4f5c0e01a8f19eef49 (plain)
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


iterating and modifying lists
-----------------------------

If lists could be modified directly while being iterated over, then we
could encounter bugs (and memory corruption if the current element is
deleted from a linked list).

As a safety measure, iterators could take ownership of the list, but
implement and forward all list operations to the real list.


    list[item] exclusive_write^ a = ...;
    
    iterator[item] exclusive_write^ iter = a^.iterator()
    a = undefined;
    
    item e;
    while (iter.get_elem(@e)) {
        
        if (e.is_old)
            iter.remove_current();
        
        if (e.previous_is_old)
            iter.remove_relative(-1);
        
        if (e.first_is_old)
            iter.remove_at(0);
    }
    
    a = iter.list();
    iter = undefined;