Coder Social home page Coder Social logo

Comments (2)

pjebs avatar pjebs commented on August 17, 2024

Since writing this, have you discovered any issues with your implementation suggestion?

from go-radix.

FrankReh avatar FrankReh commented on August 17, 2024

No problems. The whole design requires the slice of edges remain in sort order so it makes sense to take advantage of that when adding an edge. And there are lots of examples of go code using the results of sort.Search for inserting and for using copy to create the space needed for the insertion.

Of course it also has to work when the list being added to is empty and it does that too. So no problems.

For those to whom speed really matters, I'll point out that the three places addEdge is called can be further optimized. I've gone with removing the addEdge method completely and using a new insertEdge method that takes the insertion index too because even the binary search in addEdge was unnecessary - in the first usage, the edge list had already been searched and found not to contain the edge - hence the call to addEdge, so I used the result of the getEdge binary search; in the second call to addEdge, the list is known to be empty so I passed an index of 0 to insertEdge, and in the third case, where we know the list of edges is size one, I added the code to test whether the new label was less than or greater than the existing label so could pass an insert index of 0 or 1.

Finally, I later found examples in the go std library where they expanded the call to sort.Search. It is a surprisingly small, easy piece of code, once one has it (one 'for' loop, no recursion), and it can be nice not having to create a closure to perform the binary search when the input is just a slice of bytes and a byte. I don't know if the go compiler will ever be smart enough to optimize that kind of thing away on its own.

from go-radix.

Related Issues (10)

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.