5 releases
0.4.4 | Apr 30, 2021 |
---|---|
0.4.3 | Apr 26, 2021 |
0.4.2 | Apr 22, 2021 |
0.4.1 | Apr 22, 2021 |
0.4.0 | Apr 22, 2021 |
#1874 in Data structures
57KB
1.5K
SLoC
FlowArena
A HashMap
managed graph model with the concept of ownership.
Components
The flow_arena
package consists of
- a flow data model representation
trait Flow
andstruct FlowArena
- a node representation
trait Node
andstruct FlowNode
- variants like
GraphNode
andGraphArena
.
Motivation
FlowArena
grants you the ability to:
- represent a tree / graph data model in a memory-safe way
- use the concept of "ownership" to switch between the tree form and the graph form, along with:
- the ability to move / delete nodes recursively in a controlled way - if node A is owned by node B, then B's removal will trigger A's removal
- the ability to visit and traverse the data model in two different ways, namely the tree-ish way and the graph-ish way
In fact, it all started with Rust's ownership rules...
Arena
When it comes to relation driven data models, it's easy to make Rustaceans headache - just try and write a safe doubly-linked list. The concept of Arena
, therefore, is raised to implement a simple and memory-safe representation of such models. ^1
Consider a tree model. Basically it's a group of nodes where each node can point to some other nodes. How do we denote this relation? Well, we give every node a unique mark which claims its reference. We used to use pointers to cleverly mark this uniqueness with the help of memory addresses; but due to Rust's ownership rule, this approach is verbose to implement. So, why not mark this uniqueness by ourselves, with indices or ids?
The idea of the Arena
is simple: use a Vec<Node>
or a HashMap<Id, Node<Id>>
to contain all the nodes, where each node contains a collection of indices / ids, e.g. Vec<Id>
, as well as its own data. We can mark a node with its index / id, thus we can designate a root node; to traverse, just recursively get a node and traverse its "node marker collection".
Flow
FlowArena
represents a directed graph with the idea of Arena
. More than that, it's also able to represent the "flow model".
Flow model is different from graph or tree model alone because it provides both ownership (tree-ish) and linkage (graph-ish) features. Suppose you are removing a node from a tree, the children of it will automatically be removed, which is an implication of the "ownership" of a node to its children. In contrast, a graph node's removal will not cause the surrounding nodes to be removed.
Using the concept of ownership in the current graph-ish model, we yield a new kind of data model that can freely switch its behaviour between the two modes. We call such a data model a "flow model", which clearly indicates the functionality of FlowArena
.
Important: there are some extra concepts in FlowArena
compared to Arena
:
- nodes have not only children(
Vec<Id>
), but also a optional parent (Option<Id>
)- from tree-ish prospective, parent implies ownership,
None
means root; and children is only extra notation for possible ownership - from graph-ish prospective, children implies one-way edge
- note that a "pure link" means linkage without ownership
- however, "ownership" implies "linkage": if A has parent B, then B must have A as one of its children
- from tree-ish prospective, parent implies ownership,
- any node can have its sub tree or sub graph, given that no external linkage exist
- all the nodes owned recursively by the node are called "sub-nodes" of the node
- the node is called "tree node" or "graph node", respectively
- the nodes with no parent are called
orphan
s which is similar to a group of "root" nodes.
Usage
- directly use
struct FlowNode<Id>
and thestruct FlowArena<Id, Entity>
to represent a flow model - impl
trait Node<Id>
andtrait Flow
- see Trait Implementation for reference - when called upon,
use
the corresponding trait
Triat Implementation
FlowBase
: provides basic node-reflection abiliy; no checkFlowCheck
: checks the Flow's properties and see whether they holdFlowMap
: provides hashmap functionalityFlowLink
: provides ability to link nodes; graph-ishFlowDevote
: provides ability to devote / own nodes; tree-ishFlowDock
: provides ability to cut (undock) and copy (snap) a flow from a node and paste it to another node (dock)FlowShift
: provides ability to move around in flow withDirection
Flow
: checks all the traits are implemented
Related App
The FlowArena
is serving as the underlying data model of flow.er, a notebook, mindmap, todo-list and agenda app.
Dependencies
~165KB