aboutsummaryrefslogtreecommitdiff
path: root/src/view_stack.zig
blob: 0543d0343352a510c3e6cfec7a7eb6bc2eebe7d4 (plain) (blame)
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
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2020 Isaac Freund
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

const View = @import("View.zig");

/// A specialized doubly-linked stack that allows for filtered iteration
/// over the nodes. T must be View or *View.
pub fn ViewStack(comptime T: type) type {
    if (!(T == View or T == *View)) {
        @compileError("ViewStack: T must be View or *View");
    }
    return struct {
        const Self = @This();

        pub const Node = struct {
            /// Previous/next nodes in the stack
            prev: ?*Node,
            next: ?*Node,

            /// The view stored in this node
            view: T,
        };

        /// Top/bottom nodes in the stack
        first: ?*Node,
        last: ?*Node,

        /// Initialize an undefined stack
        pub fn init(self: *Self) void {
            self.first = null;
            self.last = null;
        }

        /// Add a node to the top of the stack.
        pub fn push(self: *Self, new_node: *Node) void {
            // Set the prev/next pointers of the new node
            new_node.prev = null;
            new_node.next = self.first;

            if (self.first) |first| {
                // If the list is not empty, set the prev pointer of the current
                // first node to the new node.
                first.prev = new_node;
            } else {
                // If the list is empty set the last pointer to the new node.
                self.last = new_node;
            }

            // Set the first pointer to the new node
            self.first = new_node;
        }

        /// Remove a node from the view stack. This removes it from the stack of
        /// all views as well as the stack of visible ones.
        pub fn remove(self: *Self, target_node: *Node) void {
            // Set the previous node/list head to the next pointer
            if (target_node.prev) |prev_node| {
                prev_node.next = target_node.next;
            } else {
                self.first = target_node.next;
            }

            // Set the next node/list tail to the previous pointer
            if (target_node.next) |next_node| {
                next_node.prev = target_node.prev;
            } else {
                self.last = target_node.prev;
            }
        }

        const Iterator = struct {
            it: ?*Node,
            tags: u32,
            reverse: bool,
            pending: bool,

            /// Returns the next node in iteration order, or null if done.
            /// This function is horribly ugly, but it's well tested below.
            pub fn next(self: *Iterator) ?*Node {
                while (self.it) |node| : (self.it = if (self.reverse) node.prev else node.next) {
                    if (if (self.pending)
                        if (node.view.pending_tags) |pending_tags|
                            self.tags & pending_tags != 0
                        else
                            self.tags & node.view.current_tags != 0
                    else
                        self.tags & node.view.current_tags != 0) {
                        self.it = if (self.reverse) node.prev else node.next;
                        return node;
                    }
                }
                return null;
            }
        };

        /// Returns an iterator starting at the passed node and filtered by
        /// checking the passed tags against the current tags of each view.
        pub fn iterator(start: ?*Node, tags: u32) Iterator {
            return Iterator{
                .it = start,
                .tags = tags,
                .reverse = false,
                .pending = false,
            };
        }

        /// Returns a reverse iterator starting at the passed node and filtered by
        /// checking the passed tags against the current tags of each view.
        pub fn reverseIterator(start: ?*Node, tags: u32) Iterator {
            return Iterator{
                .it = start,
                .tags = tags,
                .reverse = true,
                .pending = false,
            };
        }

        /// Returns an iterator starting at the passed node and filtered by
        /// checking the passed tags against the pending tags of each view.
        /// If a view has no pending tags, the current tags are used.
        pub fn pendingIterator(start: ?*Node, tags: u32) Iterator {
            return Iterator{
                .it = start,
                .tags = tags,
                .reverse = false,
                .pending = true,
            };
        }
    };
}

