class SquareGraph

Attributes

length[R]
width[R]

Public Class Methods

create_truthy(objClass, &truthyMethod) click to toggle source
# File lib/square_graph.rb, line 129
def self.create_truthy (objClass, &truthyMethod)
  return nil if objClass.method_defined? :truthy?
  objClass.send :define_method, :truthy?, truthyMethod
end
create_truthy!(objClass, &truthyMethod) click to toggle source
# File lib/square_graph.rb, line 134
def self.create_truthy! (objClass, &truthyMethod)
  objClass.send :define_method, :truthy?, truthyMethod
end
new(*dimen) click to toggle source
# File lib/square_graph.rb, line 5
def initialize(*dimen)
  raise ArgumentError if ![0,2].include?(dimen.size)
  if dimen.size == 2
    test_fixnum(dimen[0], dimen[1])
    raise ArgumentError if test_range([1, dimen[0]], [1, dimen[1]])
    @length, @width = dimen[0], dimen[1]
    @sized = true
  else
    @sized = false
  end
  @sg = Hash.new
end

Public Instance Methods

==(sg2) click to toggle source
# File lib/square_graph.rb, line 151
def == sg2
  sg2.each_pos do |p|
    return false if self.get(p[:x], p[:y]) != sg2.get(p[:x], p[:y])
  end
  true
end
anytrue?() click to toggle source
# File lib/square_graph.rb, line 60
def anytrue?
  !(@sg.any? do |f|
    @sg[f[0]] and @sg[f[0]].object
  end)
end
clear() click to toggle source
# File lib/square_graph.rb, line 52
def clear
  @sg.clear
end
each() { |f| ... } click to toggle source
# File lib/square_graph.rb, line 66
def each
  return nil if @sg.empty?
  @sg.each_pair do |p, f|
    yield(f)
  end if block_given?
end
each_obj() { |object| ... } click to toggle source
# File lib/square_graph.rb, line 73
def each_obj
  return nil if @sg.empty?
  @sg.each_pair do |f, o|
    yield(o.object)
  end if block_given?
  @sg.each_pair if not block_given?
end
each_pos() { |p| ... } click to toggle source
# File lib/square_graph.rb, line 81
def each_pos
  return nil if @sg.empty?
  pos = Array.new
  (@length||@sg.keys.max(&comp('x'))[0]).downto(@sized?1:@sg.keys.min(&comp('x'))[0]).each do |l|
    (@width||@sg.keys.max(&comp('y'))[1]).downto(@sized?1:@sg.keys.min(&comp('y'))[1]).each do |w|
      p = Hash.new
      p[:x] = l
      p[:y] = w
      pos.push(p)
    end
  end
  pos.each do |p|
    yield(p)
  end if block_given?
  pos.each if not block_given?
end
empty?() click to toggle source
# File lib/square_graph.rb, line 56
def empty?
  @sg.none?
end
falsey?() click to toggle source
# File lib/square_graph.rb, line 146
def falsey?
  return false if @sg.size == 0
  self.truthy.nil?
end
fill(x, y, o = true) click to toggle source
# File lib/square_graph.rb, line 28
def fill(x, y, o = true)
  test_fixnum(x, y)
  if not o
    self.remove(x, y) if @sg[[x, y]]
    return nil
  end
  if @sized == true
    return nil if test_range([1, x], [1, y], [x, @length], [y, @length])
  end
  return nil if @sg[[x,y]]
  @sg[[x,y]] = Face.new(x, y, o)
end
get(x, y) click to toggle source
# File lib/square_graph.rb, line 41
def get(x, y)
  return nil if !@sg[[x,y]]
  @sg[[x,y]].object
end
need_truthy() click to toggle source
# File lib/square_graph.rb, line 120
def need_truthy
  result = Array.new
  @sg.each_pair do |p, f|
    result.push(f.object.class) if not f.object.class.method_defined? :truthy?
  end
  return nil if result.empty?
  result
end
objects() click to toggle source
# File lib/square_graph.rb, line 98
def objects
  rt = Array.new
  @sg.values.each do |f|
    rt.push (f.object)
  end
  return nil if rt.size == 0
  rt
end
remove(*args) click to toggle source
# File lib/square_graph.rb, line 46
def remove(*args)
  x, y = args[0], args[1]
  test_fixnum(x, y)
  @sg.delete([x, y])
end
resize(length, width) click to toggle source
# File lib/square_graph.rb, line 18
def resize(length, width)
  raise NoMethodError if @sized == false
  test_fixnum(length, width)
  return nil if test_range([1, length], [1, width])
  if !@sg.empty?
    return nil if test_range([@sg.keys.max_by {|x| x[0]} [0], length], [@sg.keys.max_by {|y| y[1]} [1], width])
  end
  @length, @width = length, width
end
truthy() click to toggle source
# File lib/square_graph.rb, line 107
def truthy
  if @sized
    result = SquareGraph.new(@length, @width)
  else
    result = SquareGraph.new
  end
  @sg.each_pair do  |p, f|
    result.fill(f.x, f.y, f.object) if f.truthy?
  end
  return nil if result.objects.nil?
  result
end
truthy?(&alt_cond) click to toggle source
# File lib/square_graph.rb, line 138
def truthy? (&alt_cond)
  return false if @sg.size == 0
  @sg.each_pair do |p, f|
    return false if not f.truthy?(&alt_cond)
  end
  true
end

Private Instance Methods

comp(pos) click to toggle source
# File lib/square_graph.rb, line 170
def comp(pos)
  Proc.new do |x,y|
    if pos == 'x'
      x[0] <=> y[0]
    else
      x[1] <=> y[1]
    end
  end
end
test_fixnum(*value) click to toggle source
# File lib/square_graph.rb, line 160
def test_fixnum(*value)
  value.each do |v|
    raise ArgumentError if v.class != Fixnum
  end
end
test_range(*value) click to toggle source
# File lib/square_graph.rb, line 166
def test_range(*value)
  value.any? {|pair| pair[0] > pair[1]}
end