home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / libgpp.i03 < prev    next >
Encoding:
GNU Info File  |  1993-06-12  |  48.0 KB  |  1,369 lines

  1. This is Info file libgpp, produced by Makeinfo-1.47 from the input file
  2. libgpp.tex.
  3.  
  4. START-INFO-DIR-ENTRY
  5. * Libg++: (libg++).        The g++ library.
  6. END-INFO-DIR-ENTRY
  7.  
  8.    This file documents the features and implementation of The GNU C++
  9. library
  10.  
  11.    Copyright (C) 1988, 1991, 1992 Free Software Foundation, Inc.
  12.  
  13.    Permission is granted to make and distribute verbatim copies of this
  14. manual provided the copyright notice and this permission notice are
  15. preserved on all copies.
  16.  
  17.    Permission is granted to copy and distribute modified versions of
  18. this manual under the conditions for verbatim copying, provided also
  19. that the section entitled "GNU Library General Public License" is
  20. included exactly as in the original, and provided that the entire
  21. resulting derived work is distributed under the terms of a permission
  22. notice identical to this one.
  23.  
  24.    Permission is granted to copy and distribute translations of this
  25. manual into another language, under the above conditions for modified
  26. versions, except that the section entitled "GNU Library General Public
  27. License" and this permission notice may be included in translations
  28. approved by the Free Software Foundation instead of in the original
  29. English.
  30.  
  31. 
  32. File: libgpp,  Node: Obstack,  Next: AllocRing,  Prev: Stream,  Up: Top
  33.  
  34. The Obstack class
  35. *****************
  36.  
  37.    The `Obstack' class is a simple rewrite of the C obstack macros and
  38. functions provided in the GNU CC compiler source distribution.
  39.  
  40.    Obstacks provide a simple method of creating and maintaining a string
  41. table, optimized for the very frequent task of building strings
  42. character-by-character, and sometimes keeping them, and sometimes not.
  43. They seem especially useful in any parsing application. One of the test
  44. files demonstrates usage.
  45.  
  46.    A brief summary:
  47. `grow'
  48.      places something on the obstack without committing to wrap it up
  49.      as a single entity yet.
  50.  
  51. `finish'
  52.      wraps up a constructed object as a single entity, and returns the
  53.      pointer to its start address.
  54.  
  55. `copy'
  56.      places things on the obstack, and *does* wrap them up. `copy' is
  57.      always equivalent to first grow, then finish.
  58.  
  59. `free'
  60.      deletes something, and anything else put on the obstack since its
  61.      creation.
  62.  
  63.    The other functions are less commonly needed:
  64. `blank'
  65.      is like grow, except it just grows the space by size units without
  66.      placing anything into this space
  67.  
  68. `alloc'
  69.      is like `blank', but it wraps up the object and returns its
  70.      starting address.
  71.  
  72. `chunk_size, base, next_free, alignment_mask, size, room'
  73.      returns the appropriate class variables.
  74.  
  75. `grow_fast'
  76.      places a character on the obstack without checking if there is
  77.      enough room.
  78.  
  79. `blank_fast'
  80.      like `blank', but without checking if there is enough room.
  81.  
  82. `shrink(int n)'
  83.      shrink the current chunk by n bytes.
  84.  
  85. `contains(void* addr)'
  86.      returns true if the Obstack holds the address addr.
  87.  
  88.    Here is a lightly edited version of the original C documentation:
  89.  
  90.    These functions operate a stack of objects.  Each object starts life
  91. small, and may grow to maturity.  (Consider building a word syllable by
  92. syllable.)  An object can move while it is growing.  Once it has been
  93. "finished" it never changes address again.  So the "top of the stack"
  94. is typically an immature growing object, while the rest of the stack is
  95. of mature, fixed size and fixed address objects.
  96.  
  97.    These routines grab large chunks of memory, using the GNU C++ `new'
  98. operator.  On occasion, they free chunks, via `delete'.
  99.  
  100.    Each independent stack is represented by a Obstack.
  101.  
  102.    One motivation for this package is the problem of growing char
  103. strings in symbol tables.  Unless you are a "fascist pig with a
  104. read-only mind" [Gosper's immortal quote from HAKMEM item 154, out of
  105. context] you would not like to put any arbitrary upper limit on the
  106. length of your symbols.
  107.  
  108.    In practice this often means you will build many short symbols and a
  109. few long symbols.  At the time you are reading a symbol you don't know
  110. how long it is.  One traditional method is to read a symbol into a
  111. buffer, `realloc()'ating the buffer every time you try to read a symbol
  112. that is longer than the buffer.  This is beaut, but you still will want
  113. to copy the symbol from the buffer to a more permanent symbol-table
  114. entry say about half the time.
  115.  
  116.    With obstacks, you can work differently.  Use one obstack for all
  117. symbol names.  As you read a symbol, grow the name in the obstack
  118. gradually. When the name is complete, finalize it.  Then, if the symbol
  119. exists already, free the newly read name.
  120.  
  121.    The way we do this is to take a large chunk, allocating memory from
  122. low addresses.  When you want to build a symbol in the chunk you just
  123. add chars above the current "high water mark" in the chunk.  When you
  124. have finished adding chars, because you got to the end of the symbol,
  125. you know how long the chars are, and you can create a new object.
  126. Mostly the chars will not burst over the highest address of the chunk,
  127. because you would typically expect a chunk to be (say) 100 times as
  128. long as an average object.
  129.  
  130.    In case that isn't clear, when we have enough chars to make up the
  131. object, *they are already contiguous in the chunk* (guaranteed) so we
  132. just point to it where it lies.  No moving of chars is needed and this
  133. is the second win: potentially long strings need never be explicitly
  134. shuffled. Once an object is formed, it does not change its address
  135. during its lifetime.
  136.  
  137.    When the chars burst over a chunk boundary, we allocate a larger
  138. chunk, and then copy the partly formed object from the end of the old
  139. chunk to the beginning of the new larger chunk.  We then carry on
  140. accreting characters to the end of the object as we normally would.
  141.  
  142.    A special version of grow is provided to add a single char at a time
  143. to a growing object.
  144.  
  145.    Summary:
  146.  
  147.    * We allocate large chunks.
  148.  
  149.    * We carve out one object at a time from the current chunk.
  150.  
  151.    * Once carved, an object never moves.
  152.  
  153.    * We are free to append data of any size to the currently growing
  154.      object.
  155.  
  156.    * Exactly one object is growing in an obstack at any one time.
  157.  
  158.    * You can run one obstack per control block.
  159.  
  160.    * You may have as many control blocks as you dare.
  161.  
  162.    * Because of the way we do it, you can `unwind' a obstack back to a
  163.      previous state. (You may remove objects much as you would with a
  164.      stack.)
  165.  
  166.    The obstack data structure is used in many places in the GNU C++
  167. compiler.
  168.  
  169.    Differences from the the GNU C version
  170.   1. The obvious differences stemming from the use of classes and
  171.      inline functions instead of structs and macros. The C `init' and
  172.      `begin' macros are replaced by constructors.
  173.  
  174.   2. Overloaded function names are used for grow (and others), rather
  175.      than the C `grow', `grow0', etc.
  176.  
  177.   3. All dynamic allocation uses the the built-in `new' operator. This
  178.      restricts flexibility by a little, but maintains compatibility
  179.      with usual C++ conventions.
  180.  
  181.   4. There are now two versions of finish:
  182.  
  183.        1. finish() behaves like the C version.
  184.  
  185.        2. finish(char terminator) adds `terminator', and then calls
  186.           `finish()'.  This enables the normal invocation of
  187.           `finish(0)' to wrap up a string being grown
  188.           character-by-character.
  189.  
  190.   5. There are special versions of grow(const char* s) and copy(const
  191.      char* s) that add the null-terminated string `s' after computing
  192.      its length.
  193.  
  194.   6. The shrink and contains functions are provided.
  195.  
  196.  
  197. 
  198. File: libgpp,  Node: AllocRing,  Next: String,  Prev: Obstack,  Up: Top
  199.  
  200. The AllocRing class
  201. *******************
  202.  
  203.    An AllocRing is a bounded ring (circular list), each of whose
  204. elements contains a pointer to some space allocated via `new
  205. char[some_size]'. The entries are used cyclicly.  The size, n, of the
  206. ring is fixed at construction. After that, every nth use of the ring
  207. will reuse (or reallocate) the same space. AllocRings are needed in
  208. order to temporarily hold chunks of space that are needed transiently,
  209. but across constructor-destructor scopes. They mainly useful for storing
  210. strings containing formatted characters to print acrosss various
  211. functions and coercions. These strings are needed across routines, so
  212. may not be deleted in any one of them, but should be recovered at some
  213. point. In other words, an AllocRing is an extremely simple minded
  214. garbage collection mechanism. The GNU C++ library used to use one
  215. AllocRing for such formatting purposes, but it is being phased out, and
  216. is now only used by obsolete functions. These days, AllocRings are
  217. probably not very useful.
  218.  
  219.    Support includes:
  220.  
  221. `AllocRing a(int n)'
  222.      constructs an Alloc ring with n entries, all null.
  223.  
  224. `void* mem = a.alloc(sz)'
  225.      moves the ring pointer to the next entry, and reuses the space if
  226.      their is enough, also allocates space via new char[sz].
  227.  
  228. `int present = a.contains(void* ptr)'
  229.      returns true if ptr is held in one of the ring entries.
  230.  
  231. `a.clear()'
  232.      deletes all space pointed to in any entry. This is called
  233.      automatically upon destruction.
  234.  
  235. `a.free(void* ptr)'
  236.      If ptr is one of the entries, calls delete of the pointer, and
  237.      resets to entry pointer to null.
  238.  
  239. 
  240. File: libgpp,  Node: String,  Next: Integer,  Prev: AllocRing,  Up: Top
  241.  
  242. The String class
  243. ****************
  244.  
  245.    The `String' class is designed to extend GNU C++ to support string
  246. processing capabilities similar to those in languages like Awk.  The
  247. class provides facilities that ought to be convenient and efficient
  248. enough to be useful replacements for `char*' based processing via the C
  249. string library (i.e., `strcpy, strcmp,' etc.) in many applications.
  250. Many details about String representations are described in the
  251. Representation section.
  252.  
  253.    A separate `SubString' class supports substring extraction and
  254. modification operations. This is implemented in a way that user
  255. programs never directly construct or represent substrings, which are
  256. only used indirectly via String operations.
  257.  
  258.    Another separate class, `Regex' is also used indirectly via String
  259. operations in support of regular expression searching, matching, and the
  260. like.  The Regex class is based entirely on the GNU Emacs regex
  261. functions.  *Note Syntax of Regular Expressions: (emacs.info)Regexps,
  262. for a full explanation of regular expression syntax.  (For
  263. implementation details, see the internal documentation in files
  264. `regex.h' and `regex.c'.)
  265.  
  266. Constructors
  267. ============
  268.  
  269.    Strings are initialized and assigned as in the following examples:
  270.  
  271. `String x;  String y = 0; String z = "";'
  272.      Set x, y, and z to the nil string. Note that either 0 or "" may
  273.      always be used to refer to the nil string.
  274.  
  275. `String x = "Hello"; String y("Hello");'
  276.      Set x and y to a copy of the string "Hello".
  277.  
  278. `String x = 'A'; String y('A');'
  279.      Set x and y to the string value "A"
  280.  
  281. `String u = x; String v(x);'
  282.      Set u and v to the same string as String x
  283.  
  284. `String u = x.at(1,4); String v(x.at(1,4));'
  285.      Set u and v to the length 4 substring of x starting at position 1
  286.      (counting indexes from 0).
  287.  
  288. `String x("abc", 2);'
  289.      Sets x to "ab", i.e., the first 2 characters of "abc".
  290.  
  291. `String x = dec(20);'
  292.      Sets x to "20". As here, Strings may be initialized or assigned
  293.      the results of any `char*' function.
  294.  
  295.    There are no directly accessible forms for declaring SubString
  296. variables.
  297.  
  298.    The declaration `Regex r("[a-zA-Z_][a-zA-Z0-9_]*");' creates a
  299. compiled regular expression suitable for use in String operations
  300. described below. (In this case, one that matches any C++ identifier).
  301. The first argument may also be a String. Be careful in distinguishing
  302. the role of backslashes in quoted GNU C++ char* constants versus those
  303. in Regexes. For example, a Regex that matches either one or more tabs
  304. or all strings beginning with "ba" and ending with any number of
  305. occurrences of "na" could be declared as `Regex r =
  306. "\\(\t+\\)\\|\\(ba\\(na\\)*\\)"' Note that only one backslash is needed
  307. to signify the tab, but two are needed for the parenthesization and
  308. virgule, since the GNU C++ lexical analyzer decodes and strips
  309. backslashes before they are seen by Regex.
  310.  
  311.    There are three additional optional arguments to the Regex
  312. constructor that are less commonly useful:
  313.  
  314. `fast (default 0)'
  315.      `fast' may be set to true (1) if the Regex should be
  316.      "fast-compiled". This causes an additional compilation step that
  317.      is generally worthwhile if the Regex will be used many times.
  318.  
  319. `bufsize (default max(40, length of the string))'
  320.      This is an estimate of the size of the internal compiled
  321.      expression. Set it to a larger value if you know that the
  322.      expression will require a lot of space. If you do not know, do not
  323.      worry: realloc is used if necessary.
  324.  
  325. `transtable (default none == 0)'
  326.      The address of a byte translation table (a char[256]) that
  327.      translates each character before matching.
  328.  
  329.    As a convenience, several Regexes are predefined and usable in any
  330. program. Here are their declarations from `String.h'.
  331.  
  332.      extern Regex RXwhite;      // = "[ \n\t]+"
  333.      extern Regex RXint;        // = "-?[0-9]+"
  334.      extern Regex RXdouble;     // = "-?\\(\\([0-9]+\\.[0-9]*\\)\\|
  335.                                 //    \\([0-9]+\\)\\|
  336.                                 //    \\(\\.[0-9]+\\)\\)
  337.                                 //    \\([eE][---+]?[0-9]+\\)?"
  338.      extern Regex RXalpha;      // = "[A-Za-z]+"
  339.      extern Regex RXlowercase;  // = "[a-z]+"
  340.      extern Regex RXuppercase;  // = "[A-Z]+"
  341.      extern Regex RXalphanum;   // = "[0-9A-Za-z]+"
  342.      extern Regex RXidentifier; // = "[A-Za-z_][A-Za-z0-9_]*"
  343.  
  344. Examples
  345. ========
  346.  
  347.    Most `String' class capabilities are best shown via example. The
  348. examples below use the following declarations.
  349.  
  350.          String x = "Hello";
  351.          String y = "world";
  352.          String n = "123";
  353.          String z;
  354.          char*  s = ",";
  355.          String lft, mid, rgt;
  356.          Regex  r = "e[a-z]*o";
  357.          Regex  r2("/[a-z]*/");
  358.          char   c;
  359.          int    i, pos, len;
  360.          double f;
  361.          String words[10];
  362.          words[0] = "a";
  363.          words[1] = "b";
  364.          words[2] = "c";
  365.  
  366. Comparing, Searching and Matching
  367. =================================
  368.  
  369.    The usual lexicographic relational operators (`==, !=, <, <=, >, >=')
  370. are defined. A functional form `compare(String, String)' is also
  371. provided, as is `fcompare(String, String)', which compares Strings
  372. without regard for upper vs. lower case.
  373.  
  374.    All other matching and searching operations are based on some form
  375. of the (non-public) `match' and `search' functions.  `match' and
  376. `search' differ in that `match' attempts to match only at the given
  377. starting position, while `search' starts at the position, and then
  378. proceeds left or right looking for a match.  As seen in the following
  379. examples, the second optional `startpos' argument to functions using
  380. `match' and `search' specifies the starting position of the search: If
  381. non-negative, it results in a left-to-right search starting at position
  382. `startpos', and if negative, a right-to-left search starting at
  383. position `x.length() + startpos'. In all cases, the index returned is
  384. that of the beginning of the match, or -1 if there is no match.
  385.  
  386.    Three String functions serve as front ends to `search' and `match'.
  387. `index' performs a search, returning the index, `matches' performs a
  388. match, returning nonzero (actually, the length of the match) on success,
  389. and `contains' is a boolean function performing either a search or
  390. match, depending on whether an index argument is provided:
  391.  
  392. `x.index("lo")'
  393.      returns the zero-based index of the leftmost occurrence of
  394.      substring "lo" (3, in this case).  The argument may be a String,
  395.      SubString, char, char*, or Regex.
  396.  
  397. `x.index("l", 2)'
  398.      returns the index of the first of the leftmost occurrence of "l"
  399.      found starting the search at position x[2], or 2 in this case.
  400.  
  401. `x.index("l", -1)'
  402.      returns the index of the rightmost occurrence of "l", or 3 here.
  403.  
  404. `x.index("l", -3)'
  405.      returns the index of the rightmost occurrence of "l" found by
  406.      starting the search at the 3rd to the last position of x,
  407.      returning 2 in this case.
  408.  
  409. `pos = r.search("leo", 3, len, 0)'
  410.      returns the index of r in the `char*' string of length 3, starting
  411.      at position 0, also placing the  length of the match in reference
  412.      parameter len.
  413.  
  414. `x.contains("He")'
  415.      returns nonzero if the String x contains the substring "He". The
  416.      argument may be a String, SubString, char, char*, or Regex.
  417.  
  418. `x.contains("el", 1)'
  419.      returns nonzero if x contains the substring "el" at position 1. As
  420.      in this example, the second argument to `contains', if present,
  421.      means to match the substring only at that position, and not to
  422.      search elsewhere in the string.
  423.  
  424. `x.contains(RXwhite);'
  425.      returns nonzero if x contains any whitespace (space, tab, or
  426.      newline). Recall that `RXwhite' is a global whitespace Regex.
  427.  
  428. `x.matches("lo", 3)'
  429.      returns nonzero if x starting at position 3 exactly matches "lo",
  430.      with no trailing characters (as it does in this example).
  431.  
  432. `x.matches(r)'
  433.      returns nonzero if String x as a whole matches Regex r.
  434.  
  435. `int f = x.freq("l")'
  436.      returns the number of distinct, nonoverlapping matches to the
  437.      argument (2 in this case).
  438.  
  439. Substring extraction
  440. ====================
  441.  
  442.    Substrings may be extracted via the `at', `before', `through',
  443. `from', and `after' functions. These behave as either lvalues or
  444. rvalues.
  445.  
  446. `z = x.at(2, 3)'
  447.      sets String z to be equal to the length 3 substring of String x
  448.      starting at zero-based position 2, setting z to "llo" in this
  449.      case. A nil String is returned if the arguments don't make sense.
  450.  
  451. `x.at(2, 2) = "r"'
  452.      Sets what was in positions 2 to 3 of x to "r", setting x to "Hero"
  453.      in this case. As indicated here, SubString assignments may be of
  454.      different lengths.
  455.  
  456. `x.at("He") = "je";'
  457.      x("He") is the substring of x that matches the first occurrence of
  458.      it's argument. The substitution sets x to "jello". If "He" did not
  459.      occur, the substring would be nil, and the assignment would have
  460.      no effect.
  461.  
  462. `x.at("l", -1) = "i";'
  463.      replaces the rightmost occurrence of "l" with "i", setting x to
  464.      "Helio".
  465.  
  466. `z = x.at(r)'
  467.      sets String z to the first match in x of Regex r, or "ello" in this
  468.      case. A nil String is returned if there is no match.
  469.  
  470. `z = x.before("o")'
  471.      sets z to the part of x to the left of the first occurrence of
  472.      "o", or "Hell" in this case. The argument may also be a String,
  473.      SubString, or Regex.  (If there is no match, z is set to "".)
  474.  
  475. `x.before("ll") = "Bri";'
  476.      sets the part of x to the left of "ll" to "Bri", setting x to
  477.      "Brillo".
  478.  
  479. `z = x.before(2)'
  480.      sets z to the part of x to the left of x[2], or "He" in this case.
  481.  
  482. `z = x.after("Hel")'
  483.      sets z to the part of x to the right of "Hel", or "lo" in this
  484.      case.
  485.  
  486. `z = x.through("el")'
  487.      sets z to the part of x up and including "el", or "Hel" in this
  488.      case.
  489.  
  490. `z = x.from("el")'
  491.      sets z to the part of x from "el" to the end, or "ello" in this
  492.      case.
  493.  
  494. `x.after("Hel") = "p";'
  495.      sets x to "Help";
  496.  
  497. `z = x.after(3)'
  498.      sets z to the part of x to the right of x[3] or "o" in this case.
  499.  
  500. `z = "  ab c"; z = z.after(RXwhite)'
  501.      sets z to the part of its old string to the right of the first
  502.      group of whitespace, setting z to "ab c"; Use gsub(below) to strip
  503.      out multiple occurrences of whitespace or any pattern.
  504.  
  505. `x[0] = 'J';'
  506.      sets the first element of x to 'J'. x[i] returns a reference to
  507.      the ith element of x, or triggers an error if i is out of range.
  508.  
  509. `common_prefix(x, "Help")'
  510.      returns the String containing the common prefix of the two Strings
  511.      or "Hel" in this case.
  512.  
  513. `common_suffix(x, "to")'
  514.      returns the String containing the common suffix of the two Strings
  515.      or "o" in this case.
  516.  
  517. Concatenation
  518. =============
  519.  
  520. `z = x + s + ' ' + y.at("w") + y.after("w") + ".";'
  521.      sets z to "Hello, world."
  522.  
  523. `x += y;'
  524.      sets x to "Helloworld"
  525.  
  526. `cat(x, y, z)'
  527.      A faster way to say z = x + y.
  528.  
  529. `cat(z, y, x, x)'
  530.      Double concatenation; A faster way to say x = z + y + x.
  531.  
  532. `y.prepend(x);'
  533.      A faster way to say y = x + y.
  534.  
  535. `z = replicate(x, 3);'
  536.      sets z to "HelloHelloHello".
  537.  
  538. `z = join(words, 3, "/")'
  539.      sets z to the concatenation of the first 3 Strings in String array
  540.      words, each separated by "/", setting z to "a/b/c" in this case. 
  541.      The last argument may be "" or 0, indicating no separation.
  542.  
  543. Other manipulations
  544. ===================
  545.  
  546. `z = "this string has five words"; i = split(z, words, 10, RXwhite);'
  547.      sets up to 10 elements of String array words to the parts of z
  548.      separated by whitespace, and returns the number of parts actually
  549.      encountered (5 in this case). Here, words[0] = "this", words[1] =
  550.      "string", etc.  The last argument may be any of the usual. If
  551.      there is no match, all of z ends up in words[0]. The words array
  552.      is *not* dynamically created by split.
  553.  
  554. `int nmatches x.gsub("l","ll")'
  555.      substitutes all original occurrences of "l" with "ll", setting x
  556.      to "Hellllo". The first argument may be any of the usual,
  557.      including Regex.  If the second argument is "" or 0, all
  558.      occurrences are deleted. gsub returns the number of matches that
  559.      were replaced.
  560.  
  561. `z = x + y;  z.del("loworl");'
  562.      deletes the leftmost occurrence of "loworl" in z, setting z to
  563.      "Held".
  564.  
  565. `z = reverse(x)'
  566.      sets z to the reverse of x, or "olleH".
  567.  
  568. `z = upcase(x)'
  569.      sets z to x, with all letters set to uppercase, setting z to
  570.      "HELLO"
  571.  
  572. `z = downcase(x)'
  573.      sets z to x, with all letters set to lowercase, setting z to
  574.      "hello"
  575.  
  576. `z = capitalize(x)'
  577.      sets z to x, with the first letter of each word set to uppercase,
  578.      and all others to lowercase, setting z to "Hello"
  579.  
  580. `x.reverse(), x.upcase(), x.downcase(), x.capitalize()'
  581.      in-place, self-modifying versions of the above.
  582.  
  583. Reading, Writing and Conversion
  584. ===============================
  585.  
  586. `cout << x'
  587.      writes out x.
  588.  
  589. `cout << x.at(2, 3)'
  590.      writes out the substring "llo".
  591.  
  592. `cin >> x'
  593.      reads a whitespace-bounded string into x.
  594.  
  595. `x.length()'
  596.      returns the length of String x (5, in this case).
  597.  
  598. `s = (const char*)x'
  599.      can be used to extract the `char*' char array. This coercion is
  600.      useful for sending a String as an argument to any function
  601.      expecting a `const char*' argument (like `atoi', and
  602.      `File::open'). This operator must be used with care, since the
  603.      conversion returns a pointer to `String' internals without copying
  604.      the characters: The resulting `(char*)' is only valid until the
  605.      next String operation,  and you must not modify it. (The
  606.      conversion is defined to return a const value so that GNU C++ will
  607.      produce warning and/or error messages if changes are attempted.)
  608.  
  609. 
  610. File: libgpp,  Node: Integer,  Next: Rational,  Prev: String,  Up: Top
  611.  
  612. The Integer class.
  613. ******************
  614.  
  615.    The `Integer' class provides multiple precision integer arithmetic
  616. facilities. Some representation details are discussed in the
  617. Representation section.
  618.  
  619.    `Integers' may be up to `b * ((1 << b) - 1)' bits long, where `b' is
  620. the number of bits per short (typically 1048560 bits when `b = 16'). 
  621. The implementation assumes that a `long' is at least twice as long as a
  622. `short'. This assumption hides beneath almost all primitive operations,
  623. and would be very difficult to change. It also relies on correct
  624. behavior of *unsigned* arithmetic operations.
  625.  
  626.    Some of the arithmetic algorithms are very loosely based on those
  627. provided in the MIT Scheme `bignum.c' release, which is Copyright (c)
  628. 1987 Massachusetts Institute of Technology. Their use here falls within
  629. the provisions described in the Scheme release.
  630.  
  631.    Integers may be constructed in the following ways:
  632. `Integer x;'
  633.      Declares an uninitialized Integer.
  634.  
  635. `Integer x = 2; Integer y(2);'
  636.      Set x and y to the Integer value 2;
  637.  
  638. `Integer u(x); Integer v = x;'
  639.      Set u and v to the same value as x.
  640.  
  641.    `Integers' may be coerced back into longs via the `long' coercion
  642. operator. If the Integer cannot fit into a long, this returns MINLONG
  643. or MAXLONG (depending on the sign) where MINLONG is the most negative,
  644. and MAXLONG is the most positive representable long.  The member
  645. function `fits_in_long()' may be used to test this. `Integers' may also
  646. be coerced into `doubles', with potential loss of precision. `+/-HUGE'
  647. is returned if the Integer cannot fit into a double. `fits_in_double()'
  648. may be used to test this.
  649.  
  650.    All of the usual arithmetic operators are provided (`+, -, *, /, %,
  651. +=, ++, -=, --, *=, /=, %=, ==, !=, <, <=, >, >=').  All operators
  652. support special versions for mixed arguments of Integers and regular
  653. C++ longs in order to avoid useless coercions, as well as to allow
  654. automatic promotion of shorts and ints to longs, so that they may be
  655. applied without additional Integer coercion operators.  The only
  656. operators that behave differently than the corresponding int or long
  657. operators are `++' and `--'.  Because C++ does not distinguish prefix
  658. from postfix application, these are declared as `void' operators, so
  659. that no confusion can result from applying them as postfix.  Thus, for
  660. Integers x and y, ` ++x; y = x; ' is correct, but ` y = ++x; ' and ` y
  661. = x++; ' are not.
  662.  
  663.    Bitwise operators (`~', `&', `|', `^', `<<', `>>', `&=', `|=', `^=',
  664. `<<=', `>>=') are also provided.  However, these operate on
  665. sign-magnitude, rather than two's complement representations. The sign
  666. of the result is arbitrarily taken as the sign of the first argument.
  667. For example, `Integer(-3) & Integer(5)' returns `Integer(-1)', not -3,
  668. as it would using two's complement. Also, `~', the complement operator,
  669. complements only those bits needed for the representation.  Bit
  670. operators are also provided in the BitSet and BitString classes. One of
  671. these classes should be used instead of Integers when the results of
  672. bit manipulations are not interpreted numerically.
  673.  
  674.    The following utility functions are also provided. (All arguments
  675. are Integers unless otherwise noted).
  676.  
  677. `void divide(x, y, q, r);'
  678.      Sets q to the quotient and r to the remainder of x and y. (q and r
  679.      are returned by reference).
  680.  
  681. `Integer pow(Integer x, Integer p)'
  682.      returns x raised to the power p.
  683.  
  684. `Integer Ipow(long x, long p)'
  685.      returns x raised to the power p.
  686.  
  687. `Integer gcd(x, y)'
  688.      returns the greatest common divisor of x and y.
  689.  
  690. `Integer lcm(x, y)'
  691.      returns the least common multiple of x and y.
  692.  
  693. `Integer abs(x);'
  694.      returns the absolute value of x.
  695.  
  696. `void x.negate();'
  697.      negates x.
  698.  
  699. `Integer sqr(x)'
  700.      returns x * x;
  701.  
  702. `Integer sqrt(x)'
  703.      returns the floor of the  square root of x.
  704.  
  705. `long lg(x);'
  706.      returns the floor of the base 2 logarithm of abs(x)
  707.  
  708. `int sign(x)'
  709.      returns -1 if x is negative, 0 if zero, else +1. Using `if
  710.      (sign(x) == 0)' is a generally faster method of testing for zero
  711.      than using relational operators.
  712.  
  713. `int even(x)'
  714.      returns true if x is an even number
  715.  
  716. `int odd(x)'
  717.      returns true if x is an odd number.
  718.  
  719. `void setbit(Integer& x, long b)'
  720.      sets the b'th bit (counting right-to-left from zero) of x to 1.
  721.  
  722. `void clearbit(Integer& x, long b)'
  723.      sets the b'th bit of x to 0.
  724.  
  725. `int testbit(Integer x, long b)'
  726.      returns true if the b'th bit of x is 1.
  727.  
  728. `Integer atoI(char* asciinumber, int base = 10);'
  729.      converts the base base char* string into its Integer form.
  730.  
  731. `void Integer::printon(ostream& s, int base = 10, int width = 0);'
  732.      prints the ascii string value of `(*this)' as a base `base'
  733.      number, in field width at least `width'.
  734.  
  735. `ostream << x;'
  736.      prints x in base ten format.
  737.  
  738. `istream >> x;'
  739.      reads x as a base ten number.
  740.  
  741. `int compare(Integer x, Integer y)'
  742.      returns a negative number if x<y, zero if x==y, or positive if x>y.
  743.  
  744. `int ucompare(Integer x, Integer y)'
  745.      like compare, but performs unsigned comparison.
  746.  
  747. `add(x, y, z)'
  748.      A faster way to say z = x + y.
  749.  
  750. `sub(x, y, z)'
  751.      A faster way to say z = x - y.
  752.  
  753. `mul(x, y, z)'
  754.      A faster way to say z = x * y.
  755.  
  756. `div(x, y, z)'
  757.      A faster way to say z = x / y.
  758.  
  759. `mod(x, y, z)'
  760.      A faster way to say z = x % y.
  761.  
  762. `and(x, y, z)'
  763.      A faster way to say z = x & y.
  764.  
  765. `or(x, y, z)'
  766.      A faster way to say z = x | y.
  767.  
  768. `xor(x, y, z)'
  769.      A faster way to say z = x ^ y.
  770.  
  771. `lshift(x, y, z)'
  772.      A faster way to say z = x << y.
  773.  
  774. `rshift(x, y, z)'
  775.      A faster way to say z = x >> y.
  776.  
  777. `pow(x, y, z)'
  778.      A faster way to say z = pow(x, y).
  779.  
  780. `complement(x, z)'
  781.      A faster way to say z = ~x.
  782.  
  783. `negate(x, z)'
  784.      A faster way to say z = -x.
  785.  
  786. 
  787. File: libgpp,  Node: Rational,  Next: Complex,  Prev: Integer,  Up: Top
  788.  
  789. The Rational Class
  790. ******************
  791.  
  792.    Class `Rational' provides multiple precision rational number
  793. arithmetic. All rationals are maintained in simplest form (i.e., with
  794. the numerator and denominator relatively prime, and with the
  795. denominator strictly positive). Rational arithmetic and relational
  796. operators are provided (`+, -, *, /, +=, -=, *=, /=, ==, !=, <, <=, >,
  797. >='). Operations resulting in a rational number with zero denominator
  798. trigger an exception.
  799.  
  800.    Rationals may be constructed and used in the following ways:
  801.  
  802. `Rational x;'
  803.      Declares an uninitialized Rational.
  804.  
  805. `Rational x = 2; Rational y(2);'
  806.      Set x and y to the Rational value 2/1;
  807.  
  808. `Rational x(2, 3);'
  809.      Sets x to the Rational value 2/3;
  810.  
  811. `Rational x = 1.2;'
  812.      Sets x to a Rational value close to 1.2. Any double precision value
  813.      may be used to construct a Rational. The Rational will possess
  814.      exactly as much precision as the double. Double values that do not
  815.      have precise floating point equivalents (like 1.2) produce
  816.      similarly imprecise rational values.
  817.  
  818. `Rational x(Integer(123), Integer(4567));'
  819.      Sets x to the Rational value 123/4567.
  820.  
  821. `Rational u(x); Rational v = x;'
  822.      Set u and v to the same value as x.
  823.  
  824. `double(Rational x)'
  825.      A Rational may be coerced to a double with potential loss of
  826.      precision. +/-HUGE is returned if it will not fit.
  827.  
  828. `Rational abs(x)'
  829.      returns the absolute value of x.
  830.  
  831. `void x.negate()'
  832.      negates x.
  833.  
  834. `void x.invert()'
  835.      sets x to 1/x.
  836.  
  837. `int sign(x)'
  838.      returns 0 if x is zero, 1 if positive, and -1 if negative.
  839.  
  840. `Rational sqr(x)'
  841.      returns x * x.
  842.  
  843. `Rational pow(x, Integer y)'
  844.      returns x to the y power.
  845.  
  846. `Integer x.numerator()'
  847.      returns the numerator.
  848.  
  849. `Integer x.denominator()'
  850.      returns the denominator.
  851.  
  852. `Integer floor(x)'
  853.      returns the greatest Integer less than x.
  854.  
  855. `Integer ceil(x)'
  856.      returns the least Integer greater than x.
  857.  
  858. `Integer trunc(x)'
  859.      returns the Integer part of x.
  860.  
  861. `Integer round(x)'
  862.      returns the nearest Integer to x.
  863.  
  864. `int compare(x, y)'
  865.      returns a negative, zero, or positive number signifying whether x
  866.      is less than, equal to, or greater than y.
  867.  
  868. `ostream << x;'
  869.      prints x in the form num/den, or just num if the denominator is
  870.      one.
  871.  
  872. `istream >> x;'
  873.      reads x in the form num/den, or just num in which case the
  874.      denominator is set to one.
  875.  
  876. `add(x, y, z)'
  877.      A faster way to say z = x + y.
  878.  
  879. `sub(x, y, z)'
  880.      A faster way to say z = x - y.
  881.  
  882. `mul(x, y, z)'
  883.      A faster way to say z = x * y.
  884.  
  885. `div(x, y, z)'
  886.      A faster way to say z = x / y.
  887.  
  888. `pow(x, y, z)'
  889.      A faster way to say z = pow(x, y).
  890.  
  891. `negate(x, z)'
  892.      A faster way to say z = -x.
  893.  
  894. 
  895. File: libgpp,  Node: Complex,  Next: Fix,  Prev: Rational,  Up: Top
  896.  
  897. The Complex class.
  898. ******************
  899.  
  900.    Class `Complex' is implemented in a way similar to that described by
  901. Stroustrup. In keeping with libg++ conventions, the class is named
  902. `Complex', not `complex'. Complex arithmetic and relational operators
  903. are provided (`+, -, *, /, +=, -=, *=, /=, ==, !='). Attempted division
  904. by (0, 0) triggers an exception.
  905.  
  906.    Complex numbers may be constructed and used in the following ways:
  907.  
  908. `Complex x;'
  909.      Declares an uninitialized Complex.
  910.  
  911. `Complex x = 2; Complex y(2.0);'
  912.      Set x and y to the Complex value (2.0, 0.0);
  913.  
  914. `Complex x(2, 3);'
  915.      Sets x to the Complex value (2, 3);
  916.  
  917. `Complex u(x); Complex v = x;'
  918.      Set u and v to the same value as x.
  919.  
  920. `double real(Complex& x);'
  921.      returns the real part of x.
  922.  
  923. `double imag(Complex& x);'
  924.      returns the imaginary part of x.
  925.  
  926. `double abs(Complex& x);'
  927.      returns the magnitude of x.
  928.  
  929. `double norm(Complex& x);'
  930.      returns the square of the magnitude of x.
  931.  
  932. `double arg(Complex& x);'
  933.      returns the argument (amplitude) of x.
  934.  
  935. `Complex polar(double r, double t = 0.0);'
  936.      returns a Complex with abs of r and arg of t.
  937.  
  938. `Complex conj(Complex& x);'
  939.      returns the complex conjugate of x.
  940.  
  941. `Complex cos(Complex& x);'
  942.      returns the complex cosine of x.
  943.  
  944. `Complex sin(Complex& x);'
  945.      returns the complex sine of x.
  946.  
  947. `Complex cosh(Complex& x);'
  948.      returns the complex hyperbolic cosine of x.
  949.  
  950. `Complex sinh(Complex& x);'
  951.      returns the complex hyperbolic sine of x.
  952.  
  953. `Complex exp(Complex& x);'
  954.      returns the exponential of x.
  955.  
  956. `Complex log(Complex& x);'
  957.      returns the natural log of x.
  958.  
  959. `Complex pow(Complex& x, long p);'
  960.      returns x raised to the p power.
  961.  
  962. `Complex pow(Complex& x, Complex& p);'
  963.      returns x raised to the p power.
  964.  
  965. `Complex sqrt(Complex& x);'
  966.      returns the square root of x.
  967.  
  968. `ostream << x;'
  969.      prints x in the form (re, im).
  970.  
  971. `istream >> x;'
  972.      reads x in the form (re, im), or just (re) or re in which case the
  973.      imaginary part is set to zero.
  974.  
  975. 
  976. File: libgpp,  Node: Fix,  Next: Bit,  Prev: Complex,  Up: Top
  977.  
  978. Fixed precision numbers
  979. ***********************
  980.  
  981.    Classes `Fix16', `Fix24', `Fix32', and `Fix48' support operations on
  982. 16, 24, 32, or 48 bit quantities that are considered as real numbers in
  983. the range [-1, +1).  Such numbers are often encountered in digital
  984. signal processing applications. The classes may be be used in isolation
  985. or together.  Class `Fix32' operations are entirely self-contained. 
  986. Class `Fix16' operations are self-contained except that the
  987. multiplication operation `Fix16 * Fix16' returns a `Fix32'. `Fix24' and
  988. `Fix48' are similarly related.
  989.  
  990.    The standard arithmetic and relational operations are supported
  991. (`=', `+', `-', `*', `/', `<<', `>>', `+=', `-=', `*=', `/=', `<<=',
  992. `>>=', `==', `!=', `<', `<=', `>', `>='). All operations include
  993. provisions for special handling in cases where the result exceeds +/-
  994. 1.0. There are two cases that may be handled separately: "overflow"
  995. where the results of addition and subtraction operations go out of
  996. range, and all other "range errors" in which resulting values go
  997. off-scale (as with division operations, and assignment or
  998. initialization with off-scale values). In signal processing
  999. applications, it is often useful to handle these two cases differently.
  1000. Handlers take one argument, a reference to the integer mantissa of the
  1001. offending value, which may then be manipulated.  In cases of overflow,
  1002. this value is the result of the (integer) arithmetic computation on the
  1003. mantissa; in others it is a fully saturated (i.e., most positive or
  1004. most negative) value. Handling may be reset to any of several provided
  1005. functions or any other user-defined function via `set_overflow_handler'
  1006. and `set_range_error_handler'. The provided functions for `Fix16' are
  1007. as follows (corresponding functions are also supported for the others).
  1008.  
  1009. `Fix16_overflow_saturate'
  1010.      The default overflow handler. Results are "saturated": positive
  1011.      results are set to the largest representable value (binary
  1012.      0.111111...), and negative values to -1.0.
  1013.  
  1014. `Fix16_ignore'
  1015.      Performs no action. For overflow, this will allow addition and
  1016.      subtraction operations to "wrap around" in the same manner as
  1017.      integer arithmetic, and for saturation, will leave values
  1018.      saturated.
  1019.  
  1020. `Fix16_overflow_warning_saturate'
  1021.      Prints a warning message on standard error, then saturates the
  1022.      results.
  1023.  
  1024. `Fix16_warning'
  1025.      The default range_error handler. Prints a warning message on
  1026.      standard error; otherwise leaving the argument unmodified.
  1027.  
  1028. `Fix16_abort'
  1029.      prints an error message on standard error, then aborts execution.
  1030.  
  1031.    In addition to arithmetic operations, the following are provided:
  1032.  
  1033. `Fix16 a = 0.5;'
  1034.      Constructs fixed precision objects from double precision values.
  1035.      Attempting to initialize to a value outside the range invokes the
  1036.      range_error handler, except, as a convenience, initialization to
  1037.      1.0 sets the variable to the most positive representable value
  1038.      (binary 0.1111111...) without invoking the handler.
  1039.  
  1040. `short& mantissa(a); long& mantissa(b);'
  1041.      return a * pow(2, 15) or b * pow(2, 31) as an integer. These are
  1042.      returned by reference, to enable "manual" data manipulation.
  1043.  
  1044. `double value(a); double value(b);'
  1045.      return a or b as floating point numbers.
  1046.  
  1047. 
  1048. File: libgpp,  Node: Bit,  Next: Random,  Prev: Fix,  Up: Top
  1049.  
  1050. Classes for Bit manipulation
  1051. ****************************
  1052.  
  1053.    libg++ provides several different classes supporting the use and
  1054. manipulation of collections of bits in different ways.
  1055.  
  1056.    * Class `Integer' provides "integer" semantics. It supports
  1057.      manipulation of bits in ways that are often useful when treating
  1058.      bit arrays as numerical (integer) quantities.  This class is
  1059.      described elsewhere.
  1060.  
  1061.    * Class `BitSet' provides "set" semantics. It supports operations
  1062.      useful when treating collections of bits as representing
  1063.      potentially infinite sets of integers.
  1064.  
  1065.    * Class `BitSet32' supports fixed-length BitSets holding exactly 32
  1066.      bits.
  1067.  
  1068.    * Class `Bitset256'supports fixed-length BitSets holding exactly 256
  1069.      bits.
  1070.  
  1071.    * Class `BitString' provides "string" (or "vector") semantics. It
  1072.      supports operations useful when treating collections of bits as
  1073.      strings of zeros and ones.
  1074.  
  1075.    These classes also differ in the following ways:
  1076.  
  1077.    * BitSets are logically infinite. Their space is dynamically altered
  1078.      to adjust to the smallest number of consecutive bits actually
  1079.      required to represent the sets. Integers also have this property.
  1080.      BitStrings are logically finite, but their sizes are internally
  1081.      dynamically managed to maintain proper length. This means that,
  1082.      for example, BitStrings are concatenatable while BitSets and
  1083.      Integers are not.
  1084.  
  1085.    * BitSet32 and BitSet256 have precisely the same properties as
  1086.      BitSets, except that they use constant fixed length bit vectors.
  1087.  
  1088.    * While all classes support basic unary and binary operations `~, &,
  1089.      |, ^, -', the semantics differ. BitSets perform bit operations that
  1090.      precisely mirror those for infinite sets. For example,
  1091.      complementing an empty BitSet returns one representing an infinite
  1092.      number of set bits. Operations on BitStrings and Integers operate
  1093.      only on those bits actually present in the representation.  For
  1094.      BitStrings and Integers, the the `&' operation returns a BitString
  1095.      with a length equal to the minimum length of the operands, and `|,
  1096.      ^' return one with length of the maximum.
  1097.  
  1098.    * Only BitStrings support substring extraction and bit pattern
  1099.      matching.
  1100.  
  1101. BitSet
  1102. ======
  1103.  
  1104.    Bitsets are objects that contain logically infinite sets of
  1105. nonnegative integers.  Representational details are discussed in the
  1106. Representation chapter. Because they are logically infinite, all
  1107. BitSets possess a trailing, infinitely replicated 0 or 1 bit, called
  1108. the "virtual bit", and indicated via 0* or 1*.
  1109.  
  1110.    BitSet32 and BitSet256 have they same properties, except they are of
  1111. fixed length, and thus have no virtual bit.
  1112.  
  1113.    BitSets may be constructed as follows:
  1114.  
  1115. `BitSet a;'
  1116.      declares an empty BitSet.
  1117.  
  1118. `BitSet a = atoBitSet("001000");'
  1119.      sets a to the BitSet 0010*, reading left-to-right. The "0*"
  1120.      indicates that the set ends with an infinite number of zero
  1121.      (clear) bits.
  1122.  
  1123. `BitSet a = atoBitSet("00101*");'
  1124.      sets a to the BitSet 00101*, where "1*" means that the set ends
  1125.      with an infinite number of one (set) bits.
  1126.  
  1127. `BitSet a = longtoBitSet((long)23);'
  1128.      sets a to the BitSet 111010*, the binary representation of decimal
  1129.      23.
  1130.  
  1131. `BitSet a = utoBitSet((unsigned)23);'
  1132.      sets a to the BitSet 111010*, the binary representation of decimal
  1133.      23.
  1134.  
  1135.    The following functions and operators are provided (Assume the
  1136. declaration of BitSets a = 0011010*, b = 101101*, throughout, as
  1137. examples).
  1138.  
  1139. `~a'
  1140.      returns the complement of a, or 1100101* in this case.
  1141.  
  1142. `a.complement()'
  1143.      sets a to ~a.
  1144.  
  1145. `a & b; a &= b;'
  1146.      returns a intersected with b, or 0011010*.
  1147.  
  1148. `a | b; a |= b;'
  1149.      returns a unioned with b, or 1011111*.
  1150.  
  1151. `a - b; a -= b;'
  1152.      returns the set difference of a and b, or 000010*.
  1153.  
  1154. `a ^ b; a ^= b;'
  1155.      returns the symmetric difference of a and b, or 1000101*.
  1156.  
  1157. `a.empty()'
  1158.      returns true if a is an empty set.
  1159.  
  1160. `a == b;'
  1161.      returns true if a and b contain the same set.
  1162.  
  1163. `a <= b;'
  1164.      returns true if a is a subset of b.
  1165.  
  1166. `a < b;'
  1167.      returns true if a is a proper subset of b;
  1168.  
  1169. `a != b; a >= b; a > b;'
  1170.      are the converses of the above.
  1171.  
  1172. `a.set(7)'
  1173.      sets the 7th (counting from 0) bit of a, setting a to 001111010*
  1174.  
  1175. `a.clear(2)'
  1176.      clears the 2nd bit bit of a, setting a to 00011110*
  1177.  
  1178. `a.clear()'
  1179.      clears all bits of a;
  1180.  
  1181. `a.set()'
  1182.      sets all bits of a;
  1183.  
  1184. `a.invert(0)'
  1185.      complements the 0th bit of a, setting a to 10011110*
  1186.  
  1187. `a.set(0,1)'
  1188.      sets the 0th through 1st bits of a, setting a to 110111110* The
  1189.      two-argument versions of clear and invert are similar.
  1190.  
  1191. `a.test(3)'
  1192.      returns true if the 3rd bit of a is set.
  1193.  
  1194. `a.test(3, 5)'
  1195.      returns true if any of bits 3 through 5 are set.
  1196.  
  1197. `int i = a[3]; a[3] = 0;'
  1198.      The subscript operator allows bits to be inspected and changed via
  1199.      standard subscript semantics, using a friend class BitSetBit. The
  1200.      use of the subscript operator a[i] rather than a.test(i) requires
  1201.      somewhat greater overhead.
  1202.  
  1203. `a.first(1) or a.first()'
  1204.      returns the index of the first set bit of a (2 in this case), or
  1205.      -1 if no bits are set.
  1206.  
  1207. `a.first(0)'
  1208.      returns the index of the first clear bit of a (0 in this case), or
  1209.      -1 if no bits are clear.
  1210.  
  1211. `a.next(2, 1) or a.next(2)'
  1212.      returns the index of the next bit after position 2 that is set (3
  1213.      in this case) or -1. `first' and `next' may be used as iterators,
  1214.      as in `for (int i = a.first(); i >= 0; i = a.next(i))...'.
  1215.  
  1216. `a.last(1)'
  1217.      returns the index of the rightmost set bit, or -1 if there or no
  1218.      set bits or all set bits.
  1219.  
  1220. `a.previous(3, 0)'
  1221.      returns the index of the previous clear bit before position 3.
  1222.  
  1223. `a.count(1)'
  1224.      returns the number of set bits in a, or -1 if there are an
  1225.      infinite number.
  1226.  
  1227. `a.virtual_bit()'
  1228.      returns the trailing (infinitely replicated) bit of a.
  1229.  
  1230. `a = atoBitSet("ababX", 'a', 'b', 'X');'
  1231.      converts the char* string into a bitset, with 'a' denoting false,
  1232.      'b' denoting true, and 'X' denoting infinite replication.
  1233.  
  1234. `a.printon(cout, '-', '.', 0)'
  1235.      prints `a' to `cout' represented with `'-'' for falses, `'.'' for
  1236.      trues, and no replication marker.
  1237.  
  1238. `cout << a'
  1239.      prints `a' to `cout' (representing lases by `'f'', trues by `'t'',
  1240.      and using `'*'' as the replication marker).
  1241.  
  1242. `diff(x, y, z)'
  1243.      A faster way to say z = x - y.
  1244.  
  1245. `and(x, y, z)'
  1246.      A faster way to say z = x & y.
  1247.  
  1248. `or(x, y, z)'
  1249.      A faster way to say z = x | y.
  1250.  
  1251. `xor(x, y, z)'
  1252.      A faster way to say z = x ^ y.
  1253.  
  1254. `complement(x, z)'
  1255.      A faster way to say z = ~x.
  1256.  
  1257. BitString
  1258. =========
  1259.  
  1260.    BitStrings are objects that contain arbitrary-length strings of
  1261. zeroes and ones. BitStrings possess some features that make them behave
  1262. like sets, and others that behave as strings. They are useful in
  1263. applications (such as signature-based algorithms) where both
  1264. capabilities are needed.  Representational details are discussed in the
  1265. Representation chapter.  Most capabilities are exact analogs of those
  1266. supported in the BitSet and String classes.  A BitSubString is used
  1267. with substring operations along the same lines as the String SubString
  1268. class.  A BitPattern class is used for masked bit pattern searching.
  1269.  
  1270.    Only a default constructor is supported.  The declaration `BitString
  1271. a;' initializes a to be an empty BitString. BitStrings may often be
  1272. initialized via `atoBitString' and `longtoBitString'.
  1273.  
  1274.    Set operations (` ~, complement, &, &=, |, |=, -, ^, ^=') behave
  1275. just as the BitSet versions, except that there is no "virtual bit":
  1276. complementing complements only those bits in the BitString, and all
  1277. binary operations across unequal length BitStrings assume a virtual bit
  1278. of zero. The `&' operation returns a BitString with a length equal to
  1279. the minimum length of the operands, and `|, ^' return one with length
  1280. of the maximum.
  1281.  
  1282.    Set-based relational operations (`==, !=, <=, <, >=, >') follow the
  1283. same rules. A string-like lexicographic comparison function,
  1284. `lcompare', tests the lexicographic relation between two BitStrings.
  1285. For example, lcompare(1100, 0101) returns 1, since the first BitString
  1286. starts with 1 and the second with 0.
  1287.  
  1288.    Individual bit setting, testing, and iterator operations (`set,
  1289. clear, invert, test, first, next, last, previous') are also like those
  1290. for BitSets. BitStrings are automatically expanded when setting bits at
  1291. positions greater than their current length.
  1292.  
  1293.    The string-based capabilities are just as those for class String.
  1294. BitStrings may be concatenated (`+, +='), searched (`index, contains,
  1295. matches'), and extracted into BitSubStrings (`before, at, after') which
  1296. may be assigned and otherwise manipulated. Other string-based utility
  1297. functions (`reverse, common_prefix, common_suffix') are also provided.
  1298. These have the same capabilities and descriptions as those for Strings.
  1299.  
  1300.    String-oriented operations can also be performed with a mask via
  1301. class BitPattern. BitPatterns consist of two BitStrings, a pattern and
  1302. a mask. On searching and matching, bits in the pattern that correspond
  1303. to 0 bits in the mask are ignored. (The mask may be shorter than the
  1304. pattern, in which case trailing mask bits are assumed to be 0). The
  1305. pattern and mask are both public variables, and may be individually
  1306. subjected to other bit operations.
  1307.  
  1308.    Converting to char* and printing (`(atoBitString, atoBitPattern,
  1309. printon, ostream <<)') are also as in BitSets, except that no virtual
  1310. bit is used, and an 'X' in a BitPattern means that the pattern bit is
  1311. masked out.
  1312.  
  1313.    The following features are unique to BitStrings.
  1314.  
  1315.    Assume declarations of BitString a = atoBitString("01010110") and b =
  1316. atoBitSTring("1101").
  1317.  
  1318. `a = b + c;'
  1319.      Sets a to the concatenation of b and c;
  1320.  
  1321. `a = b + 0; a = b + 1;'
  1322.      sets a to b, appended with a zero (one).
  1323.  
  1324. `a += b;'
  1325.      appends b to a;
  1326.  
  1327. `a += 0; a += 1;'
  1328.      appends a zero (one) to a.
  1329.  
  1330. `a << 2; a <<= 2'
  1331.      return a with 2 zeros prepended, setting a to 0001010110. (Note
  1332.      the necessary confusion of << and >> operators. For consistency
  1333.      with the integer versions, << shifts low bits to high, even though
  1334.      they are printed low bits first.)
  1335.  
  1336. `a >> 3; a >>= 3'
  1337.      return a with the first 3 bits deleted, setting a to 10110.
  1338.  
  1339. `a.left_trim(0)'
  1340.      deletes all 0 bits on the left of a, setting a to 1010110.
  1341.  
  1342. `a.right_trim(0)'
  1343.      deletes all trailing 0 bits of a, setting a to 0101011.
  1344.  
  1345. `cat(x, y, z)'
  1346.      A faster way to say z = x + y.
  1347.  
  1348. `diff(x, y, z)'
  1349.      A faster way to say z = x - y.
  1350.  
  1351. `and(x, y, z)'
  1352.      A faster way to say z = x & y.
  1353.  
  1354. `or(x, y, z)'
  1355.      A faster way to say z = x | y.
  1356.  
  1357. `xor(x, y, z)'
  1358.      A faster way to say z = x ^ y.
  1359.  
  1360. `lshift(x, y, z)'
  1361.      A faster way to say z = x << y.
  1362.  
  1363. `rshift(x, y, z)'
  1364.      A faster way to say z = x >> y.
  1365.  
  1366. `complement(x, z)'
  1367.      A faster way to say z = ~x.
  1368.  
  1369.