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.  
  22. /**
  23.  * Verifica che una chiave esista.
  24.  *
  25.  * @return -1 se non esiste, 0 se esiste (e poi dicono che non
  26.  * rispetto gli standard)
  27.  */
  28. int lookup_exists(
  29. const int kFirst,
  30. const int kSecond,
  31. const int kThird,
  32. int* firstRow,
  33. int* secondRow,
  34. int* thirdRow
  35. ) {
  36.  
  37. /* Gli esempi pratici sono:
  38.   1 combo) azbdce||dfef
  39.   2 combo) azbd|ce|dfef
  40.   3 combo) az|bdce|dfef
  41.  
  42.   Quindi trasformo le "tabelle" in stringhe così da poterle convertire
  43.   in hash. */
  44. int key = 0;
  45. for ( int i = 0; i < kFirst; i++ ) {
  46. key |= 1<<firstRow[ i ];
  47. }
  48. key = key<<5;
  49. for ( int i = 0; i < kSecond; i++ ) {
  50. key |= 1<<secondRow[ i ];
  51. }
  52. key = key<<5;
  53. for ( int i = 0; i < kThird; i++ ) {
  54. key |= 1<<thirdRow[ i ];
  55. }
  56.  
  57. next = &combs[key];
  58.  
  59. if(*next == 0) {
  60. return -1;
  61. }
  62.  
  63. return 0;
  64. }
  65.  
  66. void lookup_insert()
  67. {
  68. *next = 1;
  69. }
  70.  
  71. void lookup_update()
  72. {
  73. ++(*next);
  74. }
  75.  
  76. void lookup_free()
  77. {
  78.  
  79. }
  80.  
  81. int main(void)
  82. {
  83. /*
  84.   Ipotizzando di avere una tabella da 3 righe e 5 colonne:
  85.   +----+----+----+----+----+
  86.   | | | | | |
  87.   +----+----+----+----+----+
  88.   | | | | | |
  89.   +----+----+----+----+----+
  90.   | | | | | |
  91.   +----+----+----+----+----+
  92.  
  93.   e di riempirla casualmente (popolandola da sinistra a destra con un
  94.   indice che corrisponde ad una lettera della array "ab")
  95.  
  96.   static char* ab[] = {
  97.   "az", "bd", "ce", "df", "ef", "fd", "gl"
  98.   */
  99. int _1row[5];
  100. int _2row[5];
  101. int _3row[5];
  102.  
  103. lookup_init();
  104.  
  105. /*
  106.   Prima combinazione:
  107.   +----+----+----+----+----+
  108.   | az | bd | ce | | |
  109.   +----+----+----+----+----+
  110.   | | | | | |
  111.   +----+----+----+----+----+
  112.   | df | ef | | | |
  113.   +----+----+----+----+----+
  114.   */
  115. _1row[0] = 0;
  116. _1row[1] = 1;
  117. _1row[2] = 2;
  118. _3row[0] = 3;
  119. _3row[1] = 4;
  120. for ( int c = 0; c < 5; c++ ) {
  121. /* Verifico che la combinazione esista già, se non esiste la creo
  122.   altrimenti aggiorno il campo "data".
  123.   In questo la prima combinazione avrà il campo "data" con il valore
  124.   4 (visto che incrementerà ad ogni "lookup_update") */
  125. if ( lookup_exists( 3, 0, 2, _1row, _2row, _3row ) == -1 ) {
  126. lookup_insert();
  127. } else {
  128. lookup_update();
  129. }
  130. }
  131.  
  132. /*
  133.   Seconda combinazione:
  134.   +----+----+----+----+----+
  135.   | az | bd | | | |
  136.   +----+----+----+----+----+
  137.   | ce | | | | |
  138.   +----+----+----+----+----+
  139.   | df | ef | | | |
  140.   +----+----+----+----+----+
  141.  
  142.   Campo "data" con valore 1.
  143.   */
  144. _1row[0] = 0;
  145. _1row[1] = 1;
  146. _2row[0] = 2;
  147. _3row[0] = 3;
  148. _3row[1] = 4;
  149. for ( int c = 0; c < 2; c++ ) {
  150. if ( lookup_exists( 2, 1, 2, _1row, _2row, _3row ) == -1 ) {
  151. lookup_insert();
  152. } else {
  153. lookup_update();
  154.  
  155. }
  156. }
  157.  
  158. /*
  159.   Terza combinazione:
  160.   +----+----+----+----+----+
  161.   | az | | | | |
  162.   +----+----+----+----+----+
  163.   | bd | ce | | | |
  164.   +----+----+----+----+----+
  165.   | df | ef | | | |
  166.   +----+----+----+----+----+
  167.  
  168.   Campo "data" con valore 2.
  169.   */
  170. _1row[0] = 0;
  171. _2row[0] = 1;
  172. _2row[1] = 2;
  173. _3row[0] = 3;
  174. _3row[1] = 4;
  175. for ( int c = 0; c < 3; c++) {
  176. if ( lookup_exists( 1, 2, 2, _1row, _2row, _3row ) == -1 ) {
  177. lookup_insert();
  178. } else {
  179. lookup_update();
  180.  
  181. }
  182. }
  183.  
  184. /*
  185.   Questa è sempre la seconda combinazione, perciò incrementa il campo
  186.   "data" di 15 valori.
  187.   */
  188. _1row[0] = 0;
  189. _1row[1] = 1;
  190. _2row[0] = 2;
  191. _3row[0] = 3;
  192. _3row[1] = 4;
  193. for ( int c = 0; c < 15; c++ ) {
  194. if ( lookup_exists( 2, 1, 2, _1row, _2row, _3row ) == -1 ) {
  195. lookup_insert();
  196. } else {
  197. lookup_update();
  198. }
  199. }
  200.  
  201. printf("%d", *next);
  202.  
  203. lookup_free();
  204. }
Success #stdin #stdout 0s 9456KB
stdin
Standard input is empty
stdout
17