Class: Algorithmix::DataStructure::Generic::Stack

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

Instance Method Summary collapse

Constructor Details

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

Creates a new stack.



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

def initialize(obj = nil, copy: false)

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

Instance Method Details

#!=(stack) ⇒ true, false

Compares the self stack with given as argument stack.

Raises:

  • (ArgumentError)


88
89
90
91
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 88

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

#&(stack) ⇒ Stack

Finds intersection of contents of the self stack and stack given as argument, and returns a new stack with the result.

Raises:

  • ArgumentError, if given object is not a stack



157
158
159
160
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 157

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

#+(stack) ⇒ Stack

Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.

Raises:

  • ArgumentError, if given object is not a stack



115
116
117
118
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 115

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

#-(stack) ⇒ Stack

Finds difference of the self stack object and that given as argument.

Raises:

  • ArgumentError, if given object is not a stack



181
182
183
184
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 181

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

#<<(value) ⇒ Stack

Inserts a new element at the top of the stack.



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

def <<(value)
  push(value)
end

#<=>(stack) ⇒ Object

Compares the self stack with given as argument stck.

Raises:

  • ArgumentError, if given object is not a stack



105
106
107
108
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 105

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

#==(stack) ⇒ true, false

Compares the self stack with given as argument stack.

Raises:

  • (ArgumentError)


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

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

#^(stack) ⇒ Stack

Finds the symmetric difference of the stack and that given as argument.

Raises:

  • ArgumentError, if given object is not a stack



237
238
239
240
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 237

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

#apply(&block) ⇒ Stack

Applies a function to each element of the stack.



321
322
323
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 321

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

#apply!(&block) ⇒ Object

Same as #map, but the result is kept in the current object.



326
327
328
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 326

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

#assign(obj, copy: false) ⇒ Stack

Copies the content of an object to the content of the stack, removing previous elements inserted in it.



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

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

#clearObject

Clears content of current stack.



265
266
267
268
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 265

def clear
  @container = []
  self
end

#concat(stack) ⇒ Stack

Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.

Raises:

  • ArgumentError, if given object is not a stack



139
140
141
142
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 139

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

#concat!(stack) ⇒ Stack

Merges contents of the self stack, and stack given as argument. Modifies self object.

Raises:

  • ArgumentError, if given object is not a stack



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

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

#diff?(stack) ⇒ true, false

Compares the self stack with given as argument stack.

Raises:

  • (ArgumentError)


94
95
96
97
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 94

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

#difference(stack) ⇒ Stack

Finds difference of the self stack object and that given as argument.

Raises:

  • ArgumentError, if given object is not a stack



187
188
189
190
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 187

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

#difference!(stack) ⇒ Stack

As difference, finds the difference of the stack and that given as argument, but modifies self object.

Raises:

  • ArgumentError, if given object is not a stack



198
199
200
201
202
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 198

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

#empty?Boolean

Checks if the stack contains any elements.



68
69
70
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 68

def empty?
  @container.empty?
end

#eql?(stack) ⇒ true, false

Compares the self stack with given as argument stack.

Raises:

  • (ArgumentError)


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

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

#filter(&block) ⇒ Stack

Filters elements of the stack, by given condition.



285
286
287
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 285

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

#filter!(&block) ⇒ Object

Same as #select, but modifying the self object.



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

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

#find_all(&block) ⇒ Stack

Filters elements of the stack, by given condition.



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

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

#find_all!(&block) ⇒ Object

Same as #select, but modifying the self object.



300
301
302
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 300

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

#intersect(stack) ⇒ Stack

Finds intersection of contents of the self stack and stack given as argument, and returns a new stack with the result.

Raises:

  • ArgumentError, if given object is not a stack



163
164
165
166
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 163

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

#intersect!(stack) ⇒ Object

Like intersect, finds intersection of the self stack object and stack given as argument, but modifies the object to which operation was called.

Raises:

  • (ArgumentError)


170
171
172
173
174
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 170

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

#lengthObject

Returns the number of elements in the stack.



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

def length
  @container.size
end

#map(&block) ⇒ Stack

Applies a function to each element of the stack.



308
309
310
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 308

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

#map!(&block) ⇒ Object

Same as #map, but the result is kept in the current object.



315
316
317
318
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 315

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

#merge(stack) ⇒ Stack

Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.

Raises:

  • ArgumentError, if given object is not a stack



121
122
123
124
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 121

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

#merge!(stack) ⇒ Stack

Merges contents of the self stack, and stack given as argument. Modifies self object.

Raises:

  • ArgumentError, if given object is not a stack



132
133
134
135
136
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 132

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

#popObject

Removes the top element of the stack.

Raises:

  • Algorithmix::EmptyContainerError, if there are no elements in the stack.



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

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

#push(value) ⇒ Stack

Inserts a new element at the top of the stack.



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

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

#select(&block) ⇒ Stack

Filters elements of the stack, by given condition.



274
275
276
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 274

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

#select!(&block) ⇒ Object

Same as #select, but modifying the self object.



279
280
281
282
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 279

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

#sizeObject

Returns the number of elements in the stack.



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

def size
  @container.size
end

#symmetric_difference(stack) ⇒ Stack

Finds the symmetric difference of the stack and that given as argument.

Raises:

  • ArgumentError, if given object is not a stack



243
244
245
246
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 243

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

#symmetric_difference!(stack) ⇒ Stack

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

Raises:

  • ArgumentError, if given object is not a stack



253
254
255
256
257
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 253

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

#to_aObject

Returns current object as array.



260
261
262
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 260

def to_a
  @container
end

#topObject

Returns the top element of the stack, without removing it. If the stack is empty, returns nil.



63
64
65
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 63

def top
  @container.last
end

#union(stack) ⇒ Stack

Finds union of the self object and stack given as argument.

Raises:

  • ArgumentError, if given object is not a stack



215
216
217
218
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 215

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

#union!(stack) ⇒ Stack

Like #|, finds the union of the self object and the stack given as argument, but keeps the result in the current stack.

Raises:

  • ArgumentError, if given object is not a stack



226
227
228
229
230
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 226

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

#|(stack) ⇒ Stack

Finds union of the self object and stack given as argument.

Raises:

  • ArgumentError, if given object is not a stack



209
210
211
212
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 209

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