Code Monkey home page Code Monkey logo

swiftdag's Introduction

SwiftDAG

Warning: API IS NOT STABLE, still evaluating different approaches.

How a DAG can be used to represent data structure relations

DAG is the acronym for Directed Acyclic Graph which is basically a graph without closed loops made of 'single way' directed connections. It has a lot of pratical applications, usually related to shortest-path or topological ordering.

While the basic algorithms and the same SwiftDAG is modelled to provide a simple interface to represent data structure relations in memory, where each Node (or vertex) is an object and each Edge (or connection) is a relation between nodes.

Example:

class UFrame: Node {
    var size: CGRect?
    var origin: CGPoint?
}

class UView: Node {
    lazy var subviews = EdgeArray<UView>(parent: self)
    lazy var frame = Edge<UFrame>(parent: self)
    var backgroundColor = UIColor.black
}

UView is connected to UFrame via the Edge variable frame and to multiple subviews via the EdgeArray variable subviews.

The connections are created ar run-time.

Example, explicit:

    let view = UView()
    let frame = UFrame()
    view.frame.connect(to: frame)

Example, operator:

    let view = UView()
    let frame = UFrame()
    view.frame <= frame

Pratically speaking the base Node class contains the list of connetions and the Edge classes helps to link/unlink the Nodes

By storing the conections from a Node to its siblings it's possible to traverse the tree to create a Topological Order

    print(view.topologicalOrder())

    [UFrame(uuid: 140418024708768), UView(uuid: 140418022602544)]

Documentation TODO:

  1. try/catch: exceptions are used to avoid closed loops and integrity checks
  2. behind the scens, inner links and outer links
  3. direct graph manipulation
  4. simple queries
  5. serialization/deserialization
  6. PRO users: sourcery based API

Things to TODO:

  1. Integration with CocoaPods / Carthage
  2. JSON serialization/deserialization using the hash
  3. more examples and more tests
  4. travis integration

How to generate Examples interface

./Sourcery --templates SwiftDAG/Templates --sources . --output SwiftDAGTests --watch --verbose

swiftdag's People

Contributors

nferruzzi avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.