@@ -42,13 +42,17 @@ pub fn BoundedArrayAligned(
42
42
const Len = std .math .IntFittingRange (0 , buffer_capacity );
43
43
44
44
buffer : [buffer_capacity ]T align (alignment ) = undefined ,
45
- len : Len = 0 ,
45
+
46
+ /// The active array length. Not that in order to save space, this is not
47
+ /// a `usize`, but rather the smallest integer type that can hold the
48
+ /// maximum capacity. In order to get the length as a `usize`, use `len()`.
49
+ active_len : Len = 0 ,
46
50
47
51
/// Set the actual length of the slice.
48
52
/// Returns error.Overflow if it exceeds the length of the backing array.
49
- pub fn init (len : usize ) error {Overflow }! Self {
50
- if (len > buffer_capacity ) return error .Overflow ;
51
- return Self { .len = @intCast (len ) };
53
+ pub fn init (initial_len : usize ) error {Overflow }! Self {
54
+ if (initial_len > buffer_capacity ) return error .Overflow ;
55
+ return Self { .active_len = @intCast (initial_len ) };
52
56
}
53
57
54
58
/// View the internal array as a slice whose size was previously set.
@@ -57,7 +61,7 @@ pub fn BoundedArrayAligned(
57
61
* align (alignment ) const [buffer_capacity ]T = > []align (alignment ) const T ,
58
62
else = > unreachable ,
59
63
} {
60
- return self .buffer [0.. self .len ];
64
+ return self .buffer [0.. self .active_len ];
61
65
}
62
66
63
67
/// View the internal array as a constant slice whose size was previously set.
@@ -67,9 +71,9 @@ pub fn BoundedArrayAligned(
67
71
68
72
/// Adjust the slice's length to `len`.
69
73
/// Does not initialize added items if any.
70
- pub fn resize (self : * Self , len : usize ) error {Overflow }! void {
71
- if (len > buffer_capacity ) return error .Overflow ;
72
- self .len = @intCast (len );
74
+ pub fn resize (self : * Self , new_len : usize ) error {Overflow }! void {
75
+ if (new_len > buffer_capacity ) return error .Overflow ;
76
+ self .active_len = @intCast (new_len );
73
77
}
74
78
75
79
/// Copy the content of an existing slice.
@@ -94,9 +98,14 @@ pub fn BoundedArrayAligned(
94
98
return self .buffer .len ;
95
99
}
96
100
101
+ /// Return the current length of the slice.
102
+ pub fn len (self : Self ) usize {
103
+ return self .active_len ;
104
+ }
105
+
97
106
/// Check that the slice can hold at least `additional_count` items.
98
107
pub fn ensureUnusedCapacity (self : Self , additional_count : usize ) error {Overflow }! void {
99
- if (self .len + additional_count > buffer_capacity ) {
108
+ if (self .active_len + additional_count > buffer_capacity ) {
100
109
return error .Overflow ;
101
110
}
102
111
}
@@ -110,39 +119,39 @@ pub fn BoundedArrayAligned(
110
119
/// Increase length by 1, returning pointer to the new item.
111
120
/// Asserts that there is space for the new item.
112
121
pub fn addOneAssumeCapacity (self : * Self ) * T {
113
- assert (self .len < buffer_capacity );
114
- self .len += 1 ;
115
- return & self .slice ()[self .len - 1 ];
122
+ assert (self .active_len < buffer_capacity );
123
+ self .active_len += 1 ;
124
+ return & self .slice ()[self .active_len - 1 ];
116
125
}
117
126
118
127
/// Resize the slice, adding `n` new elements, which have `undefined` values.
119
128
/// The return value is a slice pointing to the uninitialized elements.
120
129
pub fn addManyAsArray (self : * Self , comptime n : usize ) error {Overflow }! * align (alignment ) [n ]T {
121
- const prev_len = self .len ;
122
- try self .resize (self .len + n );
130
+ const prev_len = self .active_len ;
131
+ try self .resize (self .active_len + n );
123
132
return self .slice ()[prev_len .. ][0.. n ];
124
133
}
125
134
126
135
/// Remove and return the last element from the slice.
127
136
/// Asserts the slice has at least one item.
128
137
pub fn pop (self : * Self ) T {
129
- const item = self .get (self .len - 1 );
130
- self .len -= 1 ;
138
+ const item = self .get (self .active_len - 1 );
139
+ self .active_len -= 1 ;
131
140
return item ;
132
141
}
133
142
134
143
/// Remove and return the last element from the slice, or
135
144
/// return `null` if the slice is empty.
136
145
pub fn popOrNull (self : * Self ) ? T {
137
- return if (self .len == 0 ) null else self .pop ();
146
+ return if (self .active_len == 0 ) null else self .pop ();
138
147
}
139
148
140
149
/// Return a slice of only the extra capacity after items.
141
150
/// This can be useful for writing directly into it.
142
151
/// Note that such an operation must be followed up with a
143
152
/// call to `resize()`
144
153
pub fn unusedCapacitySlice (self : * Self ) []align (alignment ) T {
145
- return self .buffer [self .len .. ];
154
+ return self .buffer [self .active_len .. ];
146
155
}
147
156
148
157
/// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.
@@ -152,7 +161,7 @@ pub fn BoundedArrayAligned(
152
161
i : usize ,
153
162
item : T ,
154
163
) error {Overflow }! void {
155
- if (i > self .len ) {
164
+ if (i > self .active_len ) {
156
165
return error .Overflow ;
157
166
}
158
167
_ = try self .addOne ();
@@ -165,8 +174,8 @@ pub fn BoundedArrayAligned(
165
174
/// This operation is O(N).
166
175
pub fn insertSlice (self : * Self , i : usize , items : []const T ) error {Overflow }! void {
167
176
try self .ensureUnusedCapacity (items .len );
168
- self .len = @intCast (self .len + items .len );
169
- mem .copyBackwards (T , self .slice ()[i + items .len .. self .len ], self .constSlice ()[i .. self .len - items .len ]);
177
+ self .active_len = @intCast (self .active_len + items .len );
178
+ mem .copyBackwards (T , self .slice ()[i + items .len .. self .active_len ], self .constSlice ()[i .. self .active_len - items .len ]);
170
179
@memcpy (self .slice ()[i .. ][0.. items .len ], items );
171
180
}
172
181
@@ -176,10 +185,10 @@ pub fn BoundedArrayAligned(
176
185
pub fn replaceRange (
177
186
self : * Self ,
178
187
start : usize ,
179
- len : usize ,
188
+ range_len : usize ,
180
189
new_items : []const T ,
181
190
) error {Overflow }! void {
182
- const after_range = start + len ;
191
+ const after_range = start + range_len ;
183
192
var range = self .slice ()[start .. after_range ];
184
193
185
194
if (range .len == new_items .len ) {
@@ -195,7 +204,7 @@ pub fn BoundedArrayAligned(
195
204
for (self .constSlice ()[after_range .. ], 0.. ) | item , i | {
196
205
self .slice ()[after_subrange .. ][i ] = item ;
197
206
}
198
- self .len = @intCast (self .len - len + new_items .len );
207
+ self .active_len = @intCast (self .active_len - range_len + new_items .len );
199
208
}
200
209
}
201
210
@@ -217,20 +226,20 @@ pub fn BoundedArrayAligned(
217
226
/// Asserts the slice has at least one item.
218
227
/// This operation is O(N).
219
228
pub fn orderedRemove (self : * Self , i : usize ) T {
220
- const newlen = self .len - 1 ;
229
+ const newlen = self .active_len - 1 ;
221
230
if (newlen == i ) return self .pop ();
222
231
const old_item = self .get (i );
223
232
for (self .slice ()[i .. newlen ], 0.. ) | * b , j | b .* = self .get (i + 1 + j );
224
233
self .set (newlen , undefined );
225
- self .len = newlen ;
234
+ self .active_len = newlen ;
226
235
return old_item ;
227
236
}
228
237
229
238
/// Remove the element at the specified index and return it.
230
239
/// The empty slot is filled from the end of the slice.
231
240
/// This operation is O(1).
232
241
pub fn swapRemove (self : * Self , i : usize ) T {
233
- if (self .len - 1 == i ) return self .pop ();
242
+ if (self .active_len - 1 == i ) return self .pop ();
234
243
const old_item = self .get (i );
235
244
self .set (i , self .pop ());
236
245
return old_item ;
@@ -245,26 +254,26 @@ pub fn BoundedArrayAligned(
245
254
/// Append the slice of items to the slice, asserting the capacity is already
246
255
/// enough to store the new items.
247
256
pub fn appendSliceAssumeCapacity (self : * Self , items : []const T ) void {
248
- const old_len = self .len ;
249
- self .len = @intCast (self .len + items .len );
257
+ const old_len = self .active_len ;
258
+ self .active_len = @intCast (self .active_len + items .len );
250
259
@memcpy (self .slice ()[old_len .. ][0.. items .len ], items );
251
260
}
252
261
253
262
/// Append a value to the slice `n` times.
254
263
/// Allocates more memory as necessary.
255
264
pub fn appendNTimes (self : * Self , value : T , n : usize ) error {Overflow }! void {
256
- const old_len = self .len ;
265
+ const old_len = self .active_len ;
257
266
try self .resize (old_len + n );
258
- @memset (self .slice ()[old_len .. self .len ], value );
267
+ @memset (self .slice ()[old_len .. self .active_len ], value );
259
268
}
260
269
261
270
/// Append a value to the slice `n` times.
262
271
/// Asserts the capacity is enough.
263
272
pub fn appendNTimesAssumeCapacity (self : * Self , value : T , n : usize ) void {
264
- const old_len = self .len ;
265
- assert (self .len + n <= buffer_capacity );
266
- self .len = @intCast (self .len + n );
267
- @memset (self .slice ()[old_len .. self .len ], value );
273
+ const old_len = self .active_len ;
274
+ assert (self .active_len + n <= buffer_capacity );
275
+ self .active_len = @intCast (self .active_len + n );
276
+ @memset (self .slice ()[old_len .. self .active_len ], value );
268
277
}
269
278
270
279
pub const Writer = if (T != u8 )
@@ -295,7 +304,7 @@ test BoundedArray {
295
304
try testing .expectEqual (a .constSlice ().len , 32 );
296
305
297
306
try a .resize (48 );
298
- try testing .expectEqual (a .len , 48 );
307
+ try testing .expectEqual (a .len () , 48 );
299
308
300
309
const x = [_ ]u8 {1 } ** 10 ;
301
310
a = try BoundedArray (u8 , 64 ).fromSlice (& x );
@@ -313,18 +322,18 @@ test BoundedArray {
313
322
try a .ensureUnusedCapacity (a .capacity ());
314
323
(try a .addOne ()).* = 0 ;
315
324
try a .ensureUnusedCapacity (a .capacity () - 1 );
316
- try testing .expectEqual (a .len , 1 );
325
+ try testing .expectEqual (a .len () , 1 );
317
326
318
327
const uninitialized = try a .addManyAsArray (4 );
319
328
try testing .expectEqual (uninitialized .len , 4 );
320
- try testing .expectEqual (a .len , 5 );
329
+ try testing .expectEqual (a .len () , 5 );
321
330
322
331
try a .append (0xff );
323
- try testing .expectEqual (a .len , 6 );
332
+ try testing .expectEqual (a .len () , 6 );
324
333
try testing .expectEqual (a .pop (), 0xff );
325
334
326
335
a .appendAssumeCapacity (0xff );
327
- try testing .expectEqual (a .len , 6 );
336
+ try testing .expectEqual (a .len () , 6 );
328
337
try testing .expectEqual (a .pop (), 0xff );
329
338
330
339
try a .resize (1 );
@@ -338,46 +347,46 @@ test BoundedArray {
338
347
try a .resize (10 );
339
348
340
349
try a .insert (5 , 0xaa );
341
- try testing .expectEqual (a .len , 11 );
350
+ try testing .expectEqual (a .len () , 11 );
342
351
try testing .expectEqual (a .get (5 ), 0xaa );
343
352
try testing .expectEqual (a .get (9 ), 3 );
344
353
try testing .expectEqual (a .get (10 ), 4 );
345
354
346
355
try a .insert (11 , 0xbb );
347
- try testing .expectEqual (a .len , 12 );
356
+ try testing .expectEqual (a .len () , 12 );
348
357
try testing .expectEqual (a .pop (), 0xbb );
349
358
350
359
try a .appendSlice (& x );
351
- try testing .expectEqual (a .len , 11 + x .len );
360
+ try testing .expectEqual (a .len () , 11 + x .len );
352
361
353
362
try a .appendNTimes (0xbb , 5 );
354
- try testing .expectEqual (a .len , 11 + x .len + 5 );
363
+ try testing .expectEqual (a .len () , 11 + x .len + 5 );
355
364
try testing .expectEqual (a .pop (), 0xbb );
356
365
357
366
a .appendNTimesAssumeCapacity (0xcc , 5 );
358
- try testing .expectEqual (a .len , 11 + x .len + 5 - 1 + 5 );
367
+ try testing .expectEqual (a .len () , 11 + x .len + 5 - 1 + 5 );
359
368
try testing .expectEqual (a .pop (), 0xcc );
360
369
361
- try testing .expectEqual (a .len , 29 );
370
+ try testing .expectEqual (a .len () , 29 );
362
371
try a .replaceRange (1 , 20 , & x );
363
- try testing .expectEqual (a .len , 29 + x .len - 20 );
372
+ try testing .expectEqual (a .len () , 29 + x .len - 20 );
364
373
365
374
try a .insertSlice (0 , & x );
366
- try testing .expectEqual (a .len , 29 + x .len - 20 + x .len );
375
+ try testing .expectEqual (a .len () , 29 + x .len - 20 + x .len );
367
376
368
377
try a .replaceRange (1 , 5 , & x );
369
- try testing .expectEqual (a .len , 29 + x .len - 20 + x .len + x .len - 5 );
378
+ try testing .expectEqual (a .len () , 29 + x .len - 20 + x .len + x .len - 5 );
370
379
371
380
try a .append (10 );
372
381
try testing .expectEqual (a .pop (), 10 );
373
382
374
383
try a .append (20 );
375
384
const removed = a .orderedRemove (5 );
376
385
try testing .expectEqual (removed , 1 );
377
- try testing .expectEqual (a .len , 34 );
386
+ try testing .expectEqual (a .len () , 34 );
378
387
379
388
a .set (0 , 0xdd );
380
- a .set (a .len - 1 , 0xee );
389
+ a .set (a .len () - 1 , 0xee );
381
390
const swapped = a .swapRemove (0 );
382
391
try testing .expectEqual (swapped , 0xdd );
383
392
try testing .expectEqual (a .get (0 ), 0xee );
@@ -397,8 +406,8 @@ test "BoundedArray sizeOf" {
397
406
try testing .expectEqual (@sizeOf (BoundedArray (u8 , 3 )), 4 );
398
407
399
408
// `len` is the minimum required size to hold the maximum capacity
400
- try testing .expectEqual (@TypeOf (@as (BoundedArray (u8 , 15 ), undefined ).len ), u4 );
401
- try testing .expectEqual (@TypeOf (@as (BoundedArray (u8 , 16 ), undefined ).len ), u5 );
409
+ try testing .expectEqual (@TypeOf (@as (BoundedArray (u8 , 15 ), undefined ).active_len ), u4 );
410
+ try testing .expectEqual (@TypeOf (@as (BoundedArray (u8 , 16 ), undefined ).active_len ), u5 );
402
411
}
403
412
404
413
test "BoundedArrayAligned" {
0 commit comments