Created
July 14, 2012 14:12
-
-
Save siscia/3111539 to your computer and use it in GitHub Desktop.
Simple first approach to a DAG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns bayle.core | |
(require clojure.set)) | |
(defprotocol NODE | |
(change [this key new-value]) | |
(add-parent [this new-parent]) | |
(add-non-descendent [this new]) | |
(node? [this]) | |
(add-chile [this child])) | |
(defprotocol DAG | |
(add-node [this node]) | |
(remove-node [this node]) | |
(descendents [this node]) | |
(non-descendents [this node child])) | |
(defrecord Node [name table parents non-descendents child] | |
NODE | |
(node? [this] | |
true) | |
(change [this key new-value] | |
(assoc this key new-value)) | |
(add-parent [_ new-parent] | |
(Node. name table (clojure.set/union parents new-parent) non-descendents child)) | |
(add-non-descendent [_ new-non-des] | |
(Node. name table parents (clojure.set/union non-descendents new-non-des) child)) | |
(add-child [_ new-child] | |
(Node. name table parents non-descendents (clojure.set/union non-descendents new-child)))) | |
(defn new-node | |
[name & {:keys [table parents non-descendents child] :or {table {} | |
parents #{} | |
non-descendents #{} | |
child #{}}}] | |
(Node. name table parents non-descendents child)) | |
(extend-type clojure.lang.PersistentHashSet | |
DAG | |
(add-node [this node] ;; TODO CHECK EDGE | |
{:pre [(node? node)] | |
:post [(set? %)]} | |
(conj this node)) | |
(descendents [this node] | |
{:pre [(node? node)] | |
:post [(set? %)]} | |
(clojure.set/difference this (:non-descendents node))) | |
(non-descendents [this node child] | |
{:pre [(node? node) (node? child)] | |
:post [(set? %)]} | |
(clojure.set/difference this (clojure.set/union (map #(descendents this %) child))))) | |
(defn new-DAG [] | |
#{}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment