Skip to content

infinity.list

Provides efficient list-type operations with data collections. Should be preferred over arrays for performance reasons.

Module: infinity.list

Classes:
Enums:

Example:

infinity.loadModule('infinity.list');

var list = new infinity.list(infinity.list.type.generic);
list.add(1);
console.debug(list.toArray());
//[1]
list.add(true);
console.debug(list.toArray());
//[1,true]
list.add('Hello World!');
console.debug(list.toArray());
//[1,true,"Hello World!"]
list.add({id: 1, name: 'Tobias'});
console.debug(list.toArray());
//[1,true,"Hello World!",{"id":1,"name":"Tobias"}]
list.add([1,2,3]);
console.debug(list.toArray());
//[1,true,"Hello World!",{"id":1,"name":"Tobias"},[1,2,3]]
list.insert(1, false);
console.debug(list.toArray());
//[1,false,true,"Hello World!",{"id":1,"name":"Tobias"},[1,2,3]]
list.remove(0);
console.debug(list.toArray());
//[false,true,"Hello World!",{"id":1,"name":"Tobias"},[1,2,3]]
list.exchange(1,2);
console.debug(list.toArray());
//[false,"Hello World!",true,{"id":1,"name":"Tobias"},[1,2,3]]
list.move(1,2);
console.debug(list.toArray());
//[false,true,"Hello World!",{"id":1,"name":"Tobias"},[1,2,3]]
list.put(0, 'Hi');
console.debug(list.toArray());
//["Hi",true,"Hello World!",{"id":1,"name":"Tobias"},[1,2,3]]
console.debug(list.indexOf('Hi'));
//0
list.sort();
var a = list.toArray();
console.debug(a);
//["Hello World!","Hi",[1,2,3],{"id":1,"name":"Tobias"},true]
console.debug(list.indexOf('Hi'));
//1
list.clear();
list.fromArray(a);
console.debug(list.toArray());
//["Hello World!","Hi",[1,2,3],{"id":1,"name":"Tobias"},true]
list.free();

var list = new infinity.list(infinity.list.type.number);
list.sorted = true;
list.add(1);
console.debug(list.toArray());
//[1]
list.add(5);
console.debug(list.toArray());
//[1,5]
list.add(3);
console.debug(list.toArray());
//[1,3,5]
list.add(8);
console.debug(list.toArray());
//[1,3,5,8]
list.add(2);
console.debug(list.toArray());
//[1,2,3,5,8]
list.insert(1, 10);
console.debug(list.toArray());
//[1,10,2,3,5,8]
list.remove(0);
console.debug(list.toArray());
//[10,2,3,5,8]
list.exchange(1,2);
console.debug(list.toArray());
//[10,3,2,5,8]
list.move(1,2);
console.debug(list.toArray());
//[10,2,3,5,8]
list.put(0, 7);
console.debug(list.toArray());
//[7,2,3,5,8]
console.debug(list.indexOf(7));
//0
list.sort();
var a = list.toArray();
console.debug(a);
//[2,3,5,7,8]
console.debug(list.indexOf(7));
//3
list.add(4);
console.debug(list.toArray());
//[2,3,4,5,7,8]
list.clear();
list.fromArray(a);

console.debug('--------------------');
console.debug('list a: ' + list.toArray());
//list a: 2,3,5,7,8

var list2 = new infinity.list(infinity.list.type.number);
list2.fromArray([1, 5, 3, 9, 11, 12]);
console.debug('list b: ' + list2.toArray());
// list b: 1,5,3,9,11,12
list.assign(list2, infinity.list.operator.or);
console.debug('list a or b: ' + list.toArray());
//list a or b: 1,2,3,5,7,8,9,11,12
list2.free();
console.debug('--------------------');

//Comparison to JavaScript arrays:

list.clear();
list.sorted = false;
console.time('a');

for (var i = 0; i < 1000000; i++)
  list.add(Math.round(Math.random() * 1000000));

