Over a million developers have joined DZone.

Implementing Low-Level Trie: Solving With C++

DZone's Guide to

Implementing Low-Level Trie: Solving With C++

Check out Ayende's nice walk through of creating a low level trie in C++, but first he offers up a warning of trying to do the same in Rust. Full source code is provided via GitHub.

· Web Dev Zone
Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

The low-level trie question has been a favorite question of mine for a while. It is simple in concept, but the limitations placed on it make it pretty hard to actually build. Previous posts in this series outlined the approach I had for solving this, but I always got caught up with something and didn’t get around to actually sitting down and resolving this completely.

As part of learning Rust, I decided to go ahead and implement this low-level trie using Rust. I failed, it was just too much babysitting by the compiler and having to fight it. I knew exactly what I wanted to do, but I kept having to jump through hoops to get it to it. Eventually, I just called it quits and decided to abandon the attempt to use Rust.

But I still want to do something out of my comfort zone, so I decided to run this exercise using C++. Now, I used to write quite a lot of C++ (along with VB, VBScript, and ASP classic). But that was in the late 90's, and very early 2000's. I heard it through the grapevine that someone kicked the C++ standard committee into high gear and started to actually improve the language.

The result was three evenings spent on building a low-level trie impl in C++, and it was quite a lot of fun. I’ll have another post about the actual details of the implementation, but in this post, I mostly wanted to talk about the experience of getting back to C++. And it is… strange.

On the one hand, because I’m so used to C# and have used C++ before, this is oh so comfortable. Like wearing an old set of gloves that you forgot that you even had.

On the other hand, I'd forgotten quite a lot of details about the language and the libraries, and they changed. My old C++ code would be newing up stuff and fighting to manage memory and very likely leaking like crazy. In this codebase? I don’t have a single new call throughout. And being able to do things like lambdas in C++ feels like magic.

I’ll admit that the codebase is heavily influenced by my Rust work. To start with, I’m using snake_case convention, and I found that I’m using a lot more std::pair that I would expect myself to use.

I would appreciate any code review on this, the core code is about 400 lines or so, and I’m mostly interested to know whatever I managed to write idiomatic modern C++, and if not, how this can be improved.

GitHub:  https://github.com/ayende/trie-cpp

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

standard ,c++ ,code ,rust ,modern

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}