Module: AnnotatedArray

Defined in:
lib/rbbt/annotations/annotated_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#list_idObject

Returns the value of attribute list_id.



2
3
4
# File 'lib/rbbt/annotations/annotated_array.rb', line 2

def list_id
  @list_id
end

Instance Method Details

#[](pos, clean = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rbbt/annotations/annotated_array.rb', line 16

def [](pos, clean = false)

  value = super(pos)
  return value if value.nil? or clean

  value = value.dup if value.frozen?

  value = annotate(value)

  value.extend AnnotatedArray if Array === value

  if value.respond_to? :container
    value.container       = self
    value.container_index = pos
  end

  value
end

#collect(&block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rbbt/annotations/annotated_array.rb', line 61

def collect(&block)

  if block_given?

    res = []
    each do |value|
      res << yield(value)
    end

    res
  else

    res = []
    each do |value|
      res << value
    end

    res
  end
end

#double_arrayObject



4
5
6
# File 'lib/rbbt/annotations/annotated_array.rb', line 4

def double_array
  AnnotatedArray === self.send(:[], 0, true)
end

#each(&block) ⇒ Object



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
# File 'lib/rbbt/annotations/annotated_array.rb', line 35

def each(&block)

  pos = 0
  super do |value|

    if value.nil?

      block.call value
    else

      value = value.dup if value.frozen?

      value = annotate(value)

      value.extend AnnotatedArray if Array === value

      value.container       = self
      value.container_index = pos

      pos += 1

      block.call value
    end
  end
end

#firstObject



8
9
10
# File 'lib/rbbt/annotations/annotated_array.rb', line 8

def first
  self[0]
end

#lastObject



12
13
14
# File 'lib/rbbt/annotations/annotated_array.rb', line 12

def last
  self[-1]
end

#rejectObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rbbt/annotations/annotated_array.rb', line 105

def reject
  res = []

  each do |value|
    res << value unless yield(value)
  end

  annotate(res)
  res.extend AnnotatedArray

  res
end

#remove(list) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/rbbt/annotations/annotated_array.rb', line 141

def remove(list)

  res = (self - list)

  annotate(res)
  res.extend AnnotatedArray

  res
end

#selectObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbbt/annotations/annotated_array.rb', line 82

def select(method = nil, *args)

  if method

    res = self.zip( self.send(method, *args) ).
      select{|e,result| result }. 
      collect{|element,r| element }
  else

    return self unless block_given?

    res = []
    each do |value|
      res << value if yield(value)
    end
  end

  annotate(res)
  res.extend AnnotatedArray

  res
end

#select_by(method, *args, &block) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbbt/annotations/annotated_array.rb', line 160

def select_by(method, *args, &block)
  return [] if self.empty? and not self.respond_to? :annotate

  values = self.send(method, *args)
  values = values.clean_annotations if values.respond_to? :clean_annotations

  new = []
  if block_given?
    self.clean_annotations.each_with_index do |e,i|
      new << e if yield(values[i])
    end
  else
    self.clean_annotations.each_with_index do |e,i|
      new << e if values[i]
    end
  end
  self.annotate(new)
  new.extend AnnotatedArray

  new
end

#sort(&block) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/rbbt/annotations/annotated_array.rb', line 151

def sort(&block)
  res = self.collect.sort(&block).collect{|value| value.respond_to?(:clean_annotations) ? value.clean_annotations.dup : value.dup }

  annotate(res)
  res.extend AnnotatedArray

  res
end

#subset(list) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/rbbt/annotations/annotated_array.rb', line 131

def subset(list)

  res = (self & list)

  annotate(res)
  res.extend AnnotatedArray

  res
end