Skip to content

Instantly share code, notes, and snippets.

@AssortedFantasy
Created May 28, 2023 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AssortedFantasy/cf2d1ceed61a6829ae5e7eedf2e7ed33 to your computer and use it in GitHub Desktop.
Save AssortedFantasy/cf2d1ceed61a6829ae5e7eedf2e7ed33 to your computer and use it in GitHub Desktop.
Triangular Index Helpers
// Triangle indexes
// if you have a set of N items with indexes.
// 0,1,2,3, ... , N-1
// You want a linear index for every pair i,j, i != j.
// N Choose 2.
inline int triangleSize(int N) {
return N*(N-1)/2;
}
// You need
// i < j < N.
inline int triangleIndex(int N, int i, int j) {
return i*(2*N - i - 1)/2 + j - i - 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment