Код
Язык:
Jass, vJass
Тип:
Библиотека
Шаблон
Данный ресурс является шаблоном, а также частью стандартов форматирования Волчьей Повести.
Библиотека Randomizer v0.1
library Randomizer
    globals
        constant integer IRON_RANDOM_MAX_SIZE    = 256 
    endglobals

    struct IronRandom
        integer         Size        = 0
        integer         SizeZ       = 0
        integer array   Pool    [IRON_RANDOM_MAX_SIZE]
        integer array   Integer [IRON_RANDOM_MAX_SIZE]
        real    array   Float   [IRON_RANDOM_MAX_SIZE]
        string  array   String  [IRON_RANDOM_MAX_SIZE]


        method random takes nothing returns integer
            return GetRandomInt( 0, this.Size - 1 )
        endmethod

        method randstr takes nothing returns string
            return this.String[this.random()]
        endmethod

        method randint takes nothing returns integer
            return this.Integer[this.random()]
        endmethod

        method randfloat takes nothing returns real
            return this.Float[this.random()]
        endmethod

        method lottery_reset takes nothing returns nothing
            local integer F = 0
            set this.SizeZ = this.Size
            loop
                set this.Pool[F] = F 
                set F = F + 1 
            exitwhen F == this.SizeZ
            endloop
        endmethod

        method lottery takes nothing returns integer
            local integer lotto     = GetRandomInt( 0, this.SizeZ - 1 )
            local integer newSizeZ  = this.SizeZ - 1
            local integer F         = 0
            local integer Result    = this.Pool[ lotto ]
            
            set this.Pool[ lotto ]  = -1

            if newSizeZ > 1 then
                
                loop
                    if this.Pool[ F ] < 0 then
                        set this.Pool[ F ] = this.Pool[ F + 1]
                        set this.Pool[ F + 1] = -1
                    endif
                    set F = F + 1 
                exitwhen F == newSizeZ    
                endloop

                set this.SizeZ = newSizeZ
            else
                if newSizeZ > 0 then 
                    if this.Pool[0] < 0 then
                        set this.Pool[0] = this.Pool[1]
                        set this.Pool[1] = -1 
                    endif
                    set this.SizeZ = newSizeZ
                endif
            endif


            if Result < 0 then 
                return 0
            endif

            return Result
        endmethod

        method lottery_str takes nothing returns string
            return this.String[ this.lottery() ]
        endmethod

        method lottery_int takes nothing returns integer
            return this.Integer[ this.lottery() ]
        endmethod

        method lottery_float takes nothing returns real
            return this.Float[ this.lottery() ]
        endmethod

        method add takes integer intValue, real fltValue, string strValue returns nothing
            if this.Size < IRON_RANDOM_MAX_SIZE then 
                
                set this.Integer[ this.Size ] = intValue
                set this.Float  [ this.Size ] = fltValue
                set this.String [ this.Size ] = strValue

                set this.Pool       [ this.SizeZ ] = this.Size

                set this.Size   = this.Size + 1
                set this.SizeZ  = this.Size
            endif
        endmethod

        method add_int takes integer intValue returns nothing
            if this.Size < IRON_RANDOM_MAX_SIZE then 
            
                set this.Integer[ this.Size ] = intValue

                set this.Pool       [ this.SizeZ ] = this.Size

                set this.Size   = this.Size + 1
                set this.SizeZ  = this.Size
            endif
        endmethod

        method add_str takes string strValue returns nothing
            if this.Size < IRON_RANDOM_MAX_SIZE then 
            
                set this.String[ this.Size ] = strValue

                set this.Pool       [ this.SizeZ ] = this.Size

                set this.Size   = this.Size + 1
                set this.SizeZ  = this.Size
            endif
        endmethod

        method add_float takes real fltValue returns nothing
            if this.Size < IRON_RANDOM_MAX_SIZE then 
            
                set this.Float[ this.Size ] = fltValue

                set this.Pool       [ this.SizeZ ] = this.Size

                set this.Size   = this.Size + 1
                set this.SizeZ  = this.Size
            endif
        endmethod

        method destory takes nothing returns nothing
            local integer i = 0
            loop

                set this.Float  [i] = 0
                set this.String [i] = null
                set this.Integer[i] = 0
                set this.Pool   [i] = 0
                 
                set i = i + 1
            exitwhen i == this.Size
            endloop
            
            set this.Size   = 0
            set this.SizeZ  = 0
            call this.deallocate()
        endmethod

        static method create takes nothing returns thistype
            return thistype.allocate()
        endmethod

    endstruct

    // Extra Pool Index Xash

    struct epix 
        static hashtable hash       = InitHashtable()
        integer          Size       = 0
        integer          hashSize   = 0

        method destroy takes nothing returns nothing
            call FlushChildHashtable( thistype.hash, this )
            set this.Size       = 0
            set this.hashSize   = 0
            call this.deallocate()
        endmethod

        method reset takes nothing returns nothing
            local integer i = 0
            set this.hashSize = this.Size
            loop
                call SaveInteger( this.hash, this, i, i )
                set i = i + 1
            exitwhen i == this.hashSize 
            endloop
        endmethod

        method random takes nothing returns integer
            local integer Index     = GetRandomInt(0, this.hashSize - 1 )
            local integer Result    = LoadInteger( thistype.hash, this, Index )
            local integer Pointer   = 0
            local integer NewSize   = this.hashSize - 1 

            call SaveInteger( thistype.hash, this, Index, -1 )
            
            if NewSize > 1 then 

                loop
                    if LoadInteger( thistype.hash, this, Pointer )  < 0 then
                        call SaveInteger( thistype.hash, this, Pointer, LoadInteger( thistype.hash, this, Pointer + 1) ) 
                        call SaveInteger( thistype.hash, this, Pointer + 1 , -1 ) 
                    endif
                    set Pointer = Pointer + 1
                exitwhen Pointer == NewSize
                endloop

                set this.hashSize = NewSize
            else
                if NewSize > 0 then 
                    if LoadInteger( thistype.hash, this, 0 ) < 0 then
                        call SaveInteger( thistype.hash, this, 0, LoadInteger( thistype.hash, this, 1 ) ) 
                        call SaveInteger( thistype.hash, this, 1, -1 ) 
                        set this.hashSize = NewSize
                    endif
                endif
            endif
            
            if Result < 0 then
                set Result = 0 
            endif 

            return Result
        endmethod

        static method create takes integer MaxIndex returns thistype
            local thistype  this            = thistype.allocate()
            local integer   i               = 0

            if MaxIndex > JASS_MAX_ARRAY_SIZE then 
                set             this.Size       = 1024
                set             this.hashSize   = 1024
            else
                set             this.Size       = MaxIndex
                set             this.hashSize   = MaxIndex
            endif

            loop
                call SaveInteger( this.hash, this, i, i )
                set i = i + 1
            exitwhen i == this.hashSize 
            endloop

            return this
        endmethod
    endstruct

