Vector

From FlightGear wiki
Revision as of 18:32, 15 April 2019 by Legoboyvdlp (talk | contribs) (Describe how to call functions of objects stored in a vector)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


std.Vector provides an object-oriented way to use vectors in Nasal in FlightGear 3.3 or higher.

Creating a new vector

A new empty Vector can be created as follows:

var v = std.Vector.new();

You can also wrap a standard Nasal vector in an instance of Vector by adding the Nasal vector as a parameter to new():

var v = std.Vector.new(['a', 'b', 'c']);

Manipulating vector objects

Adding items

A single item can be added using append():

v.append('x');

To insert an item on a specific position, use insert(). The index is zero-based. The previous item on the given index will be shifted one position to the right:

var v = std.Vector.new(['a', 'c', 'd']);

v.insert(1, 'b');

To insert items to the front of the vector, use index 0. If a Vector has n items, then you can use indices 0 to n. Using index n will append the item to the Vector. It is also possible to use negative indices in order to insert items at a position relative to the end of the Vector:

var v = std.Vector.new(['a', 'b', 'd']);

v.insert(-1, 'c');

This example inserts 'c' at the old position of 'd'. 'd' will be shifted to the right. To insert an item at the front of the Vector with currently n items using a negative index, use index -n:

var v = std.Vector.new(['b', 'c', 'd']);

v.insert(-3, 'a');

Adding vectors

The Vector can be extended with a Nasal vector using extend():

v.extend(['x', 'y', 'z']);

Removing items

To remove the first occurence of an item, use remove():

v.remove('x');

If you try to remove a non-existing item, a ValueError will be raised. Use Nasal's call() function to catch this error. To remove and return an item at a given index, use pop():

var v = std.Vector.new(['a', 'b', 'd', 'c']);

var result = v.pop(2);
assert(result == 'd');

Just like with insert(), the index given to pop() can be negative. If you try to call pop() using an invalid index, then an IndexError will be raised.

To remove all items from the Vector, use clear():

v.clear();

Querying vector objects

To query the size of the Vector, use size():

var v = std.Vector.new(['a', 'b', 'c']);

var result = v.size();
assert(result == 3);

To query the index of the first occurence of an item, use index():

var v = std.Vector.new(['a', 'b', 'c']);

var result = v.index('c');
assert(result == 2);

contains() can be used to determine if the Vector contains the given item:

var v = std.Vector.new(['a', 'b', 'c']);

var result = v.contains('c');
assert(result);

Iteration using vector objects

In order to iterate over the items, you need to iterate over the wrapped Nasal vector itself:

var v = std.Vector.new(['a', 'b', 'c']);

foreach (var item; v.vector) {
    print(item);
}


Calling functions using vector objects

In order to call functions of objects stored in a vector, you need to refer to the wrapped Nasal vector itself:

var object = {
    char: '',
    new: func(char) {
        var objectChild = {parents:[object]};
        objectChild.char = char;
        return objectChild;
    },
    outputInfo: func() {
        print(me.char);
    }
};

var objects = std.Vector.new([object.new('a')]);

objects.vector[0].outputInfo();