Module: Musicality::BiInfiniteSequence

Included in:
AddingSequence, RepeatingSequence
Defined in:
lib/musicality/composition/util/biinfinite_sequence.rb

Instance Method Summary collapse

Instance Method Details

#at(offset) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/musicality/composition/util/biinfinite_sequence.rb', line 14

def at offset
  if offset.is_a? Enumerable
    return enum_for(:at,offset) unless block_given?
  else
    return at_one(offset)
  end
  
  offset_index_pairs = offset.map_with_index {|x,i| [x,i] }.sort
  results = Array.new(offset.size)
  
  past = offset_index_pairs.select {|p| p[0] < 0 }
  present = offset_index_pairs.select {|p| p[0] == 0 }
  future = offset_index_pairs.select {|p| p[0] > 0 }
  
  start_val = start_value
  
  if past.any?
    value = start_val
    j = past.size - 1
    tgt_offset = past[j][0]
    0.downto(past.first[0]+1) do |i|
      value = prev_value(value,i)
      while (i-1) == tgt_offset
        results[past[j][1]] = value
        j -= 1
        tgt_offset = j >= 0 ? past[j][0] : nil
      end
    end
  end
  
  if present.any?
    present.each do |off,index|
      results[index] = start_val
    end
  end
  
  if future.any?
    value = start_val
    j = 0
    tgt_offset = future[j][0]
    0.upto(future.last[0]-1) do |i|
      value = next_value(value,i)
      while (i+1) == tgt_offset
        results[future[j][1]] = value
        j += 1
        tgt_offset = j < future.size ? future[j][0] : nil
      end
    end
  end
  
  results.each {|x| yield x }
end

#over(range) ⇒ Object

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/musicality/composition/util/biinfinite_sequence.rb', line 90

def over range
  min, max = range.minmax
  raise EmptyError, "given range (#{range}) is empty" if min.nil?
  return enum_for(:over,range) unless block_given?

  if min >= 0 && max >= 0
    value = at(min)
    range.each do |i|
      yield value
      value = next_value(value,i)
    end
  elsif max < 0
    value = at(max+1)
    values = range.entries.reverse.map do |i|
      value = prev_value(value,i+1)
    end
    values.reverse_each {|x| yield x }
  else
    take_back(-min).reverse_each {|x| yield x }
    take(max + 1){ |x| yield x }
  end
end

#take(n) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/musicality/composition/util/biinfinite_sequence.rb', line 67

def take n
  raise NegativeError if n < 0
  return enum_for(:take,n) unless block_given?
  return if n == 0
  
  value = start_value
  0.upto(n - 1) do |i|
    yield value
    value = next_value(value,i)
  end
end

#take_back(n) ⇒ Object

Raises:



79
80
81
82
83
84
85
86
87
88
# File 'lib/musicality/composition/util/biinfinite_sequence.rb', line 79

def take_back n
  raise NegativeError if n < 0
  return enum_for(:take_back,n) unless block_given?
  return if n == 0
  
  value = start_value
  0.downto(1 - n) do |i|
    yield value = prev_value(value,i)
  end
end