Skip to content

Commit

Permalink
Renaming Size() to Cardinality() to be more in-line with true set nam…
Browse files Browse the repository at this point in the history
…ing conventions
  • Loading branch information
rcaraveo committed Oct 18, 2013
1 parent da1fb3b commit fb91abd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (set Set) Union(other Set) Set {
func (set Set) Intersect(other Set) Set {
intersection := NewSet()
// loop over smaller set
if set.Size() < other.Size() {
if set.Cardinality() < other.Cardinality() {
for elem := range set {
if other.Contains(elem) {
intersection.Add(elem)
Expand Down Expand Up @@ -149,16 +149,16 @@ func (set Set) Remove(i interface{}) {
delete(set, i)
}

// Size returns the how many items are currently in the set.
func (set Set) Size() int {
// Cardinality returns the how many items are currently in the set.
func (set Set) Cardinality() int {
return len(set)
}

// Equal determines if two sets are equal to each other.
// If they both are the same size and have the same items they are considered equal.
// Order of items is not relevent for sets to be equal.
func (set Set) Equal(other Set) bool {
if set.Size() != other.Size() {
if set.Cardinality() != other.Cardinality() {
return false
}
for elem := range set {
Expand Down
40 changes: 20 additions & 20 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ func makeSet(ints []int) Set {
func Test_NewSet(t *testing.T) {
a := NewSet()

if a.Size() != 0 {
if a.Cardinality() != 0 {
t.Error("NewSet should start out as an empty set")
}
}

func Test_AddSet(t *testing.T) {
a := makeSet([]int{1, 2, 3})

if a.Size() != 3 {
if a.Cardinality() != 3 {
t.Error("AddSet does not have a size of 3 even though 3 items were added to a new set")
}
}

func Test_AddSetNoDuplicate(t *testing.T) {
a := makeSet([]int{7, 5, 3, 7})

if a.Size() != 3 {
if a.Cardinality() != 3 {
t.Error("AddSetNoDuplicate set should have 3 elements since 7 is a duplicate")
}

Expand All @@ -71,7 +71,7 @@ func Test_RemoveSet(t *testing.T) {

a.Remove(3)

if a.Size() != 2 {
if a.Cardinality() != 2 {
t.Error("RemoveSet should only have 2 items in the set")
}

Expand All @@ -82,7 +82,7 @@ func Test_RemoveSet(t *testing.T) {
a.Remove(6)
a.Remove(1)

if a.Size() != 0 {
if a.Cardinality() != 0 {
t.Error("RemoveSet should be an empty set after removing 6 and 1")
}
}
Expand Down Expand Up @@ -128,39 +128,39 @@ func Test_ClearSet(t *testing.T) {

a.Clear()

if a.Size() != 0 {
if a.Cardinality() != 0 {
t.Error("ClearSet should be an empty set")
}
}

func Test_SizeSet(t *testing.T) {
func Test_CardinalitySet(t *testing.T) {
a := NewSet()

if a.Size() != 0 {
if a.Cardinality() != 0 {
t.Error("set should be an empty set")
}

a.Add(1)

if a.Size() != 1 {
if a.Cardinality() != 1 {
t.Error("set should have a size of 1")
}

a.Remove(1)

if a.Size() != 0 {
if a.Cardinality() != 0 {
t.Error("set should be an empty set")
}

a.Add(9)

if a.Size() != 1 {
if a.Cardinality() != 1 {
t.Error("set should have a size of 1")
}

a.Clear()

if a.Size() != 0 {
if a.Cardinality() != 0 {
t.Error("set should have a size of 1")
}
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func Test_SetUnion(t *testing.T) {

c := a.Union(b)

if c.Size() != 5 {
if c.Cardinality() != 5 {
t.Error("set c is unioned with an empty set and therefore should have 5 elements in it")
}

Expand All @@ -230,7 +230,7 @@ func Test_SetUnion(t *testing.T) {
d.Add(0)

e := c.Union(d)
if e.Size() != 8 {
if e.Cardinality() != 8 {
t.Error("set e should should have 8 elements in it after being unioned with set c to d")
}

Expand All @@ -239,7 +239,7 @@ func Test_SetUnion(t *testing.T) {
f.Add(3)

g := f.Union(e)
if g.Size() != 8 {
if g.Cardinality() != 8 {
t.Error("set g should still ahve 8 elements in it after being unioned with set f that has duplicates")
}
}
Expand All @@ -257,7 +257,7 @@ func Test_SetIntersect(t *testing.T) {

c := a.Intersect(b)

if c.Size() != 0 {
if c.Cardinality() != 0 {
t.Error("set c should be the empty set because there is no common items to intersect")
}

Expand All @@ -266,7 +266,7 @@ func Test_SetIntersect(t *testing.T) {

d := a.Intersect(b)

if !(d.Size() == 1 && d.Contains(10)) {
if !(d.Cardinality() == 1 && d.Contains(10)) {
t.Error("set d should have a size of 1 and contain the item 10")
}
}
Expand All @@ -287,7 +287,7 @@ func Test_SetDifference(t *testing.T) {

c := a.Difference(b)

if !(c.Size() == 1 && c.Contains(2)) {
if !(c.Cardinality() == 1 && c.Contains(2)) {
t.Error("the difference of set a to b is the set of 1 item: 2")
}
}
Expand All @@ -309,7 +309,7 @@ func Test_SetSymmetricDifference(t *testing.T) {

c := a.SymmetricDifference(b)

if !(c.Size() == 6 && c.Contains(2) && c.Contains(45) && c.Contains(4) && c.Contains(5) && c.Contains(6) && c.Contains(99)) {
if !(c.Cardinality() == 6 && c.Contains(2) && c.Contains(45) && c.Contains(4) && c.Contains(5) && c.Contains(6) && c.Contains(99)) {
t.Error("the symmetric difference of set a to b is the set of 6 items: 2, 45, 4, 5, 6, 99")
}
}
Expand Down Expand Up @@ -409,7 +409,7 @@ func Test_Example(t *testing.T) {
fmt.Println(scienceClasses.Intersect(requiredClasses)) //Set{Biology}

//How many bonus classes do you offer?
fmt.Println(bonusClasses.Size()) //2
fmt.Println(bonusClasses.Cardinality()) //2

//Do you have the following classes? Welding, Automotive and English?
fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English"))
Expand Down

0 comments on commit fb91abd

Please sign in to comment.