File size: 8,819 Bytes
5641073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
/**
 * @author jdiaz5513
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.set = exports.get = exports.initList = exports.List = void 0;
const tslib_1 = require("tslib");
const debug_1 = tslib_1.__importDefault(require("debug"));
const errors_1 = require("../../errors");
const util_1 = require("../../util");
const list_element_size_1 = require("../list-element-size");
const object_size_1 = require("../object-size");
const pointer_1 = require("./pointer");
const trace = debug_1.default("capnp:list");
trace("load");
/**
 * A generic list class. Implements Filterable,
 */
class List extends pointer_1.Pointer {
    static toString() {
        return this._capnp.displayName;
    }
    all(callbackfn) {
        const length = this.getLength();
        for (let i = 0; i < length; i++) {
            if (!callbackfn(this.get(i), i))
                return false;
        }
        return true;
    }
    any(callbackfn) {
        const length = this.getLength();
        for (let i = 0; i < length; i++) {
            if (callbackfn(this.get(i), i))
                return true;
        }
        return false;
    }
    ap(callbackfns) {
        const length = this.getLength();
        const res = [];
        for (let i = 0; i < length; i++) {
            res.push(...callbackfns.map((f) => f(this.get(i), i)));
        }
        return res;
    }
    concat(other) {
        const length = this.getLength();
        const otherLength = other.getLength();
        const res = new Array(length + otherLength);
        for (let i = 0; i < length; i++)
            res[i] = this.get(i);
        for (let i = 0; i < otherLength; i++)
            res[i + length] = other.get(i);
        return res;
    }
    drop(n) {
        const length = this.getLength();
        const res = new Array(length);
        for (let i = n; i < length; i++)
            res[i] = this.get(i);
        return res;
    }
    dropWhile(callbackfn) {
        const length = this.getLength();
        const res = [];
        let drop = true;
        for (let i = 0; i < length; i++) {
            const v = this.get(i);
            if (drop)
                drop = callbackfn(v, i);
            if (!drop)
                res.push(v);
        }
        return res;
    }
    empty() {
        return [];
    }
    every(callbackfn) {
        return this.all(callbackfn);
    }
    filter(callbackfn) {
        const length = this.getLength();
        const res = [];
        for (let i = 0; i < length; i++) {
            const value = this.get(i);
            if (callbackfn(value, i))
                res.push(value);
        }
        return res;
    }
    find(callbackfn) {
        const length = this.getLength();
        for (let i = 0; i < length; i++) {
            const value = this.get(i);
            if (callbackfn(value, i))
                return value;
        }
        return undefined;
    }
    findIndex(callbackfn) {
        const length = this.getLength();
        for (let i = 0; i < length; i++) {
            const value = this.get(i);
            if (callbackfn(value, i))
                return i;
        }
        return -1;
    }
    forEach(callbackfn) {
        const length = this.getLength();
        for (let i = 0; i < length; i++)
            callbackfn(this.get(i), i);
    }
    get(_index) {
        return get(_index, this);
    }
    /**
     * Get the length of this list.
     *
     * @returns {number} The number of elements in this list.
     */
    getLength() {
        return pointer_1.getTargetListLength(this);
    }
    groupBy(callbackfn) {
        const length = this.getLength();
        const res = {};
        for (let i = 0; i < length; i++) {
            const v = this.get(i);
            res[callbackfn(v, i)] = v;
        }
        return res;
    }
    intersperse(sep) {
        const length = this.getLength();
        const res = new Array(length);
        for (let i = 0; i < length; i++) {
            if (i > 0)
                res.push(sep);
            res.push(this.get(i));
        }
        return res;
    }
    map(callbackfn) {
        const length = this.getLength();
        const res = new Array(length);
        for (let i = 0; i < length; i++)
            res[i] = callbackfn(this.get(i), i);
        return res;
    }
    reduce(callbackfn, initialValue) {
        let i = 0;
        let res;
        if (initialValue === undefined) {
            res = this.get(0);
            i++;
        }
        else {
            res = initialValue;
        }
        for (; i < this.getLength(); i++)
            res = callbackfn(res, this.get(i), i);
        return res;
    }
    set(_index, _value) {
        set(_index, _value, this);
    }
    slice(start = 0, end) {
        const length = end ? Math.min(this.getLength(), end) : this.getLength();
        const res = new Array(length - start);
        for (let i = start; i < length; i++)
            res[i] = this.get(i);
        return res;
    }
    some(callbackfn) {
        return this.any(callbackfn);
    }
    take(n) {
        const length = Math.min(this.getLength(), n);
        const res = new Array(length);
        for (let i = 0; i < length; i++)
            res[i] = this.get(i);
        return res;
    }
    takeWhile(callbackfn) {
        const length = this.getLength();
        const res = [];
        let take;
        for (let i = 0; i < length; i++) {
            const v = this.get(i);
            take = callbackfn(v, i);
            if (!take)
                return res;
            res.push(v);
        }
        return res;
    }
    toArray() {
        return this.map(util_1.identity);
    }
    toString() {
        return `List_${super.toString()}`;
    }
}
exports.List = List;
List._capnp = {
    displayName: "List<Generic>",
    size: list_element_size_1.ListElementSize.VOID,
};
List.get = get;
List.initList = initList;
List.set = set;
/**
 * Initialize the list with the given element size and length. This will allocate new space for the list, ideally in
 * the same segment as this pointer.
 *
 * @param {ListElementSize} elementSize The size of each element in the list.
 * @param {number} length The number of elements in the list.
 * @param {List<T>} l The list to initialize.
 * @param {ObjectSize} [compositeSize] The size of each element in a composite list. This value is required for
 * composite lists.
 * @returns {void}
 */
