Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Jun 24, 2024
1 parent 2301b16 commit 93b18bb
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions exercises/practice/custom-set/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ pub impl CustomSetImpl<
return false;
}
let mut result = true;
let mut i = self.collection.len();
while i != 0 {
i -= 1;
if !other.contains(self.collection[i]) {
result = false;
break;
}
};
let mut i = 0;
while let Option::Some(val) = self
.collection
.get(i) {
if !other.contains(val.unbox()) {
result = false;
break;
}
i += 1;
};
result
}

Expand All @@ -91,14 +93,16 @@ pub impl CustomSetImpl<
to_compare = self;
};

let mut i = to_iterate.collection.len();
while i != 0 {
i -= 1;
if to_compare.contains(to_iterate.collection[i]) {
are_disjoint = false;
break;
}
};
let mut i = 0;
while let Option::Some(val) = to_iterate
.collection
.get(i) {
if to_compare.contains(val.unbox()) {
are_disjoint = false;
break;
}
i += 1;
};

are_disjoint
}
Expand All @@ -115,52 +119,54 @@ pub impl CustomSetImpl<
to_compare = self;
};

let mut i = to_iterate.collection.len();
while i != 0 {
i -= 1;
if to_compare.contains(to_iterate.collection[i]) {
collection.append(*to_iterate.collection[i]);
}
};
let mut i = 0;
while let Option::Some(val) = to_iterate
.collection
.get(i) {
let unboxed = val.unbox();
if to_compare.contains(unboxed) {
collection.append(*unboxed);
}
i += 1;
};

CustomSetImpl::<T>::new(@collection)
}

#[must_use]
fn union(self: @CustomSet<T>, other: @CustomSet<T>) -> CustomSet<T> {
let mut collection: Array<T> = array![];

let mut i = self.collection.len();
while i != 0 {
i -= 1;
collection.append(*self.collection[i]);
};
println!("self appended");
i = other.collection.len();
while i != 0 {
i -= 1;
collection.append(*other.collection[i]);
};
println!("other appended");
let mut i = 0;
while let Option::Some(val) = self
.collection
.get(i) {
collection.append(*val.unbox());
i += 1;
};
i = 0;
while let Option::Some(val) = other
.collection
.get(i) {
collection.append(*val.unbox());
i += 1;
};
CustomSetImpl::<T>::new(@collection)
}

#[must_use]
fn difference(self: @CustomSet<T>, other: @CustomSet<T>) -> CustomSet<T> {
let mut diff = CustomSetImpl::<T>::new(@array![]);

let mut collection: Array<T> = array![];
let mut i = 0;
while let Option::Some(val) = self
.collection
.get(i) {
let unboxed = val.unbox();
if !other.contains(unboxed) {
diff.add(unboxed.clone());
collection.append(unboxed.clone());
}
i += 1;
};

diff
CustomSetImpl::<T>::new(@collection)
}
}

Expand Down

0 comments on commit 93b18bb

Please sign in to comment.