forked from santanche/mlca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlca_LayerDataStructure.js
116 lines (101 loc) · 2.61 KB
/
mlca_LayerDataStructure.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
/*
LayerDataStructure:
-topology: Layer's edge rule ('noloop','xloop','yloop','xyloop')
-dimensions: the size of the LayerDataStructure
specs: {
dimensions,
topology
}
+clear(state): set all cells to the state given by parameters.
-isValidCoord(coord): check if the coordinate given is within the layer dimension or is
validated by the topology, if so, returns true, else returns false.
-topologyHandler(coord): ajust the coordinate values given, if greater than the dimension or negative,
according to the layer topology
+cell(coordinate, value): Return a cell value or set a cell value if value is given
-getCell(coord): Dummy method, must be overwritten by the children according to its dataStructure.
-setCell(coord,newValue): Dummy method, must be overwritten by the children according to its dataStructure.
*/
mlca.LayerDataStructure = function (specs) {
'use strict';
this.dimensions = specs.dimensions;
this.topology = specs.topology;
};
mlca.LayerDataStructure.prototype = {
clear: function(state){
'use strict';
console.log('clear');
var it = {x:0,y:0};
for (it.x = 0;it.x < this.dimensions.x;it.x++){
for (it.y = 0;it.y < this.dimensions.y;it.y++){
this.setCell(it,state);
}
}
},
isValidCoord: function(coord){
'use strict';
if (coord.x >= this.dimensions.x ||
coord.x < 0){
switch (this.topology){
case 'xloop':
case 'xyloop':
break;
default:
return false;
}
}
if (coord.y >= this.dimensions.y ||
coord.y < 0){
switch (this.topology){
case 'yloop':
case 'xyloop':
break;
default:
return false;
}
}
return true;
},
topologyHandler: function(coord){
'use strict';
var ret = {};
ret.x = coord.x;
ret.y = coord.y;
if (coord.x >= this.dimensions.x || coord.x < 0){
switch (this.topology){
case 'xloop':
case 'xyloop':
ret.x = ret.x%this.dimensions.x;
if (ret.x<0) {ret.x += this.dimensions.x;}
break;
}
}
if (coord.y >= this.dimensions.y || coord.y < 0){
switch (this.topology){
case 'yloop':
case 'xyloop':
ret.y = ret.y%this.dimensions.y;
if (ret.y<0) {ret.y += this.dimensions.y;}
break;
}
}
return ret;
},
cell: function() {
'use strict';
var ret = -1;
var coords = this.topologyHandler(arguments[0]);
if (arguments.length === 1) {
ret = this.getCell(coords);
}
else if(arguments.length === 2) {
ret = this.setCell(coords, arguments[1]);
}
return ret;
},
getCell: function(coord) {
// Dummy method.
},
setCell: function(coord,newValue) {
// Dummy method.
}
};