init commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
target
|
||||
Generated
+75
@@ -0,0 +1,75 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "cs2430-project2"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.149"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "cs2430-project2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
@@ -0,0 +1,120 @@
|
||||
### 1. Modules and Imports
|
||||
|
||||
```rust
|
||||
mod multiset_operations;
|
||||
mod set_operations;
|
||||
|
||||
use multiset_operations::MultiSet;
|
||||
use set_operations::Set;
|
||||
```
|
||||
|
||||
- `mod`: Declares a module. The actual code for the modules `multiset_operations` and `set_operations` is not shown, but they should be either in the same file or in `multiset_operations.rs` and `set_operations.rs` files respectively.
|
||||
- `use`: Imports types or functions from a module into the current scope. Here, `MultiSet` and `Set` are imported.
|
||||
|
||||
### 2. Main Function
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
- `fn`: Keyword to define a new function.
|
||||
- `main`: The entry point of a Rust program.
|
||||
|
||||
### 3. Variable Declaration and Initialization
|
||||
|
||||
```rust
|
||||
let a = Set::from_vec(vec![true, false, true, false, true, false, true, false, true, false]);
|
||||
```
|
||||
|
||||
- `let`: Used for variable declaration.
|
||||
- `Set::from_vec`: Calls a associated function (similar to a static method in other languages) named `from_vec` of the `Set` struct.
|
||||
|
||||
### 4. Mutable Variable Declaration
|
||||
|
||||
```rust
|
||||
let mut a_mult = MultiSet::from_vec(vec![3, 2, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
```
|
||||
|
||||
- `mut`: Indicates that the variable is mutable, i.e., its value can be changed.
|
||||
|
||||
### 5. Array/Vector Indexing
|
||||
|
||||
```rust
|
||||
a_mult.elements[0] = 3;
|
||||
```
|
||||
|
||||
- Elements in a vector/array can be accessed using the index.
|
||||
|
||||
### 6. Printing to Console
|
||||
|
||||
```rust
|
||||
println!("Set A:");
|
||||
```
|
||||
|
||||
- `println!`: A macro (not a function) to print to the console with a newline at the end.
|
||||
|
||||
### 7. Method Calls on Structs
|
||||
|
||||
```rust
|
||||
let not_a = a.complement();
|
||||
```
|
||||
|
||||
- Methods are called on instances of structs using the dot notation.
|
||||
|
||||
### 8. Public Struct Declaration
|
||||
|
||||
```rust
|
||||
pub struct Set {
|
||||
pub elements: Vec<bool>,
|
||||
}
|
||||
```
|
||||
|
||||
- `pub`: A visibility modifier that makes the struct and its fields public.
|
||||
- `struct`: Keyword to define a new structure.
|
||||
- `Vec<bool>`: A vector of boolean values.
|
||||
|
||||
### 9. Implementing Methods for a Struct
|
||||
|
||||
```rust
|
||||
impl Set {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
- `impl`: Begins an implementation block for methods of a struct.
|
||||
|
||||
### 10. Function Definitions with Parameters
|
||||
|
||||
```rust
|
||||
pub fn union(&self, other: &Set) -> Set {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
- `&self`: A reference to the current instance of the struct (similar to `this` in other languages).
|
||||
- `other: &Set`: A reference to another `Set` instance.
|
||||
- `-> Set`: Indicates the return type of the function.
|
||||
|
||||
### 11. Iterating Over Collections
|
||||
|
||||
```rust
|
||||
for &element in &self.elements {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
- `for`: Begins a for loop.
|
||||
- `&element`: Pattern matching to destructure and get the value from the reference.
|
||||
|
||||
### 12. Printing with Format Specifiers
|
||||
|
||||
```rust
|
||||
println!("Sum of elements in A: {}", a_mult.sum());
|
||||
```
|
||||
|
||||
- `{}`: A placeholder that will be replaced by the value specified after the format string.
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
mod multiset_operations;
|
||||
mod set_operations;
|
||||
|
||||
use multiset_operations::MultiSet;
|
||||
use set_operations::Set;
|
||||
|
||||
fn main() {
|
||||
let a = Set::from_vec(vec![
|
||||
true, false, true, false, true, false, true, false, true, false,
|
||||
]);
|
||||
|
||||
let b = Set::from_vec(vec![
|
||||
false, true, false, true, false, true, false, true, false, true,
|
||||
]);
|
||||
|
||||
// Multisets, where each element (ie. element 0, 1, 2) has a count
|
||||
let a_mult = MultiSet::from_vec(vec![3, 2, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
let b_mult = MultiSet::from_vec(vec![0, 1, 4, 0, 0, 0, 0, 0, 0, 0]);
|
||||
|
||||
println!("Set A:");
|
||||
a.display();
|
||||
println!("Set B:");
|
||||
b.display();
|
||||
|
||||
// A and B sets together
|
||||
let a_union_b = a.union(&b);
|
||||
println!("A union B:");
|
||||
a_union_b.display();
|
||||
|
||||
// The common elements between sets A and B.
|
||||
let a_intersection_b = a.intersection(&b);
|
||||
println!("A intersection B:");
|
||||
a_intersection_b.display();
|
||||
|
||||
// Elements that are a part of set B are no longer a part of set A.
|
||||
let a_difference_b = a.difference(&b);
|
||||
println!("A difference B:");
|
||||
a_difference_b.display();
|
||||
|
||||
// Remove the common elements from set A and B.
|
||||
let a_symmetric_difference_b = a.symmetric_difference(&b);
|
||||
println!("A symmetric difference B:");
|
||||
a_symmetric_difference_b.display();
|
||||
|
||||
println!("MultiSet A:");
|
||||
a_mult.display();
|
||||
println!("MultiSet B:");
|
||||
b_mult.display();
|
||||
|
||||
// Union of both multisets A and B
|
||||
let a_union_b = a_mult.union(&b_mult);
|
||||
println!("A union B:");
|
||||
a_union_b.display();
|
||||
|
||||
// Common elements between multisets A and B
|
||||
let a_intersection_b = a_mult.intersection(&b_mult);
|
||||
println!("A intersection B:");
|
||||
a_intersection_b.display();
|
||||
|
||||
// Removing any common elements of set B from set A
|
||||
let a_difference_b = a_mult.difference(&b_mult);
|
||||
println!("A difference B:");
|
||||
a_difference_b.display();
|
||||
|
||||
println!("Sum of elements in A: {}", a_mult.sum());
|
||||
println!("Sum of elements in B: {}", b_mult.sum());
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
pub struct MultiSet {
|
||||
pub elements: Vec<usize>,
|
||||
}
|
||||
|
||||
impl MultiSet {
|
||||
pub fn from_vec(vec: Vec<usize>) -> Self {
|
||||
MultiSet { elements: vec }
|
||||
}
|
||||
|
||||
// Get the combination of values between the two multisets.
|
||||
pub fn union(&self, other: &MultiSet) -> MultiSet {
|
||||
MultiSet {
|
||||
elements: self
|
||||
.elements
|
||||
.iter()
|
||||
.zip(&other.elements)
|
||||
.map(|(&x, &y)| x + y)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
// Get the minimum count of an element between two multisets.
|
||||
pub fn intersection(&self, other: &MultiSet) -> MultiSet {
|
||||
MultiSet {
|
||||
elements: self
|
||||
.elements
|
||||
.iter()
|
||||
.zip(&other.elements)
|
||||
.map(|(&x, &y)| usize::min(x, y))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
// Subtract the count of the element in multiset B from multiset A.
|
||||
pub fn difference(&self, other: &MultiSet) -> MultiSet {
|
||||
MultiSet {
|
||||
elements: self
|
||||
.elements
|
||||
.iter()
|
||||
.zip(&other.elements)
|
||||
.map(|(&x, &y)| if x > y { x - y } else { 0 })
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
// Add the count of the elements between two multisets
|
||||
pub fn sum(&self) -> usize {
|
||||
self.elements.iter().sum()
|
||||
}
|
||||
|
||||
// List each element value and the count of that element.
|
||||
pub fn display(&self) {
|
||||
for (index, &count) in self.elements.iter().enumerate() {
|
||||
println!("Element {index}: Count {count}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
pub struct Set {
|
||||
pub elements: Vec<bool>,
|
||||
}
|
||||
|
||||
impl Set {
|
||||
pub fn from_vec(vec: Vec<bool>) -> Self {
|
||||
Self { elements: vec }
|
||||
}
|
||||
|
||||
// Combine sets A and B.
|
||||
pub fn union(&self, other: &Set) -> Set {
|
||||
Set {
|
||||
elements: self
|
||||
.elements
|
||||
.iter()
|
||||
.zip(&other.elements)
|
||||
.map(|(&x, &y)| x || y)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
// Get the shared values between sets A and B.
|
||||
pub fn intersection(&self, other: &Set) -> Set {
|
||||
Set {
|
||||
elements: self
|
||||
.elements
|
||||
.iter()
|
||||
.zip(&other.elements)
|
||||
.map(|(&x, &y)| x && y)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
// Determine the set where set A has no ocommon elements with set B.
|
||||
pub fn difference(&self, other: &Set) -> Set {
|
||||
Set {
|
||||
elements: self
|
||||
.elements
|
||||
.iter()
|
||||
.zip(&other.elements)
|
||||
.map(|(&x, &y)| x && !y)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the union of A diff B and B diff A between two sets.
|
||||
pub fn symmetric_difference(&self, other: &Set) -> Set {
|
||||
let a_minus_b = self.difference(other);
|
||||
let b_minus_a = other.difference(self);
|
||||
a_minus_b.union(&b_minus_a)
|
||||
}
|
||||
|
||||
// Format and display the set.
|
||||
pub fn display(&self) {
|
||||
for &element in &self.elements {
|
||||
print!("{}", if element { 1 } else { 0 })
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user