class BOAST::Slice

Attributes

alignment[RW]
slices[R]
source[R]

Public Class Methods

new(source, *slices) click to toggle source
# File lib/BOAST/Language/Slice.rb, line 94
def initialize(source, *slices)
  raise "Cannot slice a non array Variable!" unless source.dimension?
  raise "Invalid slice!" if slices.length != source.dimension.length
  @source = source
  @slices = slices.collect{ |s| SliceItem::new(s) }
end

Public Instance Methods

[](*args) click to toggle source

Indexes a {Slice} @param [Array{#to_s, Range, [first, last, step], :all, nil}] args one entry for each {SliceItem} of the {Slice}.

* Range: if an index is a Range, the result will be a {Slice}. The Range can be exclusive. The first and last item of the Range will be considered first and last index in the corresponding {SliceItem}.
* [first, last, step]: if an index is an Array, the result will be a {Slice}. The first and last item of the array will be considered first and last index in the corresponding {SliceItem}. If a step is given the range will be iterated by step.
* :all, nil: The whole corresponding {SliceItem} will be used for the slice.
* #to_s: If an index is none of the above it will be considered a scalar index. If all indexes are scalar an {Index} will be returned.

@return [Slice, Index]

# File lib/BOAST/Language/Slice.rb, line 158
def [](*args)
  slice = false
  args.each { |a|
    slice = true if a.kind_of?(Range) or a.kind_of?(Array) or a.kind_of?(Symbol) or a.nil?
  }
  new_args = []
  slices.each_with_index { |s, i|
    unless s.scalar?
      raise "Invalid slice!" if args.length == 0
      new_arg = SliceItem::new(args.shift)
      new_arg.recurse!(s, @source.dimension[i])
      new_args.push new_arg
    else
      new_args.push s
    end
  }
  if slice then
    return Slice::new(@source, *new_args)
  else
    return Index::new(@source, *(new_args.collect(&:first)))
  end
end
align?() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 137
def align?
  return @alignment
end
dimension() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 105
def dimension
  dims = []
  slices.each_with_index { |slice, i|
    unless slice.scalar? then
      if slice.all? then
        if source.dimension[i].size then
          dims.push Dimension::new( source.dimension[i].size )
        else
          dims.push Dimension::new
        end
      else
        dims.push Dimension::new( slice.length )
      end
    end
  }
  return dims
end
dimension?() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 101
def dimension?
  true
end
pr() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 128
def pr
  s=""
  s << indent
  s << to_s
  s << ";" if [C, CL, CUDA].include?( lang )
  output.puts s
  return self
end
set_align(align) click to toggle source
# File lib/BOAST/Language/Slice.rb, line 141
def set_align(align)
  @alignment = align
  return self
end
to_s() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 123
def to_s
  return to_s_fortran if lang == FORTRAN
  return to_s_c if [C, CL, CUDA].include?( lang )
end
to_var() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 146
def to_var
  var = @source.copy("#{self}", :const => nil, :constant => nil, :dim => nil, :dimension => nil, :direction => nil, :dir => nil, :align => alignment)
  return var
end

Private Instance Methods

to_s_c() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 183
    def to_s_c
      dims = @source.dimension.reverse
      slices_to_c = @slices.reverse.each_with_index.collect { |slice, indx|
        if slice.all? then
          ":"
        else
          start = Expression::new(Subtraction, slice.first, dims[indx].start)
          s = "#{start}"
          unless slice.scalar? then
            s << ":#{slice.length}"
#           s << ":#{slice.step}" if slice.step
            raise "Slice don't support step in C!" if slice.step
          end
          s
        end
      }
      return "#{@source}[#{slices_to_c.join("][")}]"
    end
to_s_fortran() click to toggle source
# File lib/BOAST/Language/Slice.rb, line 202
def to_s_fortran
  slices_to_fortran = @slices.collect { |slice|
    if slice.all? then
      ":"
    else
      s = "#{slice.first}"
      unless slice.scalar? then
        s << ":#{slice.last}"
        s << ":#{slice.step}" if slice.step
      end
      s
    end
  }
  return "#{@source}(#{slices_to_fortran.join(", ")})"
end