Class: VirtFS::ByteRange

Inherits:
Object
  • Object
show all
Defined in:
lib/virtfs/byte_range.rb

Overview

ByteRange utility class, encapsulate a range of bytes as given by their first / last offsets

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first = nil, last = nil) ⇒ ByteRange

Returns a new instance of ByteRange.



7
8
9
# File 'lib/virtfs/byte_range.rb', line 7

def initialize(first = nil, last = nil)
  set(first, last)
end

Instance Attribute Details

#firstObject

Returns the value of attribute first.



5
6
7
# File 'lib/virtfs/byte_range.rb', line 5

def first
  @first
end

#lastObject

Returns the value of attribute last.



5
6
7
# File 'lib/virtfs/byte_range.rb', line 5

def last
  @last
end

Instance Method Details

#adjacent?(*args) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/virtfs/byte_range.rb', line 26

def adjacent?(*args)
  return false if empty?
  nrange = range_arg(args)
  nrange.first == @last + 1 || nrange.last == @first - 1
end

#clearObject



49
50
51
# File 'lib/virtfs/byte_range.rb', line 49

def clear
  set(nil, nil)
end

#contiguous?(*args) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/virtfs/byte_range.rb', line 38

def contiguous?(*args)
  nrange = range_arg(args)
  adjacent?(nrange) || overlap?(nrange)
end

#empty?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/virtfs/byte_range.rb', line 11

def empty?
  @first.nil? || @last.nil?
end

#expand(*args) ⇒ Object



43
44
45
46
47
# File 'lib/virtfs/byte_range.rb', line 43

def expand(*args)
  nrange = range_arg(args)
  @first = nrange.first if empty? || nrange.first < @first
  @last  = nrange.last  if empty? || nrange.last  > @last
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/virtfs/byte_range.rb', line 20

def include?(obj)
  return false if empty?
  return (obj.first >= @first && obj.last <= @last) if obj.is_a?(self.class)
  obj >= @first && obj <= @last
end

#lengthObject



15
16
17
18
# File 'lib/virtfs/byte_range.rb', line 15

def length
  return 0 if empty?
  @last - @first + 1
end

#overlap?(*args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/virtfs/byte_range.rb', line 32

def overlap?(*args)
  return false if empty?
  nrange = range_arg(args)
  include?(nrange.first) || include?(nrange.last) || nrange.include?(@first) || nrange.include?(@last)
end

#set(first, last) ⇒ Object



53
54
55
56
# File 'lib/virtfs/byte_range.rb', line 53

def set(first, last)
  @first = first
  @last  = last
end