Class: Atig::Db::SizedUniqArray

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/atig/db/sized_uniq_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ SizedUniqArray

Returns a new instance of SizedUniqArray.



14
15
16
17
18
19
# File 'lib/atig/db/sized_uniq_array.rb', line 14

def initialize(capacity)
  @size     = 0
  @index    = 0
  @capacity = capacity
  @xs       = Array.new(capacity, nil)
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



12
13
14
# File 'lib/atig/db/sized_uniq_array.rb', line 12

def size
  @size
end

Instance Method Details

#each(&f) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/atig/db/sized_uniq_array.rb', line 21

def each(&f)
  if @size < @capacity then
    0.upto(@size - 1) {|i|
      f.call @xs[i]
    }
  else
    0.upto(@size - 1){|i|
      f.call @xs[ (i + @index) % @capacity ]
    }
  end
end

#include?(item) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/atig/db/sized_uniq_array.rb', line 45

def include?(item)
  self.any?{|x|
    x.id == item.id
  }
end

#push(item) ⇒ Object Also known as: <<



51
52
53
54
55
56
57
58
# File 'lib/atig/db/sized_uniq_array.rb', line 51

def push(item)
  return nil if include? item
  i = @index
  @xs[i] = item
  @size = [ @size + 1, @capacity ].min
  @index = ( @index + 1 ) % @capacity
  i
end

#reverse_each(&f) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/atig/db/sized_uniq_array.rb', line 33

def reverse_each(&f)
  if @size < @capacity then
    (@size - 1).downto(0) {|i|
      f.call @xs[i]
    }
  else
    (@size - 1).downto(0){|i|
      f.call @xs[ (i + @index) % @capacity ]
    }
  end
end