Git Product home page Git Product logo

isbsrn's Introduction

ISBSRN - I Should Be Studying Right Now

But I needed to write something to parse a file of brackets for syntax highlighting.

There are probably millions of implementations of this, millions of which are better than mine. So be it, because C++ can be fun to write in.

Usage

open_node indicates an opening bracket. It adds a child to the current bracket and recursively starts again.

closed_node indicates a closing bracket. It marks the current node as closed, and returns to the parent.

Both return a pointer to a parse_node which indicates the current state of the parsing tree.

Code to parse a list of booleans giving open or close:

int main () {
    parse_node *current = new parse_node((parse_node *)0);
    
    std::vector<bool> bracks = {1, 0, 1, 0};
    
    for (bool b : bracks) {
        if (b) current = current->open_node();
        else current = current->close_node();
    }
}

Speed

This algorithm is O(n) for both parsing and deletion.

Bracket count Parse speed (μs) Delete speed (μs)
4 1.609 0.699
8 1.999 0.843
16 3.473 1.265
32 6.085 1.99
64 11.734 3.469
128 22.007 6.35
256 42.594 12.221
512 86.098 24.334
1024 167.722 48.455
2048 336.097 100.87
4096 670.882 199.087
8192 1342.11 389.083
16384 2774.77 755.241
32768 5545.65 1531.56

103μs = 1ms, 106μs = 1s

plot image

Example

Parsing ((())()(()

tree image

By hand, we can also get the answer of

(
    (
        (
        )
    )
    (
    )
    (
    	(
        )


isbsrn's People

Contributors

pseudophpt avatar

Watchers

James Cloos 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.