Skip to content

Prototype int_newtype and typed vec<K, V> - #19

Draft
devin-ai-integration[bot] wants to merge 5 commits into
masterfrom
devin/1784785488-int-newtypes
Draft

Prototype int_newtype and typed vec<K, V>#19
devin-ai-integration[bot] wants to merge 5 commits into
masterfrom
devin/1784785488-int-newtypes

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Prototype of strongly-typed integer newtypes for keeping separate index spaces as separate types, plus a key-typed contiguous vector, plus a retrofit of level_ancestor as a worked example.

src/newtype.hpp:

  • int_newtype<Tag, T = int> with:
    • implicit conversion from raw integers (per review discussion): from T itself at runtime, and from compile-time constants of any integral type (range-checked via a consteval constructor). Conversion back out (operator T) is explicit;
    • explicit converting constructor between any two newtypes — the inter-space wall is the invariant;
    • ring semantics closed within a type (0 and 1 are distinguished points): + - * / % & | ^ ~, shifts, ++/--, compound assignments, ==/<=>, iostream, std::hash. Raw ints embed implicitly as operands; other newtypes never do;
    • INT_NEWTYPE(name[, type]) macro to mint a fresh tag.
  • IntKey concept (IntNewtype or plain integral) + newtype_raw/newtype_underlying_t helpers, so structures can be templated on a key type with int as the default and zero migration cost.
  • vec<K, V>: a thin wrapper over std::vector<V> (contiguous) whose operator[]/at accept only K (or things implicitly convertible to it); size() returns the past-the-end key K; push_back/emplace_back return the inserted key; value iteration via begin()/end() and key iteration via keys().

src/level_ancestor.hpp retrofit:

  • Now template <IntKey Node = int> struct level_ancestor, with preorder indices in a private internal newtype space pre_t (nested tag): preorder: vec<pre_t, Node>, idx: vec<Node, pre_t>, heavyPar: vec<pre_t, pair<pre_t, pre_t>> (heavy parent, distance). Depth arguments/results (get_ancestor's k, dist) are pre_t too, but raw ints convert implicitly so untyped call sites are unaffected.
  • The compiler now enforces the node-id vs preorder-index distinction the old code tracked by convention (e.g. the old a = idx[a] reuse of one variable across both spaces is no longer possible).
  • level_ancestor<> with std::vector<int> is a drop-in for the old interface; new tests cover both int and newtype instantiations.

Tests (src/newtype.test.cpp) also include a CSR-graph example where node and edge_slot are distinct spaces (mixing them fails to compile, checked via static_assert), and a values-as-newtypes example (val_t = int32, prefix sums in sum_t = int64 with explicit widening).

Link to Devin session: https://app.devin.ai/sessions/7fff37ee1500445fb693de007d24a2eb
Requested by: @ecnerwala

Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
@ecnerwala ecnerwala self-assigned this Jul 23, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

devin-ai-integration Bot and others added 4 commits July 23, 2026 06:02
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant