Class: FrugalTimeout::SortedQueue

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/frugal_timeout.rb

Overview

{{{1 SortedQueue

Instance Method Summary collapse

Constructor Details

#initialize(storage = []) ⇒ SortedQueue

Returns a new instance of SortedQueue.



208
209
210
211
# File 'lib/frugal_timeout.rb', line 208

def initialize storage=[]
  super()
  @array, @unsorted = storage, false
end

Instance Method Details

#each(&b) ⇒ Object



213
214
215
# File 'lib/frugal_timeout.rb', line 213

def each &b
  synchronize { @array.each &b }
end

#empty?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/frugal_timeout.rb', line 217

def empty?
  synchronize { @array.empty? }
end

#firstObject



221
222
223
# File 'lib/frugal_timeout.rb', line 221

def first
  synchronize { @array.first }
end

#lastObject



225
226
227
228
229
230
# File 'lib/frugal_timeout.rb', line 225

def last
  synchronize {
	sort!
	@array.last
  }
end

#push(*args) ⇒ Object Also known as: <<



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/frugal_timeout.rb', line 232

def push *args
  synchronize {
	args.each { |arg|
	  case @array.first <=> arg
	  when -1, 0, nil
 @array.push arg
	  when 1
 @array.unshift arg
	  end
	}
	@unsorted = true
  }
end

#reject!(&b) ⇒ Object



247
248
249
250
251
252
# File 'lib/frugal_timeout.rb', line 247

def reject! &b
  synchronize {
	sort!
	@array.reject! &b
  }
end

#reject_and_get!(&b) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/frugal_timeout.rb', line 254

def reject_and_get! &b
  res = []
  reject! { |el|
	break unless b.call el

	res << el
  }
  res
end

#sizeObject



264
265
266
# File 'lib/frugal_timeout.rb', line 264

def size
  synchronize { @array.size }
end