- Characteristics
- Performance
- Supported Element Types
- Unsafe
- Declaration
- Literal Syntax
- Access
- Iteration
- Sorting
Fix sequences are fixed-size arrays without any bounds checking.
Characteristics
- Unsafe (see below)
- Preserves order
- No append / insert / remove
- No contains / indexOf
- No built-in iteration
Performance
- Fast indexing (constant time, O(1))
- Small memory footprint (elementSize*capacity, no overhead)
Supported Element Types
All types are supported.
Unsafe
Almost all kinds of memory access in CM is safe. But fix sequences are one of the exceptions. This means that:
- You can read and write outside the bounds of the fixed array. There will (probably) not be any exception thrown, but you will of course access memory that you are not supposed to access. This means that you can read uninitialized memory and even modify the memory that is used by other objects, possibly corrupting their integrity and also break the garbage collector. This must of course be avoided at all costs.
- The fix seq has no idea what size or count it has, so you must keep track of the size yourself.
Some operations return fix sequences, for instance Sequence.array and Stream.get(int n). These are only exposed for C++ interfacing and extremely time-critical routines.
Declaration
fix elementType[] identifier;
Example
Declare a fix seq of integers initialized to null:
fix int[] integers;
Declare and create an empty fix seq (you must specify the size):
fix int[] integers(16); fix int[] integers = new fix int[](16);
It's also possible to create the fix seq using the init feature:
fix int[] integers; ... init integers(16);
Literal Syntax
fix int[] array = [int: 1, 2, 3, 4].array;
Access
{ fix int[] seq = [1, 2, 3, 4].array; int x = unsafe seq[0]; // indexed get unsafe seq[3] = 9; // indexed assign }
Iteration
There is no built-in for iteration for fixed seq, but you can iterate like this:
{ fix int[] array = [int: 1, 2, 3, 4].array; while (int i=0; i < 4; i++) pln(unsafe array[i]); }
Note that you must specify the count (4) yourself.
Sorting
Sorting is not supported. You can write your own, but it's recommended that you use a real Sequence instead.
Comments
0 comments
Please sign in to leave a comment.