home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / _STR2SET.C < prev    next >
Text File  |  1984-12-31  |  2KB  |  63 lines

  1. /*  File   : _str2set.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 20 April 1984
  4.     Defines: _set_ctr, _set_vec[], _str2set().
  5.     Purpose: Convert a character string to a set.
  6. */
  7.  
  8. /*  The obvious way of representing a set of characters  is  as  a
  9.     vector  of 0s and 1s.  The snag with that is that to convert a
  10.     string to such a vector, we have to clear all the elements  to
  11.     0,  and  then  set the elements corresponding to characters in
  12.     the string to 1, so the cost is  O(|alphabet|+|string|).  This
  13.     package  uses another method, where there is a vector of small
  14.     numbers and a counter.  A character is in the current  set  if
  15.     and  only  if the corresponding element of the vector is equal
  16.     to the current value of  the  counter.   Every  so  often  the
  17.     vector  elements  would  overflow  and  we  have  to clear the
  18.     vector, but the cost is reduced to O(|string|+1).
  19.  
  20.     Note that NUL ('\0') will never be in any set built by str2set.
  21.  
  22.     While this method reduces the cost of building a set, it would
  23.     be useful to avoid it entirely.  So when the "set" argument is
  24.     NullS the set is not changed.  Use NullS to mean "the same set
  25.     as before."  MaxPosChar is the largest integer value which can
  26.     be stored in a "char".  Although we might get a slightly wider
  27.     range by using "unsigned char", "char" may be cheaper (as on a
  28.     PDP-11).  By all means change the number from 127 if your C is
  29.     one of those that treats char as unsigned, but don't change it
  30.     just because _AlphabetSize is 256, the two are unrelated.  And
  31.     don't dare change it on a VAX: it is built into the asm code!
  32. */
  33.  
  34. #include "strings.h"
  35. #include "_str2set.h"
  36.  
  37. #if    CharsAreSigned
  38. #define    MaxPosChar    127
  39. #else  ~CharsAreSigned
  40. #define MaxPosChar    255
  41. #endif    CharsAreSigned
  42.  
  43. int  _set_ctr = MaxPosChar;
  44. char _set_vec[_AlphabetSize];
  45.  
  46.  
  47. void _str2set(set)
  48.     register char *set;
  49.     {
  50.     if (set == NullS) return;
  51.     if (++_set_ctr == MaxPosChar+1) {
  52. #if    VaxAsm
  53.         asm("movc5 $0,4(ap),$0,$128,__set_vec");
  54. #else  ~VaxAsm
  55.         register char *w = &_set_vec[_AlphabetSize];
  56.         do *--w = NUL; while (w != &_set_vec[0]);
  57. #endif    VaxAsm
  58.         _set_ctr = 1;
  59.     }
  60.     while (*set) _set_vec[*set++] = _set_ctr;
  61.     }
  62.