diff --git a/rust/.envrc b/rust/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/rust/.envrc @@ -0,0 +1 @@ +use flake diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 0000000..c4da8e7 --- /dev/null +++ b/rust/Cargo.lock @@ -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" diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..ded0d37 --- /dev/null +++ b/rust/Cargo.toml @@ -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" diff --git a/rust/flake.lock b/rust/flake.lock new file mode 100644 index 0000000..5fd17d6 --- /dev/null +++ b/rust/flake.lock @@ -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 +} diff --git a/rust/flake.nix b/rust/flake.nix new file mode 100644 index 0000000..2f907a6 --- /dev/null +++ b/rust/flake.nix @@ -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; + }; + }); +} diff --git a/rust/src/graph.rs b/rust/src/graph.rs new file mode 100644 index 0000000..eda2206 --- /dev/null +++ b/rust/src/graph.rs @@ -0,0 +1,66 @@ +use std::hash::Hash; +use im_rc::{HashMap,HashSet}; + +#[derive(Clone, Eq, Hash, PartialEq, Debug)] +pub enum OpenEdgeSource{ + Value(V), + Tuple(Vec>), +} + +#[derive(Clone, Eq, Hash, PartialEq, Debug)] +pub struct OpenEdge{ + value: E, + source: OpenEdgeSource, +} + +#[derive(Clone, Eq, Hash, PartialEq, Debug)] +pub struct Graph +where + E: Hash + Eq + Clone, + V: Hash + Eq + Clone, +{ + roots: HashMap>>, +} + +impl Graph +where + E: Hash + Eq + Clone, + V: Hash + Eq + Clone, +{ + + pub fn new() -> Graph { + Graph{roots: HashMap::new()} + } + + pub fn from_value(edge_value: E, source_value: V) -> OpenEdge { + OpenEdge{ + value: edge_value, + source: OpenEdgeSource::Value(source_value), + } + } + + pub fn from_tuple(edge_value: E, source_tuple: Vec>) -> OpenEdge { + OpenEdge{ + value: edge_value, + source: OpenEdgeSource::Tuple(source_tuple), + } + } + + pub fn with(&self, root_value: V, open_edge: OpenEdge) -> Self { + + let new_roots = self.roots.alter( + |set_option: Option>>| -> Option>> { + match set_option { + None => Some(HashSet::unit(open_edge)), + Some(set) => Some(set.update(open_edge)), + } + }, + root_value, + ); + + Graph{ + roots: new_roots, + } + } +} + diff --git a/rust/src/lib.rs b/rust/src/lib.rs new file mode 100644 index 0000000..39a113d --- /dev/null +++ b/rust/src/lib.rs @@ -0,0 +1,3 @@ +mod graph; + +pub use graph::Graph; diff --git a/rust/src/main.rs b/rust/src/main.rs new file mode 100644 index 0000000..bfb62af --- /dev/null +++ b/rust/src/main.rs @@ -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); +}