Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/libs/Editor_files/Iterator.js
4627 views
1
var pgli = pgli || {};
2
pgli.lang = pgli.lang || {};
3
4
pgli.lang.Iterator = gamecore.Base.extend('Iterator',
5
// Static
6
{
7
MAX_ITERATIONS: 1000,
8
9
COMPARATORS: {
10
"<": 0,
11
">": 1,
12
"<=": 2,
13
">=": 3
14
},
15
16
genComparatorMethod: function(type)
17
{
18
switch(type)
19
{
20
case 0:
21
return function(a, b){ return a < b };
22
case 1:
23
return function(a, b){ return a > b };
24
case 2:
25
return function(a, b){ return a <= b };
26
case 3:
27
return function(a, b){ return a >= b };
28
default:
29
return function(a, b){ return false };
30
}
31
},
32
33
genStepMethod: function(type, scope, attr)
34
{
35
switch(type)
36
{
37
case 1:
38
case 3:
39
return function(){ return --scope[attr] };
40
case 0:
41
case 2:
42
default:
43
return function(){ return ++scope[attr] };
44
}
45
}
46
},
47
// Instance
48
{
49
varname: "i",
50
start: 0,
51
end: 1,
52
comparator: 0,
53
compMethod: null,
54
stepMethod: null,
55
step: 0,
56
iter: 0,
57
58
init: function(name, start, comparator, end)
59
{
60
var static = pgli.lang.Iterator;
61
62
if(comparator in static.COMPARATORS)
63
this.comparator = static.COMPARATORS[comparator];
64
else
65
this.comparator = static.COMPARATORS["<"];
66
67
this.start = start;
68
this.end = end;
69
this.step = start;
70
71
this.varname = name;
72
this.compMethod = static.genComparatorMethod(this.comparator);
73
this.stepMethod = static.genStepMethod(this.comparator, this, "step");
74
},
75
76
loop: function()
77
{
78
return (this.iter < pgli.lang.Iterator.MAX_ITERATIONS && this.compMethod(this.step, this.end));
79
},
80
81
next: function()
82
{
83
++this.iter;
84
return this.stepMethod();
85
},
86
87
toString: function()
88
{
89
return "Iterator("+this.varname+") "+this.start+" - "+this.step+" - "+this.end;
90
}
91
92
});
93