- Joined
- Apr 30, 2011
- Messages
- 359
JASS:
//========================================================================================
//
// Real Integer
// -*- overcold_ice -*-
//
// -[*] Requirements:
// - JNGP
// - latest version of JassHelper
//
// For your high integer value needs
// This snippet uses real to represent integer values
// Normally an integer ranges from -2^31 to 2^31-1
// But in this snippet, it can be a maximum of 2^47*10^9
// This will use all available decimal digits too
//
// -[*] API:
//
// struct RealInt extends array
// readonly real integer
// formatted integer in real
//
// static method create takes nothing returns RealInt
// basic allocator for the struct
// static method operator [] takes real RealInt returns RealInt
// allocate a struct with an already formatted real
// static method convertInt takes integer Int returns RealInt
// static method convertStr takes string Str returns RealInt
// allocator for the struct, converts an integer/string into a formatted real
// allocated instances are automatically destroyed if not locked
//
// method lock takes nothing returns nothing
// method unlock takes nothing returns nothing
// increases/decreases the lock count of the struct
// if the lock count decreased to 0, destroy the struct
//
// method operator realInt= takes real RealInt returns nothing
// method operator int= takes integer Int returns nothing
// method operator str= takes string Str returns nothing
// set the value of this.integer from an already formatted real,
// an original integer, or a string
//
// method toInt takes nothing returns integer
// returns original integer value of this.integer
// will return unknown value if the converted integer exceed its limit
// method toStr takes nothing returns string
// returns the string equivalent of this.integer
// this will remove the decimal separator of this.integer and
// unnecessary zeros
//
//========================================================================================
library RealInt
globals
private constant real POW10_E9 = 1000000000
endglobals
private function GetFirstDot takes string s returns integer
local integer i = 0
local integer l = StringLength(s)
loop
exitwhen SubString(s, i, i + 1) == "." or i == l
set i = i + 1
endloop
return i
endfunction
private function GetNineDigit takes string s returns string
local integer i = StringLength(s)
loop
exitwhen i == 9
set i = i + 1
set s = s + "0"
endloop
return s
endfunction
private function GetRightInteger takes string s returns string
local integer i = StringLength(s)
loop
exitwhen i == 0 or SubString(s, i - 1, i) == "0"
set i = i - 1
endloop
return SubString(s, i, StringLength(s))
endfunction
struct RealInt extends array
readonly real integer
private integer lc
private static integer c
private thistype r
private static thistype array ti
private method destroy takes nothing returns nothing
set this.integer = 0
set this.r = thistype(0).r
set thistype(0).r = this
endmethod
private static method onExpire takes nothing returns nothing
local thistype this = .ti [GetHandleId(GetExpiredTimer())]
if this.lc == 0 then
call this.destroy()
endif
call DestroyTimer(GetExpiredTimer())
endmethod
static method create takes nothing returns thistype
local thistype this = thistype(0).r
local timer t = CreateTimer()
if this == 0 then
set .c = .c + 1
set this = .c
else
set thistype(0).r = this.r
endif
call TimerStart(t, 0, false, function thistype.onExpire)
set .ti [GetHandleId(t)] = this
set t = null
return this
endmethod
static method operator [] takes real i returns thistype
local thistype this = .create()
set this.integer = i
return this
endmethod
static method convertInt takes integer i returns thistype
local thistype this = .create()
set this.integer = i / POW10_E9
return this
endmethod
static method convertStr takes string s returns thistype
local thistype this = .create()
set this.integer = S2R(s) / POW10_E9
return this
endmethod
method operator realInt= takes real i returns nothing
set this.integer = i
endmethod
method operator int= takes integer i returns nothing
set this.integer = i / POW10_E9
endmethod
method operator str= takes string s returns nothing
set this.integer = S2R(s) / POW10_E9
endmethod
method toInt takes nothing returns integer
return R2I(this.integer * POW10_E9)
endmethod
method toStr takes nothing returns string
local string s = R2S(this.integer)
local integer f = GetFirstDot(s)
if SubString(s, 0, f) == "0" then
return GetRightInteger(SubString(s, f + 1, StringLength(s)))
endif
return SubString(s, 0, f) + GetNineDigit(SubString(s, f + 1, StringLength(s)))
endmethod
method lock takes nothing returns nothing
set this.lc = this.lc + 1
endmethod
method unlock takes nothing returns nothing
if this.lc == 0 then
return
endif
set this.lc = this.lc - 1
if this.lc == 0 then
call this.destroy()
endif
endmethod
endstruct
endlibrary
can someone give me the exact range of reals?
Last edited: