aboutsummaryrefslogtreecommitdiff
path: root/stdlib/base.lh
blob: 210471b13c4b821621f64a9c64cae7ae2700677a (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

// TODO perhaps we should put these under the count, int, int8, etc. namespaces?
namespace range
{
    typedef type = (int current, int end, int step);
    alias type here(int current, int end, int step)
    {
        return (current, end, step);
    }
    
    alias bool type:next_element(var type^ r, var int^ result)
    {
        if r^.step > 0 and r^.current > r^.end return :false;
        if r^.step < 0 and r^.current < r^.end return :false;
        result^ = r^.current;
        r^.current += r^.step;
        return :true;
    }
}

namespace indexrange
{
    typedef type = (count current, count length);
    alias type here(count length)
    {
        return (0, length);
    }
    
    alias bool type:next_element(var type^ r, var count^ result)
    {
        if r^.current >= r^.length return :false;
        result^ = r^.current;
        r^.current += 1;
        return :true;
    }
}

namespace revindexrange
{
    typedef type = (count current);
    alias type here(count length)
    {
        return (length,);
    }
    
    alias bool type:next_element(var type^ r, var count^ result)
    {
        if r^.current == 0 return :false;
        r^.current -= 1;
        result^ = r^.current;
        return :true;
    }
}