list.sort();
console.debug(list.toArray(0, 30));
//[1,4,4,4,5,9,9,12,12,13,15,15,16,20,20,21,23,23,23,23,24,25,25,26,26,26,27,27,29,30]
console.timeEnd('a');
//a: 3,53103140s
list.free();

var a = [];
console.time('b');

for (var i = 0; i < 1000000; i++)
  a.push(Math.round(Math.random() * 1000000));

a.sort(function(a, b){return a-b});
console.debug(a.slice(0, 30));
//[3,3,4,4,5,7,8,8,8,9,10,11,13,15,18,19,19,20,21,22,22,23,24,26,28,30,32,33,35,37]
console.timeEnd('b');
//b: 15,54191280s

Classes

list

The class for operations with lists.

Example:

infinity.loadModule('infinity.list');
let myList = new infinity.list();

Properties


capacity

Type: number

Gets or sets the number of elements that the current list can contain before resizing is required.


sorted

Type: boolean

Specifies whether the current list is sorted or not. While sorted is true, add() inserts new elements at their sorted position and indexOf() uses a binary search.

Setting sorted to true does not sort the list, it only marks it as sorted; call sort() for an unsorted list instead. The flag is set to true by sort() and by assign() with the operators and and or, and is reset to false by insert(), put(), exchange(), move() and assign() with the operator copy.


count

Type: number

Gets the number of elements contained in the current list.


Methods


constructor()

Lets you create an INFINITY.JS list object instance. Lets you optionally specify the list type.

Signature:

constructor( type?: infinity.list.type )

Parameters:

  • type: infinity.list.type, optional

    The type of list to create. Default is infinity.list.type.generic.

Example:

infinity.loadModule('infinity.list');
let myList  = new infinity.list(infinity.list.type.generic);

add()

Adds the given data to the end of the current list and returns its position. If the list is sorted, the data is inserted at its sorted position instead.

Signature:

add( value: any ): number

Parameters:

  • value: any

    The data to add. For lists of type number the value is converted to a number, for lists of type string to a string.

Return type: number

Example:

myList.add(1);
console.debug(myList.toArray());
//[1]
myList.add([3, 4, 5, {prop1: 1, prop2: [1,2,'Somedata']}]);
console.debug(myList.toArray());
//[1,[3,4,5,{"prop1":1,"prop2":[1,2,"Somedata"]}]]

assign()

Assigns data from another specified list to the current list. Both lists must be of the same type, otherwise an error is thrown. The operators and, or and not sort both lists (including the source list) before combining them.

Signature:

assign( src: infinity.list, mode?: infinity.list.operator ): void

Parameters:

  • src: infinity.list

    The list to assign data from.

  • mode: infinity.list.operator, optional

    The assignment mode. Default is infinity.list.operator.copy.

Example:

infinity.loadModule('infinity.list');
let myList = new infinity.list(infinity.list.type.number);
myList.add(12);
let myList2 = new infinity.list(infinity.list.type.number);
myList2.add(345);
myList.assign(myList2, infinity.list.operator.or);
console.debug(myList.toArray());
//[12,345]

clear()

Clears data from the current list.

Signature:

clear(): void

Example:

myList.clear();

contains()

Reports, whether the current list contains the specified data. In generic lists, values are compared by their JSON representation.

Signature:

contains( value: any ): boolean

Parameters:

  • value: any

    The data to check for.

Return type: boolean

Example:

if(myList.contains(3)){
  //...
}

exchange()

Exchanges the position of the specified elements inside the current list. Throws an error if an index is out of range.

Signature:

exchange( index1: number, index2: number ): void

Parameters:

  • index1: number

    The first element to exchange.

  • index2: number

    The second element to exchange.

Example:

myList.exchange(3, 5);

first()

Returns the value of the first element in the current list, or undefined if the list is empty.

Signature:

first(): any

Return type: any

Example:

let firstData = myList.first();

free()

Frees the memory previously occupied by the list object instance. The object must not be used afterwards.

Signature:

free(): void

Example:

myList.free();

fromArray()

