lib/setfu.rb in setfu-1.2.1 vs lib/setfu.rb in setfu-2.0.0

- old
+ new

@@ -1,106 +1,164 @@ require "setfu/version" -class Set +class BitSet attr_reader :entropy def entropy=(ent) # entropy only gets bigger, just like the universe! @entropy = ent unless @entropy > ent end def self.uppercase_chars - set = Set.new + set = BitSet.new set.set_bits!(2475880041677272402379145216) set.entropy=91 return set end def self.lowercase_chars - set = Set.new + set = BitSet.new set.set_bits!(10633823807823001954701781295154855936) set.entropy=123 return set end def self.letter_chars - set = Set.new + set = BitSet.new set.set_bits!(10633823810298881996379053697534001152) set.entropy=123 return set end def self.digit_chars - set = Set.new + set = BitSet.new set.set_bits!(287948901175001088) set.entropy=58 return set end def self.parse_chars - set = Set.new + set = BitSet.new set.set_bits!(159507359650170349735020301117175103487) set.entropy=127 return set end def replace(ent) - ent = ent.to_set + ent = ent.to_bset @mode = ent.mode @entropy = ent.entropy @bits = ent.to_i self end def add_parse_chars! # add! [0..47, 58..64, 91..96, 123..126] - add! Set.parse_chars + add! BitSet.parse_chars self end def add_parse_chars - return Set.parse_chars | self + return BitSet.parse_chars | self end def add_digit_chars! - add! Set.digit_chars + add! BitSet.digit_chars self end def add_digit_chars - return Set.digit_chars | self + return BitSet.digit_chars | self end def add_uppercase_chars! - add! Set.uppercase_chars + add! BitSet.uppercase_chars self end def add_uppercase_chars - return Set.uppercase_chars | self + return BitSet.uppercase_chars | self end def add_lowercase_chars! - add! Set.lowercase_chars + add! BitSet.lowercase_chars self end def add_lowercase_chars - return Set.lowercase_chars | self + return BitSet.lowercase_chars | self end def add_letter_chars - return Set.letter_chars | self + return BitSet.letter_chars | self end def add_letter_chars! - add! Set.letter_chars + add! BitSet.letter_chars self end + # 01100010011111111100010101001101 ... test + # 01100010011111111100010101001101 ... left shift + # 001000000011111111000000000001000 ... and with left shift + # + # 01100010011111111100010101001101 ... test + # 00110001001111111110001010100110 ... right shift + # 00100000001111111100000000000100 ... and with right shift + # + # 001000000011111111000000000001000 ... AND with left shift + # 00100000001111111100000000000100 ... AND with right shift + # 001100000011111111100000000001100 ... OR the two ands + # 01100010011111111100010101001101 ... test original + # 01100000011111111100000000001100 ... BINGO ... original with the OR gives us the runs only + # 00000010000000000000010101000001 ... BINGO ... original XOR with OR gives us the singles! + + def runs # runs << creates a set of touching elements + an_l = (@bits << 1) & @bits + an_r = (@bits >> 1) & @bits + or_lr = an_l | an_r + rtn = BitSet.new + rtn.set_bits!(or_lr & @bits) + rtn.entropy=self.entropy + return rtn + end + + def singles # singles << creates a set of non-touching elements + an_l = (@bits << 1) & @bits + an_r = (@bits >> 1) & @bits + or_lr = an_l | an_r + rtn = BitSet.new + rtn.set_bits!(or_lr ^ @bits) + rtn.entropy=self.entropy + return rtn + end + + def max_run # max_run << returns maximum number of consecutive numbers + return 0 if @bits==0 + mx = 1 + bits = @bits + loop do + bits <<= 1 + bits &= @bits + break if 0 == bits + mx += 1 + end + return mx + end + + def runs?(cnt=2) + bits = @bits + (cnt-1).times do + bits <<= 1 + bits &= @bits + return false if 0 == bits + end + return true + end + def add_opposing_case! - aa = Set.lowercase_chars - bb = Set.uppercase_chars + aa = BitSet.lowercase_chars + bb = BitSet.uppercase_chars ka = aa & self # subset lower case kb = bb & self # subset upper case @bits |= ka.to_i >> 32 @bits |= kb.to_i << 32 self.entropy = 32 + kb.recalculate_entropy! if self.entropy <= 123 @@ -109,31 +167,50 @@ def add_opposing_case dup.add_opposing_case! end - def to_set + def to_bset return self end def mode return @mode end - def initialize(data=nil) + def initialize(*data) @bits = 0 @entropy = 0 - add!(data) unless data.nil? + add!(data) self end def zap! @bits = 0 @entropy = 0 self end + def inc! + @entropy += 1 + @bits <<= 1 + self + end + + def dec! + @bits >>= 1 + self + end + + def inc + return dup.inc! + end + + def dec + return dup.dec! + end + def recalculate_entropy! @entropy = 0 bits = @bits num = 1 << 8192 while(bits > num) @@ -163,88 +240,88 @@ #puts "TESTING ... coerce called!" return [self, other] # does not seem to get called ... end def dup - rtn = Set.new + rtn = BitSet.new rtn.replace(self) return rtn end def ^(item) rtn = self.dup - if(item.class==Set) + if(item.class==BitSet) rtn.set_bits!(rtn.to_i ^ item.to_i) else - rtn = Set.new(item) + rtn = BitSet.new(item) rtn.set_bits!(@bits ^ rtn.to_i) end rtn end def |(item) rtn = self.dup - if(item.class==Set) + if(item.class==BitSet) rtn.set_bits!(rtn.to_i | item.to_i) self.entropy=item.entropy else - rtn = Set.new(item) + rtn = BitSet.new(item) rtn.set_bits!(@bits | rtn.to_i) end rtn end def &(item) rtn = self.dup - if(item.class==Set) + if(item.class==BitSet) rtn.set_bits!(rtn.to_i & item.to_i) else - rtn = Set.new(item) + rtn = BitSet.new(item) rtn.set_bits!(@bits & rtn.to_i) end rtn end def -(item) - rtn = Set.new + rtn = BitSet.new rtn.entropy = self.entropy a = self.to_i - if(item.class==Set) + if(item.class==BitSet) b = item.to_i rtn.entropy = item.entropy else - b = Set.new(item) + b = BitSet.new(item) rtn.entropy = b.entropy b = b.to_i end c = a & b d = c ^ a rtn.set_bits!(d) rtn end def **(item) # intersection test - set_item = Set.new(item) + set_item = BitSet.new(item) return false if (self & set_item).empty? return true end # comparison operators: def ==(item) - if(item.class==Set) + if(item.class==BitSet) rtn = item.to_i == self.to_i else - rtn = Set.new(item).to_i == self.to_i + rtn = BitSet.new(item).to_i == self.to_i end rtn end def !=(item) - if(item.class==Set) + if(item.class==BitSet) rtn = item.to_i != self.to_i else - rtn = Set.new(item).to_i != self.to_i + rtn = BitSet.new(item).to_i != self.to_i end rtn end def set_case(mode=:mode_equal) @@ -255,11 +332,11 @@ def ===(item) # self ... when clause ... # item ... case clause ... case(item) # Note: coerce does not work in this context ... - md = item.to_set.mode || @mode + md = item.to_bset.mode || @mode case md when :mode_intersection return item ** self when :mode_sub @@ -274,22 +351,22 @@ return self == item end end def <=(item) - si = Set.new item + si = BitSet.new item return si.include?(self) end def <(item) - si = Set.new item + si = BitSet.new item return false if (si == self) # not a 'proper' subset return si.include?(self) end def add!(items) - if(items.class==Set) + if(items.class==BitSet) @bits |= items.to_i entropy = items.entropy elsif(items.class==Range) f=items.first.ord l=items.last.ord @@ -323,12 +400,13 @@ def empty? @bits == 0 end def include?(items) + return false if items.nil? # sets never include nil return false if @bits == 0 # empty sets include nothing including other empty sets - if(items.class==Set) + if(items.class==BitSet) tmp = @bits & items.to_i return false if tmp==0 return (tmp) == items.to_i elsif(items.class==Range) f=items.first.ord @@ -456,21 +534,21 @@ idx = pset.first.ord end end end unless idx.nil? - raise "Negative indexes are illegal for Set" if idx < 0 + raise "Negative indexes are illegal for BitSet" if idx < 0 self.entropy = idx+1 y = @bits & (1<<idx) return true if y > 0 return false end - return pset.to_set & self + return pset.to_bset & self end def []=(*pset,value) # pset goes in the box, value after '=' - pset = pset.to_set + pset = pset.to_bset state = value ? true : false if state replace pset | self # add elements to set else replace self - pset # remove elements from set @@ -527,84 +605,84 @@ ary.each do |elm| rtn.push elm.chr(Encoding::UTF_8) end return rtn.join "" else # :set - return ary.to_set + return ary.to_bset end end -end # end Set +end # end BitSet module SetFuMixinBinaryAndOperator def &(item) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a & b end end module SetFuMixinBinaryOrOperator def |(item) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a | b end end module SetFuMixinBinaryXorOperator def ^(item) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a ^ b end end module SetFuMixinBinaryIntersectionOperator def **(item) # intersection test - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a ** b end end module SetFuMixinToSetMethod - def to_set - rtn = Set.new(self) + def to_bset + rtn = BitSet.new(self) # byebug return rtn end end module SetFuMixinTrippleEqualsOperator alias_method :old_triple_equal4Set, :=== def ===(item) - return old_triple_equal4Set(item) unless (item.class==Set) - return self.to_set === item + return old_triple_equal4Set(item) unless (item.class==BitSet) + return self.to_bset === item end end module SetFuMixinBinarySubtractOperator def -(item) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a - b end end module SetFuMixinBinarySubsetOperator def <=(item) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a <= b end end module SetFuMixinBinaryProperOperator def <(item) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a < b end end class String @@ -619,18 +697,18 @@ include SetFuMixinTrippleEqualsOperator include SetFuMixinBinarySubtractOperator def <=(item) return old_string_lte4set(item) if (item.class==String) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a <= b end def <(item) return old_string_lt4set(item) if (item.class==String) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a < b end end class Array @@ -645,23 +723,23 @@ include SetFuMixinBinarySubsetOperator include SetFuMixinBinaryProperOperator def -(item) return old_subtract_method4set(item) if (item.class==Array) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a - b end def &(item) return old_and_method4set(item) if (item.class==Array) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a & b end def |(item) return old_or_method4set(item) if (item.class==Array) - a = Set.new(self) - b = Set.new(item) + a = BitSet.new(self) + b = BitSet.new(item) return a | b end end class Range