Prototype int_newtype and typed vec<K, V> - #19
Draft
devin-ai-integration[bot] wants to merge 5 commits into
Draft
Conversation
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_ancestoras a worked example.src/newtype.hpp:int_newtype<Tag, T = int>with:Titself at runtime, and from compile-time constants of any integral type (range-checked via aconstevalconstructor). Conversion back out (operator T) is explicit;+ - * / % & | ^ ~, 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.IntKeyconcept (IntNewtypeor plain integral) +newtype_raw/newtype_underlying_thelpers, so structures can be templated on a key type withintas the default and zero migration cost.vec<K, V>: a thin wrapper overstd::vector<V>(contiguous) whoseoperator[]/ataccept onlyK(or things implicitly convertible to it);size()returns the past-the-end keyK;push_back/emplace_backreturn the inserted key; value iteration viabegin()/end()and key iteration viakeys().src/level_ancestor.hppretrofit:template <IntKey Node = int> struct level_ancestor, with preorder indices in a private internal newtype spacepre_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'sk,dist) arepre_ttoo, but raw ints convert implicitly so untyped call sites are unaffected.a = idx[a]reuse of one variable across both spaces is no longer possible).level_ancestor<>withstd::vector<int>is a drop-in for the old interface; new tests cover bothintand newtype instantiations.Tests (
src/newtype.test.cpp) also include a CSR-graph example wherenodeandedge_slotare distinct spaces (mixing them fails to compile, checked viastatic_assert), and a values-as-newtypes example (val_t= int32, prefix sums insum_t= int64 with explicit widening).Link to Devin session: https://app.devin.ai/sessions/7fff37ee1500445fb693de007d24a2eb
Requested by: @ecnerwala