| 1 |
// name: array_capacity.ck |
| 2 |
// desc: this demonstrates how array.size() array.capacity() works; |
| 3 |
// added: 1.4.1.0 (ge) |
| 4 |
// |
| 5 |
// (NOTE: .capacity() is not the same as .cap(); the latter is |
| 6 |
// maintained for historical / compatibility reasons; .cap() |
| 7 |
// actually is the same as .size() -- which is rather confusing! |
| 8 |
// |
| 9 |
// It is strongly recommended that .cap() be avoided; instead, |
| 10 |
// explicitly use .size() or .capacity() as appropriate.) |
| 11 |
|
| 12 |
// declare an array |
| 13 |
// (size = array length; capacity >= size) |
| 14 |
int a[4]; |
| 15 |
// print size and capacity |
| 16 |
print(1); |
| 17 |
|
| 18 |
// reset: clear array |
| 19 |
// (size is reset to 0; capacity is reset to at least 8) |
| 20 |
a.reset(); |
| 21 |
// print size and capacity |
| 22 |
print(2); |
| 23 |
|
| 24 |
// append to array |
| 25 |
// (size should reflect the number of elements appended) |
| 26 |
a << 1 << 2 << 3 << 5 << 8; |
| 27 |
// print size and capacity |
| 28 |
print(3); |
| 29 |
|
| 30 |
// resize to 15 |
| 31 |
// (this changes both size to 15 and capacity to at least 15) |
| 32 |
15 => a.size; |
| 33 |
// print size and capacity |
| 34 |
print(4); |
| 35 |
|
| 36 |
// append to array |
| 37 |
// (size should reflect the number of elements appended) |
| 38 |
a << 13 << 21 << 34; |
| 39 |
// print size and capacity |
| 40 |
print(5); |
| 41 |
|
| 42 |
// *reserve* capacity |
| 43 |
// (since this is less than current capacity, |
| 44 |
// neither size nor capacity is actually changed) |
| 45 |
10 => a.capacity; |
| 46 |
// print size and capacity |
| 47 |
print(6); |
| 48 |
|
| 49 |
// *reserve* a larger capacity |
| 50 |
// (this should result in a larger capacity without changing size) |
| 51 |
50 => a.capacity; |
| 52 |
// print size and capacity |
| 53 |
print(7); |
| 54 |
|
| 55 |
// helper print function |
| 56 |
fun void print( int iteration ) |
| 57 |
{ |
| 58 |
<<< iteration, "| array size:", a.size(), |
| 59 |
"capacity:", a.capacity() >>>; |
| 60 |
} |