home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Tex / Tex29 / StTeXsrc.zoo / src / hash.c < prev    next >
C/C++ Source or Header  |  1988-03-13  |  3KB  |  143 lines

  1.  
  2. /*
  3.  * @(#)hash.c 2.5 EPA
  4.  *
  5.  * Copyright 1987,1988 Pat J Monardo
  6.  *
  7.  * Redistribution of this file is permitted through
  8.  * the specifications in the file COPYING.
  9.  *
  10.  * 
  11.  */
  12.  
  13. #include "tex.h"
  14. #include "box.h"
  15. #include "scan.h"
  16. #include "math.h"
  17. #include "boxlists.h"
  18.  
  19. hh      hash[UNDEFINED_CONTROL_SEQUENCE+1];
  20. ptr     hash_used = FROZEN_CONTROL_SEQUENCE;
  21. bool    no_new_control_sequence = TRUE;
  22. int     cs_count = 0;
  23.  
  24. ptr
  25. id_lookup (j, l)
  26.     int     j;
  27.     int     l;
  28. {
  29.     int     h;
  30.     int     k;
  31.     ptr     p;
  32.  
  33.     h = buffer[j];
  34.     for (k = j + 1; k < j + l; incr(k)) {
  35.         h = h + h + buffer[k];
  36.         while (h >= HASH_PRIME)
  37.             h -= HASH_PRIME;
  38.     }
  39.     for (p = h + HASH_BASE; ; p = next(p)) {
  40.         if (text(p) > 0 && length(text(p)) == l)
  41.             if (str_eq_buf(text(p), j))
  42.                 return p;
  43.         if (next(p) == 0) {
  44.             if (no_new_control_sequence)
  45.                 return UNDEFINED_CONTROL_SEQUENCE;
  46.             if (text(p) > 0) {
  47.                 do
  48.                     if (hash_is_full)
  49.                         overflow("hash size", HASH_SIZE);
  50.                     else decr(hash_used);
  51.                 while (text(hash_used) != 0);
  52.                 next(p) = hash_used;
  53.                 p = hash_used;
  54.             }
  55.             str_room(l);
  56.             for (k = j; k < j + l; incr(k))
  57.                 append_char(buffer[k]);
  58.             text(p) = make_str();
  59. #ifdef STAT
  60.             incr(cs_count);
  61. #endif
  62.             return p;
  63.         }
  64.     }
  65. }
  66.  
  67. print_cs (p)
  68.     ptr     p;
  69. {   
  70.     if (p < HASH_BASE) {
  71.         if (p >= SINGLE_BASE) {
  72.             if (p == NULL_CS) {
  73.                 print_esc("csname");
  74.                 print_esc("endcsname");
  75.             } else {
  76.                 print_esc(""); 
  77.                 print_str(p - SINGLE_BASE); 
  78.                 if (cat_code(p - SINGLE_BASE) == LETTER)
  79.                     print_char(' ');
  80.             }
  81.         } else if (p < ACTIVE_BASE)
  82.             print_esc("IMPOSSIBLE.");
  83.         else print_str(p - ACTIVE_BASE);
  84.     } else if (p >= UNDEFINED_CONTROL_SEQUENCE) {
  85.         print_esc("IMPOSSIBLE.");
  86.     } else if (text(p) < 0 || text(p) >= str_ptr) {
  87.         print_esc("NONEXISTENT.");
  88.     } else {
  89.         print_esc("");
  90.         slow_print(text(p));
  91.         print_char(' ');
  92.     }
  93. }
  94.  
  95. sprint_cs (p)
  96.     ptr     p;
  97. {
  98.     if (p < HASH_BASE) {
  99.         if (p < SINGLE_BASE) {
  100.             print_str(p - ACTIVE_BASE);
  101.         } else if (p < NULL_CS) {
  102.             print_esc("");
  103.             print_str(p - SINGLE_BASE);
  104.         } else {
  105.             print_esc("csname");
  106.             print_esc("endcsname");
  107.         }
  108.     } else {
  109.         print_esc("");
  110.         slow_print(text(p));
  111.     }
  112. }
  113.  
  114. #ifdef INIT
  115. primitive (s, code, order)
  116.     chrs    s;
  117.     qword   code;
  118.     hword   order;
  119. {
  120.     ascii   c;
  121.     int     j;
  122.     int     k;
  123.     int     l;
  124.     str     new_str;
  125.  
  126.     if (s[1] == NUL)
  127.         cur_val = s[0] + SINGLE_BASE;
  128.     else {
  129.         new_str = make_str_given(s);
  130.         k = str_start[new_str];
  131.         l = length(new_str);
  132.         for (j = 0; j < l; incr(j))
  133.             buffer[j] = str_pool[k + j];
  134.         cur_val = id_lookup(0, l);
  135.         flush_string();
  136.         text(cur_val) = new_str;
  137.     }
  138.     eq_level(cur_val) = LEVEL_ONE;
  139.     eq_type(cur_val) = code;
  140.     equiv(cur_val) = order;
  141. }
  142. #endif
  143.