Module: Shared

Overview

Some odds and ends shared by other classes

Defined Under Namespace

Modules: BinaryTreeArithmetic Classes: DataError, InternalLogicError, LogicError, Point

Constant Summary collapse

INFINITY =

Infinity without having to put a Float:: prefix every time

Float::INFINITY

Instance Method Summary collapse

Instance Method Details

#contains_duplicates?(enum, by: nil) ⇒ Boolean

Simple O(n) check for duplicates in an enumerable.

It may be worse than O(n), depending on how close to constant set insertion is.

Parameters:

  • enum

    the enumerable to check for duplicates

  • by (defaults to: nil)

    a method to call on each element of enum before checking. The results of these methods are checked for duplication. When nil we don’t call anything and just use the elements themselves.

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'lib/data_structures_rmolinari/shared.rb', line 70

def contains_duplicates?(enum, by: nil)
  seen = Set.new
  enum.each do |v|
    v = v.send(by) if by
    return true if seen.include? v

    seen << v
  end
  false
end