function initList(elementSize, length, l, compositeSize) {
    let c;
    switch (elementSize) {
        case list_element_size_1.ListElementSize.BIT:
            c = l.segment.allocate(Math.ceil(length / 8));
            break;
        case list_element_size_1.ListElementSize.BYTE:
        case list_element_size_1.ListElementSize.BYTE_2:
        case list_element_size_1.ListElementSize.BYTE_4:
        case list_element_size_1.ListElementSize.BYTE_8:
        case list_element_size_1.ListElementSize.POINTER:
            c = l.segment.allocate(length * pointer_1.getListElementByteLength(elementSize));
            break;
        case list_element_size_1.ListElementSize.COMPOSITE: {
            if (compositeSize === undefined) {
                throw new Error(util_1.format(errors_1.PTR_COMPOSITE_SIZE_UNDEFINED));
            }
            compositeSize = object_size_1.padToWord(compositeSize);
            const byteLength = object_size_1.getByteLength(compositeSize) * length;
            // We need to allocate an extra 8 bytes for the tag word, then make sure we write the length to it. We advance
            // the content pointer by 8 bytes so that it then points to the first list element as intended. Everything
            // starts off zeroed out so these nested structs don't need to be initialized in any way.
            c = l.segment.allocate(byteLength + 8);
            pointer_1.setStructPointer(length, compositeSize, c);
            trace("Wrote composite tag word %s for %s.", c, l);
            break;
        }
        case list_element_size_1.ListElementSize.VOID:
            // No need to allocate anything, we can write the list pointer right here.
            pointer_1.setListPointer(0, elementSize, length, l);
            return;
        default:
            throw new Error(util_1.format(errors_1.PTR_INVALID_LIST_SIZE, elementSize));
    }
    const res = pointer_1.initPointer(c.segment, c.byteOffset, l);
    pointer_1.setListPointer(res.offsetWords, elementSize, length, res.pointer, compositeSize);
}
exports.initList = initList;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function get(_index, _l) {
    throw new TypeError();
}
exports.get = get;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function set(_index, _value, _l) {
    throw new TypeError();
}
exports.set = set;
//# sourceMappingURL=list.js.map