Class: GroupLabels

Inherits:
Object
  • Object
show all
Defined in:
lib/carray/axis_group.rb

Overview


GroupLabels -- lazy, factorized label view.

Stores one 1-D label vector per surviving output axis (group axis = its categorical labels; band axis = identity 0...len). Block label tuples are built on demand from the product -- the N-tuple table is NEVER materialised,

so storage is linear in the number of axes, not the block count.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vectors) ⇒ GroupLabels

Returns a new instance of GroupLabels.



251
252
253
254
255
256
# File 'lib/carray/axis_group.rb', line 251

def initialize (vectors)
  @vectors = vectors          # Array of [:names, array] / [:identity, len]
  @shape   = vectors.map { |kind, data| kind == :names ? data.size : data }
                    .freeze
  freeze
end

Instance Attribute Details

#shapeObject (readonly)

Returns the value of attribute shape.



258
259
260
# File 'lib/carray/axis_group.rb', line 258

def shape
  @shape
end

Instance Method Details

#[](*idx) ⇒ Object

Block label tuple at the given index (length = ndim).



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/carray/axis_group.rb', line 266

def [] (*idx)
  if idx.size != @vectors.size
    raise IndexError,
          "GroupLabels: expected #{@vectors.size} indices, got #{idx.size}"
  end
  @vectors.each_with_index.map do |(kind, data), ax|
    i = idx[ax]
    len = (kind == :names) ? data.size : data
    i += len if i < 0
    if i < 0 || i >= len
      raise IndexError, "GroupLabels: index #{idx[ax]} out of range for axis #{ax}"
    end
    (kind == :names) ? data[i] : i
  end
end

#axis(k) ⇒ Object

The label vector for output axis k (Array): group labels or the identity range of a band axis.



284
285
286
287
# File 'lib/carray/axis_group.rb', line 284

def axis (k)
  kind, data = @vectors[k]
  (kind == :names) ? data.dup : (0...data).to_a
end

#coordsObject

All per-axis label vectors (the table axis headings).



290
291
292
# File 'lib/carray/axis_group.rb', line 290

def coords
  (0...@vectors.size).map { |k| axis(k) }
end

#inspectString

Returns:

  • (String)


295
296
297
# File 'lib/carray/axis_group.rb', line 295

def inspect
  "#<GroupLabels shape=#{@shape.inspect}>"
end

#ndimInteger

Returns the number of grouped axes.

Returns:

  • (Integer)

    the number of grouped axes.



261
262
263
# File 'lib/carray/axis_group.rb', line 261

def ndim
  @vectors.size
end