Module: Moon::Tabular

Included in:
MatrixLike::Partition, MatrixLike::TableAdaptor, Table, Partition
Defined in:
lib/moon/packages/data_bags/tabular/iterator_base.rb,
lib/moon/packages/data_bags/tabular/cursor.rb,
lib/moon/packages/data_bags/tabular/partition.rb

Overview

Tabular objects are expected to respond to:

xsize -> Integer
ysize -> Integer
[](x, y) -> Integer
[]=(x, y, value)

As well as implementing the methods required by IteratorBase

Defined Under Namespace

Classes: Cursor, IteratorBase, Partition

Instance Method Summary collapse

Instance Method Details

#contains?(x, y) ⇒ Boolean

Determines if given position is within the table bounds.


67
68
69
70
71
# File 'lib/moon/packages/data_bags/tabular/iterator_base.rb', line 67

def contains?(x, y)
  x, y = x.to_i, y.to_i
  return ((x >= 0) && (x < xsize)) &&
         ((y >= 0) && (y < ysize))
end

#iterInterator

Initializes and returns an Iterator


86
87
88
# File 'lib/moon/packages/data_bags/tabular/iterator_base.rb', line 86

def iter
  @iter ||= IteratorBase.new(self)
end

#map_with_xy {|value, x, y| ... } ⇒ Object

Yield Parameters:


93
94
95
96
97
# File 'lib/moon/packages/data_bags/tabular/iterator_base.rb', line 93

def map_with_xy
  iter.each_with_xy do |n, x, y|
    self[x, y] = yield self[x, y], x, y
  end
end

#rectMoon::Rect


79
80
81
# File 'lib/moon/packages/data_bags/tabular/iterator_base.rb', line 79

def rect
  Moon::Rect.new 0, 0, xsize, ysize
end

#set_by_dict(str, dict) ⇒ self

Set a Table's data from a String and a Dictionary


104
105
106
107
108
109
110
111
# File 'lib/moon/packages/data_bags/tabular/iterator_base.rb', line 104

def set_by_dict(str, dict)
  str.split("\n").each do |row|
    row.size.times do |i|
      self[i % xsize, i / xsize] = dict.fetch(row[i])
    end
  end
  self
end

#sizesMoon::Vector2


74
75
76
# File 'lib/moon/packages/data_bags/tabular/iterator_base.rb', line 74

def sizes
  Moon::Vector2.new xsize, ysize
end