Skip to content

Automated Pretty Printing for STL-like Containers in H5CPP

🛠️ Why not just print?

Debugging C++ is an art and often demands the setup of tools like valgrind, cachegrind, or gdb. These tools are powerful, but sometimes all you need is a quick look at what's inside your container.

Enter: H5CPP’s feature detection idiom-based pretty-printing system for STL-like containers.

Inspired by Walter Brown’s WG21 N4436 paper, we offer a mechanism that allows you to simply:

std::cout << stl_like_object;
````

Where `stl_like_object` can be any type that:

* provides `.begin()` and `.end()` (like vectors, lists, maps)
* offers stack/queue-like interfaces (`.top()`, `.pop()`, `.size()`)
* or even composite/ragged containers like `vector<vector<T>>`, `tuple<...>`, etc.

## 🔍 What it does

The current implementation supports:

* **Recursive pretty-printing**
* **Line width control** (via `H5CPP_CONSOLE_WIDTH`)
* **In-line visualization** of arbitrarily nested structures

This will eventually replace/enhance the H5CPP persistence layer with general I/O capabilities.

---

## 📦 Example Output

Here's what `./pprint-test` prints to `stdout`:

```text
LISTS/VECTORS/SETS:
---------------------------------------------------------------------
   array<string,7>:[xSs,wc,gu,Ssi,Sx,pzb,OY]
            vector:[CDi,PUs,zpf,Hm,teO,XG,bu,QZs]
             deque:[256,233,23,89,128,268,69,278,130]
              list:[95,284,24,124,49,40,200,108,281,251,57, ...]
      forward_list:[147,76,81,193,44]
               set:[V,f,szy,v]
     unordered_set:[2.59,1.86,2.93,1.78,2.43,2.04,1.69]
          multiset:[3,5,12,21,23,28,30,30]
unordered_multiset:[gZ,rb,Dt,Q,Ark,dW,Ez,wmE,GwF]

And yes, it continues with:

  • Adaptors: stack, queue, priority_queue
  • Associative Containers: map, multimap, unordered_map, ...
  • Ragged Arrays: like vector<vector<string>>
  • Tuples and Pairs: including nested structures

Here’s a teaser for ragged and tuple structures:

vector<vector<string>>:[[pkwZZ,lBqsR,cmKt,PDjaS,Zj],[Nr,jj,xe,uC,bixzV],[uBAU,pXCa,fZEH,FIAIO]]
pair<int,string>:{4:upgdAdbvIB}
tuple<string,int,float,short>:<NHCmzhVVXJ,8,2.01756,7>

🧪 Run it yourself

g++ -I./include -o pprint-test.o -std=c++17 -DFMT_HEADER_ONLY -c pprint-test.cpp
g++ pprint-test.o -lhdf5 -lz -ldl -lm -o pprint-test
./pprint-test

You can set the line width by defining H5CPP_CONSOLE_WIDTH (e.g. -DH5CPP_CONSOLE_WIDTH=10).

📁 Code snippet

#include <h5cpp/H5Uall.hpp>
#include <h5cpp/H5cout.hpp>
#include <utils/types.hpp>

std::cout << mock::data<vector<vector<string>>>::get(2, 5, 3, 7) << "\n";

🔮 What’s Next?

If you’d like to bring this up to the level of Julia’s pretty print system, get in touch! The data generators that support arbitrary C++ type generation are part of the larger h5rnd project — a Prüfer sequence-based HDF5 dataset generator.