/
collection.js
160 lines (131 loc) · 3.93 KB
/
collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Simple collection module for the neko framework. It contains some useful functions to work with arrays and
* collections.
*
* SINCE: 30.01.12
*/
if( DEBUG ) expect("collection").not.existsAsNamespace();
/**
* Converts the arguments of the function to an array and returns it.
*/
function collection(){
var res = [];
var i=0, e=arguments.length;
for(; i<e; i++ ) res[i] = arguments[i];
return res;
}
/**
* Adds all elements from append to list.
*/
function collection_addAll(list, append){
if( DEBUG ){
expect(list).isArray();
expect(append).isArray();
}
var i = 0, e = append.length;
for(; i<e; i++ ) list.push( append[i] );
}
/**
* Copies all elements from one array to an other. This functions not adds the elements, it copies the whole array.
* After call of this function the following is true:
* - for index of list => list[index] === list2[index]
* - list.length === list2.length
* - list !== list2
*
* @param list {array} target array
* @param list2 {array} source array
*/
function collection_copy(list, list2){
if(TYPED) expect(list,append).isArray();
list.splice(0,list.length);
var i = 0,
e = list2.length;
for(; i<e; i++ ) list[i] = list2[i];
}
function collection_fillWithElement( list, element, start ){
if( !start ) start = 0;
var e = list.length;
for(; start<e; start++ ) list[start] = element;
}
var collection__sliceFn = Array.prototype.slice;
/**
* Slices a part out of an array and returns it. This implementation is uses the native
* Array class implementation. This function can be used on arguments arrays as well.
*/
function collection_slice(array,startIndex,endIndex){
return collection__sliceFn.apply(startIndex,endIndex)
}
/**
* Calls a function on each element of a list.
*/
function collection_each( list, fN ){
if( TYPED ){
expect( list ).isArray();
expect( fN ).isFunction();
}
var i = 0,
e = list.length;
for(; i<e; i++ ) fN( list[i], i, list ); // fN( element, index, array )
}
/**
* Calls a function on each element of a list.
*/
function collection_eachInRange( list, start, end, fN ){
if( TYPED ){
expect( list ).isArray();
expect( fN ).isFunction();
}
// check index parameters
if( DEBUG ){
expect(start).ge(0).lt(end);
expect(end).gt(start).lt(list.length);
}
for(; start<end; start++ ) fN( list[start], start, list );
}
/**
* Returns a simple enumeration object of a given list of strings.
* This function uses identical id numbers as values for the given enumeration keys.
*/
function collection_enum(){
if( typeof arguments[0] === 'string' ){
if( TYPED ) expect(arguments).each.isString();
var result = {};
for( var i=0, e=arguments.length; i<e; i++ ) result[ arguments[i] ] = i;
// values contains all enumeration values
result.__values__ = [];
collection_copy( result.__values__, arguments );
return result;
}
else return arguments[0];
}
/**
* Creates an array of structs. This function creates the array and fills it with empty struct objects.
*/
function collection_fixedStructArray( structName, size ){
if( DEBUG ){
expect( structName ).isString().size.gt(0);
expect( size ).isInteger().gt(0);
}
var a = [];
for( var i=0; i<size; i++ ) a[i] = neko_structObject(structName);
a.__struct__ = "StructArray";
return a;
}
/**
* Creates a simple matrix by creating a top array and sub arrays for the second dimensions. The sub dimension will be
* empty.
*/
function collection_matrix( width ){
if( DEBUG ){
expect( width ).isInteger().gt(0);
}
var a = [];
for( var i=0; i<width; i++ ) a[i] = [];
return a;
}
/**
* Clears an array. Uses array.splice method to realize it.
*/
function collection_clear( array ){
array.splice(0);
}