lib/setfu.rb in setfu-3.0.1 vs lib/setfu.rb in setfu-3.1.0

- old
+ new

@@ -13,9 +13,191 @@ better_install_as!(:tag, :setfu_tag) better_install_as!(:tag, :setfu_count) better_install_as!(:delete_many_at, :setfu_delete_many_at) end +### +### Version 3.2.0 additions +### + +# ::TODO:: add document +class Array + def stack_swap + a = pop + b = pop + push a + push b + end + def stack_rot + a = pop + b = pop + c = pop + push a + push c + push b + end + def pop2 + a = pop + b = pop + return [a,b] + end + def peek(n=0) + return self[count-n-1] + end +end + +# ::TODO:: add document +class Array # additions to be used by other future gems: + def ascending? + return false unless count >=2 + cnt = self.length - 1 + cnt.times do |ii| + return false unless self[ii+1] >= self[ii] + end + return false if first==last # ascending means something has to get bigger + true + end + def descending? + return false unless count >=2 + cnt = self.length - 1 + cnt.times do |ii| + return false unless self[ii+1] <= self[ii] + end + return false if first==last # descending means something has to get smaller + true + end + def sorted? + return false unless count >=1 + return true if (self & self).count == 1 # [4,4,4,4] is sorted, but not ascending or desending + return true if ascending? + return true if descending? + false + end +end + +# ::TODO:: add document +class BitSet + def swap!(pos1,pos2) + self[pos1],self[pos2] = self[pos2], self[pos1] + self + end + def swap(pos1,pos2) + bs = dup + bs[pos1],bs[pos2] = self[pos2], self[pos1] + bs + end +end + +# ::TODO:: add document +# this routine takes a linear binary table and finds a coordinate neighbor. +# not to be confused with finding a neighbor in a Logic Map. +# of a given map +class BitSet + def neighbor(nvars, dir) + raise "too many neighbors for #{nvars} variables" if dir >= nvars + raise "dir must be positive" if dir.negative? + rtn = dup + + if 1==nvars # two squares + rtn[dir]=!self[dir] + elsif 2==nvars # a 2x2 box + if 0==dir + rtn[0]=!self[0] + else + rtn[1]=!self[1] + end + end + # now programmatically do the remaining pattern + if nvars.even? + if dir.even? # up if dir==0 and nvars==4 ... left if dir==2 and nvars==4 + rtn[dir]=self[dir+1] + rtn[dir+1]=!self[dir] + else # down if dir ==1 and nvars==4 ... right if dir==3 and nvars==4 + rtn[dir-1]=!self[dir] # modified on dec 21, 2019 9:34AM + rtn[dir]=self[dir-1] + end + else # odd .......... compare with neighbor5 dir [0,1,2,3,4] + if dir==(nvars-1) # dir==4 + rtn[dir]=!self[dir] + elsif dir.even? # 0,2 validated + rtn[dir]=self[dir+1] + rtn[dir+1]=!self[dir] + else # 1,3 validated + rtn[dir-1]=!self[dir] + rtn[dir]=self[dir-1] + end + end + return rtn + end +end + +# this one will get added to setfu soon. +class BitSet + def self.generate_pattern(nbits, flag_zero_first, zeros, ones ) + bs = BitSet.new + bs.entropy = nbits + return bs if ones<=0 + return bs | [0..(nbits-1)] if zeros<=0 + k=0 + hash = {:zero => zeros, :one => ones} + if flag_zero_first + ptr = :zero + val = false + else + ptr = :one + val = true + end + nbits.times do |ii| + if (k >= hash[ptr]) + k = 1 + val = !val + ptr = (ptr == :zero) ? :one : :zero + else + k += 1 + end + bs[ii] = val + end + return bs + end +end + +# add this one to setfu ... also create non bang version +class BitSet + def double! + @bits |= @bits << @entropy + @entropy <<= 1 + self + end + def double + dup.double! + end +end + +# this should be added to gem ... +class BitSet + def self.int(int,ent=nil) + bs = BitSet.new + bs.set_bits!(int) + if ent.nil? + bs.recalculate_entropy! + else + bs.entropy=ent + end + bs + end +end + +class BitSet + def set_all! # set all possible bits ... make sure this does not already exist ... if it does, then use the proper name + bits = (2 ** entropy) - 1 + set_bits! bits + end +end + +### +### Version 3.1.0 and older +### + module Setfu def self.bset_elements(ary) ary.count.times do |ii| ary[ii] = ary[ii].to_bset end