library Base requires Table
//struct Base extends array
//-------------------------------------------
//static method operator [] takes string base returns thistype
//method to takes integer i returns string
//method from takes string i returns integer
//method ord takes string c returns integer
//method char takes integer i returns string
globals
private TableArray TA
private TableArray TR
private Table TB
private integer c = 0
private integer array s
endglobals
private module M
static method operator [] takes string base returns thistype
local integer value = StringHash(base)
local thistype this = TB[value]
local Table ta
local Table tr
local string char
if (integer(this) == 0) then
set this = c + 1
set c = this
set TB[value] = this
set value = StringLength(base)
set s[this] = value
set ta = TA[this]
set tr = TR[this]
loop
set value = value - 1
set char = SubString(base, value, value+1)
set ta[StringHash(char)] = value
set tr.string[value] = char
exitwhen value == 0
endloop
endif
return this
endmethod
method to takes integer i returns string
local integer k = s[this]
local string n = ""
local Table t = TR[this]
loop
exitwhen i < k
set n = t.string[i - i / k * k] + n
set i = i/k
endloop
return t.string[i] + n
endmethod
method from takes string i returns integer
local integer n = 0
local integer p = StringLength(i)
local integer l = 0
local integer k = s[this]
local Table t = TA[this]
loop
exitwhen p == 0
set p = p - 1
set l = l + 1
set n = n + t[StringHash(SubString(i, l - 1, l))] * R2I(Pow(k, p))
endloop
return n
endmethod
method ord takes string c returns integer
return TA[this][StringHash(c)]
endmethod
method char takes integer i returns string
return TR[this][i]
endmethod
method operator size takes nothing returns integer
return s[this]
endmethod
private static method onInit takes nothing returns nothing
set TA = TableArray[8191]
set TR = TableArray[8191]
set TB = Table.create()
endmethod
endmodule
struct Base extends array
implement M
endstruct
endlibrary