Class: Algorithmix::DataStructure::Generic::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/algorithmix/data_structure/generic/queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, copy: false) ⇒ Queue

Creates a new queue.

Parameters:

  • obj (#to_a) (defaults to: nil)

    Any objects which responds to #to_a method



14
15
16
17
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 14

def initialize(obj = nil, copy: false)
  @container = []
  obj.nil? ? nil : from_obj(obj, copy)
end

Instance Method Details

#!=(queue) ⇒ true, false

Compares contents of the self object and that given as argument.

Parameters:

Returns:

  • (true, false)

Raises:

  • ArgumentError, if given object is not a queue.



92
93
94
95
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 92

def !=(queue)
  raise ArgumentError, "Undefined method Queue#!= for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container != queue.to_a
end

#&(queue) ⇒ Queue

Finds the intersection of the self object and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



178
179
180
181
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 178

def &(queue)
  raise ArgumentError, "Undefined method Queue#& for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  Queue.new(@container & queue.to_a)
end

#+(queue) ⇒ Queue

Concatenates contents of two queues.

Parameters:

Returns:

  • (Queue)

    a new queue object

Raises:

  • (ArgumentError)


119
120
121
122
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 119

def +(queue)
  raise ArgumentError, "Undefined method Queue#+ for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  Queue.new(@container + queue.to_a)
end

#-(queue) ⇒ Queue

Finds the difference of the current queue and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



201
202
203
204
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 201

def -(queue)
  raise ArgumentError, "Undefined method Queue#- for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  Queue.new(@container - queue.to_a)
end

#<<(value) ⇒ Queue

Inserts a value at the end of the queue.

Parameters:

  • value

Returns:

  • (Queue)

    self object



38
39
40
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 38

def <<(value)
  push(value)
end

#<=>(queue) ⇒ Object

Compares contents of the self object and that given as argument.

Parameters:

Returns:

  • 1, if content of the first queue is greater than content of the second 0, if contents of both queues are equal -1, if content of the second queue is greater than content of the first

Raises:

  • ArgumentError, if given object is not a queue.



110
111
112
113
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 110

def <=>(queue)
  raise ArgumentError, "Undefined method Queue#<=> for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container <=> queue.to_a
end

#==(queue) ⇒ true, false

Compares contents of the self object and that given as argument.

Parameters:

Returns:

  • (true, false)

Raises:

  • ArgumentError, if given object is not a queue.



76
77
78
79
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 76

def ==(queue)
  raise ArgumentError, "Undefined method Queue#== for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container == queue.to_a
end

#^(queue) ⇒ Queue

Finds the symmetric difference of the self object and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



224
225
226
227
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 224

def ^(queue)
  raise ArgumentError, "Undefined method Queue#^ for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  Queue.new((@container | queue.to_a) - (@container & queue.to_a))
end

#apply(&block) ⇒ Queue

Applies a function to each element of the queue.

Parameters:

  • &block

Returns:

  • (Queue)

    a new queue



301
302
303
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 301

def apply(&block)
  map(&block)
end

#apply!(&block) ⇒ Object

Same as #map, but modifies the current object.



306
307
308
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 306

def apply!(&block)
  map!(&block)
end

#assign(obj, copy: false) ⇒ Object

Assigns content of an object, to stack’s content, removing previous elements.

Parameters:

  • obj (#to_a)

    Any object which responds to #to_a method.

Returns:

  • self object [Queue]



24
25
26
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 24

def assign(obj, copy: false)
  from_obj(obj, copy)
end

#clearObject

Clears content of the queue.



247
248
249
250
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 247

def clear
  @container = []
  self
end

#concat(queue) ⇒ Queue

Concatenates contents of two queues.

Parameters:

Returns:

  • (Queue)

    a new queue object

Raises:

  • (ArgumentError)


125
126
127
128
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 125

def concat(queue)
  raise ArgumentError, "Undefined method Queue#concat for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self + queue
end

#concat!(queue) ⇒ Object

Same as #+, but modifies the current object

Raises:

  • (ArgumentError)


137
138
139
140
141
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 137

def concat!(queue)
  raise ArgumentError, "Undefined method Queue#concat! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container += queue.to_a
  self
end

#diff?(queue) ⇒ true, false

Compares contents of the self object and that given as argument.

Parameters:

Returns:

  • (true, false)

Raises:

  • ArgumentError, if given object is not a queue.



98
99
100
101
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 98

def diff?(queue)
  raise ArgumentError, "Undefined method Queue#diff? for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self != queue
end

#difference(queue) ⇒ Queue

Finds the difference of the current queue and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



207
208
209
210
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 207

def difference(queue)
  raise ArgumentError, "Undefined method Queue#difference for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self - queue
end

#difference!(queue) ⇒ Object

Same as #-, but modifies the current queue.

Raises:

  • (ArgumentError)


213
214
215
216
217
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 213

def difference!(queue)
  raise ArgumentError, "Undefined method Queue#difference! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container -= queue.to_a
  self
end

#empty?Boolean

Checks if the queue is empty.

Returns:

  • (Boolean)


67
68
69
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 67

def empty?
  @container.empty?
end

#eql?(queue) ⇒ true, false

Compares contents of the self object and that given as argument.

Parameters:

Returns:

  • (true, false)

Raises:

  • ArgumentError, if given object is not a queue.



82
83
84
85
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 82

def eql?(queue)
  raise ArgumentError, "Undefined method Queue#eql? for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self == queue
end

#filter(&block) ⇒ Queue

Filters elements of the queue by given condition.

Parameters:

  • &block

Returns:

  • (Queue)

    a new queue



267
268
269
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 267

def filter(&block)
  select(&block)
end

#filter!(&block) ⇒ Object

Same as #select, but modofies the current object.



272
273
274
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 272

def filter!(&block)
  select!(&block)
end

#find_all(&block) ⇒ Queue

Filters elements of the queue by given condition.

Parameters:

  • &block

Returns:

  • (Queue)

    a new queue



277
278
279
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 277

def find_all(&block)
  select(&block)
end

#find_all!(&block) ⇒ Object

Same as #select, but modofies the current object.



282
283
284
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 282

def find_all!(&block)
  select!(&block)
end

#frontObject

Returns the front element of the queue, or nil if it’s empty.



52
53
54
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 52

def front
  @container.first
end

#intersect(queue) ⇒ Queue

Finds the intersection of the self object and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



184
185
186
187
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 184

def intersect(queue)
  raise ArgumentError, "Undefined method Queue#intersect for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self & queue
end

#intersect!(queue) ⇒ Object

Same as #&, but modifies the current object.

Raises:

  • (ArgumentError)


190
191
192
193
194
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 190

def intersect!(queue)
  raise ArgumentError, "Undefined method Queue#intersect! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container &= queue.to_a
  self
end

#lengthObject

Returns number of elements in the queue.



62
63
64
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 62

def length
  @container.size
end

#map(&block) ⇒ Queue

Applies a function to each element of the queue.

Parameters:

  • &block

Returns:

  • (Queue)

    a new queue



290
291
292
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 290

def map(&block)
  Queue.new(@container.map { |e| block.call(e)})
end

#map!(&block) ⇒ Object

Same as #map, but modifies the current object.



295
296
297
298
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 295

def map!(&block)
  @container.map! { |e| block.call(e) }
  self
end

#merge(queue) ⇒ Queue

Concatenates contents of two queues.

Parameters:

Returns:

  • (Queue)

    a new queue object

Raises:

  • (ArgumentError)


131
132
133
134
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 131

def merge(queue)
  raise ArgumentError, "Undefined method Queue#merge for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self + queue
end

#merge!(queue) ⇒ Object

Same as #+, but modifies the current object

Raises:

  • (ArgumentError)


144
145
146
147
148
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 144

def merge!(queue)
  raise ArgumentError, "Undefined method Queue#merge! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container += queue.to_a
  self
end

#popObject

Removes the front element of the queue.

Returns:

  • front element

Raises:

  • Algorithmix::EmptyContainerError if the queue is empty.



46
47
48
49
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 46

def pop
  raise EmptyContainerError, "The queue is empty." if @container.empty?
  @container.shift
end

#push(value) ⇒ Queue

Inserts a value at the end of the queue.

Parameters:

  • value

Returns:

  • (Queue)

    self object



32
33
34
35
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 32

def push(value)
  @container << value
  self
end

#select(&block) ⇒ Queue

Filters elements of the queue by given condition.

Parameters:

  • &block

Returns:

  • (Queue)

    a new queue



256
257
258
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 256

def select(&block)
  Queue.new(@container.select { |e| block.call(e) })
end

#select!(&block) ⇒ Object

Same as #select, but modofies the current object.



261
262
263
264
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 261

def select!(&block)
  @container.select! { |e| block.call(e) }
  self
end

#sizeObject

Returns number of elements in the queue.



57
58
59
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 57

def size
  @container.size
end

#symmetric_difference(queue) ⇒ Queue

Finds the symmetric difference of the self object and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



230
231
232
233
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 230

def symmetric_difference(queue)
  raise ArgumentError, "Undefined method Queue#symmetric_difference for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self ^ queue
end

#symmetric_difference!(queue) ⇒ Object

Same as #^, but modifies the current object.

Raises:

  • (ArgumentError)


236
237
238
239
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 236

def symmetric_difference!(queue)
  raise ArgumentError, "Undefined method Queue#symmetric_difference! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container = (@container | queue.to_a) - (@container & queue.to_a)
end

#to_aObject

Converts the queue to an array with elements.



242
243
244
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 242

def to_a
  @container
end

#union(queue) ⇒ Queue

Unites contents of the self queue and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



161
162
163
164
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 161

def union(queue)
  raise ArgumentError, "Undefined method Queue#union for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  self | queue
end

#union!(queue) ⇒ Object

Same as #union, but modifies the current object

Raises:

  • (ArgumentError)


167
168
169
170
171
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 167

def union!(queue)
  raise ArgumentError, "Undefined method Queue#union! for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  @container |= queue.to_a
  self
end

#|(queue) ⇒ Queue

Unites contents of the self queue and queue given as argument.

Parameters:

Returns:

  • (Queue)

    a new queue

Raises:

  • ArgumentError, if given object is not a queue



155
156
157
158
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 155

def |(queue)
  raise ArgumentError, "Undefined method Queue#| for #{queue}:#{queue.class}" unless queue.is_a?(Queue)
  Queue.new(@container | queue.to_a)
end