Class: Depq::Locator
- Inherits:
-
Struct
- Object
- Struct
- Depq::Locator
- Defined in:
- lib/depq.rb,
lib/depq.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#depq ⇒ Object
(also: #queue)
returns the queue.
-
#in_queue? ⇒ Boolean
returns true if the locator is in a queue.
-
#initialize(value, priority = value, subpriority = nil) ⇒ Locator
constructor
Create a Depq::Locator object.
-
#initialize_copy(obj) ⇒ Object
:nodoc:.
- #inspect ⇒ Object (also: #to_s)
-
#priority ⇒ Object
returns the priority.
-
#subpriority ⇒ Object
returns the subpriority.
-
#update(value, priority = value, subpriority = nil) ⇒ Object
update the value, priority and subpriority.
-
#update_priority(priority, subpriority = nil) ⇒ Object
update the priority and subpriority.
-
#update_value(value) ⇒ Object
update the value.
Constructor Details
#initialize(value, priority = value, subpriority = nil) ⇒ Locator
244 245 246 |
# File 'lib/depq.rb', line 244 def initialize(value, priority=value, subpriority=nil) super value, subpriority, priority end |
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value
198 199 200 |
# File 'lib/depq.rb', line 198 def value @value end |
Instance Method Details
#depq ⇒ Object Also known as: queue
returns the queue.
nil is returned if the locator is not in a queue.
280 281 282 |
# File 'lib/depq.rb', line 280 def depq in_queue? ? depq_or_subpriority() : nil end |
#in_queue? ⇒ Boolean
returns true if the locator is in a queue.
273 274 275 |
# File 'lib/depq.rb', line 273 def in_queue? depq_or_subpriority().kind_of? Depq end |
#initialize_copy(obj) ⇒ Object
:nodoc:
268 269 270 |
# File 'lib/depq.rb', line 268 def initialize_copy(obj) # :nodoc: raise TypeError, "can not duplicated" end |
#inspect ⇒ Object Also known as: to_s
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/depq.rb', line 253 def inspect prio = self.priority if self.value == prio s = self.value.inspect else s = "#{self.value.inspect}:#{prio.inspect}" end if in_queue? "\#<#{self.class}: #{s}>" else "\#<#{self.class}: #{s} (no queue)>" end end |
#priority ⇒ Object
returns the priority.
299 300 301 302 303 304 305 306 307 |
# File 'lib/depq.rb', line 299 def priority if in_queue? q = depq_or_subpriority() priority, _subpriority = q.send(:internal_get_priority, self) priority else index_or_priority() end end |
#subpriority ⇒ Object
returns the subpriority.
310 311 312 313 314 315 316 317 318 |
# File 'lib/depq.rb', line 310 def subpriority if in_queue? q = depq_or_subpriority() _priority, subpriority = q.send(:internal_get_priority, self) subpriority else depq_or_subpriority() end end |
#update(value, priority = value, subpriority = nil) ⇒ Object
update the value, priority and subpriority.
subpriority cannot be nil if the locator is in a queue. So subpriority is not changed if subpriority is not specified or nil for a locator in a queue. subpriority is set to nil if subpriority is not specified or nil for a locator not in a queue.
q = Depq.new
loc1 = q.insert 1, 2, 3
p [loc1.value, loc1.priority, loc1.subpriority] #=> [1, 2, 3]
loc1.update(11, 12)
p [loc1.value, loc1.priority, loc1.subpriority] #=> [11, 12, 3]
loc2 = Depq::Locator.new(4, 5, 6)
p [loc2.value, loc2.priority, loc2.subpriority] #=> [4, 5, 6]
loc2.update(14, 15)
p [loc2.value, loc2.priority, loc2.subpriority] #=> [14, 15, nil]
This feature is called as decrease-key/increase-key in Computer Science terminology.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/depq.rb', line 339 def update(value, priority=value, subpriority=nil) subpriority = Integer(subpriority) if subpriority != nil if in_queue? q = depq_or_subpriority() if subpriority == nil subpriority = self.subpriority else subpriority = Integer(subpriority) end q.send(:internal_set_priority, self, priority, subpriority) else self.index_or_priority = priority self.depq_or_subpriority = subpriority end self.value = value nil end |
#update_priority(priority, subpriority = nil) ⇒ Object
update the priority and subpriority.
This method doesn’t change the value.
q = Depq.new
loc = q.insert 1, 2, 3
p [loc.value, loc.priority, loc.subpriority] #=> [1, 2, 3]
loc.update_priority 10
p [loc.value, loc.priority, loc.subpriority] #=> [1, 10, 3]
loc.update_priority 20, 30
p [loc.value, loc.priority, loc.subpriority] #=> [1, 20, 30]
383 384 385 |
# File 'lib/depq.rb', line 383 def update_priority(priority, subpriority=nil) update(self.value, priority, subpriority) end |
#update_value(value) ⇒ Object
367 368 369 |
# File 'lib/depq.rb', line 367 def update_value(value) update(value, self.priority, self.subpriority) end |