vyo / graph

A lightweight, fast and easy to use directed acyclic graph (DAG) implementation in Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fireblade Graph

Build Status license swift version platforms

This is a lightweight, fast and easy to use directed acyclic graph (DAG) implementation in Swift.
It is developed and maintained as part of the Fireblade Game Engine project.

πŸš€ Getting Started

These instructions will get you a copy of the project up and running on your local machine and provide a code example.

πŸ“‹ Prerequisites

πŸ’» Installing

Fireblade Graph is available for all platforms that support Swift 5 and higher and the Swift Package Manager (SPM).

Extend the following lines in your Package.swift file or use it to create a new project.

// swift-tools-version:5.2

import PackageDescription

let package = Package(
    name: "YourPackageName",
    dependencies: [
    .package(url: "https://github.com/fireblade-engine/graph.git", from: "1.3.0")
    ],
    targets: [
        .target(
            name: "YourTargetName",
            dependencies: ["FirebladeGraph"])
    ]
)

πŸ“ Code Example

The Node is the core element of the package.
It is representing a node in a directed acyclic graph (DAG) and holds a (weak) reference to it's parent node and references to it's child nodes.

To create a graph create a node and add children.

let rootNode = Node<Void>()

let child1 = Node<Void>()
let child2 = Node<Void>()

rootNode.addChild(child1)
rootNode.addChild(child2)

A DAG implementation starts to shine when you can add functionality or behavior to it's nodes.
This is acchieved by subclassing Node and implementing the desired behavior in it's .updateFromParent() method as well as setting the node's generic Content constraint.

class StringNode: Node<String> {

	let content: String
	
	func myFunc() { ... } // or functions

    override open func updateFromParent() {
        super.updateFromParent()
    
        // ... and do fancy stuff here ...
    }
}

let node = StringNode("Hello World!")

To traverse through the graph from root to leave nodes call .update() on the root node of the graph.

let rootNode = Node<Void>()

// ... build up your graph here ...

rootNode.update()

🏷️ Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

✍️ Authors

See also the list of contributors who participated in this project.

πŸ” License

This project is licensed under the MIT License - see the LICENSE file for details

About

A lightweight, fast and easy to use directed acyclic graph (DAG) implementation in Swift.

License:MIT License


Languages

Language:Swift 96.9%Language:Makefile 3.1%