It's much nicer to code with now
This commit is contained in:
97
Set.gd
97
Set.gd
@@ -1,4 +1,5 @@
|
||||
class_name Set extends RefCounted
|
||||
## Adapted from: https://gist.github.com/NoodleSushi/3eb9cc08eb1d1c369bef308262804f1e
|
||||
## A custom implementation of a set data structure in GDScript.
|
||||
##
|
||||
## Usage Example:
|
||||
@@ -48,47 +49,47 @@ func duplicate() -> Set:
|
||||
# O(min(|self|, |set|))
|
||||
## Returns a new [Set] containing elements that are present in the current set
|
||||
## but not in the provided [param set].
|
||||
func difference(set: Set) -> Set:
|
||||
if set.hash() == self.hash():
|
||||
func difference(param: Set) -> Set:
|
||||
if param.hash() == self.hash():
|
||||
return Set.new()
|
||||
var out := self.duplicate()
|
||||
if set.size() > self.size():
|
||||
if param.size() > self.size():
|
||||
for element in self.elements():
|
||||
if set.has(element):
|
||||
if param.has(element):
|
||||
out.erase(element)
|
||||
else:
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
out.erase(element)
|
||||
return out
|
||||
|
||||
# O(min(|self|, |set|))
|
||||
## Modifies the current [Set] to contain elements present in the current set\
|
||||
## but not in the provided [param set].
|
||||
func difference_update(set: Set) -> void:
|
||||
if set.hash() == self.hash():
|
||||
func difference_update(param: Set) -> void:
|
||||
if param.hash() == self.hash():
|
||||
self.clear()
|
||||
return
|
||||
if set.size() > self.size():
|
||||
if param.size() > self.size():
|
||||
for element in self.elements():
|
||||
if set.has(element):
|
||||
if param.has(element):
|
||||
self.erase(element)
|
||||
else:
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
self.erase(element)
|
||||
|
||||
# O(min(|self|, |set|))
|
||||
## Returns a new [Set] containing elements common to both the current set and
|
||||
## the provided [param set].
|
||||
func intersection(set: Set) -> Set:
|
||||
if set.hash() == self.hash():
|
||||
func intersection(param: Set) -> Set:
|
||||
if param.hash() == self.hash():
|
||||
return duplicate()
|
||||
var out := Set.new()
|
||||
if set.size() > self.size():
|
||||
if param.size() > self.size():
|
||||
for element in self.elements():
|
||||
if set.has(element):
|
||||
if param.has(element):
|
||||
out.add(element)
|
||||
else:
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
if self.has(element):
|
||||
out.add(element)
|
||||
return out
|
||||
@@ -96,16 +97,16 @@ func intersection(set: Set) -> Set:
|
||||
# O(min(|self|, |set|))
|
||||
## Modifies the current set to contain only elements common to both the
|
||||
## current set and the provided [param set].
|
||||
func intersection_update(set: Set) -> void:
|
||||
if set.hash() == self.hash():
|
||||
func intersection_update(param: Set) -> void:
|
||||
if param.hash() == self.hash():
|
||||
return
|
||||
var out := Set.new()
|
||||
if set.size() > self.size():
|
||||
if param.size() > self.size():
|
||||
for element in self.elements():
|
||||
if set.has(element):
|
||||
if param.has(element):
|
||||
out.add(element)
|
||||
else:
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
if self.has(element):
|
||||
out.add(element)
|
||||
self.clear()
|
||||
@@ -114,13 +115,13 @@ func intersection_update(set: Set) -> void:
|
||||
# O([1, min(|self|, |set|)])
|
||||
## Returns [code]true[/code] if the sets have no elements in common;
|
||||
## otherwise, returns [code]false[/code].
|
||||
func isdisjoint(set: Set) -> bool:
|
||||
if set.size() > self.size():
|
||||
func isdisjoint(param: Set) -> bool:
|
||||
if param.size() > self.size():
|
||||
for element in self.elements():
|
||||
if set.has(element):
|
||||
if param.has(element):
|
||||
return false
|
||||
else:
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
if self.has(element):
|
||||
return false
|
||||
return true
|
||||
@@ -128,23 +129,23 @@ func isdisjoint(set: Set) -> bool:
|
||||
# O([1, |self|])
|
||||
## Returns [code]true[/code] if every element of the current set is present
|
||||
## in the provided [param set]; otherwise, returns [code]false[/code].
|
||||
func issubset(set: Set) -> bool:
|
||||
if set.size() < self.size():
|
||||
func issubset(param: Set) -> bool:
|
||||
if param.size() < self.size():
|
||||
return false
|
||||
else:
|
||||
for element in self.elements():
|
||||
if !set.has(element):
|
||||
if !param.has(element):
|
||||
return false
|
||||
return true
|
||||
|
||||
# O([1, |set|])
|
||||
## Returns [code]true[/code] if every element of the provided [param set]
|
||||
## is present in the current set; otherwise, returns [code]false[/code].
|
||||
func issuperset(set: Set) -> bool:
|
||||
if self.size() < set.size():
|
||||
func issuperset(param: Set) -> bool:
|
||||
if self.size() < param.size():
|
||||
return false
|
||||
else:
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
if !self.has(element):
|
||||
return false
|
||||
return true
|
||||
@@ -164,18 +165,18 @@ func erase(element: Variant) -> void:
|
||||
# O(min(|self|, |set|))
|
||||
## Returns a new [Set] containing elements that are present in either
|
||||
## the current set or the provided [param set], but not in both.
|
||||
func symmetric_difference(set: Set) -> Set:
|
||||
if set.hash() == self.hash():
|
||||
func symmetric_difference(param: Set) -> Set:
|
||||
if param.hash() == self.hash():
|
||||
return Set.new()
|
||||
if set.size() > self.size():
|
||||
var out := set.duplicate()
|
||||
if param.size() > self.size():
|
||||
var out := param.duplicate()
|
||||
for element in self.elements():
|
||||
if set.has(element):
|
||||
if param.has(element):
|
||||
out.remove(element)
|
||||
return out
|
||||
else:
|
||||
var out := self.duplicate()
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
if self.has(element):
|
||||
out.remove(element)
|
||||
return out
|
||||
@@ -183,9 +184,9 @@ func symmetric_difference(set: Set) -> Set:
|
||||
# O(min(|self|, |set|))
|
||||
## Modifies the current set to contain elements that are present in either
|
||||
## the current set or the provided [param set], but not in both.
|
||||
func symmetric_difference_update(set: Set) -> void:
|
||||
if set.size() > self.size():
|
||||
var temp := set.duplicate()
|
||||
func symmetric_difference_update(param: Set) -> void:
|
||||
if param.size() > self.size():
|
||||
var temp := param.duplicate()
|
||||
for element in self.elements():
|
||||
if temp.has(element):
|
||||
temp.erase(element)
|
||||
@@ -193,7 +194,7 @@ func symmetric_difference_update(set: Set) -> void:
|
||||
temp.add(element)
|
||||
self._set = temp._set
|
||||
else:
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
if self.has(element):
|
||||
self.erase(element)
|
||||
else:
|
||||
@@ -202,30 +203,30 @@ func symmetric_difference_update(set: Set) -> void:
|
||||
# O(min(|self|, |set|))
|
||||
## Returns a new [Set] containing all elements from both the current
|
||||
## set and the provided [param set].
|
||||
func union(set: Set) -> Set:
|
||||
if set.size() > self.size():
|
||||
var out := set.duplicate()
|
||||
func union(param: Set) -> Set:
|
||||
if param.size() > self.size():
|
||||
var out := param.duplicate()
|
||||
for element in self.elements():
|
||||
out.add(element)
|
||||
return out
|
||||
else:
|
||||
var out := self.duplicate()
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
out.add(element)
|
||||
return out
|
||||
|
||||
# O(min(|self|, |set|))
|
||||
## Modifies the current set to contain all elements from both the current set
|
||||
## and the provided [param set].
|
||||
func update(set: Set) -> void:
|
||||
if set.size() > self.size():
|
||||
var temp := set.duplicate()
|
||||
func update(param: Set) -> void:
|
||||
if param.size() > self.size():
|
||||
var temp := param.duplicate()
|
||||
for element in self.elements():
|
||||
temp.add(element)
|
||||
self._dict = temp._dict
|
||||
else:
|
||||
var temp := self.duplicate()
|
||||
for element in set.elements():
|
||||
for element in param.elements():
|
||||
temp.add(element)
|
||||
self._dict = temp.dict
|
||||
|
||||
|
||||
Reference in New Issue
Block a user