• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] Bit Array

Level 7
Joined
Apr 30, 2011
Messages
359
JASS:
//========================================================================================
//      
//      Bit Array
//      -*- overcold_ice -*-
//      
//     -[*] Requirements:
//          - JNGP
//          - latest version of JassHelper
//          - RealInt
//              [url]http://www.hiveworkshop.com/forums/submissions-414/snippet-real-integer-214307/[/url]
//      
//          A Bit Array snippet used to store booleans effectively
//      
//     -[*] API:
//      
//      struct BitArray extends array
//          readonly string bit
//      
//          static method bit2Int takes string Bit returns RealInt
//          static method int2Bit takes RealInt RealInt returns string
//              converter between Bit and RealInt
//      
//          static method create takes nothing returns BitArray
//          method destroy takes nothing returns nothing
//              allocator and deallocator for the struct
//      
//          method addBit takes integer Key, string Bit returns nothing
//          method addBool takes integer Key, boolean Bool returns nothing
//              inserts a series of bits after 'Key'
//              doing this will shift the keys behind 'Key'
//              if 'Key' == 1, then the bits will get inserted at key 2, 3, 4, . . .
//      
//          method setBit takes integer Key, string Bit returns nothing
//          method setBool takes integer Key, boolean Bool returns nothing
//              replaces a series of bits with another bits after 'Key'
//              if 'Key' == 1, then the bits will get replaced at key 2, 3, 4, . . .
//      
//          method clear takes integer KeyStart, integer KeyEnd returns nothing
//              resets a series of bits to 'false' from 'KeyStart + 1' to 'KeyEnd'
//          method remove takes integer KeyStart, integer KeyEnd returns nothing
//              removes a series of bits from 'KeyStart + 1' to 'KeyEnd'
//              doing this will shift keys behind 'KeyEnd'
//      
//          method getValue takes integer Key returns integer
//              returns the integer value of 'Key'
//              0 -> false, 1 -> true
//          method getBool takes integer Key returns boolean
//              returns the boolean value of 'Key'
//      
//          method operator int takes nothing returns RealInt
//              returns the integer value of current bits as RealInt
//          method operator notInt takes nothing returns RealInt
//              returns the not integer value of current bits
//              this will shift all false -> true and true -> false
//              as a new RealInt, without modifying the struct's int
//          method operator notBit takes nothing returns string
//              returns the not bit value of current bits
//              this will shift all false -> true and true -> false
//              as a new string, without modifying the struct's bits
//      
//          method andBit takes string Bit returns string
//          method andInt takes RealInt RealInt returns RealInt
//          method orBit takes string Bit returns string
//          method orInt takes RealInt RealInt returns RealInt
//          method xorBit takes string Bit returns string
//          method xorInt takes RealInt RealInt returns RealInt
//              compares each keys of both bits
//              then set the resulting bit to true if it mets these requirements:
//              and => if both bits are 'true'
//              or  => if either bits or both bits are 'true'
//              xor => if either bits are 'true' but not both
//      
//========================================================================================
library BitArray requires RealInt
    
    globals
        private constant real REAL_INT = 1000000000
    endglobals
    
    private function Max takes integer i1, integer i2 returns integer
        if i1 > i2 then
            return i1
        endif
        
        return i2
    endfunction
    
    private function Mod2 takes real ri returns real
        return ri - (ri / 2) * 2
    endfunction
    private function Pow2 takes integer i returns real
        local real r = 1 / REAL_INT
        
        loop
            exitwhen i == 0
            set i = i - 1
            
            set r = r * 2
        endloop
        
        return r
    endfunction
    
    private function GetVal takes string bit, integer i returns integer
        return S2I(SubString(bit, StringLength(bit) - i - 1, StringLength(bit) - i))
    endfunction
    private function Int2Bit takes real ri returns string
        local real    i = ri
        local string  b = ""
        local RealInt r = RealInt.create()
        
        if ri < 0 then
            set i = -i
        endif
        
        loop
            exitwhen i == 0
            
            set r.realInt = Mod2(i)
            set b = r.toStr() + b
            
            set i = i / 2
        endloop
        
        if ri < 0 then
            set i = -ri
            set i = Pow2(StringLength(b)) - i
            set b = ""
            
            loop
                exitwhen i == 0
                
                set r.realInt = Mod2(i)
                set b = r.toStr() + b
                
                set i = i / 2
            endloop
        endif
        
        return b
    endfunction
    private function Bit2Int takes string bit returns real
        local integer l  = StringLength(bit)
        local real    ri = 0
        
        loop
            exitwhen l == 0
            
            set ri = ri + GetVal(bit, l) * Pow2(l - 1)
            
            set l = l - 1
        endloop
        
        return ri
    endfunction
    
    private function B2I takes boolean b returns integer
        if b then
            return 1
        endif
        
        return 0
    endfunction
    private function I2B takes integer i returns boolean
        if i != 0 then
            return true
        endif
        
        return false
    endfunction
    
    struct BitArray extends array
        private  real   pint
        readonly string bit
        
        private static integer c
        private        integer r
        
        static method bit2Int takes string bit returns RealInt
            return RealInt [Bit2Int(bit)]
        endmethod
        static method int2Bit takes RealInt int returns string
            return Int2Bit(int.integer)
        endmethod
        
        static method create takes nothing returns thistype
            local thistype this = thistype(0).r
            
            if this == 0 then
                set .c   = .c + 1
                set this = .c
            else
                set thistype(0).r = this.r
            endif
            
            return this
        endmethod
        
        method addBit takes integer a, string bit returns nothing
            set this.bit  = SubString(this.bit, 0, a) + bit + SubString(this.bit, a, StringLength(this.bit))
            set this.pint = Bit2Int(this.bit)
        endmethod
        method addBool takes integer a, boolean bool returns nothing
            call this.addBit(a, Int2Bit(B2I(bool)))
        endmethod
        
        method setBit takes integer a, string bit returns nothing
            set this.bit  = SubString(this.bit, 0, a) + bit + SubString(this.bit, a + StringLength(bit), StringLength(this.bit))
            set this.pint = Bit2Int(this.bit)
        endmethod
        method setBool takes integer a, boolean bool returns nothing
            call this.setBit(a, Int2Bit(B2I(bool)))
        endmethod
        
        method clear takes integer a, integer e returns nothing
            local integer l = a
            local string  b = ""
            
            if a > e then
                return
            endif
            
            loop
                set l = l + 1
                
                set b = b + "0"
                
                exitwhen l == e
            endloop
            
            call this.setBit(a, b)
        endmethod
        
        method remove takes integer a, integer e returns nothing
            set this.bit  = SubString(this.bit, 0, a) + SubString(this.bit, e, StringLength(this.bit))
            set this.pint = Bit2Int(this.bit)
        endmethod
        
        method getValue takes integer a returns integer
            return GetVal(this.bit, a)
        endmethod
        method getBool takes integer a returns boolean
            return I2B(this.getValue(a))
        endmethod
        
        method operator int takes nothing returns RealInt
            return RealInt [this.pint]
        endmethod
        method operator notInt takes nothing returns RealInt
            return RealInt [-this.pint - 1]
        endmethod
        method operator notBit takes nothing returns string
            return Int2Bit(-this.pint - 1)
        endmethod
        
        //! textmacro BitArray___Logic takes NAME, CHECK
            method $NAME$Bit takes string bit returns string
                local integer l  = 0
                local integer e  = Max(StringLength(this.bit), StringLength(bit))
                local boolean b1
                local boolean b2
                local string  b  = ""
                
                loop
                    set l = l + 1
                    
                    set b1 = I2B(GetVal(this.bit, l))
                    set b2 = I2B(GetVal(bit, l))
                    
                    if $CHECK$ then
                        set b = "1" + b
                    else
                        set b = "0" + b
                    endif
                    
                    exitwhen l == e
                endloop
                
                return b
            endmethod
            method $NAME$Int takes RealInt ri returns RealInt
                return RealInt [Bit2Int(this.$NAME$Bit(Int2Bit(ri.integer)))]
            endmethod
        //! endtextmacro
        
        //! runtextmacro BitArray___Logic("and", "b1 and b2")
        //! runtextmacro BitArray___Logic( "or", "b1  or b2")
        //! runtextmacro BitArray___Logic("xor", "b1  != b2")
        
        method destroy takes nothing returns nothing
            set this.pint = 0
            set this.bit  = ""
            
            set this.r        = thistype(0).r
            set thistype(0).r = this
        endmethod
    endstruct
endlibrary
 
Last edited:
Level 7
Joined
Apr 30, 2011
Messages
359
@mag: stop complaining !! haha
@bribe: what do you mean ?_?

edit: i got it . . .
edit: this system still needs many fix xD
 
Last edited:
Level 17
Joined
Apr 27, 2008
Messages
2,455
Just to clear, when i said "make a map", it's "make a playable map instead of all these useless resources".
I know you enjoy make them, but don't follow Nestharus, you should submit something only if you have already used it, not for the fun.
And at very least you should have test it before submit.
I'm not trying to be a jerk, but really you're just giving more work to Bribe.
 

BBQ

BBQ

Level 4
Joined
Jun 7, 2011
Messages
97
Bit shifting is really easy with JASS.

JASS:
//Shift 42 onto 1069 resulting with 106942
local integer result = S2I(I2S(1069) + I2S(42))
Sorry, but what are you talking about?

Last time I checked, 1069 << 42 = 4701511720370176

By the way, if you ever need bitwise operations in something as silly as WC3, check this out.
 
Level 7
Joined
Apr 30, 2011
Messages
359
he means that . . .
'in' the array to shift just do that, but its integer value will result really differently

isn't that thing too complicated . . . ?
i'll stick using my method to compare each digits . .
 
Top