Skip to content

Commit 421170e

Browse files
committed
Introduce the growable vector API
1 parent 24e8178 commit 421170e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/data.table.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ void hash_set(hashtab *, SEXP key, R_xlen_t value);
298298
// Returns the value corresponding to the key present in the hash, otherwise returns ifnotfound.
299299
R_xlen_t hash_lookup(const hashtab *, SEXP key, R_xlen_t ifnotfound);
300300

301+
// growable.c
302+
// Return a new vector of given type. Initially its xlength() is equal to size. Using growable_resize(), it can be increased to up to max_size.
303+
SEXP growable_allocate(SEXPTYPE type, R_xlen_t size, R_xlen_t max_size);
304+
// Return the max_size of a growable vector. Behaviour is undefined if x was not allocated by growable_allocate.
305+
R_xlen_t growable_max_size(SEXP x);
306+
// Resize a growable vector to newsize. Will signal an error if newsize exceeds max_size.
307+
void growable_resize(SEXP x, R_xlen_t newsize);
308+
301309
// functions called from R level .Call/.External and registered in init.c
302310
// these now live here to pass -Wstrict-prototypes, #5477
303311
// all arguments must be SEXP since they are called from R level

src/growable.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "data.table.h"
2+
3+
SEXP growable_allocate(SEXPTYPE type, R_xlen_t size, R_xlen_t max_size) {
4+
SEXP ret = PROTECT(allocVector(type, max_size));
5+
SET_TRUELENGTH(ret, max_size);
6+
SET_GROWABLE_BIT(ret);
7+
SETLENGTH(ret, size);
8+
UNPROTECT(1);
9+
return ret;
10+
}
11+
12+
R_xlen_t growable_max_size(SEXP x) {
13+
return TRUELENGTH(x);
14+
}
15+
16+
void growable_resize(SEXP x, R_xlen_t newsize) {
17+
R_xlen_t max_size;
18+
if (newsize > (max_size = growable_max_size(x))) internal_error(
19+
__func__, "newsize=%g > max_size=%g",
20+
(double)newsize, (double)max_size
21+
);
22+
SETLENGTH(x, newsize);
23+
}

0 commit comments

Comments
 (0)