fork(1) download
  1. #include <iostream>
  2. #include <assert.h>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. struct VeryElaboratedDataType
  8. {
  9. int a;
  10. int b;
  11. };
  12.  
  13. namespace amsoft
  14. {
  15. namespace inutils
  16. {
  17. enum EShiftDirection
  18. {
  19. Left,
  20. Right
  21. };
  22. template
  23. <typename T,size_t len>
  24. void infernalShift(T infernalArray[],int positions,EShiftDirection direction = EShiftDirection::Right)
  25. {
  26. //assert the dudes
  27. assert(len > 0 && "what the fuck dude?");
  28. assert(positions >= 0 && "what the fuck dude?");
  29.  
  30. if(positions > 0)
  31. {
  32. ++positions;
  33. //let's make it fit the range
  34. positions %= len;
  35.  
  36. //if y want to live as a forcio, i'l get y change direction by force
  37. if(!direction)
  38. {
  39. positions = len - positions;
  40. }
  41.  
  42. //here i prepare a fine block of raw memory... allocate once per thread
  43. static unsigned char WORK_BUFFER[len * sizeof(T)];
  44. // std::memset (WORK_BUFFER,0,len * sizeof(T));
  45. // clean or not clean?, well
  46. // Hamlet is a prince, a prince does not clean
  47.  
  48. //copy the first chunk of data to the 0 position
  49. std::memcpy(WORK_BUFFER,reinterpret_cast<unsigned char *>(infernalArray) + (positions)*sizeof(T),(len - positions)*sizeof(T));
  50. //copy the second chunk of data to the len - positions position
  51. std::memcpy(WORK_BUFFER+(len - positions)*sizeof(T),reinterpret_cast<unsigned char *>(infernalArray),positions * sizeof(T));
  52.  
  53. //now bulk copy back to original one
  54. std::memcpy(reinterpret_cast<unsigned char *>(infernalArray),WORK_BUFFER,len * sizeof(T));
  55.  
  56. }
  57.  
  58. }
  59. template
  60. <typename T>
  61. void printArray(T infernalArrayPrintable[],int len)
  62. {
  63. for(int i=0;i<len;i++)
  64. {
  65. std::cout << infernalArrayPrintable[i] << " ";
  66. }
  67. std::cout << std::endl;
  68.  
  69. }
  70. template
  71. <>
  72. void printArray(VeryElaboratedDataType infernalArrayPrintable[],int len)
  73. {
  74. for(int i=0;i<len;i++)
  75. {
  76. std::cout << infernalArrayPrintable[i].a << "," << infernalArrayPrintable[i].b << " ";
  77. }
  78. std::cout << std::endl;
  79.  
  80. }
  81. }
  82. }
  83.  
  84.  
  85.  
  86.  
  87. int main() {
  88. // your code goes here
  89. int myInfernalArray[] = {1,2,3,4,5,6,7,8,9};
  90.  
  91. VeryElaboratedDataType myInfernalArrayV[] = {{1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7},{8,8},{9,9}};
  92. amsoft::inutils::printArray(myInfernalArray,sizeof(myInfernalArray)/sizeof(int));
  93. amsoft::inutils::infernalShift<int,sizeof(myInfernalArray)/sizeof(int)>(myInfernalArray,4);
  94. amsoft::inutils::printArray(myInfernalArray,sizeof(myInfernalArray)/sizeof(int));
  95. amsoft::inutils::infernalShift<int,sizeof(myInfernalArray)/sizeof(int)>(myInfernalArray,4,amsoft::inutils::EShiftDirection::Left);
  96. amsoft::inutils::printArray(myInfernalArray,sizeof(myInfernalArray)/sizeof(int));
  97. amsoft::inutils::infernalShift<int,sizeof(myInfernalArray)/sizeof(int)>(myInfernalArray,10);
  98. amsoft::inutils::printArray(myInfernalArray,sizeof(myInfernalArray)/sizeof(int));
  99.  
  100.  
  101. amsoft::inutils::printArray(myInfernalArrayV,sizeof(myInfernalArrayV)/sizeof(VeryElaboratedDataType));
  102. amsoft::inutils::infernalShift<VeryElaboratedDataType,sizeof(myInfernalArrayV)/sizeof(VeryElaboratedDataType)>(myInfernalArrayV,4);
  103. amsoft::inutils::printArray(myInfernalArrayV,sizeof(myInfernalArrayV)/sizeof(VeryElaboratedDataType));
  104. amsoft::inutils::infernalShift<VeryElaboratedDataType,sizeof(myInfernalArrayV)/sizeof(VeryElaboratedDataType)>(myInfernalArrayV,4,amsoft::inutils::EShiftDirection::Left);
  105. amsoft::inutils::printArray(myInfernalArrayV,sizeof(myInfernalArrayV)/sizeof(VeryElaboratedDataType));
  106. amsoft::inutils::infernalShift<VeryElaboratedDataType,sizeof(myInfernalArrayV)/sizeof(VeryElaboratedDataType)>(myInfernalArrayV,10);
  107. amsoft::inutils::printArray(myInfernalArrayV,sizeof(myInfernalArrayV)/sizeof(VeryElaboratedDataType));
  108.  
  109. return 0;
  110. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 
6 7 8 9 1 2 3 4 5 
1 2 3 4 5 6 7 8 9 
3 4 5 6 7 8 9 1 2 
1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 
6,6 7,7 8,8 9,9 1,1 2,2 3,3 4,4 5,5 
1,1 2,2 3,3 4,4 5,5 6,6 7,7 8,8 9,9 
3,3 4,4 5,5 6,6 7,7 8,8 9,9 1,1 2,2