test "push/remove (*View)" {
    const testing = @import("std").testing;

    const allocator = testing.allocator;

    var views: ViewStack(*View) = undefined;
    views.init();

    const one = try allocator.create(ViewStack(*View).Node);
    defer allocator.destroy(one);
    const two = try allocator.create(ViewStack(*View).Node);
    defer allocator.destroy(two);
    const three = try allocator.create(ViewStack(*View).Node);
    defer allocator.destroy(three);
    const four = try allocator.create(ViewStack(*View).Node);
    defer allocator.destroy(four);
    const five = try allocator.create(ViewStack(*View).Node);
    defer allocator.destroy(five);

    views.push(three); // {3}
    views.push(one); // {1, 3}
    views.push(four); // {4, 1, 3}
    views.push(five); // {5, 4, 1, 3}
    views.push(two); // {2, 5, 4, 1, 3}

    // Simple insertion
    {
        var it = views.first;
        testing.expect(it == two);
        it = it.?.next;
        testing.expect(it == five);
        it = it.?.next;
        testing.expect(it == four);
        it = it.?.next;
        testing.expect(it == one);
        it = it.?.next;
        testing.expect(it == three);
        it = it.?.next;

        testing.expect(it == null);

        testing.expect(views.first == two);
        testing.expect(views.last == three);
    }

    // Removal of first
    views.remove(two);
    {
        var it = views.first;
        testing.expect(it == five);
        it = it.?.next;
        testing.expect(it == four);
        it = it.?.next;
        testing.expect(it == one);
        it = it.?.next;
        testing.expect(it == three);
        it = it.?.next;

        testing.expect(it == null);

        testing.expect(views.first == five);
        testing.expect(views.last == three);
    }

    // Removal of last
    views.remove(three);
    {
        var it = views.first;
        testing.expect(it == five);
        it = it.?.next;
        testing.expect(it == four);
        it = it.?.next;
        testing.expect(it == one);
        it = it.?.next;

        testing.expect(it == null);

        testing.expect(views.first == five);
        testing.expect(views.last == one);
    }

    // Remove from middle
    views.remove(four);
    {
        var it = views.first;
        testing.expect(it == five);
        it = it.?.next;
        testing.expect(it == one);
        it = it.?.next;

        testing.expect(it == null);

        testing.expect(views.first == five);
        testing.expect(views.last == one);
    }

    // Reinsertion
    views.push(two);
    views.push(three);
    views.push(four);
    {
        var it = views.first;
        testing.expect(it == four);
        it = it.?.next;
        testing.expect(it == three);
        it = it.?.next;
        testing.expect(it == two);
        it = it.?.next;
        testing.expect(it == five);
        it = it.?.next;
        testing.expect(it == one);
        it = it.?.next;

        testing.expect(it == null);

        testing.expect(views.first == four);
        testing.expect(views.last == one);
    }

    // Clear
    views.remove(four);
    views.remove(two);
    views.remove(three);
    views.remove(one);
    views.remove(five);

    testing.expect(views.first == null);
    testing.expect(views.last == null);
}

test "iteration (View)" {
    const c = @import("c.zig");
    const testing = @import("std").testing;

    const allocator = testing.allocator;

    var views: ViewStack(View) = undefined;
    views.init();

    const one_a_pb = try allocator.create(ViewStack(View).Node);
    defer allocator.destroy(one_a_pb);
    one_a_pb.view.current_tags = 1 << 0;
    one_a_pb.view.pending_tags = 1 << 1;

    const two_a = try allocator.create(ViewStack(View).Node);
    defer allocator.destroy(two_a);
    two_a.view.current_tags = 1 << 0;
    two_a.view.pending_tags = null;

    const three_b_pa = try allocator.create(ViewStack(View).Node);
    defer allocator.destroy(three_b_pa);
    three_b_pa.view.current_tags = 1 << 1;
    three_b_pa.view.pending_tags = 1 << 0;

    const four_b = try allocator.create(ViewStack(View).Node);
    defer allocator.destroy(four_b);
    four_b.view.current_tags = 1 << 1;
    four_b.view.pending_tags = null;

    const five_b = try allocator.create(ViewStack(View).Node);
    defer allocator.destroy(five_b);
    five_b.view.current_tags = 1 << 1;
    five_b.view.pending_tags = null;

    views.push(three_b_pa); // {3}
    views.push(one_a_pb); // {1, 3}
    views.push(four_b); // {4, 1, 3}
    views.push(five_b); // {5, 4, 1, 3}
    views.push(two_a); // {2, 5, 4, 1, 3}

    // Iteration over all tags
    {
        var it = ViewStack(View).iterator(views.first, 0xFFFFFFFF);
        testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
        testing.expect(it.next() == null);
    }

    // Iteration over 'a' tags
    {
        var it = ViewStack(View).iterator(views.first, 1 << 0);
        testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
        testing.expect(it.next() == null);
    }

    // Iteration over 'b' tags
    {
        var it = ViewStack(View).iterator(views.first, 1 << 1);
        testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
        testing.expect(it.next() == null);
    }

    // Iteration over tags that aren't present
    {
        var it = ViewStack(View).iterator(views.first, 1 << 2);
        testing.expect(it.next() == null);
    }

    // Reverse iteration over all tags
    {
        var it = ViewStack(View).reverseIterator(views.last, 0xFFFFFFFF);
        testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
        testing.expect(it.next() == null);
    }

    // Reverse iteration over 'a' tags
    {
        var it = ViewStack(View).reverseIterator(views.last, 1 << 0);
        testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
        testing.expect(it.next() == null);
    }

    // Reverse iteration over 'b' tags
    {
        var it = ViewStack(View).reverseIterator(views.last, 1 << 1);
        testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
        testing.expect(it.next() == null);
    }

    // Reverse iteration over tags that aren't present
    {
        var it = ViewStack(View).reverseIterator(views.first, 1 << 2);
        testing.expect(it.next() == null);
    }

    // Iteration over (pending) 'a' tags
    {
        var it = ViewStack(View).pendingIterator(views.first, 1 << 0);
        testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
        testing.expect(it.next() == null);
    }

    // Iteration over (pending) 'b' tags
    {
        var it = ViewStack(View).pendingIterator(views.first, 1 << 1);
        testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
        testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
        testing.expect(it.next() == null);
    }

    // Iteration over (pending) tags that aren't present
    {
        var it = ViewStack(View).pendingIterator(views.first, 1 << 2);
        testing.expect(it.next() == null);
    }
}