Class: CollectionUtils::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/collection_utils/set.rb

Instance Method Summary collapse

Constructor Details

#initialize(values = []) ⇒ Set

Returns a new instance of Set.



18
19
20
21
22
23
# File 'lib/collection_utils/set.rb', line 18

def initialize(values = [])
  @set = {}
  values.each do |value|
    insert(value)
  end
end

Instance Method Details

#&(set) ⇒ Set

Intersection of two sets and return the intersected set

> set1 = CollectionUtils::Set.new()

> set2 = CollectionUtils::Set.new()

> set3 = set1 & set2 #[2,3,4]

Examples:

Intesect two sets and return it.

Parameters:

  • set (Set)

    with which Intersection of the current set needs to be taken

Returns:

  • (Set)

    intersected set



113
114
115
116
# File 'lib/collection_utils/set.rb', line 113

def &(set)
  new_set = Set.new(get_values & set.get_values)
  return new_set
end

#+(set) ⇒ Set

Union of two sets and return the united set

> set1 = CollectionUtils::Set.new()

> set2 = CollectionUtils::Set.new()

> set3 = set1 + set2 #[1,2,3,4,5]

Examples:

Union two sets and return it.

Parameters:

  • set (Set)

    with which Union of the current set needs to be taken

Returns:

  • (Set)

    united set



126
127
128
129
# File 'lib/collection_utils/set.rb', line 126

def +(set)
  new_set = Set.new(get_values + set.get_values)
  return new_set
end

#-(set) ⇒ Set

Difference of two sets and return the subtracted set

> set1 = CollectionUtils::Set.new()

> set2 = CollectionUtils::Set.new()

> set3 = set1 - set2 #[1]

Examples:

subtracted two sets and return it.

Parameters:

  • set (Set)

    with which Difference of the current set needs to be taken

Returns:

  • (Set)

    subtracted set



139
140
141
# File 'lib/collection_utils/set.rb', line 139

def -(set)
  new_set = Set.new(get_values - set.get_values)
end

#check(val) ⇒ Boolean

checks whether an element is present or not. This is done in O(1) operations

> set = CollectionUtils::Set.new()

> set.check(4) #true

> set.check(5) #false

Examples:

check an element in set

Parameters:

  • val

    which has to be checked whether in set or not

Returns:

  • (Boolean)

    true if element is present, false if element is already present



50
51
52
53
# File 'lib/collection_utils/set.rb', line 50

def check(val)
  key, value = get_key_value(val)
  return !@set[key].nil?
end

#delete(val) ⇒ Object

deletes an element from the set. This is done in O(1) operations

> set = CollectionUtils::Set.new()

> set.delete(4) #set = [1,2,3]

> set.delete(5) #set = [1,2,3]

Examples:

check an element in set

Parameters:

  • val

    which has to be deleted



62
63
64
65
# File 'lib/collection_utils/set.rb', line 62

def delete(val)
  key, value = get_key_value(val)
  @set.delete(key)
end

#getObject

Get a random element from the set.

> set = CollectionUtils::Set.new()

> set.get # 3

> set.get # 4

> set.get # 3

Examples:

get an element from set

Returns:

  • a random element from set.



76
77
78
# File 'lib/collection_utils/set.rb', line 76

def get
  @set.values.sample
end

#get_valuesObject

Get all the values from a set.

> set = CollectionUtils::Set.new()

> set.get_values #[1,2,3,4]

Examples:

get all element from set

Returns:

  • all the elements from the set.



86
87
88
# File 'lib/collection_utils/set.rb', line 86

def get_values
  @set.values
end

#insert(val) ⇒ Boolean

Insert an element into set. This is done in O(1) operations

> set = CollectionUtils::Set.new()

> set.size #4

> set.insert(5)

> set.size #5

> set.insert(4)

> set.size #5

Examples:

Add element into set

Parameters:

  • val

    element that needs to be added

Returns:

  • (Boolean)

    true if element is added, false if element is already present



36
37
38
39
40
# File 'lib/collection_utils/set.rb', line 36

def insert(val)
  key, value = get_key_value(val)
  @set[key] = value; return true if @set[key].nil?
  return false
end

#is_empty?Boolean

Returns whether the set is empty. This is just a syntax_sugar for size == 0

Returns:

  • (Boolean)

    set’s emptiness



101
102
103
# File 'lib/collection_utils/set.rb', line 101

def is_empty?
  size == 0
end

#sizeInteger

> set = CollectionUtils::Set.new()

> set.size #4

Examples:

return size of set

Returns:

  • (Integer)

    size of the



94
95
96
# File 'lib/collection_utils/set.rb', line 94

def size
  @set.keys.size
end