3 /*****************************************************************************/
4 /* MODULE NAME: BitVector.h MODULE TYPE: (adt) */
5 /*****************************************************************************/
7 /*****************************************************************************/
10 /*****************************************************************************/
11 /* NOTE: The type names that have been chosen here are somewhat weird on */
12 /* purpose, in order to avoid name clashes with system header files */
13 /* and your own application(s) which might - directly or indirectly - */
14 /* include this definitions file. */
15 /*****************************************************************************/
20 typedef unsigned char N_char;
21 typedef unsigned char N_byte;
22 typedef unsigned short N_short;
23 typedef unsigned short N_shortword;
24 typedef unsigned int N_int;
25 typedef unsigned int N_word;
26 typedef unsigned long N_long;
27 typedef unsigned long N_longword;
29 /* Mnemonic 1: The natural numbers, N = { 0, 1, 2, 3, ... } */
30 /* Mnemonic 2: Nnnn = u_N_signed, _N_ot signed */
32 typedef signed char Z_char;
33 typedef signed char Z_byte;
34 typedef signed short Z_short;
35 typedef signed short Z_shortword;
36 typedef signed int Z_int;
37 typedef signed int Z_word;
38 typedef signed long Z_long;
39 typedef signed long Z_longword;
41 /* Mnemonic 1: The whole numbers, Z = { 0, -1, 1, -2, 2, -3, 3, ... } */
42 /* Mnemonic 2: Zzzz = Ssss_igned */
44 typedef void *voidptr;
45 typedef N_char *charptr;
46 typedef N_byte *byteptr;
47 typedef N_short *shortptr;
48 typedef N_shortword *shortwordptr;
49 typedef N_int *intptr;
50 typedef N_word *wordptr;
51 typedef N_long *longptr;
52 typedef N_longword *longwordptr;
54 typedef N_char *N_charptr;
55 typedef N_byte *N_byteptr;
56 typedef N_short *N_shortptr;
57 typedef N_shortword *N_shortwordptr;
58 typedef N_int *N_intptr;
59 typedef N_word *N_wordptr;
60 typedef N_long *N_longptr;
61 typedef N_longword *N_longwordptr;
63 typedef Z_char *Z_charptr;
64 typedef Z_byte *Z_byteptr;
65 typedef Z_short *Z_shortptr;
66 typedef Z_shortword *Z_shortwordptr;
67 typedef Z_int *Z_intptr;
68 typedef Z_word *Z_wordptr;
69 typedef Z_long *Z_longptr;
70 typedef Z_longword *Z_longwordptr;
83 #ifdef MACOS_TRADITIONAL
84 #define boolean Boolean
86 typedef enum boolean { false = FALSE, true = TRUE } boolean;
90 /*****************************************************************************/
91 /* MODULE INTERFACE: */
92 /*****************************************************************************/
96 ErrCode_Ok = 0, /* everything went allright */
98 ErrCode_Type, /* types word and size_t have incompatible sizes */
99 ErrCode_Bits, /* bits of word and sizeof(word) are inconsistent */
100 ErrCode_Word, /* size of word is less than 16 bits */
101 ErrCode_Long, /* size of word is greater than size of long */
102 ErrCode_Powr, /* number of bits of word is not a power of two */
103 ErrCode_Loga, /* error in calculation of logarithm */
105 ErrCode_Null, /* unable to allocate memory */
107 ErrCode_Indx, /* index out of range */
108 ErrCode_Ordr, /* minimum > maximum index */
109 ErrCode_Size, /* bit vector size mismatch */
110 ErrCode_Pars, /* input string syntax error */
111 ErrCode_Ovfl, /* numeric overflow error */
112 ErrCode_Same, /* operands must be distinct */
113 ErrCode_Expo, /* exponent must be positive */
114 ErrCode_Zero /* division by zero error */
117 typedef wordptr *listptr;
119 /* ===> MISCELLANEOUS BASIC FUNCTIONS: <=== */
122 const char * BitVector_Error (ErrCode error); /* return string for err code */
125 ErrCode BitVector_Boot (void); /* 0 = ok, 1..7 = error */
127 void BitVector_Shutdown (void); /* undo Boot */
130 N_word BitVector_Size (N_int bits); /* bit vector size (# of words) */
132 N_word BitVector_Mask (N_int bits); /* bit vector mask (unused bits) */
134 /* ===> CLASS METHODS: <=== */
137 const char * BitVector_Version (void); /* returns version string */
140 N_int BitVector_Word_Bits (void); /* return # of bits in machine word */
142 N_int BitVector_Long_Bits (void); /* return # of bits in unsigned long */
144 /* ===> CONSTRUCTOR METHODS: <=== */
147 /*@only@*/ wordptr BitVector_Create (N_int bits, boolean clear); /* malloc */
149 listptr BitVector_Create_List(N_int bits, boolean clear, N_int count);
152 wordptr BitVector_Resize (wordptr oldaddr, N_int bits); /* realloc */
155 wordptr BitVector_Shadow (wordptr addr); /* make new same size but empty */
157 wordptr BitVector_Clone (wordptr addr); /* make exact duplicate */
160 wordptr BitVector_Concat (wordptr X, wordptr Y); /* return concatenation */
162 /* ===> DESTRUCTOR METHODS: <=== */
165 void BitVector_Dispose (/*@only@*/ /*@out@*/ charptr string); /* string */
167 void BitVector_Destroy (/*@only@*/ wordptr addr); /* bitvec */
169 void BitVector_Destroy_List (listptr list, N_int count); /* list */
171 /* ===> OBJECT METHODS: <=== */
173 /* ===> bit vector copy function: */
176 void BitVector_Copy (wordptr X, wordptr Y); /* X = Y */
178 /* ===> bit vector initialization: */
181 void BitVector_Empty (wordptr addr); /* X = {} */
183 void BitVector_Fill (wordptr addr); /* X = ~{} */
185 void BitVector_Flip (wordptr addr); /* X = ~X */
188 void BitVector_Primes (wordptr addr);
190 /* ===> miscellaneous functions: */
193 void BitVector_Reverse (wordptr X, wordptr Y);
195 /* ===> bit vector interval operations and functions: */
198 void BitVector_Interval_Empty (/*@out@*/ wordptr addr, N_int lower, N_int upper);
200 void BitVector_Interval_Fill (/*@out@*/ wordptr addr, N_int lower, N_int upper);
202 void BitVector_Interval_Flip (/*@out@*/ wordptr addr, N_int lower, N_int upper);
204 void BitVector_Interval_Reverse (/*@out@*/ wordptr addr, N_int lower, N_int upper);
207 boolean BitVector_interval_scan_inc (wordptr addr, N_int start,
208 N_intptr min, N_intptr max);
210 boolean BitVector_interval_scan_dec (wordptr addr, N_int start,
211 N_intptr min, N_intptr max);
214 void BitVector_Interval_Copy (/*@out@*/ wordptr X, wordptr Y, N_int Xoffset,
215 N_int Yoffset, N_int length);
218 wordptr BitVector_Interval_Substitute(/*@out@*/ wordptr X, wordptr Y,
219 N_int Xoffset, N_int Xlength,
220 N_int Yoffset, N_int Ylength);
222 /* ===> bit vector test functions: */
225 boolean BitVector_is_empty (wordptr addr); /* X == {} ? */
227 boolean BitVector_is_full (wordptr addr); /* X == ~{} ? */
230 boolean BitVector_equal (wordptr X, wordptr Y); /* X == Y ? */
232 Z_int BitVector_Lexicompare(wordptr X, wordptr Y); /* X <,=,> Y ? */
234 Z_int BitVector_Compare (wordptr X, wordptr Y); /* X <,=,> Y ? */
236 /* ===> bit vector string conversion functions: */
239 /*@only@*/ charptr BitVector_to_Hex (wordptr addr);
241 ErrCode BitVector_from_Hex (/*@out@*/wordptr addr, charptr string);
244 ErrCode BitVector_from_Oct(/*@out@*/ wordptr addr, charptr string);
247 /*@only@*/ charptr BitVector_to_Bin (wordptr addr);
249 ErrCode BitVector_from_Bin (/*@out@*/ wordptr addr, charptr string);
252 /*@only@*/ charptr BitVector_to_Dec (wordptr addr);
254 ErrCode BitVector_from_Dec (/*@out@*/ wordptr addr, charptr string);
256 typedef struct BitVector_from_Dec_static_data BitVector_from_Dec_static_data;
258 BitVector_from_Dec_static_data *BitVector_from_Dec_static_Boot(N_word bits);
260 void BitVector_from_Dec_static_Shutdown(/*@null@*/ BitVector_from_Dec_static_data *data);
262 ErrCode BitVector_from_Dec_static(BitVector_from_Dec_static_data *data,
263 /*@out@*/ wordptr addr, charptr string);
266 /*@only@*/ charptr BitVector_to_Enum (wordptr addr);
268 ErrCode BitVector_from_Enum (/*@out@*/ wordptr addr, charptr string);
270 /* ===> bit vector bit operations, functions & tests: */
273 void BitVector_Bit_Off (/*@out@*/ wordptr addr, N_int indx); /* X = X \ {x} */
275 void BitVector_Bit_On (/*@out@*/ wordptr addr, N_int indx); /* X = X + {x} */
277 boolean BitVector_bit_flip (/*@out@*/ wordptr addr, N_int indx); /* (X+{x})\(X*{x}) */
280 boolean BitVector_bit_test (wordptr addr, N_int indx); /* {x} in X ? */
283 void BitVector_Bit_Copy (/*@out@*/ wordptr addr, N_int indx, boolean bit);
285 /* ===> bit vector bit shift & rotate functions: */
288 void BitVector_LSB (/*@out@*/ wordptr addr, boolean bit);
290 void BitVector_MSB (/*@out@*/ wordptr addr, boolean bit);
292 boolean BitVector_lsb_ (wordptr addr);
294 boolean BitVector_msb_ (wordptr addr);
296 boolean /*@alt void@*/ BitVector_rotate_left (wordptr addr);
298 boolean /*@alt void@*/ BitVector_rotate_right (wordptr addr);
300 boolean /*@alt void@*/ BitVector_shift_left (wordptr addr, boolean carry_in);
302 boolean /*@alt void@*/ BitVector_shift_right (wordptr addr, boolean carry_in);
304 void BitVector_Move_Left (wordptr addr, N_int bits);
306 void BitVector_Move_Right (wordptr addr, N_int bits);
308 /* ===> bit vector insert/delete bits: */
311 void BitVector_Insert (wordptr addr, N_int offset, N_int count,
314 void BitVector_Delete (wordptr addr, N_int offset, N_int count,
317 /* ===> bit vector arithmetic: */
320 boolean /*@alt void@*/ BitVector_increment (wordptr addr); /* X++ */
322 boolean /*@alt void@*/ BitVector_decrement (wordptr addr); /* X-- */
325 boolean /*@alt void@*/ BitVector_compute (wordptr X, wordptr Y, wordptr Z, boolean minus,
328 boolean /*@alt void@*/ BitVector_add (wordptr X, wordptr Y, wordptr Z, boolean *carry);
330 boolean /*@alt void@*/ BitVector_sub (wordptr X, wordptr Y, wordptr Z, boolean *carry);
332 boolean /*@alt void@*/ BitVector_inc (wordptr X, wordptr Y);
334 boolean /*@alt void@*/ BitVector_dec (wordptr X, wordptr Y);
337 void BitVector_Negate (wordptr X, wordptr Y);
339 void BitVector_Absolute (wordptr X, wordptr Y);
341 Z_int BitVector_Sign (wordptr addr);
343 ErrCode BitVector_Mul_Pos (wordptr X, wordptr Y, wordptr Z, boolean strict);
345 ErrCode BitVector_Multiply (wordptr X, wordptr Y, wordptr Z);
347 ErrCode BitVector_Div_Pos (wordptr Q, wordptr X, wordptr Y, wordptr R);
349 ErrCode BitVector_Divide (wordptr Q, wordptr X, wordptr Y, wordptr R);
351 ErrCode BitVector_GCD (wordptr X, wordptr Y, wordptr Z);
353 ErrCode BitVector_GCD2 (wordptr U, wordptr V, wordptr W, /* O */
354 wordptr X, wordptr Y); /* I */
356 ErrCode BitVector_Power (wordptr X, wordptr Y, wordptr Z);
358 /* ===> direct memory access functions: */
361 void BitVector_Block_Store(wordptr addr, charptr buffer, N_int length);
363 charptr BitVector_Block_Read (wordptr addr, /*@out@*/ N_intptr length);
365 /* ===> word array functions: */
368 void BitVector_Word_Store (wordptr addr, N_int offset, N_int value);
370 N_int BitVector_Word_Read (wordptr addr, N_int offset);
373 void BitVector_Word_Insert(wordptr addr, N_int offset, N_int count,
376 void BitVector_Word_Delete(wordptr addr, N_int offset, N_int count,
379 /* ===> arbitrary size chunk functions: */
382 void BitVector_Chunk_Store(wordptr addr, N_int chunksize,
383 N_int offset, N_long value);
385 N_long BitVector_Chunk_Read (wordptr addr, N_int chunksize,
388 /* ===> set operations: */
391 void Set_Union (wordptr X, wordptr Y, wordptr Z); /* X = Y + Z */
393 void Set_Intersection (wordptr X, wordptr Y, wordptr Z); /* X = Y * Z */
395 void Set_Difference (wordptr X, wordptr Y, wordptr Z); /* X = Y \ Z */
397 void Set_ExclusiveOr (wordptr X, wordptr Y, wordptr Z); /*(Y+Z)\(Y*Z)*/
399 void Set_Complement (wordptr X, wordptr Y); /* X = ~Y */
401 /* ===> set functions: */
404 boolean Set_subset (wordptr X, wordptr Y); /* X in Y ? */
407 N_int Set_Norm (wordptr addr); /* = | X | */
409 N_int Set_Norm2 (wordptr addr); /* = | X | */
411 N_int Set_Norm3 (wordptr addr); /* = | X | */
413 Z_long Set_Min (wordptr addr); /* = min(X) */
415 Z_long Set_Max (wordptr addr); /* = max(X) */
417 /* ===> matrix-of-booleans operations: */
420 void Matrix_Multiplication(wordptr X, N_int rowsX, N_int colsX,
421 wordptr Y, N_int rowsY, N_int colsY,
422 wordptr Z, N_int rowsZ, N_int colsZ);
425 void Matrix_Product (wordptr X, N_int rowsX, N_int colsX,
426 wordptr Y, N_int rowsY, N_int colsY,
427 wordptr Z, N_int rowsZ, N_int colsZ);
430 void Matrix_Closure (wordptr addr, N_int rows, N_int cols);
433 void Matrix_Transpose (wordptr X, N_int rowsX, N_int colsX,
434 wordptr Y, N_int rowsY, N_int colsY);
436 /*****************************************************************************/
438 /*****************************************************************************/
439 /* VERSION HISTORY: */
440 /*****************************************************************************/
442 /* Version 6.4 03.10.04 Added C++ comp. directives. Improved "Norm()". */
443 /* Version 6.3 28.09.02 Added "Create_List()" and "GCD2()". */
444 /* Version 6.2 15.09.02 Overhauled error handling. Fixed "GCD()". */
445 /* Version 6.1 08.10.01 Make VMS linker happy: _lsb,_msb => _lsb_,_msb_ */
446 /* Version 6.0 08.10.00 Corrected overflow handling. */
447 /* Version 5.8 14.07.00 Added "Power()". Changed "Copy()". */
448 /* Version 5.7 19.05.99 Quickened "Div_Pos()". Added "Product()". */
449 /* Version 5.6 02.11.98 Leading zeros eliminated in "to_Hex()". */
450 /* Version 5.5 21.09.98 Fixed bug of uninitialized "error" in Multiply. */
451 /* Version 5.4 07.09.98 Fixed bug of uninitialized "error" in Divide. */
452 /* Version 5.3 12.05.98 Improved Norm. Completed history. */
453 /* Version 5.2 31.03.98 Improved Norm. */
454 /* Version 5.1 09.03.98 No changes. */
455 /* Version 5.0 01.03.98 Major additions and rewrite. */
456 /* Version 4.2 16.07.97 Added is_empty, is_full. */
457 /* Version 4.1 30.06.97 Added word-ins/del, move-left/right, inc/dec. */
458 /* Version 4.0 23.04.97 Rewrite. Added bit shift and bool. matrix ops. */
459 /* Version 3.2 04.02.97 Added interval methods. */
460 /* Version 3.1 21.01.97 Fixed bug on 64 bit machines. */
461 /* Version 3.0 12.01.97 Added flip. */
462 /* Version 2.0 14.12.96 Efficiency and consistency improvements. */
463 /* Version 1.1 08.01.96 Added Resize and ExclusiveOr. */
464 /* Version 1.0 14.12.95 First version under UNIX (with Perl module). */
465 /* Version 0.9 01.11.93 First version of C library under MS-DOS. */
466 /* Version 0.1 ??.??.89 First version in Turbo Pascal under CP/M. */
468 /*****************************************************************************/
470 /*****************************************************************************/
473 /* mailto:sb@engelschall.com */
474 /* http://www.engelschall.com/u/sb/download/ */
476 /*****************************************************************************/
478 /*****************************************************************************/
480 /* Copyright (c) 1995 - 2004 by Steffen Beyer. */
481 /* All rights reserved. */
483 /*****************************************************************************/
485 /*****************************************************************************/
486 /* This package is free software; you can use, modify and redistribute */
487 /* it under the same terms as Perl itself, i.e., under the terms of */
488 /* the "Artistic License" or the "GNU General Public License". */
490 /* The C library at the core of this Perl module can additionally */
491 /* be used, modified and redistributed under the terms of the */
492 /* "GNU Library General Public License". */
494 /*****************************************************************************/
495 /* ARTISTIC LICENSE: */
496 /*****************************************************************************/
498 The "Artistic License"
502 The intent of this document is to state the conditions under which a
503 Package may be copied, such that the Copyright Holder maintains some
504 semblance of artistic control over the development of the package,
505 while giving the users of the package the right to use and distribute
506 the Package in a more-or-less customary fashion, plus the right to make
507 reasonable modifications.
511 "Package" refers to the collection of files distributed by the
512 Copyright Holder, and derivatives of that collection of files
513 created through textual modification.
515 "Standard Version" refers to such a Package if it has not been
516 modified, or has been modified in accordance with the wishes
517 of the Copyright Holder as specified below.
519 "Copyright Holder" is whoever is named in the copyright or
520 copyrights for the package.
522 "You" is you, if you're thinking about copying or distributing
525 "Reasonable copying fee" is whatever you can justify on the
526 basis of media cost, duplication charges, time of people involved,
527 and so on. (You will not be required to justify it to the
528 Copyright Holder, but only to the computing community at large
529 as a market that must bear the fee.)
531 "Freely Available" means that no fee is charged for the item
532 itself, though there may be fees involved in handling the item.
533 It also means that recipients of the item may redistribute it
534 under the same conditions they received it.
536 1. You may make and give away verbatim copies of the source form of the
537 Standard Version of this Package without restriction, provided that you
538 duplicate all of the original copyright notices and associated disclaimers.
540 2. You may apply bug fixes, portability fixes and other modifications
541 derived from the Public Domain or from the Copyright Holder. A Package
542 modified in such a way shall still be considered the Standard Version.
544 3. You may otherwise modify your copy of this Package in any way, provided
545 that you insert a prominent notice in each changed file stating how and
546 when you changed that file, and provided that you do at least ONE of the
549 a) place your modifications in the Public Domain or otherwise make them
550 Freely Available, such as by posting said modifications to Usenet or
551 an equivalent medium, or placing the modifications on a major archive
552 site such as uunet.uu.net, or by allowing the Copyright Holder to include
553 your modifications in the Standard Version of the Package.
555 b) use the modified Package only within your corporation or organization.
557 c) rename any non-standard executables so the names do not conflict
558 with standard executables, which must also be provided, and provide
559 a separate manual page for each non-standard executable that clearly
560 documents how it differs from the Standard Version.
562 d) make other distribution arrangements with the Copyright Holder.
564 4. You may distribute the programs of this Package in object code or
565 executable form, provided that you do at least ONE of the following:
567 a) distribute a Standard Version of the executables and library files,
568 together with instructions (in the manual page or equivalent) on where
569 to get the Standard Version.
571 b) accompany the distribution with the machine-readable source of
572 the Package with your modifications.
574 c) give non-standard executables non-standard names, and clearly
575 document the differences in manual pages (or equivalent), together
576 with instructions on where to get the Standard Version.
578 d) make other distribution arrangements with the Copyright Holder.
580 5. You may charge a reasonable copying fee for any distribution of this
581 Package. You may charge any fee you choose for support of this
582 Package. You may not charge a fee for this Package itself. However,
583 you may distribute this Package in aggregate with other (possibly
584 commercial) programs as part of a larger (possibly commercial) software
585 distribution provided that you do not advertise this Package as a
586 product of your own. You may embed this Package's interpreter within
587 an executable of yours (by linking); this shall be construed as a mere
588 form of aggregation, provided that the complete Standard Version of the
589 interpreter is so embedded.
591 6. The scripts and library files supplied as input to or produced as
592 output from the programs of this Package do not automatically fall
593 under the copyright of this Package, but belong to whoever generated
594 them, and may be sold commercially, and may be aggregated with this
595 Package. If such scripts or library files are aggregated with this
596 Package via the so-called "undump" or "unexec" methods of producing a
597 binary executable image, then distribution of such an image shall
598 neither be construed as a distribution of this Package nor shall it
599 fall under the restrictions of Paragraphs 3 and 4, provided that you do
600 not represent such an executable image as a Standard Version of this
603 7. C subroutines (or comparably compiled subroutines in other
604 languages) supplied by you and linked into this Package in order to
605 emulate subroutines and variables of the language defined by this
606 Package shall not be considered part of this Package, but are the
607 equivalent of input as in Paragraph 6, provided these subroutines do
608 not change the language in any way that would cause it to fail the
609 regression tests for the language.
611 8. Aggregation of this Package with a commercial distribution is always
612 permitted provided that the use of this Package is embedded; that is,
613 when no overt attempt is made to make this Package's interfaces visible
614 to the end user of the commercial distribution. Such use shall not be
615 construed as a distribution of this Package.
617 9. The name of the Copyright Holder may not be used to endorse or promote
618 products derived from this software without specific prior written permission.
620 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
621 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
622 WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
626 /*****************************************************************************/
627 /* GNU GENERAL PUBLIC LICENSE: */
628 /*****************************************************************************/
629 /* This program is free software; you can redistribute it and/or */
630 /* modify it under the terms of the GNU General Public License */
631 /* as published by the Free Software Foundation; either version 2 */
632 /* of the License, or (at your option) any later version. */
634 /* This program is distributed in the hope that it will be useful, */
635 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
636 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
637 /* GNU General Public License for more details. */
639 /* You should have received a copy of the GNU General Public License */
640 /* along with this program; if not, write to the */
641 /* Free Software Foundation, Inc., */
642 /* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
644 /*****************************************************************************/
645 /* GNU LIBRARY GENERAL PUBLIC LICENSE: */
646 /*****************************************************************************/
648 /* This library is free software; you can redistribute it and/or */
649 /* modify it under the terms of the GNU Library General Public */
650 /* License as published by the Free Software Foundation; either */
651 /* version 2 of the License, or (at your option) any later version. */
653 /* This library is distributed in the hope that it will be useful, */
654 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
655 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
656 /* Library General Public License for more details. */
658 /* You should have received a copy of the GNU Library General Public */
659 /* License along with this library; if not, write to the */
660 /* Free Software Foundation, Inc., */
661 /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
663 /* or download a copy from ftp://ftp.gnu.org/pub/gnu/COPYING.LIB-2.0 */
665 /*****************************************************************************/