Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit d6517c7

Browse files
authored
Merge pull request #6 from arik123/b-search
Change search from linear to binary
2 parents 84a666b + 6dfb3c6 commit d6517c7

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

lib/schema.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ class Schema {
2424
* @return {Object}
2525
*/
2626
getItemByDefindex (defindex) {
27+
let start = 0;
28+
let end = this.raw.schema.items.length-1;
29+
let iterLim = Math.ceil(Math.log2(this.raw.schema.items.length)) + 2;
30+
while (start <= end) {
31+
if(iterLim<=0){
32+
break;//use fallback search
33+
}
34+
iterLim--;
35+
const mid = Math.floor((start+end)/2);
36+
if (this.raw.schema.items[mid].defindex < defindex) {
37+
start = mid + 1;
38+
} else if (this.raw.schema.items[mid].defindex > defindex) {
39+
end = mid - 1;
40+
} else {
41+
return this.raw.schema.items[mid];
42+
}
43+
}
2744
for (let i = 0; i < this.raw.schema.items.length; i++) {
2845
const item = this.raw.schema.items[i];
2946
if (item.defindex === defindex) {
@@ -56,6 +73,23 @@ class Schema {
5673
* @return {Object}
5774
*/
5875
getAttributeByDefindex (defindex) {
76+
let start = 0;
77+
let end = this.raw.schema.attributes.length-1;
78+
let iterLim = Math.ceil(Math.log2(this.raw.schema.attributes.length)) + 2;
79+
while (start <= end) {
80+
if(iterLim<=0){
81+
break;//use fallback search
82+
}
83+
iterLim--;
84+
const mid = Math.floor((start+end)/2);
85+
if (this.raw.schema.attributes[mid].defindex < defindex) {
86+
start = mid + 1;
87+
} else if (this.raw.schema.attributes[mid].defindex > defindex) {
88+
end = mid - 1;
89+
} else {
90+
return this.raw.schema.attributes[mid];
91+
}
92+
}
5993
for (let i = 0; i < this.raw.schema.attributes.length; i++) {
6094
const attribute = this.raw.schema.attributes[i];
6195
if (attribute.defindex === defindex) {
@@ -110,6 +144,24 @@ class Schema {
110144
* @return {String}
111145
*/
112146
getEffectById (id) {
147+
let start = 0;
148+
let end = this.raw.schema.attribute_controlled_attached_particles.length-1;
149+
let iterLim = Math.ceil(Math.log2(this.raw.schema.attribute_controlled_attached_particles.length)) + 2;
150+
while (start <= end) {
151+
if(iterLim<=0){
152+
break;//use fallback search
153+
}
154+
iterLim--;
155+
const mid = Math.floor((start+end)/2);
156+
if (this.raw.schema.attribute_controlled_attached_particles[mid].id < id) {
157+
start = mid + 1;
158+
} else if (this.raw.schema.attribute_controlled_attached_particles[mid].id > id) {
159+
end = mid - 1;
160+
} else {
161+
return this.raw.schema.attribute_controlled_attached_particles[mid].name;
162+
}
163+
}
164+
113165
for (let i = 0; i < this.raw.schema.attribute_controlled_attached_particles.length; i++) {
114166
const effect = this.raw.schema.attribute_controlled_attached_particles[i];
115167

0 commit comments

Comments
 (0)