Giter VIP home page Giter VIP logo

zunion's Introduction

zunion

pointer-chasing union-find in zig

Purpose

Union-Find is a critical subcomponent of many algorithms (like EGG). This is a reasonably efficient implementation under the assumption that pointer chasing is satisfactory and you don't need more constrained data types.

Installation

Copy-paste or git-subrepo or whatever. Also, ZIG HAS A PACKAGE MANAGER NOW!!! Use it with something like the following.

// build.zig.zon
.{
    .name = "foo",
    .version = "0.0.0",
    .dependencies = .{
        .zunion = .{
            .url = "https://github.com/hmusgrave/zunion/archive/9cf8401f66529b41b5cb2776dbf8cab6b4b7865c.tar.gz",
        },
    },

}
// build.zig
const zunion_pkg = b.dependency("zunion", .{
    .target = target,
    .optimize = optimize,
});
const zunion_mod = zunion_pkg.module("zunion");
lib.addModule("zunion", zunion_mod);
main_tests.addModule("zunion", zunion_mod);

Examples

test "it all works" {
    var allocator = std.testing.allocator;
    const U = DisjointSet(u32);

    // turn the value we want to store (42) into
    // a pointer to a set tracking that variable
    // and anything it might be connected to
    var a = try U.make(allocator, 42);
    defer allocator.destroy(a);

    // same for (314)
    var b = try U.make(allocator, 314);
    defer allocator.destroy(b);

    // those disjoint sets haven't been joined, so if we
    // examine the root nodes holding each of them we'll
    // find they're different
    try expectEqual(@as(u32, 42), a.find().value);
    try expectEqual(@as(u32, 314), b.find().value);

    // modify a and b (and any ancestors between them
    // and their roots) to have the same root
    a.join(b)

    // since those nodes have been joined, they have the
    // same parent value (it's deterministically 42 at this
    // point, but that's an implementation detail, and the
    // salient detail is that they're equal).
    try expectEqual(a.find().value, b.find().value);
}

zunion's People

Contributors

hmusgrave avatar

Watchers

 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.