diff options
Diffstat (limited to 'tools/gendata.js')
-rw-r--r-- | tools/gendata.js | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/tools/gendata.js b/tools/gendata.js index d51e660..c6e61d2 100644 --- a/tools/gendata.js +++ b/tools/gendata.js @@ -144,8 +144,8 @@ function def_space(type, pop, name) { function adjacent(an, bn) { ax = data.space_name.indexOf(an) bx = data.space_name.indexOf(bn) - add(data.spaces[ax].adjacent, bx) - add(data.spaces[bx].adjacent, ax) + set_add(data.spaces[ax].adjacent, bx) + set_add(data.spaces[bx].adjacent, ax) } def_space("province", 1, "Andhra") @@ -234,7 +234,7 @@ function def_city(name) { function adjacent_city(city, province) { c = data.city_name.indexOf(city) p = data.space_name.indexOf(province) - add(data.city[c].adjacent, p) + set_add(data.city[c].adjacent, p) } def_city("Chittor") @@ -299,9 +299,26 @@ def_piece(MI, TROOPS, 12) // Tools -function add(list, item) { - if (!list.includes(item)) - list.push(item) +function array_insert(array, index, item) { + for (let i = array.length; i > index; --i) + array[i] = array[i - 1] + array[index] = item +} + +function set_add(set, item) { + let a = 0 + let b = set.length - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = set[m] + if (item < x) + b = m - 1 + else if (item > x) + a = m + 1 + else + return + } + array_insert(set, a, item) } function to_ascii(s) { |