endlibrary
КОНСТАНТЫ & ПЕРЕМЕННЫЕ

IRONRANDOM
IRON_RANDOM_MAX_SIZE ( integer), значение по умолчанию 256. Максимальное значения пула для рандома.
<IR_var>.Integer[] массив целых в рандомного пула.
<IR_var>.Float[] массив вещественного в рандомного пула.
<IR_var>.String[] массив строк в рандомного пула.
ФУНКЦИИ & МЕТОДЫ

IRONRANDOM
IronRadom.create() - создаёт новый пул.
I<IR_var>.destroy() - уничтожает пул.
<IR_var>.add(integer,real,string) - добавляет в пул рандома элементы.
<IR_var>.add_int(integer) - добавляет в пул рандома элемент целого типа.
<IR_var>.add_float(real) - добавляет в пул рандома элемент типа с плавающей точкой.
<IR_var>.add_str(string) - добавляет в пул рандома элемент строкового типа.
<IR_var>.random() - возвращает id массива пула.
<IR_var>.random_int() - возвращает случайное целое.
<IR_var>.random_float() - возвращает случайное вещественное.
<IR_var>.random_str() - возвращает случайную строку.
<IR_var>.lottery() - возвращает id массива пула, который не поворотится при следующем рандоме.
<IR_var>.lottery_int() - возвращает не повторяющееся целое.
<IR_var>.lottery_floa() - возвращает не повторяющееся вещественное.
<IR_var>.lottery_str() - возвращает не повторяющуюся строку.
<IR_var>.lottery_reset() - сбрасывает пул не повторяющегося рандома.
EPIX
epix.create(integer) - создаёт тяжёлый рандом, максимальное значение 8192/32768.
<epix_var>.random() - возвращает не повторяющееся случайное целое число из тяжёлого
рандома epix.
<epix_var>.reset() - сбрасывает тяжёлый рандом.
<epix_var>.destroy() - уничтожает тяжёлый рандом.

Библиотека будет обновляться периодически.
`
ОЖИДАНИЕ РЕКЛАМЫ...