GithubHelp home page GithubHelp logo

dogonthesun / nway Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lyxell/nway

0.0 0.0 0.0 56 KB

Algorithms for n-way diffing and merging of files with a common ancestor

License: BSD 2-Clause "Simplified" License

C++ 92.08% TeX 6.33% Makefile 0.93% CMake 0.66%

nway's Introduction

n-way merge with common ancestor

nway

Algorithms to perform n-way diff and merge of files with a common ancestor, based on the 3-way diff approach described in A Formal Investigation of Diff3 by Khanna, Kunal and Pierce and the shortest edit script algorithm described in An O(ND) difference algorithm and its variations by Myers.

Distributed as a single-header C++17 library.

Examples

Merge strings

#include <cassert>
#include <iostream>
#include <string>

#include <nway.h>

using namespace std::string_literals;

int main(int argc, char** argv) {
    auto diff = nway::diff("hello world"s, {"hxxllo world"s,
                                            "hello wyyrld"s,
                                            "hello wyyrld!!"s,
                                            "a hello world"s});
    assert(!nway::has_conflict(diff));
    // prints "a hxxllo wyyrld!!"
    std::cout << nway::merge(diff) << std::endl;
    return 0;
}

Merge files

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

#include <nway.h>

using namespace std::string_literals;

int main(int argc, char** argv) {

    using file = std::vector<std::string>;

    file original = {
        "a",
        "b",
        "c",
        "d",
        "e",
    };

    // deletes third line
    file a = {
        "a",
        "b",
        "d",
        "e",
    };

    // changes fifth line
    file b = {
        "a",
        "b",
        "c",
        "d",
        "x",
    };

    // changes first line
    file c = {
        "x",
        "b",
        "c",
        "d",
        "e",
    };

    auto diff = nway::diff(original, {a, b, c});
    assert(!nway::has_conflict(diff));
    // prints
    // x
    // b
    // d
    // x
    for (auto line : nway::merge(diff)) {
        std::cout << line << std::endl;
    }
    return 0;
}

nway's People

Contributors

lyxell 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.