10BC0 Adds thin C++ wrapper by daniel-j-h · Pull Request #65 · tinygraph/tinygraph · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@daniel-j-h
Copy link
Member

This changeset adds a thin C++ header-only wrapper on top of our ABI and API stable C library.

Usage

std::vector<std::uint32_t> sources = {0, 1, 2};
std::vector<std::uint32_t> targets = {1, 2, 3};

auto graph = tg::graph{sources, targets, tg::edges_are_sorted};

There's potential to make the ergonomics still a bit nicer e.g. with iterators or similar.

The stability guarantees will not apply to this header-only wrapper - instead it's just convenience for users.

@daniel-j-h
Copy link
Member Author

I've added a graph builder for convenience, usage is as follows

auto graph = tg::builder()
  .reserve(3)
  .add_edge(0,1)
  .add_edge(1,2)
  .add_edge(2,3)
  .build()

once the graph is built it clears and shrinks the builder vectors so that the user does not accidentally have 2x memory around for the edges.

@daniel-j-h
Copy link
Member Author
daniel-j-h commented Apr 10, 2025

I have added support for the dijkstra shortest path interface; usage is as follows for now

#include <cstdint>
#include <cstdio>

#include <tinygraph/tinygraph.hpp>


int main() {
  const std::vector<std::uint32_t> sources = {0, 1, 2};
  const std::vector<std::uint32_t> targets = {1, 2, 3};
  const std::vector<std::uint16_t> weights = {1, 1, 1};

  const auto graph = tg::graph{sources, targets, tg::edges_are_sorted};

  auto dijkstra = tg::dijkstra{graph, weights};

  if (dijkstra.shortest_path(0, 3)) {
    printf("distance=%ju\n", (uintmax_t)dijkstra.get_distance());

    std::vector<std::uint32_t> path;

    if (dijkstra.get_path(path)) {
      printf("path=");

      for (const auto n : path) {
        printf("%ju ", (uintmax_t)n);
      }

      printf("\n");
    }
  }
}

I thought about adding a graph.shortest_path(s, t) function but that hides the fact that the dijkstra context allocates graph and weight dependant data structures such as heap, bitset, etc. Having it separate makes the API a bit more low level but exposes more control over what you need and when you need it.

We need to think about this before we merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0