fork(1) download
  1. char *volnitsky_strstr(const char *haystack, const char *needle)
  2. {
  3. // Only for little-endian platforms and where access to misaligned W is
  4. // allowed.
  5.  
  6. typedef uint16_t W_t;
  7. typedef uint8_t offset_t;
  8.  
  9. const size_t W_size = sizeof(W_t)
  10. , haystack_len = strlen(haystack)
  11. , needle_len = strlen(needle)
  12. , step = needle_len - W_size + 1;
  13.  
  14. int i;
  15. size_t h;
  16.  
  17. if (needle_len < 2*W_size-1 || needle_len >= UINT8_MAX)
  18. return strstr(haystack, needle); // fall-back to stdlib search.
  19.  
  20. // Make the hash table
  21. const size_t H_size = 65536;
  22. offset_t H[65536] = {0}; // initializes ALL buckets with 0
  23.  
  24. for (i = needle_len - W_size; i >= 0; i--) {
  25. h = *((W_t *) (needle + i)) % H_size;
  26.  
  27. while (H[h])
  28. h = (h+1) % H_size; // find free cell
  29.  
  30. H[h] = i+1;
  31. }
  32.  
  33. // step through text
  34. const char *P = haystack + needle_len - W_size
  35. , *P_end = haystack + haystack_len - needle_len;
  36. for (; P <= P_end; P += step) {
  37. for (h = *((W_t*) P) % H_size; H[h]; h = (h+1) % H_size) {
  38. const char* R = P - (H[h] - 1);
  39.  
  40. for (i = 0; i < needle_len; i++) {
  41. if (R[i] != needle[i])
  42. goto next_hash_cell;
  43. }
  44. return (char *) R; // found
  45.  
  46. next_hash_cell:;
  47. }
  48. }
  49.  
  50. // check tail
  51. return strstr(P - step + 1, needle);
  52. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'char* volnitsky_strstr(const char*, const char*)':
prog.cpp:6:21: error: 'uint16_t' does not name a type
     typedef         uint16_t        W_t;
                     ^
prog.cpp:7:21: error: 'uint8_t' does not name a type
     typedef         uint8_t         offset_t;
                     ^
prog.cpp:9:11: error: 'size_t' does not name a type
     const size_t W_size         = sizeof(W_t)
           ^
prog.cpp:15:5: error: 'size_t' was not declared in this scope
     size_t       h;
     ^
prog.cpp:17:9: error: 'needle_len' was not declared in this scope
     if (needle_len < 2*W_size-1 || needle_len >= UINT8_MAX)
         ^
prog.cpp:17:24: error: 'W_size' was not declared in this scope
     if (needle_len < 2*W_size-1 || needle_len >= UINT8_MAX)
                        ^
prog.cpp:17:50: error: 'UINT8_MAX' was not declared in this scope
     if (needle_len < 2*W_size-1 || needle_len >= UINT8_MAX)
                                                  ^
prog.cpp:18:39: error: 'strstr' was not declared in this scope
         return strstr(haystack, needle); // fall-back to stdlib search.
                                       ^
prog.cpp:21:11: error: 'size_t' does not name a type
     const size_t H_size         = 65536;
           ^
prog.cpp:22:5: error: 'offset_t' was not declared in this scope
     offset_t     H[65536]       = {0};     // initializes ALL buckets with 0
     ^
prog.cpp:24:14: error: 'needle_len' was not declared in this scope
     for (i = needle_len - W_size; i >= 0; i--) {
              ^
prog.cpp:24:27: error: 'W_size' was not declared in this scope
     for (i = needle_len - W_size; i >= 0; i--) {
                           ^
prog.cpp:25:9: error: 'h' was not declared in this scope
         h = *((W_t *) (needle + i)) % H_size;
         ^
prog.cpp:25:16: error: 'W_t' was not declared in this scope
         h = *((W_t *) (needle + i)) % H_size;
                ^
prog.cpp:25:21: error: expected primary-expression before ')' token
         h = *((W_t *) (needle + i)) % H_size;
                     ^
prog.cpp:25:39: error: 'H_size' was not declared in this scope
         h = *((W_t *) (needle + i)) % H_size;
                                       ^
prog.cpp:27:16: error: 'H' was not declared in this scope
         while (H[h])
                ^
prog.cpp:30:9: error: 'H' was not declared in this scope
         H[h] = i+1;
         ^
prog.cpp:34:38: error: 'needle_len' was not declared in this scope
     const char *P       = haystack + needle_len   - W_size
                                      ^
prog.cpp:34:53: error: 'W_size' was not declared in this scope
     const char *P       = haystack + needle_len   - W_size
                                                     ^
prog.cpp:36:17: error: 'P_end' was not declared in this scope
     for (; P <= P_end; P += step) {
                 ^
prog.cpp:36:29: error: 'step' was not declared in this scope
     for (; P <= P_end; P += step) {
                             ^
prog.cpp:37:14: error: 'h' was not declared in this scope
         for (h = *((W_t*) P) % H_size; H[h]; h = (h+1) % H_size) {
              ^
prog.cpp:37:21: error: 'W_t' was not declared in this scope
         for (h = *((W_t*) P) % H_size; H[h]; h = (h+1) % H_size) {
                     ^
prog.cpp:37:25: error: expected primary-expression before ')' token
         for (h = *((W_t*) P) % H_size; H[h]; h = (h+1) % H_size) {
                         ^
prog.cpp:37:27: error: expected ')' before 'P'
         for (h = *((W_t*) P) % H_size; H[h]; h = (h+1) % H_size) {
                           ^
prog.cpp:37:40: error: 'H' was not declared in this scope
         for (h = *((W_t*) P) % H_size; H[h]; h = (h+1) % H_size) {
                                        ^
prog.cpp:37:58: error: 'H_size' was not declared in this scope
         for (h = *((W_t*) P) % H_size; H[h]; h = (h+1) % H_size) {
                                                          ^
prog.cpp:51:23: error: 'step' was not declared in this scope
     return strstr(P - step + 1, needle);
                       ^
prog.cpp:51:39: error: 'strstr' was not declared in this scope
     return strstr(P - step + 1, needle);
                                       ^
stdout
Standard output is empty