fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int wrap_orig(int index, int max) {
  5. if (index < 0)
  6. return max - 1;
  7. else if (index >= max)
  8. return 0;
  9. else
  10. return index;
  11. }
  12.  
  13. size_t wrap_new(size_t index, size_t max, int delta) {
  14. if (delta >= 0)
  15. {
  16. unsigned absDelta = static_cast<unsigned>(delta);
  17. if (absDelta >= max)
  18. return 0;
  19. else if (index >= max - absDelta)
  20. return 0;
  21. return index + absDelta;
  22. }
  23. size_t absDelta = static_cast<size_t>(-static_cast<ptrdiff_t>(delta));
  24. if (index >= absDelta)
  25. return index - absDelta;
  26. else if (max == 0)
  27. return 0;
  28. return max - 1;
  29. }
  30.  
  31. int main() {
  32. cout << wrap_orig(10 + -2, 0) << "\n";
  33. cout << wrap_new(10, 0, -2) << "\n";
  34. cout << "\n";
  35. cout << wrap_orig(10 + -2, 2) << "\n";
  36. cout << wrap_new(10, 2, -2) << "\n";
  37. cout << "\n";
  38. cout << wrap_orig(10 + -2, 10) << "\n";
  39. cout << wrap_new(10, 10, -2) << "\n";
  40. cout << "\n";
  41. cout << wrap_orig(10 + -2, 15) << "\n";
  42. cout << wrap_new(10, 15, -2) << "\n";
  43. cout << "\n";
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
0
8

0
8

8
8

8
8