Class: QDA::CodeSet

Inherits:
Array
  • Object
show all
Defined in:
lib/weft/coding.rb

Overview

A collection of things that are Coding - ie that mix-in the class above.

Instance Method Summary collapse

Constructor Details

#initialize(arr = []) ⇒ CodeSet

Populate a new CodeSet from an array of coding items arr. These should either be QDA::Codes, QDA::Fragments or three-item arrays. Where the latter are found, they will be automatically turned into QDA::Code, taking the contents of each three-item array to be

docid, offset, length


131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/weft/coding.rb', line 131

def initialize(arr = [])
  arr.collect! do | item |
    case item
    when Array
      Code.new(*item)
    when Fragment, Code
      item
    else
      raise ArgumentError, "unexpected item #{item} in list"
    end
  end
  super(arr)
end

Instance Method Details

#add(code) ⇒ Object

add the extent covered by code (a QDA::Code) to the set, modifying in place.



182
183
184
# File 'lib/weft/coding.rb', line 182

def add(code)
  replace( union( CodeSet[code] ) )
end

#docidObject



149
150
151
# File 'lib/weft/coding.rb', line 149

def docid
  first ? first.docid : nil
end

#each_pairObject

iterate over each successive neighbouring pair of codings in the set, i.e. items 1, 2; items 2,3; items 3, 4 .. items n-1, n]. This is practically useful for intersect but no other use at the moment.



165
166
167
# File 'lib/weft/coding.rb', line 165

def each_pair()
  0.upto( length - 2 ) { | i | yield self[i], self[i + 1] }
end

#exclude(other) ⇒ Object

returns the set produced by removing all extents covered by other, which should be a QDA::CodeSet. Note that unlike union and intersect

self.exclude(other) != other.exclude(self)


196
197
198
199
200
201
202
203
# File 'lib/weft/coding.rb', line 196

def exclude(other)
  return self if other.nil?
  results = self.dup
  other.each do | uncode |
    results.collect! { | code | code - uncode }.flatten!
  end
  return results
end

#intersect(other) ⇒ Object



173
174
175
176
177
178
# File 'lib/weft/coding.rb', line 173

def intersect(other)
  results = CodeSet[]
  sorted  = CodeSet[ *(self  + other).sort_by { | x | x.end } ]
  sorted.each_pair { | a, b | results << a % b }
  results.compact # return less nils
end

#itemsObject



145
146
147
# File 'lib/weft/coding.rb', line 145

def items
  self
end

#num_of_charsObject



157
158
159
# File 'lib/weft/coding.rb', line 157

def num_of_chars()
  inject(0) { | total, code| total += code.length }    
end

#sortObject



169
170
171
# File 'lib/weft/coding.rb', line 169

def sort()
  block_given? ? super : super { | a, b | a <=> b }
end

#subtract(uncode) ⇒ Object

remove the extent covered by uncode (a QDA::Code) from the set, modifying it in place.



188
189
190
# File 'lib/weft/coding.rb', line 188

def subtract(uncode)
  replace( exclude( CodeSet[uncode] ) )
end

#titleObject



153
154
155
# File 'lib/weft/coding.rb', line 153

def title
  first ? first.title : nil
end

#union(other) ⇒ Object

Returns the set produced by merging all the codes in this one with those in other, which should be a QDA::CodeSet



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/weft/coding.rb', line 207

def union(other)
     results = CodeSet[]
     sorted  = CodeSet[ *(self  + other).sort_by { | f | f.end } ]

  last_code = nil
  sorted.each do | code |
	if ! last_code
	  last_code = code
	elsif last_code.touch?(code)
	  last_code = last_code + code
       else
	  results.push(last_code)
	  last_code = code.dup
	end
  end
  results.push(last_code)
  return results
end