Compare commits

..

11 Commits

20 changed files with 450 additions and 19 deletions

View File

@ -6,9 +6,11 @@
<sourceFolder url="file://$MODULE_DIR$/duplicates/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/duplicates/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/str_sort/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/str_sort/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/sparse_vector/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/sparse_vector/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/container_type/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/duplicates/target" /> <excludeFolder url="file://$MODULE_DIR$/duplicates/target" />
<excludeFolder url="file://$MODULE_DIR$/str_sort/target" /> <excludeFolder url="file://$MODULE_DIR$/str_sort/target" />
<excludeFolder url="file://$MODULE_DIR$/sparse_vector/target" /> <excludeFolder url="file://$MODULE_DIR$/sparse_vector/target" />
<excludeFolder url="file://$MODULE_DIR$/container_type/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

View File

@ -1,3 +1,7 @@
# Rust-Programming # Rust-Programming
Repository hosting code of the excercises made during class Repository hosting code of the excercises made during class.
The projects are not sorted chronologically.
The entire repository and all of its content are licensed under GPLv2 or later with exceptions to specific pieces of code written by the lecturer without licencse notice.

View File

@ -0,0 +1,8 @@
[package]
name = "container_type"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

130
container_type/src/main.rs Normal file
View File

@ -0,0 +1,130 @@
use std::{vec::{IntoIter}, ops::Index, ops::IndexMut, println};
/**
* _ _ _ _
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
* |___/
* ____ __ __ _
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
* |___/
* Licensed under the GPLv2 License, Version 2.0 (the "License");
* Copyright (c) Sven Vogel
*/
/// A vector based implementation of an associative map.
/// This strucutre maps a given key to a single value.
/// The type achives this by storing every pair of key/value pairs
/// in a single vector.
/// Thus for looking up a value takes a linear amount of time: O(n) in the worst case.
/// Adding a new value is a constant time operation since the map is not sorted in any
/// particular way.
/// Note that it is not possible to insert a new value with the same key. Instead the old value
/// associated with the already existing key will be replaced with the new value.
/// # Example
/// ```rust ignore
/// let mut map = HashishMap::new();
///
/// map.insert("abc", 99);
///
/// map[&"abc"] += 1;
/// ```
pub struct HashishMap<K, V> where K: Eq {
vec: Vec<(K, V)>
}
impl<K, V> HashishMap<K, V> where K: Eq {
/// Create a new empty instance
pub fn new() -> Self {
Self { vec: vec![] }
}
/// retrieve a reference to value associated with the specified key
/// if no such key exists in the map [`Option::None`] is returned
pub fn get(&self, key: &K) -> Option<&V> {
return match self.vec.iter().find(|(k, _)| *k == *key) {
Some((_, v)) => Some(v),
_ => None
}
}
/// retrieve a mutable reference to value associated with the specified key
/// if no such key exists in the map [`Option::None`] is returned
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
return match self.vec.iter_mut().find(|(k, _)| *k == *key) {
Some((_, v)) => Some(v),
_ => None
}
}
/// insert a new value at the specified key.
/// Overrides the existing value if the key already exists.
/// The overriden value is discarded.
pub fn insert(&mut self, key: K, value: V) {
if let Some(val) = self.get_mut(&key) {
*val = value;
} else {
self.vec.push((key, value));
}
}
/// Removes the key/value pair with the specified key from the map and return the value of the pair.
/// If no such pair can be found, [`Option::None`] is retuned
pub fn remove(&mut self, key: &K) -> Option<V> {
// find a key matching the parameter and its according index.
// remove the item at the found index from the vector and return its value
return match self.vec.iter().enumerate().find(|(_, (k, _))| *k == *key) {
Some((idx, _)) => {
Some(self.vec.remove(idx).1)
},
_ => None
}
}
}
impl<K,V> Index<K> for HashishMap<K, V> where K: Eq {
type Output=V;
fn index(&self, index: K) -> &Self::Output {
self.get(&index).unwrap()
}
}
impl<K,V> IndexMut<K> for HashishMap<K, V> where K: Eq {
fn index_mut(&mut self, index: K) -> &mut Self::Output {
self.get_mut(&index).unwrap()
}
}
impl<K, V> IntoIterator for HashishMap<K, V> where K: Eq {
type Item=(K, V);
type IntoIter = IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}
fn main() {
let mut guter_stoff = HashishMap::<&str, u64>::new();
guter_stoff.insert("helmut", 0xCafeBabe);
guter_stoff.insert("dieter", 0xDeadbeef);
guter_stoff.insert("eisele", 0xBaadF00d);
guter_stoff.insert("bohlen", 0xFaceFeed);
guter_stoff[&"bohlen"] = 0xBaadB015;
guter_stoff.remove(&"helmut");
for (k, v) in guter_stoff {
println!("({k}, {:x})", v);
}
}