Replaces the contents of the current list with the elements of the given array. If the list is sorted, the elements are inserted at their sorted positions. Throws an error if the parameter is not an array.

Signature:

fromArray( a: Array<any> ): void

Parameters:

  • a: Array<any>

    The array to build the list of.

Example:

infinity.loadModule('infinity.list');
let a = [1, 2, 'a'];
let myList = new infinity.list(infinity.list.type.generic);
myList.fromArray(a);

get()

Returns the value of the element from the specified position of the current list.

Signature:

get( index: number ): any

Parameters:

  • index: number

    The position of the element to retrieve.

Return type: any

Example:

let data = myList.get(4);

indexOf()

Returns the position of the element containing the specified data inside the current list, or -1 if the data is not found. In generic lists, values are compared by their JSON representation.

Signature:

indexOf( value: any ): number

Parameters:

  • value: any

    The data to examine the list for.

Return type: number

Example:

let indexOfDataString = myList.indexOf('data');

insert()

Inserts the data at the specified position, moving the previous element at that position and all following elements one position back.

Signature:

insert( index: number, value: any ): void

Parameters:

  • index: number

    The position at which the data should be inserted.

  • value: any

    The data to insert.

Example:

myList.insert(3, 'inserted');

last()

Returns the value of the last element inside the current list, or undefined if the list is empty.

Signature:

last(): any

Return type: any

Example:

let lastData = myList.last();

move()

Reassigns the specified element to another position inside the current list.

Signature:

move( index1: number, index2: number ): void

Parameters:

  • index1: number

    The element to be moved.

  • index2: number

    The new position to move the element to.

Example:

myList.move(3, 5);

put()

Puts the given data to the specified position, replacing data that was there before.

Signature:

put( index: number, value: any ): void

Parameters:

  • index: number

    The position inside the list to put data at.

  • value: any

    The data to put at the specified position.

Example:

myList.put(2, "replaced");

remove()

Removes an element from the specified position of the current list.

Signature:

remove( index: number ): void

Parameters:

  • index: number

    The position to remove an element from.

Example:

myList.remove(3);

sort()

Sorts the current list in ascending order and sets sorted to true. Lists of type string are sorted case-sensitively, generic lists are sorted by the JSON representation of their elements.

Signature:

sort(): void

Example:

let myList = new infinity.list(infinity.list.type.generic);
myList.fromArray([8, "data", 3, 5,"string"]);
myList.sort();
console.debug(myList.toArray());
//["data","string",3,5,8]

toArray()

Returns an array built of the data inside the current list: the elements from index to index + count - 1. Throws an error if this range exceeds the list.

Signature:

toArray( index?: number, count?: number ): Array<any>

Parameters:

  • index: number, optional

    The position inside the list to start at. Default is 0.

  • count: number, optional

    The number of elements to return. Default is all elements from index to the end of the list.

Return type: Array<any>

Example:

myList.toArray();

trim()

Trims the capacity of the current list to the current amount of present elements.

Signature:

trim(): void

Example:

infinity.loadModule('infinity.list');
let myList = new infinity.list(infinity.list.type.number);
myList.fromArray([1,2,3,4,5,6,7,8,9,10]);
console.debug(myList.capacity)
myList.remove(9);
myList.trim();
console.debug(myList.capacity)

Enums

infinity.list.operator

Values:

  • copy: 0

    The receiving list will be completely overwritten by the assigned list.

  • and: 1

    Only elements with identical data between the lists will be kept inside the receiving list (intersection).

  • or: 2

    The receiving list will be combined with the assigned list; elements already present in the receiving list are not added again (union).

  • not: 3

    Elements that are also present in the assigned list are removed from the receiving list (difference).

Example:

myList.assign(myList2, infinity.list.operator.not)

infinity.list.type

Values:

  • generic: 0

    For a list that accepts any data.

  • number: 1

    For a list that accepts only numbers.

  • string: 2

    For a list that accepts only strings.

Example:

infinity.loadModule('infinity.list');
var myList = new infinity.list(infinity.list.type.generic);