aboutsummaryrefslogtreecommitdiff
path: root/source/lru.d
blob: 2e8da731f4dee10c581e02ef6aab6f882c9d62fd (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
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
import std.array : appender;

private struct Node(T) {
    Node!T* prev;
    Node!T* next;
    T data;
}

private struct List(T) {
    Node!T* head;
    Node!T* tail;
    size_t length;

    invariant () {
        if (length > 0) {
            assert(head !is null);
            assert(tail !is null);
        }
    }

    /*private void check()
    {
        Node!T* cur = head;
        while (true)
        {
            if (cur != head)
                assert(cur.prev);
            if (cur == tail)
                break;
            else
                assert(cur.next);
            cur = cur.next;
        }
    }*/

    void remove(Node!T* n) {
        if (n.next)
            n.next.prev = n.prev;
        if (n.prev)
            n.prev.next = n.next;
        if (head == n)
            head = head.next;
        if (tail == n)
            tail = tail.prev;
        length--;
    }

    void push(Node!T* n) {
        n.prev = null;
        n.next = head;
        head = n;
        if (tail is null)
            tail = head;
        else
            head.next.prev = head;
        length++;
    }

    Node!T* pop()
    in {
        assert(tail);
    }
    do {
        Node!T* node = tail;
        remove(node);
        return node;
    }

}

unittest {
    List!int l;
    Node!int first;
    l.push(&first);
    assert(l.head == &first);
    assert(l.tail == &first);
    Node!int second;
    l.push(&second);
    assert(l.head == &second);
    assert(l.tail == &first);
    assert(l.pop() == &first);
    assert(l.head == &second);
    assert(l.tail == &second);
    l.push(&first);
    l.pop();
    l.pop();
}

class LRUCache(From, To) {
    private size_t _cap;
    private struct Record {
        To val;
        From key;
    }

    private Node!Record*[From] map;
    private List!Record queue;
    this(size_t cap) {
        _cap = cap;
    }

    private void checksz() {
        if (queue.length > _cap) {
            auto node = queue.pop();
            map.remove(node.data.key);
        }
    }

    bool opBinaryRight(string op : "in")(From key) {
        return cast(bool)(key in map);
    }

    To opIndex(From f)
    in {
        assert(f in map);
    }
    do {
        auto p = map[f];
        queue.remove(p);
        queue.push(p);
        return p.data.val;
    }

    void opIndexAssign(To t, From f) {
        auto p = f in map;
        Node!Record* node;
        bool alloc = p is null;
        if (!alloc) {
            queue.remove(*p);
            node = *p;
        } else
            node = new Node!Record();
        node.data.val = t;
        node.data.key = f;
        queue.push(node);
        map[f] = node;
        if (alloc)
            checksz();
    }
}

unittest {
    auto cache = new LRUCache!(int, int)(2);
    cache[0] = 1;
    cache[4] = 2;
    cache[5] = 1;
    assert(0 !in cache);
    assert(4 in cache);
    assert(5 in cache);
    assert(cache[5] == 1); // prioritizes
    cache[0] = 1;
    assert(4 !in cache);
}