Created
May 28, 2023 18:08
-
-
Save AssortedFantasy/cf2d1ceed61a6829ae5e7eedf2e7ed33 to your computer and use it in GitHub Desktop.
Triangular Index Helpers
This file contains 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
// 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