Module: QDA::Coding
Overview
Classes mixing-in should implement the offset, length, [x, y], and << methods
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
note that no-argument version of Array#sort does not call this method.
- #end ⇒ Object
-
#exclude(other) ⇒ Object
(also: #-)
returns a QDA::CodeSet created by removing the characters coded by
other
fromself
. - #include?(point) ⇒ Boolean (also: #contains?)
-
#intersect(other) ⇒ Object
(also: #%)
returns the code representing the intersection of
self
andother
returns nil if there is no overlap. -
#overlap?(other) ⇒ Boolean
Returns true if self and
other
overlap at any point - ie there is at least one character that is coded by both items. - #prepare_args(other) ⇒ Object
-
#touch?(other) ⇒ Boolean
Returns true if self and
other
overlap or are contiguous. -
#union(other) ⇒ Object
(also: #+)
Returns the code produced by merging
self
withother
.
Instance Method Details
#<=>(other) ⇒ Object
note that no-argument version of Array#sort does not call this method
33 34 35 36 37 |
# File 'lib/weft/coding.rb', line 33 def <=>(other) self.offset == other.offset ? self.end <=> other.end : self.offset <=> other.offset end |
#end ⇒ Object
5 6 7 |
# File 'lib/weft/coding.rb', line 5 def end() offset + length end |
#exclude(other) ⇒ Object Also known as: -
returns a QDA::CodeSet created by removing the characters coded by other
from self
. The returned CodeSet may be 0, 1 or 2 elements long. The diagram below shows how 2 results may be returned.
-----+++++++++++++++++------ # self
-
-----------++++++++--------- # exclude
=
-----++++++--------+++------ # result
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/weft/coding.rb', line 81 def exclude(other) this, other = prepare_args(other) results = QDA::CodeSet[] if offset < other.offset if this.end < other.offset results.add( this ) else results.add( this[offset, other.offset - offset] ) end end if this.end > other.end if this.offset > other.end results.add(this) else results.add( this[other.end, this.end - other.end] ) end end return results end |
#include?(point) ⇒ Boolean Also known as: contains?
9 10 11 12 13 14 15 |
# File 'lib/weft/coding.rb', line 9 def include?(point) if point.nil? raise ArgumentError, "Point should be an integer, got #{point.inspect}" end point >= self.offset && point < self.end() end |
#intersect(other) ⇒ Object Also known as: %
returns the code representing the intersection of self
and other
returns nil if there is no overlap
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/weft/coding.rb', line 58 def intersect(other) # this represents self, possibly coerced into a different class this, other = prepare_args(other) unless this.overlap?(other) return nil end sorted = QDA::CodeSet[ this, other ].sort this_start = [ other.offset, this.offset ].max this_end = [other.end, this.end ].min fragment = sorted[0][this_start, this_end - this_start ] end |
#overlap?(other) ⇒ Boolean
Returns true if self and other
overlap at any point - ie there is at least one character that is coded by both items
20 21 22 23 |
# File 'lib/weft/coding.rb', line 20 def overlap?(other) first, second = [self, other].sort_by { | x | x.offset } first.end > second.offset ? true : false end |
#prepare_args(other) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/weft/coding.rb', line 40 def prepare_args(other) # it should be a type that implements these methods unless other.kind_of?(Coding) raise ArgumentError, "Cannot combine with #{other.inspect}, should implement Coding" end # if it's not the same class, we need to determine what kind of # thing to return by combining the classes if other.is_a?(self.class) return self, other else return self.coerce(other), other.coerce(self) end end |
#touch?(other) ⇒ Boolean
Returns true if self and other
overlap or are contiguous
26 27 28 29 |
# File 'lib/weft/coding.rb', line 26 def touch?(other) first, second = [self, other].sort_by { | x | x.offset } first.end >= second.offset ? true : false end |
#union(other) ⇒ Object Also known as: +
Returns the code produced by merging self
with other
. If the two codes do not touch each other, then an CodeSet of the two codes is returned.
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/weft/coding.rb', line 105 def union(other) this, other = prepare_args(other) return this if this == other return QDA::CodeSet[ self, other ].sort unless touch?(other) # if they overlap or touch, a single coding will be returned first, second = QDA::CodeSet[ this, other ].sort fragment = first.dup() if second.end > first.end fragment << second[first.end, second.end - first.end] end return fragment end |