Swiftpack.co - Package - fireblade-engine/graph

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

Github

link
Stars: 0

Used By

Total: 0

Releases

Node<Content> - 2020-03-13 16:12:45

  • Adds generic content constraint to Node
  • Refines update implementation
  • Cleanups
  • Swift 5.1+

- 2019-09-15 10:08:49