fork(1) download
  1. /*
  2.  |r1 |r2 |r3
  3. #|012 34|01 234|0 1234
  4. 0|111 00|00 001|0 0010
  5.  
  6. Massimo indice possibile:
  7. 0|111 00|00 011|0 0000 = 2^14+2^13+2^12+2^6+2^5 = 28768
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #define SIZE 28769
  13.  
  14. char combs[SIZE];
  15.  
  16. int *next;
  17.  
  18. void lookup_init()
  19. {
  20. }
  21. void lookup_update(
  22. const int kFirst,
  23. const int kSecond,
  24. const int kThird,
  25. int* firstRow,
  26. int* secondRow,
  27. int* thirdRow
  28. ) {
  29.  
  30. /* Gli esempi pratici sono:
  31.   1 combo) azbdce||dfef
  32.   2 combo) azbd|ce|dfef
  33.   3 combo) az|bdce|dfef
  34.  
  35.   Quindi trasformo le "tabelle" in stringhe così da poterle convertire
  36.   in hash. */
  37. int key = 0;
  38. for ( int i = 0; i < kFirst; i++ ) {
  39. key |= 1<<firstRow[ i ];
  40. }
  41. key = key<<5;
  42. for ( int i = 0; i < kSecond; i++ ) {
  43. key |= 1<<secondRow[ i ];
  44. }
  45. key = key<<5;
  46. for ( int i = 0; i < kThird; i++ ) {
  47. key |= 1<<thirdRow[ i ];
  48. }
  49.  
  50. next = &combs[key];
  51.  
  52. ++(*next);
  53. }
  54.  
  55. void lookup_free()
  56. {
  57.  
  58. }
  59.  
  60. int main(void)
  61. {
  62. /*
  63.   Ipotizzando di avere una tabella da 3 righe e 5 colonne:
  64.   +----+----+----+----+----+
  65.   | | | | | |
  66.   +----+----+----+----+----+
  67.   | | | | | |
  68.   +----+----+----+----+----+
  69.   | | | | | |
  70.   +----+----+----+----+----+
  71.  
  72.   e di riempirla casualmente (popolandola da sinistra a destra con un
  73.   indice che corrisponde ad una lettera della array "ab")
  74.  
  75.   static char* ab[] = {
  76.   "az", "bd", "ce", "df", "ef", "fd", "gl"
  77.   */
  78. int _1row[5];
  79. int _2row[5];
  80. int _3row[5];
  81.  
  82. lookup_init();
  83.  
  84. /*
  85.   Prima combinazione:
  86.   +----+----+----+----+----+
  87.   | az | bd | ce | | |
  88.   +----+----+----+----+----+
  89.   | | | | | |
  90.   +----+----+----+----+----+
  91.   | df | ef | | | |
  92.   +----+----+----+----+----+
  93.   */
  94. _1row[0] = 0;
  95. _1row[1] = 1;
  96. _1row[2] = 2;
  97. _3row[0] = 3;
  98. _3row[1] = 4;
  99. for ( int c = 0; c < 5; c++ ) {
  100. /* Verifico che la combinazione esista già, se non esiste la creo
  101.   altrimenti aggiorno il campo "data".
  102.   In questo la prima combinazione avrà il campo "data" con il valore
  103.   4 (visto che incrementerà ad ogni "lookup_update") */
  104. lookup_update( 3, 0, 2, _1row, _2row, _3row );
  105. }
  106.  
  107. /*
  108.   Seconda combinazione:
  109.   +----+----+----+----+----+
  110.   | az | bd | | | |
  111.   +----+----+----+----+----+
  112.   | ce | | | | |
  113.   +----+----+----+----+----+
  114.   | df | ef | | | |
  115.   +----+----+----+----+----+
  116.  
  117.   Campo "data" con valore 1.
  118.   */
  119. _1row[0] = 0;
  120. _1row[1] = 1;
  121. _2row[0] = 2;
  122. _3row[0] = 3;
  123. _3row[1] = 4;
  124. for ( int c = 0; c < 2; c++ ) {
  125. lookup_update( 2, 1, 2, _1row, _2row, _3row );
  126. }
  127.  
  128. /*
  129.   Terza combinazione:
  130.   +----+----+----+----+----+
  131.   | az | | | | |
  132.   +----+----+----+----+----+
  133.   | bd | ce | | | |
  134.   +----+----+----+----+----+
  135.   | df | ef | | | |
  136.   +----+----+----+----+----+
  137.  
  138.   Campo "data" con valore 2.
  139.   */
  140. _1row[0] = 0;
  141. _2row[0] = 1;
  142. _2row[1] = 2;
  143. _3row[0] = 3;
  144. _3row[1] = 4;
  145. for ( int c = 0; c < 3; c++) {
  146. lookup_update( 1, 2, 2, _1row, _2row, _3row );
  147. }
  148.  
  149. /*
  150.   Questa è sempre la seconda combinazione, perciò incrementa il campo
  151.   "data" di 15 valori.
  152.   */
  153. _1row[0] = 0;
  154. _1row[1] = 1;
  155. _2row[0] = 2;
  156. _3row[0] = 3;
  157. _3row[1] = 4;
  158. for ( int c = 0; c < 15; c++ ) {
  159. lookup_update( 2, 1, 2, _1row, _2row, _3row );
  160. }
  161.  
  162. printf("%d", *next);
  163.  
  164. lookup_free();
  165. }
Success #stdin #stdout 0s 9456KB
stdin
Standard input is empty
stdout
17