Skip to content

Instantly share code, notes, and snippets.

@maxwillzq
Created April 4, 2020 03:15
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 maxwillzq/e7da9911abf9979f203b743f18e039e8 to your computer and use it in GitHub Desktop.
Save maxwillzq/e7da9911abf9979f203b743f18e039e8 to your computer and use it in GitHub Desktop.
int indexToLoc(const vector<int>& strides, vector<int> ids){
int result = 0;
for(int i = 0; i < strides.size(); i++) {
result += ids[i] * strides[i];
}
return result;
}
vector<int> locToIndex(const vector<int>& strides, int loc) {
vector<int> result;
for(int i = 0; i < strides.size(); i++) {
int tmp = loc / strides[i];
result.push_back(tmp);
loc = loc % strides[i];
}
return result;
}
int main() {
std::cout << "Hello World!\n";
vector<int> strides = {5, 1};
int loc = 13;
auto ids = locToIndex(strides, loc);
for(auto id: ids) {
std::cout <<id <<",";
}
std::cout <<"\n";
int new_loc = indexToLoc(strides, ids);
std::cout <<"loc is: " << new_loc << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment