Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.10 KB | None | 0 0
  1. class Junction
  2.   attr_accessor :value, :coelements
  3.   attr_reader :type
  4.   ANY = "|";ALL = "&";ONE = "@";NON = "~";NIL = "?"
  5.   def initialize(num_of_values,typeof,array=nil)
  6.     if block_given?
  7.       index = 0;@value = Array.new(num_of_values){|value|
  8.           yield value, index;index+=1}
  9.     elsif array;@value = array
  10.     else;@value = Array.new(num_of_values)
  11.     end
  12.     @coelements = @value.size.to_i
  13.     @type = ((typeof==ANY or typeof==ALL)?((typeof==ANY)?ANY: ALL):(NIL))
  14.     @type = ((typeof==ONE or typeof==NON)?((typeof==ONE)?ONE: NON):(@type))
  15.     return self
  16.   end
  17.   def any(num_of_values,array=nil)
  18.     a = Junction.new(num_of_values,Junction::ANY,array)
  19.     if block_given?
  20.       index = 0;a.value = Array.new(num_of_values){|value|
  21.           yield value, index;index+=1}
  22.     elsif array;a.value = array
  23.     else;a.value = Array.new(num_of_values)
  24.     end
  25.     a.coelements = a.value.size.to_i
  26.     return a
  27.   end
  28.   def all(num_of_values,array=nil)
  29.     a = Junction.new(num_of_values,Junction::ALL,array)
  30.     if block_given?
  31.       index = 0;a.value = Array.new(num_of_values){|value|
  32.           yield value, index;index+=1}
  33.     elsif array;a.value = array
  34.     else;a.value = Array.new(num_of_values)
  35.     end
  36.     a.coelements = a.value.size.to_i
  37.     return a
  38.   end
  39.   def one(num_of_values,array=nil)
  40.     a = Junction.new(num_of_values,Junction::ONE,array)
  41.     if block_given?
  42.       index = 0;a.value = Array.new(num_of_values){|value|
  43.           yield value, index;index+=1}
  44.     elsif array;a.value = array
  45.     else;a.value = Array.new(num_of_values)
  46.     end
  47.     a.coelements = a.value.size.to_i
  48.     return a
  49.   end
  50.   def non(num_of_values,array=nil)
  51.     a = Junction.new(num_of_values,Junction::NON,array)
  52.     if block_given?
  53.       index = 0;a.value = Array.new(num_of_values){|value|
  54.           yield value, index;index+=1}
  55.     elsif array;a.value = array
  56.     else;a.value = Array.new(num_of_values)
  57.     end
  58.     a.coelements = a.value.size.to_i
  59.     return a
  60.   end
  61.   def forall
  62.     if block_given?
  63.       index = 0;while index < coelements
  64.         yield @value.at(index), index;index+=1
  65.       end
  66.     else;raise "No block given to \'forall\' method"
  67.     end
  68.   end
  69.   def ==(value)
  70.     case @type
  71.     when ANY
  72.       return @value.include?(value)
  73.     when ALL
  74.       occurances=1;@value.each {|index|
  75.         occurances+=((index==value)?1:0)
  76.       }
  77.       return occurances==@coelements
  78.     when ONE
  79.       occurances=0;@value.each {|index|
  80.         occurances+=((index==value)?1:0)
  81.       }
  82.       return occurances==1
  83.     when NON
  84.       return (@value.include?(value))?false:true
  85.     when NIL;return nil
  86.     end
  87.   end
  88.   def !=(value)
  89.     case @type
  90.     when ANY
  91.       return @value.include?(value)?false:true
  92.     when ALL
  93.       occurances=1;@value.each {|index|
  94.         occurances+=((index!=value)?1:0)
  95.       }
  96.       return occurances==@coelements
  97.     when ONE
  98.       occurances=0;@value.each {|index|
  99.         occurances+=((index!=value)?1:0)
  100.       }
  101.       return occurances==1
  102.     when NON
  103.       return (@value.include?(value))
  104.     when NIL;return nil
  105.     end
  106.   end
  107.   def <(value)
  108.     case @type
  109.     when ANY
  110.       return (@value.include?(value))?(@value.max < value):(nil)
  111.     when ALL
  112.       occurances=1;@value.each {|index|
  113.         occurances+=((index < value)?1:0)
  114.       }
  115.       return occurances==@coelements
  116.     when ONE
  117.       occurances=0;@value.each {|index|
  118.         occurances+=((index < value)?1:0)
  119.       }
  120.       return occurances==1
  121.     when NON
  122.       return (@value.include?(value))?((@value.max < value)?false:true):(nil)
  123.     when NIL;return nil
  124.     end
  125.   end
  126.   def >(value)
  127.     case @type
  128.     when ANY
  129.       return (@value.include?(value))?(@value.max > value):(nil)
  130.     when ALL
  131.       occurances=1;@value.each {|index|
  132.         occurances+=((index > value)?1:0)
  133.       }
  134.       return occurances==@coelements
  135.     when ONE
  136.       occurances=0;@value.each {|index|
  137.         occurances+=((index > value)?1:0)
  138.       }
  139.       return occurances==1
  140.     when NON
  141.       return (@value.include?(value))?((@value.max > value)?false:true):(nil)
  142.     when NIL;return nil
  143.     end
  144.   end
  145.   def <=(value)
  146.     case @type
  147.     when ANY
  148.       return (@value.include?(value))?(@value.max <= value):(nil)
  149.     when ALL
  150.       occurances=1;@value.each {|index|
  151.         occurances+=((index <= value)?1:0)
  152.       }
  153.       return occurances==@coelements
  154.     when ONE
  155.       occurances=0;@value.each {|index|
  156.         occurances+=((index <= value)?1:0)
  157.       }
  158.       return occurances==1
  159.     when NON
  160.       return (@value.include?(value))?((@value.max <= value)?false:true):(nil)
  161.     when NIL;return nil
  162.     end
  163.   end
  164.   def >=(value)
  165.     case @type
  166.     when ANY
  167.       return (@value.include?(value))?(@value.max >= value):(nil)
  168.     when ALL
  169.       occurances=1;@value.each {|index|
  170.         occurances+=((index >= value)?1:0)
  171.       }
  172.       return occurances==@coelements
  173.     when ONE
  174.       occurances=0;@value.each {|index|
  175.         occurances+=((index >= value)?1:0)
  176.         return occurances==1
  177.       }
  178.     when NON
  179.       return (@value.include?(value))?((@value.max >= value)?false:true):(nil)
  180.     when NIL;return nil
  181.     end
  182.   end
  183.   def ===(value)
  184.     case @type
  185.     when ANY
  186.       return (@value.include?(value) and @value.at(@value.index(value))===value)
  187.     when ALL
  188.       occurances=1;@value.each {|index|
  189.         occurances+=((index===value)?1:0)
  190.       }
  191.       return occurances==@coelements
  192.     when ONE
  193.       occurances=0;@value.each {|index|
  194.         occurances+=((index===value)?1:0)
  195.       }
  196.       return occurances==1
  197.     when NON
  198.       return (@value.include?(value) and
  199.           @value.at(@value.index(value))===value)?false:true
  200.     when NIL;return nil
  201.     end
  202.   end
  203.   def to_a#override
  204.     return @value.to_a
  205.   end
  206.   def to_s#override
  207.     character=NIL;case @type
  208.     when ANY
  209.       character=ANY
  210.     when ALL
  211.       character=ALL
  212.     when ONE
  213.       character=ONE
  214.     when NON
  215.       character=NON
  216.     end
  217.     return @value.to_s.gsub(", ",character)
  218.   end
  219.   def to_i
  220.     @value.each {|i|(i = (i.to_i)) rescue i = 0}.to_a;return self
  221.   end
  222.   def to_f
  223.     @value.each {|i|(i = (i.to_f)) rescue i = 0.0}.to_a;return self
  224.   end
  225.   def <<(value)
  226.     @value.push(value);return self
  227.   end
  228.   def push_to_last!(value)
  229.     @value.push(value);return self
  230.   end
  231.   def pop_last!
  232.     return @value.pop
  233.   end
  234.   def pop_last_array!
  235.     @value.pop;return self
  236.   end
  237.   def type_change(new_type)
  238.     case new_type
  239.     when ANY
  240.       @type=ANY
  241.     when ALL
  242.       @type=ALL
  243.     when ONE
  244.       @type=ONE
  245.     when NON
  246.       @type=NON
  247.     else
  248.       @type=NIL
  249.     end
  250.   end
  251.   def elements?
  252.     @coelements.to_i
  253.   end
  254.   def clean!
  255.     @value.uniq!;return self
  256.   end
  257.   def pretty_puts #not needed, but cool for easy reading :3
  258.     self.forall {|i,j|puts "#{(j+1).to_s}" <<
  259.         format_number_th(j) << " possible value is #{i.to_s}" }
  260.   end
  261.   private#helpers
  262.   def format_number_th(j)#helper for pretty_puts
  263.     if [10,11,12].include?(j%100);return "th"
  264.     else;return (["st","nd","rd","th"].at((j%10>=4)?3:j%10)).to_s
  265.     end
  266.   end
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement