Guest User

Untitled

a guest
Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. for(int i = 0; i < 9; ++i) {
  2. for(int j = 0; j < 9; ++i) {
  3. //...
  4. for(int k = 0; k < 9; ++k) { //N-th loop
  5.  
  6. void doRecursion(int baseCondition){
  7.  
  8. if(baseCondition==0) return;
  9.  
  10. //place your code here
  11.  
  12. doRecursion(baseCondition-1);
  13. }
  14.  
  15. struct multi_index
  16. {
  17. std::vector<int> min_ind;
  18. std::vector<int> max_ind;
  19. std::vector<int> ind;
  20. bool _end = false;
  21.  
  22. multi_index(int N, int _max, int _min = 0)
  23. : min_ind(N,_min)
  24. , max_ind(N,_max)
  25. , ind(N, _min)
  26. {}
  27.  
  28. //possibly other constructors taking vectors for min_ind and max_ind
  29.  
  30. auto& operator++()
  31. {
  32. // increase first index i for which ind[i] < max_ind[i]
  33. // set all indices j < i equal to min_ind[j]
  34. // if no index found to increase, set end = true
  35. return *this;
  36. }
  37. auto operator[](int i) const { return ind[i]; }
  38. auto end() const { return _end; }
  39. operator bool() const { return !_end; }
  40. };
  41.  
  42. int N=5;
  43. for(auto m=multi_index(N,9); !m.end(); ++m)
  44. {
  45. // now m[i] holds index of i-th loop
  46. }
  47.  
  48. void loop_function(/*params*/,int N){
  49. for(int i=0;i<9;++i){
  50. if(N>0) loop_function(/*new params*/,N-1);
  51. }
  52.  
  53. void runNextNestedFor(std::vector<int> counters, int index)
  54. {
  55. for(counters[index] = 0; counters[index] < 9; ++counters[index]) {
  56. // DO
  57. if(index!=N)
  58. runNextNestedFor(counters, index+1);
  59. }
  60. }
  61.  
  62. std::vectors<int> counters(N);
  63. runNextNestedFor(counters, 0);
  64.  
  65. #ifndef NESTEDLOOP_HPP
  66. #define NESTEDLOOP_HPP
  67. #include <vector>
  68.  
  69. namespace nestedLoop{
  70.  
  71. class nestedLoop {
  72. public:
  73. //Variables
  74. std::vector<int> maxes;
  75. std::vector<int> idxes; //The last element is used for boundary control
  76. int N=0;
  77. int nestLevel=0;
  78.  
  79. nestedLoop();
  80. nestedLoop(int,int);
  81. nestedLoop(int,std::vector<int>);
  82.  
  83. void reset(int numberOfNests, int Max);
  84. void reset(int numberOfNests, std::vector<int> theMaxes);
  85.  
  86. bool next();
  87. void jumpNest(int theNest);
  88.  
  89. private:
  90. void clear();
  91. };
  92.  
  93. //Initialisations
  94. nestedLoop::nestedLoop(){}
  95.  
  96. nestedLoop::nestedLoop(int numberOfNests, int Max) {
  97. reset(numberOfNests, Max);
  98. }
  99.  
  100. nestedLoop::nestedLoop(int numberOfNests, std::vector<int> theMaxes) {
  101. reset(numberOfNests, theMaxes);
  102. }
  103.  
  104. void nestedLoop::clear(){
  105. maxes.clear();
  106. idxes.clear();
  107. N = 0;
  108. nestLevel = 0;
  109. }
  110.  
  111. //Reset the scene
  112. void nestedLoop::reset(int numberOfNests, int Max){
  113. std::vector<int> theMaxes;
  114. for(int i =0; i < numberOfNests; i++) theMaxes.push_back(Max);
  115. reset(numberOfNests, theMaxes);
  116. }
  117.  
  118. void nestedLoop::reset(int numberOfNests, std::vector<int> theMaxes){
  119. clear();
  120. N = numberOfNests;
  121.  
  122. maxes=theMaxes;
  123.  
  124. idxes.push_back(-1);
  125. for(int i=1; i<N; i++) idxes.push_back(theMaxes[i]-1);
  126. }
  127.  
  128. bool nestedLoop::next(){
  129. idxes[N-1]+=1;
  130.  
  131. for(int i=N-1; i>=0; i--){
  132. if(idxes[i]>=maxes[i]) {
  133. idxes[i] = 0;
  134.  
  135. if(i){ //actually, if i > 0 is needed
  136. idxes[i-1] += 1;
  137. }else{
  138. return false;
  139. }
  140. }else{
  141. nestLevel = i;
  142. break;
  143. }
  144. }
  145. return true;
  146. }
  147.  
  148. void nestedLoop::jumpNest(int theNest){
  149. for(int i = N-1; i>theNest; i--) {
  150. idxes[i] = maxes[i]-1;
  151. }
  152. }
  153. }
  154. #endif // NESTEDLOOP_HPP
  155.  
  156. #include <iostream>
  157. #include "stlvecs.hpp"
  158. #include "nestedLoop.hpp"
  159.  
  160. int main(){
  161. nestedLoop::nestedLoop looper;
  162. std::vector<int> maxes = {2, 3, 2, 2};
  163. looper.reset(4,maxes);
  164. int i = 0;
  165. while(looper.next()){
  166. std::cout << "Indices: " << looper.idxes << ", Last nest incremented: " << looper.nestLevel << std::endl;
  167. if(i == 5){
  168. std::cout << "...Jump Second Nest (index 1)..." << std::endl;
  169. looper.jumpNest(1);
  170. }
  171. i++;
  172. }
  173. }
  174.  
  175. /* Expected output
  176. Indices: 4 0 0 0 0 , Last nest incremented: 0
  177. Indices: 4 0 0 0 1 , Last nest incremented: 3
  178. Indices: 4 0 0 1 0 , Last nest incremented: 2
  179. Indices: 4 0 0 1 1 , Last nest incremented: 3
  180. Indices: 4 0 1 0 0 , Last nest incremented: 1
  181. Indices: 4 0 1 0 1 , Last nest incremented: 3
  182. ...Jump Second Nest (index 1)...
  183. Indices: 4 0 2 0 0 , Last nest incremented: 1
  184. Indices: 4 0 2 0 1 , Last nest incremented: 3
  185. Indices: 4 0 2 1 0 , Last nest incremented: 2
  186. Indices: 4 0 2 1 1 , Last nest incremented: 3
  187. Indices: 4 1 0 0 0 , Last nest incremented: 0
  188. Indices: 4 1 0 0 1 , Last nest incremented: 3
  189. Indices: 4 1 0 1 0 , Last nest incremented: 2
  190. Indices: 4 1 0 1 1 , Last nest incremented: 3
  191. Indices: 4 1 1 0 0 , Last nest incremented: 1
  192. Indices: 4 1 1 0 1 , Last nest incremented: 3
  193. Indices: 4 1 1 1 0 , Last nest incremented: 2
  194. Indices: 4 1 1 1 1 , Last nest incremented: 3
  195. Indices: 4 1 2 0 0 , Last nest incremented: 1
  196. Indices: 4 1 2 0 1 , Last nest incremented: 3
  197. Indices: 4 1 2 1 0 , Last nest incremented: 2
  198. Indices: 4 1 2 1 1 , Last nest incremented: 3
  199. */
  200.  
  201. #include <iostream>
  202.  
  203. using namespace std;
  204.  
  205. void doThingWithNumber(const int* digits, int numDigits)
  206. {
  207. int i;
  208. for (i = numDigits-1; i>=0; i--)
  209. cout << digits[i];
  210. cout << endl;
  211. }
  212.  
  213. void loopOverAllNumbers(int numDigits)
  214. {
  215. int* digits = new int [numDigits];
  216. int i;
  217. for (i = 0; i< numDigits; i++)
  218. digits[i] = 0;
  219.  
  220. int maxDigit = 0;
  221. while (maxDigit < numDigits) {
  222. doThingWithNumber(digits, numDigits);
  223. for (i = 0; i < numDigits; i++) {
  224. digits[i]++;
  225. if (digits[i] < 10)
  226. break;
  227. digits[i] = 0;
  228. }
  229. if (i > maxDigit)
  230. maxDigit = i;
  231. }
  232. }
  233.  
  234. int main()
  235. {
  236. loopOverAllNumbers(3);
  237. return 0;
  238. }
  239.  
  240. unsigned int dim = 3;
  241. unsigned int top = 5;
  242. std::vector<unsigned int> m(dim, 0);
  243. for (unsigned int i = 0; i < pow(top,dim); i++)
  244. {
  245. // What you want to do comes here
  246. // |
  247. // |
  248. // v
  249. // -----------------------------------
  250. for (unsigned int j = 0; j < dim; j++)
  251. {
  252. std::cout << m[j] << ",";
  253. }
  254. std::cout << std::endl;
  255. // -----------------------------------
  256.  
  257. // Increment m
  258. if (i == pow(top, dim) - 1) break;
  259. unsigned int index_to_increment = dim - 1;
  260. while(m[index_to_increment] == (top-1)) {
  261. index_to_increment -= 1;
  262. }
  263. m[index_to_increment] += 1;
  264. for (unsigned int j = index_to_increment + 1; j < dim; j++)
  265. {
  266. m[j] = 0;
  267. }
  268. }
Add Comment
Please, Sign In to add comment