Initial implementation of the Graph type in rust, construction functions only
This commit is contained in:
parent
2be865181d
commit
0248a11285
1
rust/.envrc
Normal file
1
rust/.envrc
Normal file
@ -0,0 +1 @@
|
|||||||
|
use flake
|
1
rust/.gitignore
vendored
Normal file
1
rust/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
target/
|
70
rust/Cargo.lock
generated
Normal file
70
rust/Cargo.lock
generated
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitmaps"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2"
|
||||||
|
dependencies = [
|
||||||
|
"typenum",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ginger"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"im-rc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "im-rc"
|
||||||
|
version = "15.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "af1955a75fa080c677d3972822ec4bad316169ab1cfc6c257a942c2265dbe5fe"
|
||||||
|
dependencies = [
|
||||||
|
"bitmaps",
|
||||||
|
"rand_core",
|
||||||
|
"rand_xoshiro",
|
||||||
|
"sized-chunks",
|
||||||
|
"typenum",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_xoshiro"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sized-chunks"
|
||||||
|
version = "0.6.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e"
|
||||||
|
dependencies = [
|
||||||
|
"bitmaps",
|
||||||
|
"typenum",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "typenum"
|
||||||
|
version = "1.16.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "version_check"
|
||||||
|
version = "0.9.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
9
rust/Cargo.toml
Normal file
9
rust/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "ginger"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
im-rc = "15.1.0"
|
77
rust/flake.lock
Normal file
77
rust/flake.lock
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"naersk": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1671096816,
|
||||||
|
"narHash": "sha256-ezQCsNgmpUHdZANDCILm3RvtO1xH8uujk/+EqNvzIOg=",
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "naersk",
|
||||||
|
"rev": "d998160d6a076cfe8f9741e56aeec7e267e3e114",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-community",
|
||||||
|
"ref": "master",
|
||||||
|
"repo": "naersk",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1671788672,
|
||||||
|
"narHash": "sha256-tLkPxJuos3jki2f/TZdHn+NuMQAzN9s2E4QudylQLg0=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "2c74fcd6c5fc14a61de158fb796243543f46b217",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"id": "nixpkgs",
|
||||||
|
"type": "indirect"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs_2": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1671788672,
|
||||||
|
"narHash": "sha256-tLkPxJuos3jki2f/TZdHn+NuMQAzN9s2E4QudylQLg0=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "2c74fcd6c5fc14a61de158fb796243543f46b217",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixpkgs-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"naersk": "naersk",
|
||||||
|
"nixpkgs": "nixpkgs_2",
|
||||||
|
"utils": "utils"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"utils": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1667395993,
|
||||||
|
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
21
rust/flake.nix
Normal file
21
rust/flake.nix
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
naersk.url = "github:nix-community/naersk/master";
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||||
|
utils.url = "github:numtide/flake-utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs, utils, naersk }:
|
||||||
|
utils.lib.eachDefaultSystem (system:
|
||||||
|
let
|
||||||
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
naersk-lib = pkgs.callPackage naersk { };
|
||||||
|
in
|
||||||
|
{
|
||||||
|
defaultPackage = naersk-lib.buildPackage ./.;
|
||||||
|
devShell = with pkgs; mkShell {
|
||||||
|
buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy ];
|
||||||
|
RUST_SRC_PATH = rustPlatform.rustLibSrc;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
66
rust/src/graph.rs
Normal file
66
rust/src/graph.rs
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
use std::hash::Hash;
|
||||||
|
use im_rc::{HashMap,HashSet};
|
||||||
|
|
||||||
|
#[derive(Clone, Eq, Hash, PartialEq, Debug)]
|
||||||
|
pub enum OpenEdgeSource<E, V>{
|
||||||
|
Value(V),
|
||||||
|
Tuple(Vec<OpenEdge<E, V>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Eq, Hash, PartialEq, Debug)]
|
||||||
|
pub struct OpenEdge<E, V>{
|
||||||
|
value: E,
|
||||||
|
source: OpenEdgeSource<E, V>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Eq, Hash, PartialEq, Debug)]
|
||||||
|
pub struct Graph<E, V>
|
||||||
|
where
|
||||||
|
E: Hash + Eq + Clone,
|
||||||
|
V: Hash + Eq + Clone,
|
||||||
|
{
|
||||||
|
roots: HashMap<V, HashSet<OpenEdge<E, V>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E, V> Graph<E, V>
|
||||||
|
where
|
||||||
|
E: Hash + Eq + Clone,
|
||||||
|
V: Hash + Eq + Clone,
|
||||||
|
{
|
||||||
|
|
||||||
|
pub fn new() -> Graph<E, V> {
|
||||||
|
Graph{roots: HashMap::new()}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_value(edge_value: E, source_value: V) -> OpenEdge<E, V> {
|
||||||
|
OpenEdge{
|
||||||
|
value: edge_value,
|
||||||
|
source: OpenEdgeSource::Value(source_value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_tuple(edge_value: E, source_tuple: Vec<OpenEdge<E, V>>) -> OpenEdge<E, V> {
|
||||||
|
OpenEdge{
|
||||||
|
value: edge_value,
|
||||||
|
source: OpenEdgeSource::Tuple(source_tuple),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with(&self, root_value: V, open_edge: OpenEdge<E, V>) -> Self {
|
||||||
|
|
||||||
|
let new_roots = self.roots.alter(
|
||||||
|
|set_option: Option<HashSet<OpenEdge<E, V>>>| -> Option<HashSet<OpenEdge<E, V>>> {
|
||||||
|
match set_option {
|
||||||
|
None => Some(HashSet::unit(open_edge)),
|
||||||
|
Some(set) => Some(set.update(open_edge)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
root_value,
|
||||||
|
);
|
||||||
|
|
||||||
|
Graph{
|
||||||
|
roots: new_roots,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
3
rust/src/lib.rs
Normal file
3
rust/src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
mod graph;
|
||||||
|
|
||||||
|
pub use graph::Graph;
|
22
rust/src/main.rs
Normal file
22
rust/src/main.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
use ginger::Graph;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
let g = Graph::new().
|
||||||
|
with("foo", Graph::from_value(1, "bar"));
|
||||||
|
|
||||||
|
let g1 = g.
|
||||||
|
with("foo", Graph::from_value(1, "bar")).
|
||||||
|
with("foo", Graph::from_value(2, "baz"));
|
||||||
|
|
||||||
|
let g2 = g.
|
||||||
|
with("bar", Graph::from_tuple(100, vec![
|
||||||
|
Graph::from_value(20, "a"),
|
||||||
|
Graph::from_value(40, "b"),
|
||||||
|
Graph::from_value(60, "c"),
|
||||||
|
]));
|
||||||
|
|
||||||
|
dbg!(g1 == g2);
|
||||||
|
dbg!(&g1);
|
||||||
|
dbg!(&g2);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user