Skip to content

Instantly share code, notes, and snippets.

@marty1885
Created March 7, 2019 14:08
Show Gist options
  • Save marty1885/9d5b7dd39d7f846c7d640ae4281cedac to your computer and use it in GitHub Desktop.
Save marty1885/9d5b7dd39d7f846c7d640ae4281cedac to your computer and use it in GitHub Desktop.
inline size_t unfoldIndex(const std::vector<size_t>& index, const std::vector<size_t>& shape) noexcept
{
size_t s = 0;
size_t v = 1;
assert(index.size() == shape.size());
for(int i=(int)index.size()-1;i>=0;i--) {
v *= (i==(int)index.size()-1?1:shape[i+1]);
s += index[i] * v;
}
return s;
}
//Example:
//a[16*321*321]
//a[unfoldIndex({1,1,1}, {16,321,321})]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment