Class: Pwrake::PriorityQueueArray

Inherits:
Array
  • Object
show all
Defined in:
lib/pwrake/queue/queue_array.rb

Direct Known Subclasses

PriorityHrfQueueArray

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ PriorityQueueArray

Returns a new instance of PriorityQueueArray.



7
8
9
# File 'lib/pwrake/queue/queue_array.rb', line 7

def initialize(n)
  super()
end

Instance Method Details

#index(t) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pwrake/queue/queue_array.rb', line 36

def index(t)
  if size < 40
    return super(t)
  end
  priority = t.priority
  if last.priority < priority || first.priority > priority
    nil
  else
    lower = 0
    upper = size-1
    while lower+1 < upper
      mid = ((lower + upper) / 2).to_i
      if self[mid].priority < priority
        lower = mid
      else
        upper = mid
      end
    end
    mid = upper
    if self[mid].priority == priority
      Log.debug "TQA#index=#{mid}, priority=#{priority}"
      mid
    end
  end
end

#push(t) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pwrake/queue/queue_array.rb', line 15

def push(t)
  priority = t.priority
  if empty? || last.priority <= priority
    super(t)
  elsif first.priority > priority
    unshift(t)
  else
    lower = 0
    upper = size-1
    while lower+1 < upper
      mid = ((lower + upper) / 2).to_i
      if self[mid].priority <= priority
        lower = mid
      else
        upper = mid
      end
    end
    insert(upper,t)
  end
end

#shiftObject



11
12
13
# File 'lib/pwrake/queue/queue_array.rb', line 11

def shift
  pop
end