Skip to content

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 1 commit into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 51 additions & 116 deletions exercises/custom-set/example.rs
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> {
Copy link
Member

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 before

Copy link
Contributor Author

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?

Copy link
Member

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.

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())
}
}
Loading