-
Notifications
You must be signed in to change notification settings - Fork 544
Update Custom Set tests to follow standard, update API #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,149 +1,84 @@ | ||
use std::iter::{IntoIterator, FromIterator}; | ||
use std::slice; | ||
use std::option::Option; | ||
use std::vec::Vec; | ||
|
||
#[derive(Debug)] | ||
pub struct CustomSet<T> { | ||
elements: Vec<T> | ||
collection: Vec<T>, | ||
} | ||
|
||
/// Create an empty set. | ||
impl <T> CustomSet<T> where T: Eq + Clone { | ||
pub fn new() -> CustomSet<T> { | ||
CustomSet { elements: Vec::new() } | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.elements.len() | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
impl<T: Ord + Clone> PartialEq for CustomSet<T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.collection.iter().all(|x| other.contains(&x)) && | ||
other.collection.iter().all(|x| self.contains(&x)) | ||
} | ||
} | ||
|
||
pub fn contains(&self, needle: &T) -> bool { | ||
for element in self.elements.iter() { | ||
if needle == element { | ||
return true; | ||
} | ||
impl<T: Ord + Clone> CustomSet<T> { | ||
pub fn new(inputs: Vec<T>) -> CustomSet<T> { | ||
let mut s = CustomSet { collection: Vec::new() }; | ||
for input in inputs { | ||
s.add(input); | ||
} | ||
false | ||
s | ||
} | ||
|
||
pub fn insert(&mut self, element: T) -> bool { | ||
pub fn add(&mut self, element: T) { | ||
if !self.contains(&element) { | ||
self.elements.push(element); | ||
return true; | ||
} | ||
false | ||
} | ||
|
||
pub fn remove(&mut self, needle: &T) -> bool { | ||
let mut index: Option<usize> = None; | ||
for (i, element) in self.iter().enumerate() { | ||
if needle == element { | ||
index = Some(i); | ||
break; | ||
} | ||
} | ||
match index { | ||
Some(index) => { | ||
self.elements.remove(index); | ||
true | ||
} | ||
None => false | ||
} | ||
} | ||
|
||
pub fn difference(&self, other: &CustomSet<T>) -> CustomSet<T> { | ||
let mut difference: CustomSet<T> = CustomSet::new(); | ||
for element in self.elements.iter() { | ||
if !other.contains(element) { | ||
difference.insert(element.clone()); | ||
} | ||
self.collection.push(element) | ||
} | ||
difference | ||
} | ||
|
||
pub fn intersection(&self, other: &CustomSet<T>) -> CustomSet<T> { | ||
let mut intersection: CustomSet<T> = CustomSet::new(); | ||
for element in self.elements.iter() { | ||
if other.contains(element) { | ||
intersection.insert(element.clone()); | ||
} | ||
} | ||
intersection | ||
pub fn delete(&mut self, element: &T) { | ||
self.collection.retain(|x| x != element) | ||
} | ||
|
||
pub fn union(&self, other: &CustomSet<T>) -> CustomSet<T> { | ||
let mut union = CustomSet::new(); | ||
for element in self.elements.iter().chain(other.elements.iter()) { | ||
union.insert(element.clone()); | ||
} | ||
union | ||
pub fn contains(&self, other: &T) -> bool { | ||
self.collection.contains(other) | ||
} | ||
|
||
pub fn is_disjoint(&self, other: &CustomSet<T>) -> bool { | ||
for element in self.elements.iter() { | ||
if other.contains(element) { | ||
return false; | ||
} | ||
} | ||
true | ||
pub fn is_empty(&self) -> bool { | ||
self.collection.is_empty() | ||
} | ||
|
||
pub fn is_subset(&self, other: &CustomSet<T>) -> bool { | ||
for element in self.elements.iter() { | ||
if !other.contains(element) { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
|
||
pub fn is_superset(&self, other: &CustomSet<T>) -> bool { | ||
other.is_subset(self) | ||
pub fn size(&self) -> usize { | ||
self.collection.len() | ||
} | ||
|
||
pub fn clear(&mut self) { | ||
self.elements.clear(); | ||
pub fn is_subset(&self, other: &Self) -> bool { | ||
self.collection.iter().all(|x| other.contains(x)) | ||
} | ||
|
||
pub fn iter(&self) -> Iter<T>{ | ||
Iter { iter: self.elements.iter() } | ||
pub fn is_disjoint(&self, other: &Self) -> bool { | ||
!self.collection.iter().any(|x| other.contains(x)) | ||
} | ||
} | ||
|
||
pub struct Iter<'a, T: 'a> { | ||
iter: slice::Iter<'a, T> | ||
} | ||
|
||
impl<'a, T> IntoIterator for &'a CustomSet<T> where T: Eq + Clone { | ||
type Item = &'a T; | ||
type IntoIter = Iter<'a, T>; | ||
|
||
fn into_iter(self) -> Iter<'a, T> { | ||
self.iter() | ||
pub fn intersection(&self, other: &Self) -> CustomSet<T> { | ||
CustomSet::new(self.collection | ||
.iter() | ||
.cloned() | ||
.filter(|c| other.contains(c)) | ||
.collect()) | ||
} | ||
} | ||
|
||
impl<'a, T> Iterator for Iter<'a, T> { | ||
type Item = &'a T; | ||
|
||
fn next(&mut self) -> Option<&'a T> { | ||
self.iter.next() | ||
pub fn union(&self, other: &Self) -> CustomSet<T> { | ||
CustomSet::new(self.collection | ||
.iter() | ||
.cloned() | ||
.chain(other.collection.iter().cloned()) | ||
.collect()) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.iter.size_hint() | ||
pub fn difference(&self, other: &Self) -> CustomSet<T> { | ||
CustomSet::new(self.collection | ||
.iter() | ||
.cloned() | ||
.filter(|c| !other.contains(c)) | ||
.collect()) | ||
} | ||
} | ||
|
||
impl<T> FromIterator<T> for CustomSet<T> where T: Eq + Clone { | ||
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> CustomSet<T> { | ||
let mut set = CustomSet::new(); | ||
set.elements = Vec::from_iter(iterable); | ||
set | ||
pub fn symmetric_difference(&self, other: &Self) -> CustomSet<T> { | ||
CustomSet::new(self.difference(other) | ||
.collection | ||
.iter() | ||
.cloned() | ||
.chain(other.difference(self).collection.iter().cloned()) | ||
.collect()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand fully, but can
inputs
be borrowed rather than owned?I'm unsure because all its elements get added to
s
so there may be lifetime problems if it's just borrowed, but I've been surprised beforeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, lifetime problems everywhere. I think it might be interesting to have some of our exercises focus on exposing students to lifetimes. Is this a good candidate for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I know
anagram
did. In general I don't have a good sense of how much we want to show people at once.However, in this case it doesn't seem like we would be able to do it anyway, so I feel like I don't need to spend too much time thinking about this.