Class: CollectionUtils::Set
- Inherits:
-
Object
- Object
- CollectionUtils::Set
- Defined in:
- lib/collection_utils/set.rb
Instance Method Summary collapse
-
#&(set) ⇒ Set
Intersection of two sets and return the intersected set.
-
#+(set) ⇒ Set
Union of two sets and return the united set.
-
#-(set) ⇒ Set
Difference of two sets and return the subtracted set.
-
#check(val) ⇒ Boolean
checks whether an element is present or not.
-
#delete(val) ⇒ Object
deletes an element from the set.
-
#get ⇒ Object
Get a random element from the set.
-
#get_values ⇒ Object
Get all the values from a set.
-
#initialize(values = []) ⇒ Set
constructor
A new instance of Set.
-
#insert(val) ⇒ Boolean
Insert an element into set.
-
#is_empty? ⇒ Boolean
Returns whether the set is empty.
-
#size ⇒ Integer
> set = CollectionUtils::Set.new() => set.size #4.
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]
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]
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]
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
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]
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 |
#get ⇒ Object
Get a random element from the set.
> set = CollectionUtils::Set.new()
> set.get # 3
> set.get # 4
> set.get # 3
76 77 78 |
# File 'lib/collection_utils/set.rb', line 76 def get @set.values.sample end |
#get_values ⇒ Object
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
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
101 102 103 |
# File 'lib/collection_utils/set.rb', line 101 def is_empty? size == 0 end |
#size ⇒ Integer
> set = CollectionUtils::Set.new()
> set.size #4
94 95 96 |
# File 'lib/collection_utils/set.rb', line 94 def size @set.keys.size end |