5
duplicates/README.md Normal file
View File

@ -0,0 +1,5 @@
# Duplicates
This project implements functionality to store either a floating point number or a character inside of an enumeration. A function can then find any duplicates in an array of these enumerations.
Licensed under GPLv2 or later, same as the entire repository

View File

@ -14,9 +14,7 @@
* Licensed under the GPLv2 License, Version 2.0 (the "License"); * Licensed under the GPLv2 License, Version 2.0 (the "License");
* Copyright (c) Sven Vogel * Copyright (c) Sven Vogel
*/ */
use crate::FloatOrChar::{Char, Float}; use crate::FloatOrChar::{Char, Float};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug; use std::fmt::Debug;
use std::usize; use std::usize;

8
fibonacci/README.md Normal file
View File

@ -0,0 +1,8 @@
# Fibonacci
This project implements two version of the famous fibonacci sequence:
* an iterative one
* and a recursive one
Licensed under GPLv2 or later, same as the entire repository

View File

@ -25,11 +25,11 @@ fn fib_rec(x: u128) -> u128 {
} }
/// iterative variant of the fibonacci function /// iterative variant of the fibonacci function
fn fib_loop(x: u128) -> u128 { fn fib_iter(x: u128) -> u128 {
let mut sum = 0; let mut sum = 0;
let mut sum2 = 1; let mut sum2 = 1;
for x in 0..(x - 1) { for _ in 0..(x - 1) {
let t = sum; let t = sum;
sum = sum2; sum = sum2;
sum2 = t + sum; sum2 = t + sum;
@ -38,5 +38,7 @@ fn fib_loop(x: u128) -> u128 {
} }
fn main() { fn main() {
println!("{}", fib_loop(5)); const VALUE: u128 = 23;
println!("fibonacci iterative of {VALUE}: {}", fib_iter(VALUE));
println!("fibonacci recursive of {VALUE}: {}", fib_rec(VALUE));
} }

8
line/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "line"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

4
line/README.md Normal file
View File

@ -0,0 +1,4 @@
# Shapes and lines
Implements some traits provided by the lecturer regarding the calculation of various distance to the origin function for various shapes.
Licensed under GPLv2 or later, same as the entire repository

96
line/src/main.rs Normal file
View File

@ -0,0 +1,96 @@
/**
* _ _ _ _
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
* |___/
* ____ __ __ _
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
* |___/
* Licensed under the GPLv2 License, Version 2.0 (the "License");
* Copyright (c) Sven Vogel
*/
use std::cmp::PartialOrd;
use std::ops::{Add, Div, Mul, Sub};
trait Calculate:
Mul<Output = Self> + Add<Output = Self> + Sub<Output = Self> + Div<Output = Self> + Copy
{
}
impl<T> Calculate for T where
T: Mul<Output = Self> + Add<Output = Self> + Sub<Output = Self> + Div<Output = Self> + Copy
{
}
struct Point<T: Mul<Output = T> + Add<Output = T> + Copy> {
x: T,
y: T,
}
#[allow(dead_code)]
impl<T: Mul<Output = T> + Add<Output = T> + Copy> Point<T> {
fn squared_dist_to_0(&self) -> T {
self.x * self.x + self.y * self.y
}
}
trait MeasureDistanceTo0<T: Calculate> {
fn squared_dist_to_0(&self) -> T;
}
struct Line<T: Calculate> {
p: Point<T>,
n: Point<T>,
}
impl<T: Calculate + Default> MeasureDistanceTo0<T> for Line<T> {
fn squared_dist_to_0(&self) -> T {
let len = self.n.x * self.n.x + self.n.y * self.n.y;
let normalized = Point {
x: self.n.x / len,
y: self.n.y / len,
};
normalized.x * self.p.x + normalized.y * self.p.y
}
}
impl<T: Calculate + Default> Line<T> {
pub fn new() -> Self {
Self {
p: Point {
x: Default::default(),
y: Default::default(),
},
n: Point {
x: Default::default(),
y: Default::default(),
},
}
}
}
fn longest_dist_to_0<T>(p1: Line<T>, p2: Line<T>) -> T
where
T: Calculate + Default + PartialOrd,
{
let d1 = p1.squared_dist_to_0();
let d2 = p2.squared_dist_to_0();
if d1 > d2 {
d1
} else {
d2
}
}
fn main() {
let l0: Line<f64> = Line::new();
let l1: Line<f64> = Line::new();
println!("{:?}", longest_dist_to_0(l0, l1));
}

85
sparse_vector/README.md Normal file
View File

@ -0,0 +1,85 @@
# Sparse Vector Implementations
This repository aims at comparing various implementations of sparse vectors.
## What is a sparse vector?
A sparse vector is a vector in which most of its elements are zero.
That makes is easier to store because the many zero elements must not be stored.
Though this comes a the cost that we may need to decide between memory saving and computation time.
## Implementations overview
* Hashmap
* Index Array
* Compressed Index Array
* Binary Heap
### Index Array
> **NOTE**
>
> Due to poor choice of names and lazyness the implementation can only be found in the branch `compressed_indices_2`
We can omit all zero elements by storing an index array alongside all non zero values. Each value will be associated with an index in from the index array. This model is only efficient in memory size when the amount of zero elements is at least 50%. Since I used `usize` to store the indices, which is equal to a `u64` in 64-bit architectures, The required memory is:
```
mem(N) = non_zero_elements * (8 Bytes + 8 Bytes)
```
One significant downside is the cost of finding each corresponding entry when performing calculations such as the dot product. For this I used a binary search which gives a nive speedup.
### Hashmap Implementation
> **NOTE**
>
> Due to poor choice of names and lazyness the implementation can only be found in the branch `hashmap`
This implementation uses a hashmap to associate a value with its corresponding index in the vectors column. In Theory this should be as efficient in memory size as the previous array index method.
But in comparision this method requires signifacantly more memory since a hashmap allocates more memory than it can fill in order to reduce collisions.
It has one significant benefit, that being speed in calculations. Looking up values in a hashmap is generally faster than performing a binary seach. Also inserting and deleting is an O(1) operation.
> NOTE
>
> Two implementations of the dot product can be found:
>
> One implemented with a simple loop and one with a binary search. From testing I can say, that the simple loop variant is significanty faster than the crude binary search.
### Compressed Index Array
In order to reduce the size required to store the indices of each value we can compress them by only storing the relative offset to the previous value:
| Uncompressed Index | 0 | 7 | 13 | 33 | 45 | 47 | 48 | 57 | ... | 34567 |
| -------------------- | --- | --- | ---- | ---- | ---- | ---- | ---- | ---- | ----- | ------- |
| Compressed Index | 0 | 7 | 6 | 20 | 12 | 2 | 1 | 9 | ... | 23 |
This yields smaller values. Thus we can savely reduce the bandwidth of available bits to store.
In this implementation I reduced to size from 64 to 16 bit. This makes memory usage a lot smaller, but computation gets a lot heavier, since all values have to be decompressed on the fly. A possible improvement would be to cache uncompressed values. May be worth investigating futher.
### Binary Heap
Implementation can be found in the main branch.
The binary heap has the advantage of being fast with inserting, removing and looking up values in logarithmic time.
We use indices again to sort the values of the vector into to binary heap.
## Comparision
The following values were achieved by using a randomly initialized vector with a length of 10^10 elements from which 2% were non zero. The dot product implementation was single threaded and run in release mode on hyperthreaded intel hardware.
| Implementation | Size on Heap (GB) | Runtime of dot product (s) |
| :----------------------- | ------------------- | ---------------------------- |
| Naive | 80 | N/A |
| Index Array | 3.6 | 6.254261896 |
| Hashmap | 5.4 | 0.732189927 |
| Compressed Index Array | 2.0 | > 120 |
| Binary Heap | 1.3 | 2.089960966 |
Licensed under GPLv2 or later, same as the entire repository

View File

@ -1,51 +1,75 @@
use std::collections::HashMap; /**
use std::ops::{Add, Mul, Sub}; * _ _ _ _
use std::thread; * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
* |___/
* ____ __ __ _
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
* |___/
* Licensed under the GPLv2 License, Version 2.0 (the "License");
* Copyright (c) Sven Vogel
*/
use std::collections::BTreeMap;
use std::time::Instant; use std::time::Instant;
use bytesize::ByteSize; use bytesize::ByteSize;
use futures::executor::block_on;
use rand::Rng; use rand::Rng;
use futures::future::{join_all};
use jemalloc_ctl::{stats, epoch}; use jemalloc_ctl::{stats, epoch};
// we use a custom allocator for tracking heap allocations
#[global_allocator] #[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
/// Only stores more efficiently when at least 50% of all elements are zeros /// Wrapper struct around a BinaryTreeMap that stores the non zero elements of a vector by using the indices
/// as keys in the tree.
pub struct SparseVec { pub struct SparseVec {
data: HashMap<usize, f64> map: BTreeMap<usize, f64>
} }
impl SparseVec { impl SparseVec {
/// Compute the dot product of two vectors
pub fn dot(&self, other: &SparseVec) -> f64 { pub fn dot(&self, other: &SparseVec) -> f64 {
let mut sum = 0.0; let mut sum = 0.0;
for (k, v) in self.data.iter() { for (k, v) in self.map.iter() {
sum += v * other.data.get(k).unwrap_or(&0.0); sum += v * other.map.get(k).unwrap_or(&0.0);
} }
sum sum
} }
/// Create a new SparseVec with a theoretical size of `elements`. `non_null`is the ration of non zero elements
/// in the sparse vector. A value of 0.0 means that all elements are zero.
pub fn new(elements: usize, non_null: f64) -> Self { pub fn new(elements: usize, non_null: f64) -> Self {
// calculate the number of non-zero elements
let non_zero_elements = (elements as f64 * non_null) as usize; let non_zero_elements = (elements as f64 * non_null) as usize;
let mut data = HashMap::with_capacity(non_zero_elements); // create the map
let mut map = BTreeMap::new();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
// generate some random values
for i in 0..non_zero_elements { for i in 0..non_zero_elements {
// generate a random index that continuesly increases
let idx = i as f32 / non_zero_elements as f32 * (elements as f32 - 4.0) + rng.gen_range(0.0..3.0); let idx = i as f32 / non_zero_elements as f32 * (elements as f32 - 4.0) + rng.gen_range(0.0..3.0);
data.insert(idx as usize, 0.5);
map.insert(idx as usize, 0.5);
} }
Self { Self {
data map
} }
} }
} }
// rudimentary macro for timing a block of code
macro_rules! time { macro_rules! time {
($name:literal, $block:expr) => {{ ($name:literal, $block:expr) => {{
let start = Instant::now(); let start = Instant::now();
@ -70,7 +94,7 @@ fn main() {
println!("Estimated size on heap: {}", ByteSize::b((non_zero_elements * heap_element_size) as u64)); println!("Estimated size on heap: {}", ByteSize::b((non_zero_elements * heap_element_size) as u64));
println!("Size on stack: {} B", std::mem::size_of::<SparseVec>()); println!("Size on stack: {} B", std::mem::size_of::<SparseVec>());
let mut vec: SparseVec; let vec: SparseVec;
time!("Sparse vector creation", { time!("Sparse vector creation", {
// generate a vector // generate a vector

8
str_sort/README.md Normal file
View File

@ -0,0 +1,8 @@
# String sorting
This repository contains a set of functions to sort the characters of a string.
Various different functions are available:
* sort the ascii characters only (unsafe but efficient)
* sort the UTF-8 characters (requires memory allocation)
* sort the UTF-8 characters (more complex implementation but efficient)
Licensed under GPLv2 or later, same as the entire repository

8
threads/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "threads"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
threads/res/1.txt Normal file
View File

@ -0,0 +1 @@
af uafi spdh has gh kajs

1
threads/res/2.txt Normal file
View File

@ -0,0 +1 @@
tj ah ldsh lkjhasl jkasl

33
threads/src/main.rs Normal file
View File

@ -0,0 +1,33 @@
use std::{thread, sync::mpsc};
fn main() {
let (path_send, path_recv) = mpsc::channel();
let (count_send, count_recv) = mpsc::channel();
let handle = thread::spawn(move || {
for path in path_recv.iter() {
if let Ok(source) = std::fs::read_to_string(&path) {
let words = source.split_whitespace().filter(|ws| !ws.is_empty()).count();
count_send.send(format!("path: {} words: {words}", &path)).unwrap();
} else {
drop(count_send);
break;
}
}
});
loop {
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).expect("unable to read from stdin");
if let Err(_) = path_send.send(String::from(buf.trim())) {
break;
}
}
count_recv.try_iter().for_each(|c| {
println!("{c}");
});
handle.join().unwrap();
}

View File

@ -0,0 +1,5 @@
# Tuple Arithmetic
This project contains some implementations of arithmetic operators for pairs of floating point numbers.
Note: no trait implementation is provided since at the time of writing this project trait were out of scope for the lesson.
Licensed under GPLv2 or later, same as the entire repository

View File

@ -28,6 +28,7 @@ fn merge_tuple(a: (f64, f64), b: (f64, f64), f: fn(a: f64, b: f64) -> f64) -> (f
(f(a.0, b.0), f(a.1, b.1)) (f(a.0, b.0), f(a.1, b.1))
} }
#[allow(dead_code)]
fn add_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) { fn add_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) {
merge_tuple(a, b, |a, b| a + b) merge_tuple(a, b, |a, b| a + b)
} }