Class: When::Parts::Enumerator::Array

Inherits:
When::Parts::Enumerator show all
Defined in:
lib/when_exe/parts/enumerator.rb

Overview

時間位置の Array を順に取り出す Enumerator

Instance Attribute Summary

Attributes inherited from When::Parts::Enumerator

#count, #count_limit, #current, #direction, #exdate, #first, #last, #parent, #processed

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from When::Parts::Enumerator

#each, #exclude_end, #has_next?, #next, #succ, #with_index, #with_object

Constructor Details

#initialize(parent, list, count_limit = nil) ⇒ Array

オブジェクトの生成

Parameters:

  • parent (Comparable)

    生成元

  • list (Array<When::TM::TemporalPosition>)

    順に取り出す時間位置の Array

  • direction (Symbol)
    :forward - 昇順
    :reverse - 降順
  • count_limit (Integer) (defaults to: nil)

    繰り返し回数(デフォルトは指定なし)

Raises:

  • (ArgumentError)


401
402
403
404
405
406
# File 'lib/when_exe/parts/enumerator.rb', line 401

def initialize(*args)
  parent, list, direction, *args = args
  raise ArgumentError, "Too few arguments" unless list
  @initial_list = self.class._sort(list, direction||:forward)
  super(parent, @initial_list[0], direction, *args)
end

Class Method Details

._sort(list, direction) ⇒ Array

Note:

eql? はオーバーライドしない

整列して、重複した候補を削除

Parameters:

  • list (Array)
  • direction (Symbol)
    :forward - 昇順
    :reverse - 降順

Returns:



354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/when_exe/parts/enumerator.rb', line 354

def self._sort(list, direction)
  list = (0...list.size).to_a.map {|i| [list[i], i]}.sort.map {|e| e[0]}
  prev = nil
  list.delete_if do |x|
    if (x == prev)
      true
    else
      prev = x
      false
    end
  end
  list.reverse! if (direction == :reverse)
  return list
end