bitset.c

Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 /* ====================================================================
00037  * ====================================================================
00038  *
00039  * Module: bitset.c
00040  *
00041  * Revision history:
00042  *  05-03-93 - Original Version
00043  *
00044  * Description:
00045  *
00046  *      Bitset implementation.
00047  *
00048  * ====================================================================
00049  * ====================================================================
00050  */
00051 
00052 #ifdef _KEEP_RCS_ID
00053 static const char source_file[] = __FILE__;
00054 #endif
00055 
00056 #include "defs.h"
00057 #include "mempool.h"
00058 #include "errors.h"
00059 #include "bitset.h"
00060 
00061 /* A designated bad memory pool to support assertions that the sets
00062  * shouldn't have to grow:
00063  */
00064 static MEM_POOL bad_pool_struct;
00065 static MEM_POOL *bad_pool = &bad_pool_struct;
00066 
00067 /* ====================================================================
00068  *
00069  *  bs_Malloc
00070  *
00071  *  Return a new BS*
00072  *
00073  *      length  is the number of words to allocate for members
00074  *      pool    is a pool in which to allocate it
00075  *
00076  *
00077  * ====================================================================
00078  */
00079 
00080 static BS*
00081 bs_Malloc(
00082   size_t    length,
00083   MEM_POOL *pool
00084 )
00085 {
00086     BS *new_set;
00087 
00088     Is_True(pool != bad_pool,("Shouldn't be allocating."));
00089 
00090     new_set = (BS *) TYPE_MEM_POOL_ALLOC_N(BS_WORD,pool,length + 1);
00091 
00092     BS_word_count(new_set) = length;
00093     return new_set;
00094 }
00095 
00096 /* ====================================================================
00097  *
00098  *  bs_Realloc
00099  *
00100  *  Grows the allocation of a BS, potentially returning a new pointer
00101  *  to it.
00102  *
00103  *      set     is the set to grow
00104  *      length  is the new number of words to allocate for members
00105  *      pool    is a pool to use for this
00106  *
00107  * ====================================================================
00108  */
00109 
00110 static BS*
00111 bs_Realloc(
00112   BS       *set,
00113   size_t    length,
00114   MEM_POOL *pool
00115 )
00116 {
00117   BS    *new_set;
00118   BS_ELT i;
00119   BS_ELT old_length = BS_word_count(set);
00120   size_t new_length;
00121 
00122   Is_True(pool != bad_pool,("Shouldn't be allocating."));
00123 
00124   if (length <= old_length) return set;
00125 
00126   /* extend length to be a power of 2 */
00127   for (new_length = 2; new_length < length; new_length <<= 1);
00128   length = new_length;
00129 
00130   new_set = (BS *)TYPE_MEM_POOL_REALLOC_N(BS_WORD, pool, set, old_length+1,
00131                                           length+1);
00132 
00133   if (!MEM_POOL_Zeroed(pool))
00134     for ( i = old_length; i < length; ++i )
00135       BS_word(new_set,i) = bs_ZEROS;
00136 
00137   BS_word_count(new_set) = length;
00138 
00139   return new_set;
00140 }
00141 
00142 /* ====================================================================
00143  *
00144  *  BS_Create
00145  *
00146  *  See interface description
00147  *
00148  * ====================================================================
00149  */
00150 
00151 extern BS*
00152 BS_Create(
00153   BS_ELT    size,
00154   MEM_POOL *pool
00155 )
00156 {
00157   Is_True(size >= 0,("BS_Create called with negative size."));
00158 
00159   return bs_Malloc(bs_QBPW(size + (BITS_PER_BS_WORD - 1)),pool);
00160 }
00161 
00162 /* ====================================================================
00163  *
00164  *  BS_Size_Alloc_Size
00165  *
00166  *  See interface description
00167  *
00168  * ====================================================================
00169  */
00170 
00171 extern size_t
00172 BS_Size_Alloc_Size(
00173   BS_ELT size
00174 )
00175 {
00176   return (1 + bs_QBPW(size + (BITS_PER_BS_WORD - 1) )) * sizeof(BS_WORD);
00177 }
00178 
00179 /* ====================================================================
00180  *
00181  *  BS_Alloc_Size
00182  *
00183  *  See interface description
00184  *
00185  * ====================================================================
00186  */
00187 
00188 extern size_t
00189 BS_Alloc_Size(
00190   BS *set
00191 )
00192 {
00193   return (BS_word_count(set) + 1) * sizeof(BS_WORD);
00194 }
00195 
00196 /* ====================================================================
00197  *
00198  *  BS_ClearD
00199  *
00200  *  See interface description
00201  *
00202  * ====================================================================
00203  */
00204 
00205 extern BS*
00206 BS_ClearD(
00207   BS *set
00208 )
00209 {
00210   BS_ELT i;
00211 
00212   for ( i = 0; i < BS_word_count(set); ++i )
00213     BS_word(set,i) = bs_ZEROS;
00214 
00215   return set;
00216 }
00217 
00218 /* ====================================================================
00219  *
00220  *  BS_Create_Empty
00221  *
00222  *  See interface description
00223  *
00224  * ====================================================================
00225  */
00226 
00227 extern BS*
00228 BS_Create_Empty(
00229   BS_ELT    size,
00230   MEM_POOL *pool
00231 )
00232 {
00233   BS *set = BS_Create(size, pool);
00234   if (!MEM_POOL_Zeroed(pool))
00235     BS_ClearD(set);
00236   return set;
00237 }
00238 
00239 /* ====================================================================
00240  *
00241  *  BS_Resize
00242  *
00243  *  See interface description
00244  *
00245  * ====================================================================
00246  */
00247 
00248 extern BS*
00249 BS_ResizeD(
00250   BS*       set,
00251   BS_ELT    new_size,
00252   MEM_POOL *pool
00253 )
00254 {
00255   BS_ELT new_words = bs_QBPW(new_size + (BITS_PER_BS_WORD - 1));
00256 
00257   if ( new_words > BS_word_count(set) ) {
00258     set = bs_Realloc(set,new_words,pool);
00259   }
00260   return set;
00261 }
00262 
00263 #ifndef MONGOOSE_BE
00264 /* ====================================================================
00265  *
00266  *  BS_Range
00267  *
00268  *  See interface description
00269  *
00270  * ====================================================================
00271  */
00272 
00273 extern BS*
00274 BS_Range(
00275   BS_ELT    low,
00276   BS_ELT    high,
00277   MEM_POOL *pool
00278 )
00279 {
00280   return BS_RangeD(BS_Create(high + 1,pool),low,high,bad_pool);
00281 }
00282 #endif /* MONGOOSE_BE */
00283 
00284 /* ====================================================================
00285  *
00286  *  BS_RangeD
00287  *
00288  *  See interface description
00289  *
00290  * ====================================================================
00291  */
00292 
00293 extern BS*
00294 BS_RangeD(
00295   BS*       set,
00296   BS_ELT    low,
00297   BS_ELT    high,
00298   MEM_POOL *pool
00299 )
00300 {
00301   BS_ELT  first_w, last_w, first_b, last_b;
00302   BS_ELT  i;
00303 
00304   Is_True(low >= 0,
00305             ("BS_RangeD called with negative low element."));
00306 
00307   Is_True(high - low + 1 >= 0,
00308             ("BS_RangeD called with negative range size."));
00309 
00310   if ( low > high )
00311     return BS_ClearD(set);
00312 
00313   first_w = bs_QBPW(low);     /* index of first non 0 word */
00314   last_w  = bs_QBPW(high);    /* index of last non 0 word  */
00315   first_b = bs_QBPB(low);
00316   last_b  = bs_QBPB(high);
00317 
00318   /* Reallocate if necessary.
00319    */
00320   if ( last_w >= BS_word_count(set) )
00321     set = bs_Realloc(set,last_w + 1,pool);
00322 
00323   set = BS_ClearD(set);
00324 
00325   /* Set every byte above the first up to and including the last to be
00326    * all ones.  This complexity is required in order to make the
00327    * representation endian independent.  We win back with a fast
00328    * endian independent ffo for machines with load byte instructions.
00329    */
00330 
00331   /* Set every word above the first and below the last to be all ones.
00332    */
00333   for ( i = first_w + 1; i < last_w; ++i )
00334     BS_word(set,i) = bs_ONES;
00335 
00336   /* Set every byte in the first word above the first byte to be all
00337    * ones.
00338    */
00339   /* for ( i = first_b; i < bs_PBPB(first_w + 1); ++i ) shin 03-29-95 */
00340   for ( i = first_b; i < bs_PBytesPW(first_w + 1) && i <= last_b; ++i )
00341     BS_byte(set,i) = (BS_BYTE)bs_ONES;
00342 
00343   /* Set every byte in the last word up to and including the last byte
00344    * to be all ones.
00345    */
00346   /* for ( i = bs_PBPB(last_w); i <= last_b; ++i ) fchow 04-11-95 */
00347   if (first_w != last_w)
00348     for ( i = bs_PBytesPW(last_w); i <= last_b; ++i )
00349       BS_byte(set,i) = (BS_BYTE)bs_ONES;
00350 
00351   /* Fix up the first and last bytes.  Order is important!  Remember
00352    * that we have already set all the bits in the last byte and that
00353    * the first and last bytes might be the same.
00354    */
00355   BS_byte(set,first_b) = bs_ONES << bs_RBPB(low);
00356   BS_byte(set,last_b) &=
00357     bs_ONES >> ((BITS_PER_BS_WORD - 1) - bs_RBPB(high));
00358 
00359   return set;
00360 }
00361 
00362 /* ====================================================================
00363  *
00364  *  BS_Singleton
00365  *
00366  *  See interface description
00367  *
00368  * ====================================================================
00369  */
00370 
00371 extern BS*
00372 BS_Singleton(
00373   BS_ELT element,
00374   MEM_POOL *pool
00375 )
00376 {
00377   return BS_SingletonD(BS_Create(element + 1,pool),
00378                        element,
00379                        bad_pool);
00380 }
00381 
00382 /* ====================================================================
00383  *
00384  *  BS_SingletonD
00385  *
00386  *  See interface description
00387  *
00388  * ====================================================================
00389  */
00390 
00391 extern BS*
00392 BS_SingletonD(
00393   BS       *set,
00394   BS_ELT    element,
00395   MEM_POOL *pool
00396 )
00397 {
00398   BS_ELT word;
00399 
00400   Is_True(element >= 0,
00401             ("BS_SingletonD called with negative element."));
00402 
00403   word = bs_QBPW(element);
00404 
00405   /* Reallocate if necessary.
00406    */
00407   if ( word >= BS_word_count(set) )
00408     set = bs_Realloc(set,word + 1,pool);
00409 
00410   set = BS_ClearD(set);
00411   BS_byte(set,bs_QBPB(element)) = bs_ONE << bs_RBPB(element);
00412 
00413   return set;
00414 }
00415 
00416 /* ====================================================================
00417  *
00418  *  BS_Universe
00419  *
00420  *  See interface description
00421  *
00422  * ====================================================================
00423  */
00424 
00425 extern BS*
00426 BS_Universe(
00427   BS_ELT    size,
00428   MEM_POOL *pool
00429 )
00430 {
00431   return BS_UniverseD(BS_Create(size,pool),size,bad_pool);
00432 }
00433 
00434 /* ====================================================================
00435  *
00436  *  BS_UniverseD
00437  *
00438  *  See interface description
00439  *
00440  * ====================================================================
00441  */
00442 
00443 extern BS*
00444 BS_UniverseD(
00445   BS       *set,
00446   BS_ELT    size,
00447   MEM_POOL *pool
00448 )
00449 {
00450   return BS_RangeD(set,0,size - 1,bad_pool);
00451 }
00452 
00453 /* ====================================================================
00454  *
00455  *  BS_Copy
00456  *
00457  *  See interface description
00458  *
00459  * ====================================================================
00460  */
00461 
00462 extern BS*
00463 BS_Copy(
00464   BS       *set,
00465   MEM_POOL *pool
00466 )
00467 {
00468   size_t size;
00469   BS*    newset;
00470   BS_ELT i;
00471 
00472   size = BS_word_count(set);
00473   newset = bs_Malloc(size,pool);
00474 
00475   for ( i = 0; i < size; ++i )
00476     BS_word(newset,i) = BS_word(set,i);
00477 
00478   return newset;
00479 }
00480 
00481 /* ====================================================================
00482  *
00483  *  BS_CopyD
00484  *
00485  *  See interface description
00486  *
00487  * ====================================================================
00488  */
00489 
00490 extern BS*
00491 BS_CopyD(
00492   BS       *set1,
00493   BS       *set2,
00494   MEM_POOL *pool
00495 )
00496 {
00497   BS_ELT i;
00498   BS_ELT size1 = BS_word_count(set1);
00499   BS_ELT size2 = BS_word_count(set2);
00500 
00501   /* Reallocate if necessary.
00502    */
00503   if ( size1 < size2 )
00504     set1 = bs_Realloc(set1,size2,pool);
00505   else {
00506     /* Else zero out excess.
00507      */
00508     for ( i = size2; i < size1; ++i )
00509       BS_word(set1,i) = bs_ZEROS;
00510   }
00511 
00512   /* Copy in common part.
00513    */
00514   for ( i = 0; i < size2; ++i )
00515     BS_word(set1,i) = BS_word(set2,i);
00516 
00517   return set1;
00518 }
00519 
00520 /* Mapping from 8 bit unsigned integers to the index of the first one
00521  * bit:
00522  */
00523 static const mUINT8 first_one [256] = {
00524   0, /*   0 */
00525   0, /*   1 */
00526   1, /*   2 */
00527   0, /*   3 */
00528   2, /*   4 */
00529   0, /*   5 */
00530   1, /*   6 */
00531   0, /*   7 */
00532   3, /*   8 */
00533   0, /*   9 */
00534   1, /*  10 */
00535   0, /*  11 */
00536   2, /*  12 */
00537   0, /*  13 */
00538   1, /*  14 */
00539   0, /*  15 */
00540   4, /*  16 */
00541   0, /*  17 */
00542   1, /*  18 */
00543   0, /*  19 */
00544   2, /*  20 */
00545   0, /*  21 */
00546   1, /*  22 */
00547   0, /*  23 */
00548   3, /*  24 */
00549   0, /*  25 */
00550   1, /*  26 */
00551   0, /*  27 */
00552   2, /*  28 */
00553   0, /*  29 */
00554   1, /*  30 */
00555   0, /*  31 */
00556   5, /*  32 */
00557   0, /*  33 */
00558   1, /*  34 */
00559   0, /*  35 */
00560   2, /*  36 */
00561   0, /*  37 */
00562   1, /*  38 */
00563   0, /*  39 */
00564   3, /*  40 */
00565   0, /*  41 */
00566   1, /*  42 */
00567   0, /*  43 */
00568   2, /*  44 */
00569   0, /*  45 */
00570   1, /*  46 */
00571   0, /*  47 */
00572   4, /*  48 */
00573   0, /*  49 */
00574   1, /*  50 */
00575   0, /*  51 */
00576   2, /*  52 */
00577   0, /*  53 */
00578   1, /*  54 */
00579   0, /*  55 */
00580   3, /*  56 */
00581   0, /*  57 */
00582   1, /*  58 */
00583   0, /*  59 */
00584   2, /*  60 */
00585   0, /*  61 */
00586   1, /*  62 */
00587   0, /*  63 */
00588   6, /*  64 */
00589   0, /*  65 */
00590   1, /*  66 */
00591   0, /*  67 */
00592   2, /*  68 */
00593   0, /*  69 */
00594   1, /*  70 */
00595   0, /*  71 */
00596   3, /*  72 */
00597   0, /*  73 */
00598   1, /*  74 */
00599   0, /*  75 */
00600   2, /*  76 */
00601   0, /*  77 */
00602   1, /*  78 */
00603   0, /*  79 */
00604   4, /*  80 */
00605   0, /*  81 */
00606   1, /*  82 */
00607   0, /*  83 */
00608   2, /*  84 */
00609   0, /*  85 */
00610   1, /*  86 */
00611   0, /*  87 */
00612   3, /*  88 */
00613   0, /*  89 */
00614   1, /*  90 */
00615   0, /*  91 */
00616   2, /*  92 */
00617   0, /*  93 */
00618   1, /*  94 */
00619   0, /*  95 */
00620   5, /*  96 */
00621   0, /*  97 */
00622   1, /*  98 */
00623   0, /*  99 */
00624   2, /* 100 */
00625   0, /* 101 */
00626   1, /* 102 */
00627   0, /* 103 */
00628   3, /* 104 */
00629   0, /* 105 */
00630   1, /* 106 */
00631   0, /* 107 */
00632   2, /* 108 */
00633   0, /* 109 */
00634   1, /* 110 */
00635   0, /* 111 */
00636   4, /* 112 */
00637   0, /* 113 */
00638   1, /* 114 */
00639   0, /* 115 */
00640   2, /* 116 */
00641   0, /* 117 */
00642   1, /* 118 */
00643   0, /* 119 */
00644   3, /* 120 */
00645   0, /* 121 */
00646   1, /* 122 */
00647   0, /* 123 */
00648   2, /* 124 */
00649   0, /* 125 */
00650   1, /* 126 */
00651   0, /* 127 */
00652   7, /* 128 */
00653   0, /* 129 */
00654   1, /* 130 */
00655   0, /* 131 */
00656   2, /* 132 */
00657   0, /* 133 */
00658   1, /* 134 */
00659   0, /* 135 */
00660   3, /* 136 */
00661   0, /* 137 */
00662   1, /* 138 */
00663   0, /* 139 */
00664   2, /* 140 */
00665   0, /* 141 */
00666   1, /* 142 */
00667   0, /* 143 */
00668   4, /* 144 */
00669   0, /* 145 */
00670   1, /* 146 */
00671   0, /* 147 */
00672   2, /* 148 */
00673   0, /* 149 */
00674   1, /* 150 */
00675   0, /* 151 */
00676   3, /* 152 */
00677   0, /* 153 */
00678   1, /* 154 */
00679   0, /* 155 */
00680   2, /* 156 */
00681   0, /* 157 */
00682   1, /* 158 */
00683   0, /* 159 */
00684   5, /* 160 */
00685   0, /* 161 */
00686   1, /* 162 */
00687   0, /* 163 */
00688   2, /* 164 */
00689   0, /* 165 */
00690   1, /* 166 */
00691   0, /* 167 */
00692   3, /* 168 */
00693   0, /* 169 */
00694   1, /* 170 */
00695   0, /* 171 */
00696   2, /* 172 */
00697   0, /* 173 */
00698   1, /* 174 */
00699   0, /* 175 */
00700   4, /* 176 */
00701   0, /* 177 */
00702   1, /* 178 */
00703   0, /* 179 */
00704   2, /* 180 */
00705   0, /* 181 */
00706   1, /* 182 */
00707   0, /* 183 */
00708   3, /* 184 */
00709   0, /* 185 */
00710   1, /* 186 */
00711   0, /* 187 */
00712   2, /* 188 */
00713   0, /* 189 */
00714   1, /* 190 */
00715   0, /* 191 */
00716   6, /* 192 */
00717   0, /* 193 */
00718   1, /* 194 */
00719   0, /* 195 */
00720   2, /* 196 */
00721   0, /* 197 */
00722   1, /* 198 */
00723   0, /* 199 */
00724   3, /* 200 */
00725   0, /* 201 */
00726   1, /* 202 */
00727   0, /* 203 */
00728   2, /* 204 */
00729   0, /* 205 */
00730   1, /* 206 */
00731   0, /* 207 */
00732   4, /* 208 */
00733   0, /* 209 */
00734   1, /* 210 */
00735   0, /* 211 */
00736   2, /* 212 */
00737   0, /* 213 */
00738   1, /* 214 */
00739   0, /* 215 */
00740   3, /* 216 */
00741   0, /* 217 */
00742   1, /* 218 */
00743   0, /* 219 */
00744   2, /* 220 */
00745   0, /* 221 */
00746   1, /* 222 */
00747   0, /* 223 */
00748   5, /* 224 */
00749   0, /* 225 */
00750   1, /* 226 */
00751   0, /* 227 */
00752   2, /* 228 */
00753   0, /* 229 */
00754   1, /* 230 */
00755   0, /* 231 */
00756   3, /* 232 */
00757   0, /* 233 */
00758   1, /* 234 */
00759   0, /* 235 */
00760   2, /* 236 */
00761   0, /* 237 */
00762   1, /* 238 */
00763   0, /* 239 */
00764   4, /* 240 */
00765   0, /* 241 */
00766   1, /* 242 */
00767   0, /* 243 */
00768   2, /* 244 */
00769   0, /* 245 */
00770   1, /* 246 */
00771   0, /* 247 */
00772   3, /* 248 */
00773   0, /* 249 */
00774   1, /* 250 */
00775   0, /* 251 */
00776   2, /* 252 */
00777   0, /* 253 */
00778   1, /* 254 */
00779   0, /* 255 */
00780 };
00781 
00782 /* ====================================================================
00783  *
00784  *  BS_Choose
00785  *
00786  *  See interface description
00787  *
00788  * ====================================================================
00789  */
00790 
00791 extern BS_ELT
00792 BS_Choose(
00793   const BS* set
00794 )
00795 {
00796   BS_ELT i, j;
00797 
00798   for ( i = 0; i < BS_word_count(set); ++i ) {
00799     if ( BS_word(set,i) != bs_ZEROS ) {
00800       BS_ELT first_byte = bs_PBytesPW(i);
00801 
00802       for ( j = 0; j < BYTES_PER_BS_WORD; ++j ) {
00803         BS_BYTE byte = BS_byte(set,j + first_byte);
00804 
00805         if ( byte != bs_ZEROS )
00806           return first_one[byte] + bs_PBPB(j + first_byte);
00807       }
00808 
00809       Is_True(FALSE,("Word not zero, but no byte found"));
00810     }
00811   }
00812 
00813   return BS_CHOOSE_FAILURE;
00814 }
00815 
00816 /* ====================================================================
00817  *
00818  *  BS_Choose_Range
00819  *
00820  *  See interface description
00821  *
00822  * ====================================================================
00823  */
00824 
00825 extern BS_ELT
00826 BS_Choose_Range(
00827   BS    *set,
00828   BS_ELT low,
00829   BS_ELT high
00830 )
00831 {
00832   BS_BYTE byte;
00833   BS_ELT  last_first_word_full_byte, i, j;
00834   BS_ELT  first_word, last_word;
00835   BS_ELT  first_byte, last_byte;
00836   BS_ELT  last_elt_in_set;
00837 
00838   Is_True(low >= 0,
00839           ("BS_Choose_Range called with negative low element."));
00840   Is_True(high - low >= -1,
00841           ("BS_Choose_Range called with negative range size."));
00842 
00843   /* Adjust high to be within the set:
00844    */
00845   last_elt_in_set = bs_PBPW(BS_word_count(set)) - 1;
00846   if ( high > last_elt_in_set )
00847     high = last_elt_in_set;
00848 
00849   /* The following check is needed becaause we won't catch it if low is in
00850    * the next byte from high.
00851    */
00852   if ( low > high )
00853     return BS_CHOOSE_FAILURE;
00854 
00855   first_byte = bs_QBPB(low);
00856   last_byte  = bs_QBPB(high);
00857 
00858   /* Check first byte.  Since it also might be the last byte, we have
00859    * to be careful to mask out both ends of the range.  After this we
00860    * can assume the first byte and last byte are different, which
00861    * simplifies things quite a bit.
00862    */
00863   byte = BS_byte(set,first_byte) & (bs_ONES << bs_RBPB(low));
00864   if ( first_byte == last_byte )
00865     byte &= bs_ONES >> ((BITS_PER_BS_WORD - 1) - bs_RBPB(high));
00866 
00867   if ( byte != bs_ZEROS )
00868     return first_one[byte] + bs_PBPB(first_byte);
00869   else if (first_byte == last_byte)
00870     return BS_CHOOSE_FAILURE;
00871 
00872   /* Chack remaining full bytes in the first word.  Since the last
00873    * byte might be in the first word, we have to avoid checking it as
00874    * a full byte.
00875    */
00876   first_word = bs_QBPW(low);
00877   last_first_word_full_byte = (first_word + 1) * sizeof(BS_WORD) - 1;
00878 
00879   if ( last_first_word_full_byte >= last_byte )
00880     last_first_word_full_byte = last_byte - 1;
00881 
00882   for ( i = first_byte + 1; i <= last_first_word_full_byte;  ++i ) {
00883     byte = BS_byte(set,i);
00884 
00885     if ( byte != bs_ZEROS )
00886       return first_one[byte] + bs_PBPB(i);
00887   }
00888 
00889   /* Now we are either full word aligned or the last byte was in the
00890    * first word.  Check full words above the first word and below the
00891    * last word.  Notice that this loop won't trip if the last byte was
00892    * in the first word (or the next one for that matter.)
00893    */
00894   last_word = bs_QBPW(high);
00895 
00896   for ( i = first_word + 1; i < last_word; ++i ) {
00897     if ( BS_word(set,i) != bs_ZEROS ) {
00898       /* Found the word, now locate the byte in that word.
00899        */
00900       for ( j = 0; j < sizeof(BS_WORD); ++j ) {
00901         byte = BS_byte(set, j + i * BYTES_PER_BS_WORD);
00902 
00903         if ( byte != 0 )
00904           return first_one[byte] + bs_PBPB(j) + bs_PBPW(i);
00905       }
00906     }
00907   }
00908 
00909   /* Check any unchecked full bytes in the last word.  This loop will
00910    * only trip if the last word is greater than the first word, since
00911    * otherwise the loop above leaves i above the first word, no matter
00912    * what.  In fact, at this point i is the index of the first
00913    * unchecked word.
00914    */
00915   for ( i *= sizeof(BS_WORD); i < last_byte; ++i ) {
00916     byte = BS_byte(set,i);
00917 
00918     if ( byte != 0 )
00919       return first_one[byte] + 8 * i;
00920   }
00921 
00922   /* Now we'll check the final byte, which we have assumed is partial.
00923    * Even if it is full, the following will work.
00924    */
00925   byte =   BS_byte(set,last_byte)
00926          & (bs_ONES >> ((BITS_PER_BS_WORD - 1) - bs_RBPB(high)));
00927 
00928   if ( byte != 0 )
00929     return first_one[byte] + bs_PBPB(last_byte);
00930 
00931   return BS_CHOOSE_FAILURE;
00932 }
00933 
00934 
00935 /* =======================================================================
00936  *
00937  *  BS_Choose_Next
00938  *
00939  *  See interface description.
00940  *
00941  * =======================================================================
00942  */
00943 
00944 extern BS_ELT
00945 BS_Choose_Next(
00946   const BS*    set,
00947   BS_ELT bound 
00948 )
00949 {
00950   BS_ELT  i, j, inx_first_byte, inx_first_byte_second_word, word_count;
00951   BS_BYTE byte;
00952 
00953   ++bound;  /* Now bound is inclusive. */
00954 
00955   if ( bound >= bs_PBPW(BS_word_count(set)) )
00956     return BS_CHOOSE_FAILURE;
00957 
00958   /* Search first byte with stuff below bound masked out:
00959    */
00960   inx_first_byte = bs_QBPB(bound);
00961   byte = BS_byte(set,inx_first_byte) & (bs_ONES << bs_RBPB(bound));
00962   if ( byte != bs_ZEROS )
00963     return first_one[byte] + bs_PBPB(inx_first_byte);
00964 
00965   /* Search remaining bytes in first word:
00966    */
00967   inx_first_byte_second_word = (bs_QBPW(bound) + 1) * sizeof(BS_WORD);
00968 
00969   for ( i = inx_first_byte + 1; i < inx_first_byte_second_word; ++i ) {
00970     byte = BS_byte(set,i);
00971 
00972     if ( byte != bs_ZEROS )
00973       return first_one[byte] + bs_PBPB(i);
00974   }
00975 
00976   /* search remaining words:
00977    */
00978   word_count = BS_word_count(set);
00979 
00980   for ( i = bs_QBPW(bound) + 1; i < word_count; ++i ) {
00981     if ( BS_word(set,i) != bs_ZEROS ) {
00982       BS_ELT first_byte = bs_PBytesPW(i);
00983 
00984       /* There's something in this word, search each byte for it:
00985        */
00986       for ( j = 0; j < BYTES_PER_BS_WORD; ++j ) {
00987         BS_BYTE byte = BS_byte(set,j + first_byte);
00988 
00989         if ( byte != bs_ZEROS )
00990           return first_one[byte] + bs_PBPB(j + first_byte);
00991       }
00992 
00993       Is_True(FALSE,("Word not zero, but no byte found"));
00994     }
00995   }
00996     
00997   return BS_CHOOSE_FAILURE;
00998 }
00999 
01000 
01001 /* ====================================================================
01002  *
01003  *  BS_Intersection_Choose
01004  *
01005  *  See interface description
01006  *
01007  * ====================================================================
01008  */
01009 
01010 extern BS_ELT
01011 BS_Intersection_Choose(
01012   BS* set1,
01013   BS* set2
01014 )
01015 {
01016   BS_ELT i, j;
01017   BS_ELT last_word;
01018 
01019   if ( BS_word_count(set1) < BS_word_count(set2) )
01020     last_word = BS_word_count(set1);
01021   else
01022     last_word = BS_word_count(set2);
01023 
01024   for ( i = 0; i < last_word; ++i ) {
01025     if ( (BS_word(set1,i) & BS_word(set2,i)) != bs_ZEROS ) {
01026       BS_ELT first_byte = bs_PBytesPW(i);
01027 
01028       for ( j = 0; j < BYTES_PER_BS_WORD; ++j ) {
01029         BS_BYTE byte =   BS_byte(set1,j + first_byte)
01030                        & BS_byte(set2,j + first_byte);
01031 
01032         if ( byte != bs_ZEROS )
01033           return first_one[byte] + bs_PBPB(j + first_byte);
01034       }
01035 
01036       Is_True(FALSE,("Word not zero, but no byte found"));
01037     }
01038   }
01039 
01040   return BS_CHOOSE_FAILURE;
01041 }
01042 
01043 
01044 
01045 /* =======================================================================
01046  *
01047  *  BS_Intersection_Choose_Next
01048  *
01049  *  See interface description.
01050  *
01051  * =======================================================================
01052  */
01053 
01054 extern BS_ELT
01055 BS_Intersection_Choose_Next(
01056   BS*    set1,
01057   BS*    set2,
01058   BS_ELT bound 
01059 )
01060 {
01061   BS_ELT  i, j, inx_first_byte, inx_first_byte_second_word, word_count;
01062   BS_BYTE byte;
01063 
01064   if ( BS_word_count(set1) < BS_word_count(set2) )
01065     word_count = BS_word_count(set1);
01066   else
01067     word_count = BS_word_count(set2);
01068 
01069   ++bound;  /* Now bound is inclusive. */
01070 
01071   if ( bound >= bs_PBPW(word_count) )
01072     return BS_CHOOSE_FAILURE;
01073 
01074   /* Search first byte with stuff below bound masked out:
01075    */
01076   inx_first_byte = bs_QBPB(bound);
01077   byte =   BS_byte(set1,inx_first_byte)
01078          & BS_byte(set2,inx_first_byte)
01079          & (bs_ONES << bs_RBPB(bound));
01080   if ( byte != bs_ZEROS )
01081     return first_one[byte] + bs_PBPB(inx_first_byte);
01082 
01083   /* Search remaining bytes in first word:
01084    */
01085   inx_first_byte_second_word = (bs_QBPW(bound) + 1) * sizeof(BS_WORD);
01086 
01087   for ( i = inx_first_byte + 1; i < inx_first_byte_second_word; ++i ) {
01088     byte =   BS_byte(set1,i)
01089            & BS_byte(set2,i);
01090 
01091     if ( byte != bs_ZEROS )
01092       return first_one[byte] + bs_PBPB(i);
01093   }
01094 
01095   /* search remaining words:
01096    */
01097   for ( i = bs_QBPW(bound) + 1; i < word_count; ++i ) {
01098     if ( (BS_word(set1,i) & BS_word(set2,i)) != bs_ZEROS ) {
01099       BS_ELT first_byte = bs_PBytesPW(i);
01100 
01101       /* There's something in this word, search each byte for it:
01102        */
01103       for ( j = 0; j < BYTES_PER_BS_WORD; ++j ) {
01104         BS_BYTE byte =   BS_byte(set1,j + first_byte)
01105                        & BS_byte(set2,j + first_byte);
01106 
01107         if ( byte != bs_ZEROS )
01108           return first_one[byte] + bs_PBPB(j + first_byte);
01109       }
01110 
01111       Is_True(FALSE,("Word not zero, but no byte found"));
01112     }
01113   }
01114     
01115   return BS_CHOOSE_FAILURE;
01116 }
01117 
01118 
01119 
01120 /* ====================================================================
01121  *
01122  *  BS_Difference
01123  *
01124  *  See interface description
01125  *
01126  * ====================================================================
01127  */
01128 
01129 extern BS*
01130 BS_Difference(
01131   BS       *set1,
01132   BS       *set2,
01133   MEM_POOL *pool
01134 )
01135 {
01136   BS    *set;
01137   BS_ELT i;
01138   BS_ELT size1   = BS_word_count(set1);
01139   BS_ELT size2   = BS_word_count(set2);
01140   BS_ELT minsize = Min(size1,size2);
01141 
01142   set = bs_Malloc(size1,pool);
01143 
01144   /* Common part: copy in the difference.
01145    */
01146   for ( i = 0; i < minsize; ++i )
01147     BS_word(set,i) = BS_word(set1,i) & ~BS_word(set2,i);
01148 
01149   /* Excess of set1 over set2: just copy it in.
01150    */
01151   for ( i = minsize; i < size1; ++i )
01152     BS_word(set,i) = BS_word(set1,i);
01153 
01154   return set;
01155 }
01156 
01157 /* ====================================================================
01158  *
01159  *  BS_DifferenceD
01160  *
01161  *  See interface description
01162  *
01163  * ====================================================================
01164  */
01165 
01166 extern BS*
01167 BS_DifferenceD(
01168   BS* set1,
01169   BS* set2
01170 )
01171 {
01172   BS_ELT i;
01173   BS_ELT minsize = Min(BS_word_count(set1),BS_word_count(set2));
01174 
01175   for ( i = 0; i < minsize; ++i )
01176     BS_word(set1,i) &= ~BS_word(set2,i);
01177 
01178   return set1;
01179 }
01180 
01181 /* ====================================================================
01182  *
01183  *  BS_Difference1
01184  *
01185  *  See interface description
01186  *
01187  * ====================================================================
01188  */
01189 
01190 extern BS*
01191 BS_Difference1(
01192   BS       *set,
01193   BS_ELT    x,
01194   MEM_POOL *pool
01195 )
01196 {
01197   return BS_Difference1D(BS_Copy(set,pool),x);
01198 }
01199 
01200 /* ====================================================================
01201  *
01202  *  BS_Difference1D
01203  *
01204  *  See interface description
01205  *
01206  * ====================================================================
01207  */
01208 
01209 extern BS*
01210 BS_Difference1D(
01211   BS    *set,
01212   BS_ELT x
01213 )
01214 {
01215   Is_True(x >= 0,
01216             ("BS_Difference1D called with negative element."));
01217 
01218   if ( bs_QBPW(x) < BS_word_count(set) )
01219     BS_byte(set,bs_QBPB(x)) &= ~(bs_ONE << bs_RBPB(x));
01220 
01221   return set;
01222 }
01223 
01224 /* ====================================================================
01225  *
01226  *  BS_Intersection
01227  *
01228  *  See interface description
01229  *
01230  * ====================================================================
01231  */
01232 
01233 extern BS*
01234 BS_Intersection(
01235   BS       *set1,
01236   BS       *set2,
01237   MEM_POOL *pool
01238 )
01239 {
01240   BS_ELT i;
01241   BS_ELT size;
01242   BS    *newset;
01243 
01244   if ( BS_word_count(set1) < BS_word_count(set2) )
01245     size = BS_word_count(set1);
01246   else
01247     size = BS_word_count(set2);
01248 
01249   newset = bs_Malloc(size,pool);
01250 
01251   for ( i = 0; i < size; ++i )
01252     BS_word(newset,i) = BS_word(set1,i) & BS_word(set2,i);
01253 
01254   return newset;
01255 }
01256 
01257 /* ====================================================================
01258  *
01259  *  BS_IntersectionD
01260  *
01261  *  See interface description
01262  *
01263  * ====================================================================
01264  */
01265 
01266 extern BS*
01267 BS_IntersectionD(
01268   BS* set1,
01269   BS* set2
01270 )
01271 {
01272   BS_ELT i;
01273   BS_ELT minsize;
01274 
01275   minsize = MIN( BS_word_count(set1), BS_word_count(set2) );
01276 
01277   /* Form intersection of common part:
01278    */
01279   for ( i = 0; i < minsize; ++i )
01280     BS_word(set1,i) &= BS_word(set2,i);
01281 
01282   /* Zero surplus words in set1:
01283    */
01284   for ( i = i; i < BS_word_count(set1); ++i )
01285     BS_word(set1,i) = bs_ZEROS;
01286 
01287   return set1;
01288 }
01289 
01290 
01291 /* ====================================================================
01292  *
01293  *  BS_IntersectionR
01294  *
01295  *  See interface description
01296  *
01297  * ====================================================================
01298  */
01299 
01300 extern BS*
01301 BS_IntersectionR(
01302   BS* result,
01303   const BS* set1,
01304   const BS* set2
01305 )
01306 {
01307   BS_ELT i;
01308   BS_ELT minsize;
01309 
01310   minsize = MIN( BS_word_count(set1), BS_word_count(set2) );
01311 
01312   /* Form intersection of common part:
01313    */
01314   for ( i = 0; i < minsize; ++i )
01315     BS_word(result,i) = BS_word(set1,i) & BS_word(set2,i);
01316 
01317   /* Zero surplus words in result:
01318    */
01319   for ( i = i; i < BS_word_count(result); ++i )
01320     BS_word(result,i) = bs_ZEROS;
01321 
01322   return result;
01323 }
01324 
01325 
01326 /* Count of bits in all the one byte numbers.  Used to count members.
01327  */
01328 static unsigned const char bit_count[256] = {
01329   0, /*   0 */
01330   1, /*   1 */
01331   1, /*   2 */
01332   2, /*   3 */
01333   1, /*   4 */
01334   2, /*   5 */
01335   2, /*   6 */
01336   3, /*   7 */
01337   1, /*   8 */
01338   2, /*   9 */
01339   2, /*  10 */
01340   3, /*  11 */
01341   2, /*  12 */
01342   3, /*  13 */
01343   3, /*  14 */
01344   4, /*  15 */
01345   1, /*  16 */
01346   2, /*  17 */
01347   2, /*  18 */
01348   3, /*  19 */
01349   2, /*  20 */
01350   3, /*  21 */
01351   3, /*  22 */
01352   4, /*  23 */
01353   2, /*  24 */
01354   3, /*  25 */
01355   3, /*  26 */
01356   4, /*  27 */
01357   3, /*  28 */
01358   4, /*  29 */
01359   4, /*  30 */
01360   5, /*  31 */
01361   1, /*  32 */
01362   2, /*  33 */
01363   2, /*  34 */
01364   3, /*  35 */
01365   2, /*  36 */
01366   3, /*  37 */
01367   3, /*  38 */
01368   4, /*  39 */
01369   2, /*  40 */
01370   3, /*  41 */
01371   3, /*  42 */
01372   4, /*  43 */
01373   3, /*  44 */
01374   4, /*  45 */
01375   4, /*  46 */
01376   5, /*  47 */
01377   2, /*  48 */
01378   3, /*  49 */
01379   3, /*  50 */
01380   4, /*  51 */
01381   3, /*  52 */
01382   4, /*  53 */
01383   4, /*  54 */
01384   5, /*  55 */
01385   3, /*  56 */
01386   4, /*  57 */
01387   4, /*  58 */
01388   5, /*  59 */
01389   4, /*  60 */
01390   5, /*  61 */
01391   5, /*  62 */
01392   6, /*  63 */
01393   1, /*  64 */
01394   2, /*  65 */
01395   2, /*  66 */
01396   3, /*  67 */
01397   2, /*  68 */
01398   3, /*  69 */
01399   3, /*  70 */
01400   4, /*  71 */
01401   2, /*  72 */
01402   3, /*  73 */
01403   3, /*  74 */
01404   4, /*  75 */
01405   3, /*  76 */
01406   4, /*  77 */
01407   4, /*  78 */
01408   5, /*  79 */
01409   2, /*  80 */
01410   3, /*  81 */
01411   3, /*  82 */
01412   4, /*  83 */
01413   3, /*  84 */
01414   4, /*  85 */
01415   4, /*  86 */
01416   5, /*  87 */
01417   3, /*  88 */
01418   4, /*  89 */
01419   4, /*  90 */
01420   5, /*  91 */
01421   4, /*  92 */
01422   5, /*  93 */
01423   5, /*  94 */
01424   6, /*  95 */
01425   2, /*  96 */
01426   3, /*  97 */
01427   3, /*  98 */
01428   4, /*  99 */
01429   3, /* 100 */
01430   4, /* 101 */
01431   4, /* 102 */
01432   5, /* 103 */
01433   3, /* 104 */
01434   4, /* 105 */
01435   4, /* 106 */
01436   5, /* 107 */
01437   4, /* 108 */
01438   5, /* 109 */
01439   5, /* 110 */
01440   6, /* 111 */
01441   3, /* 112 */
01442   4, /* 113 */
01443   4, /* 114 */
01444   5, /* 115 */
01445   4, /* 116 */
01446   5, /* 117 */
01447   5, /* 118 */
01448   6, /* 119 */
01449   4, /* 120 */
01450   5, /* 121 */
01451   5, /* 122 */
01452   6, /* 123 */
01453   5, /* 124 */
01454   6, /* 125 */
01455   6, /* 126 */
01456   7, /* 127 */
01457   1, /* 128 */
01458   2, /* 129 */
01459   2, /* 130 */
01460   3, /* 131 */
01461   2, /* 132 */
01462   3, /* 133 */
01463   3, /* 134 */
01464   4, /* 135 */
01465   2, /* 136 */
01466   3, /* 137 */
01467   3, /* 138 */
01468   4, /* 139 */
01469   3, /* 140 */
01470   4, /* 141 */
01471   4, /* 142 */
01472   5, /* 143 */
01473   2, /* 144 */
01474   3, /* 145 */
01475   3, /* 146 */
01476   4, /* 147 */
01477   3, /* 148 */
01478   4, /* 149 */
01479   4, /* 150 */
01480   5, /* 151 */
01481   3, /* 152 */
01482   4, /* 153 */
01483   4, /* 154 */
01484   5, /* 155 */
01485   4, /* 156 */
01486   5, /* 157 */
01487   5, /* 158 */
01488   6, /* 159 */
01489   2, /* 160 */
01490   3, /* 161 */
01491   3, /* 162 */
01492   4, /* 163 */
01493   3, /* 164 */
01494   4, /* 165 */
01495   4, /* 166 */
01496   5, /* 167 */
01497   3, /* 168 */
01498   4, /* 169 */
01499   4, /* 170 */
01500   5, /* 171 */
01501   4, /* 172 */
01502   5, /* 173 */
01503   5, /* 174 */
01504   6, /* 175 */
01505   3, /* 176 */
01506   4, /* 177 */
01507   4, /* 178 */
01508   5, /* 179 */
01509   4, /* 180 */
01510   5, /* 181 */
01511   5, /* 182 */
01512   6, /* 183 */
01513   4, /* 184 */
01514   5, /* 185 */
01515   5, /* 186 */
01516   6, /* 187 */
01517   5, /* 188 */
01518   6, /* 189 */
01519   6, /* 190 */
01520   7, /* 191 */
01521   2, /* 192 */
01522   3, /* 193 */
01523   3, /* 194 */
01524   4, /* 195 */
01525   3, /* 196 */
01526   4, /* 197 */
01527   4, /* 198 */
01528   5, /* 199 */
01529   3, /* 200 */
01530   4, /* 201 */
01531   4, /* 202 */
01532   5, /* 203 */
01533   4, /* 204 */
01534   5, /* 205 */
01535   5, /* 206 */
01536   6, /* 207 */
01537   3, /* 208 */
01538   4, /* 209 */
01539   4, /* 210 */
01540   5, /* 211 */
01541   4, /* 212 */
01542   5, /* 213 */
01543   5, /* 214 */
01544   6, /* 215 */
01545   4, /* 216 */
01546   5, /* 217 */
01547   5, /* 218 */
01548   6, /* 219 */
01549   5, /* 220 */
01550   6, /* 221 */
01551   6, /* 222 */
01552   7, /* 223 */
01553   3, /* 224 */
01554   4, /* 225 */
01555   4, /* 226 */
01556   5, /* 227 */
01557   4, /* 228 */
01558   5, /* 229 */
01559   5, /* 230 */
01560   6, /* 231 */
01561   4, /* 232 */
01562   5, /* 233 */
01563   5, /* 234 */
01564   6, /* 235 */
01565   5, /* 236 */
01566   6, /* 237 */
01567   6, /* 238 */
01568   7, /* 239 */
01569   4, /* 240 */
01570   5, /* 241 */
01571   5, /* 242 */
01572   6, /* 243 */
01573   5, /* 244 */
01574   6, /* 245 */
01575   6, /* 246 */
01576   7, /* 247 */
01577   5, /* 248 */
01578   6, /* 249 */
01579   6, /* 250 */
01580   7, /* 251 */
01581   6, /* 252 */
01582   7, /* 253 */
01583   7, /* 254 */
01584   8  /* 255 */
01585 };
01586 
01587 /* ====================================================================
01588  *
01589  *  BS_Size
01590  *
01591  *  See interface description
01592  *
01593  * ====================================================================
01594  */
01595 
01596 extern BS_ELT
01597 BS_Size(
01598   BS* set
01599 )
01600 {
01601   /* Add up the population count of each byte in the set.  We get the
01602    * population counts from the table above.  Great for a machine with
01603    * effecient loadbyte instructions!
01604    */
01605   BS_ELT i;
01606   BS_ELT byte_count = BS_word_count(set) * sizeof(BS_WORD);
01607   size_t result = 0;
01608 
01609   for ( i = 0; i < byte_count; ++i )
01610     result += bit_count[BS_byte(set,i)];
01611 
01612   return result;
01613 }
01614 
01615 /* ====================================================================
01616  *
01617  *  BS_Union
01618  *
01619  *  See interface description
01620  *
01621  * ====================================================================
01622  */
01623 
01624 extern BS*
01625 BS_Union(
01626   BS       *set1,
01627   BS       *set2,
01628   MEM_POOL *pool
01629 )
01630 {
01631   BS    *set;
01632   BS_ELT i;
01633   BS_ELT size1 = BS_word_count(set1);
01634   BS_ELT size2 = BS_word_count(set2);
01635 
01636   if ( size1 < size2 ) {
01637     /* Normalize so set1 is at least as large:
01638      */
01639     BS    *tmps = set1;
01640     BS_ELT tmpe = size1;
01641 
01642     set1 = set2;
01643     set2 = tmps;
01644     size1 = size2;
01645     size2 = tmpe;
01646   }
01647 
01648   set = bs_Malloc(size1,pool);
01649   for ( i = 0; i < size2; ++i )
01650     BS_word(set,i) = BS_word(set1,i) | BS_word(set2,i);
01651   for ( i = size2; i < size1; ++i )
01652     BS_word(set,i) = BS_word(set1,i);
01653 
01654   return set;
01655 }
01656 
01657 /* ====================================================================
01658  *
01659  *  BS_UnionD
01660  *
01661  *  See interface description
01662  *
01663  * ====================================================================
01664  */
01665 
01666 extern BS*
01667 BS_UnionD(
01668   BS       *set1,
01669   BS       *set2,
01670   MEM_POOL *pool
01671 )
01672 {
01673   BS_ELT i;
01674   BS_ELT size1 = BS_word_count(set1);
01675   BS_ELT size2 = BS_word_count(set2);
01676 
01677   if ( size1 < size2 )
01678     set1 = bs_Realloc(set1,size2,pool);
01679 
01680   for ( i = 0; i < size2; ++i )
01681     BS_word(set1,i) |= BS_word(set2,i);
01682 
01683   return set1;
01684 }
01685 
01686 /* ====================================================================
01687  *
01688  *  BS_UnionR
01689  *
01690  *  See interface description
01691  *
01692  * ====================================================================
01693  */
01694 
01695 extern BS*
01696 BS_UnionR(
01697   BS       *result,
01698   BS       *set1,
01699   BS       *set2,
01700   MEM_POOL *pool
01701 )
01702 {
01703   BS_ELT i;
01704   BS_ELT size1 = BS_word_count(set1);
01705   BS_ELT size2 = BS_word_count(set2);
01706   BS_ELT maxsize = MAX( size1, size2 );
01707   BS_ELT rsize = BS_word_count(result);
01708 
01709   if ( rsize < maxsize )
01710     result = bs_Realloc(result,maxsize,pool);
01711 
01712   for ( i = 0; i < maxsize; ++i )
01713     BS_word(result,i) = BS_word(set1,i) | BS_word(set2,i);
01714 
01715   return result;
01716 }
01717 
01718 /* ====================================================================
01719  *
01720  *  BS_UnionD_Intersection
01721  *
01722  *  See interface description
01723  *
01724  * ====================================================================
01725  */
01726 
01727 extern BS*
01728 BS_UnionD_Intersection(
01729   BS       *set1,
01730   BS       *set2,
01731   BS       *set3,
01732   MEM_POOL *pool
01733 )
01734 {
01735   BS_ELT i;
01736   BS_ELT size1 = BS_word_count(set1);
01737   BS_ELT minsize = MIN( BS_word_count(set2), BS_word_count(set3) );
01738 
01739   if ( size1 < minsize )
01740     set1 = bs_Realloc(set1,minsize,pool);
01741 
01742   for ( i = 0; i < minsize; ++i )
01743     BS_word(set1,i) |= BS_word(set2,i) & BS_word(set3,i);
01744 
01745   return set1;
01746 }
01747 
01748 /* ====================================================================
01749  *
01750  *  BS_Union1
01751  *
01752  *  See interface description
01753  *
01754  * ====================================================================
01755  */
01756 
01757 extern BS*
01758 BS_Union1(
01759   BS       *set,
01760   BS_ELT    x,
01761   MEM_POOL *pool
01762 )
01763 {
01764   BS_ELT newsize;
01765   BS    *newset;
01766 
01767   if ( BS_word_count(set) > bs_QBPW(x) + 1 )
01768     newsize = bs_PBPW(BS_word_count(set));
01769   else
01770     newsize = x + 1;
01771 
01772   newset = BS_Create_Empty(newsize,pool);
01773   newset = BS_CopyD(newset,set,bad_pool);
01774   return BS_Union1D(newset,x,bad_pool);
01775 }
01776 
01777 /* ====================================================================
01778  *
01779  *  BS_Union1D
01780  *
01781  *  See interface description
01782  *
01783  * ====================================================================
01784  */
01785 
01786 extern BS*
01787 BS_Union1D(
01788   BS       *set,
01789   BS_ELT    x,
01790   MEM_POOL *pool
01791 )
01792 {
01793   BS_ELT minsize = bs_QBPW(x) + 1;
01794 
01795   Is_True(x >= 0,("BS_Union1D called with negative element."));
01796 
01797   if ( minsize > BS_word_count(set) )
01798     set = bs_Realloc(set,minsize,pool);
01799 
01800   BS_byte(set,bs_QBPB(x)) |= bs_ONE << bs_RBPB(x);
01801 
01802   return set;
01803 }
01804 
01805 /* ====================================================================
01806  *
01807  *  BS_2_1_Minus_3_Or_R
01808  *
01809  *  See interface description
01810  *
01811  * ====================================================================
01812  */
01813 
01814 extern BS*
01815 BS_2_1_Minus_3_Or_R(
01816   BS       *result,
01817   const BS *set1,
01818   const BS *set2,
01819   const BS *set3,
01820   MEM_POOL *pool
01821 )
01822 {
01823   BS_ELT i;
01824   BS_ELT rsize = BS_word_count(result);
01825   BS_ELT size3 = BS_word_count(set3);
01826 
01827   Is_True( BS_word_count(set1) == size3,
01828     ("BS_2_1_Minus_3_Or_R: set1 size (%d) < set3 size (%d)",
01829      (BS_ELT)BS_word_count(set1), size3) );
01830   Is_True( BS_word_count(set2) == size3,
01831     ("BS_2_1_Minus_3_Or_R: set2 size (%d) < set3 size (%d)",
01832      (BS_ELT)BS_word_count(set2), size3) );
01833 
01834   if ( rsize < size3 )
01835     result = bs_Realloc(result,size3,pool);
01836 
01837   for ( i = 0; i < size3; ++i )
01838     BS_word(result,i) = (~BS_word(set1,i) & BS_word(set2,i)) |
01839                           BS_word(set3,i);
01840 
01841   return result;
01842 }
01843 
01844 extern BS*
01845 BS_3_2_Minus_1_Or_D(
01846   BS       *set1,
01847   const BS *set2,
01848   const BS *set3,
01849   MEM_POOL *pool
01850 )
01851 {
01852   BS_ELT i;
01853   BS_ELT size1 = BS_word_count(set1);
01854   BS_ELT size2 = BS_word_count(set2);
01855   BS_ELT size3 = BS_word_count(set3);
01856 
01857   Is_True( size2 == size3,
01858     ("BS_3_2_Minus_1_Or_D: set2 size (%d) != set3 size (%d)",
01859      size2, size3) );
01860 
01861   if ( size1 < size3 )
01862     set1 = bs_Realloc(set1,size3,pool);
01863 
01864   for ( i = 0; i < size3; ++i )
01865     BS_word(set1,i) |= (~BS_word(set2,i) & BS_word(set3,i));
01866 
01867   return set1;
01868 }
01869 
01870 /* ====================================================================
01871  * 
01872  * BS_3_2_Minus_4_Or_1_Or_D
01873  *
01874  * set1 += ((~set2) AND set3) OR set4
01875  * set1 += (set3 - set2) + set4
01876  * 
01877  * ====================================================================
01878  */
01879 
01880 extern BS*
01881 BS_3_2_Minus_4_Or_1_Or_D(
01882   BS       *set1,
01883   const BS *set2,
01884   const BS *set3,
01885   const BS *set4,
01886   MEM_POOL *pool )
01887 {
01888   BS_ELT i;
01889   BS_ELT size1 = BS_word_count(set1);
01890   BS_ELT size2 = BS_word_count(set2);
01891   BS_ELT size3 = BS_word_count(set3);
01892   BS_ELT size4 = BS_word_count(set4);
01893 
01894   Is_True( size2 == size3 && size3 == size4,
01895     ("BS_3_2_Minus_4_Or_1_Or_D: sizes not equal %d, %d, %d",
01896      size2, size3, size4) );
01897 
01898   if ( size1 < size2 )
01899     set1 = bs_Realloc(set1,size2,pool);
01900 
01901   for ( i = 0; i < size2; ++i )
01902     BS_word(set1,i) |= (~BS_word(set2,i) & BS_word(set3,i)) |
01903                        BS_word(set4,i);
01904 
01905   return set1;
01906 }
01907 
01908 
01909 /* ====================================================================
01910  * 
01911  * BS_3_2_Minus_4_Or_5_Or_1_Or_D
01912  *
01913  * set1 += ((~set2) AND set3) OR set4 OR set5
01914  * set1 += (set3 - set2) + set4 + set5
01915  * 
01916  * ====================================================================
01917  */
01918 
01919 extern BS*
01920 BS_3_2_Minus_4_Or_5_Or_1_Or_D(
01921   BS       *set1,
01922   const BS *set2,
01923   const BS *set3,
01924   const BS *set4,
01925   const BS *set5,
01926   MEM_POOL *pool )
01927 {
01928   BS_ELT i;
01929   BS_ELT size1 = BS_word_count(set1);
01930   BS_ELT size5  = BS_word_count(set5);
01931 
01932   Is_True( BS_word_count(set2) == size5,
01933     ("BS_3_2_Minus_4_Or_5_Or_1_Or_D: set2 size (%d) != set5 size (%d)",
01934      (BS_ELT)BS_word_count(set2), size5) );
01935   Is_True( BS_word_count(set3) == size5,
01936     ("BS_3_2_Minus_4_Or_5_Or_1_Or_D: set3 size (%d) != set5 size (%d)",
01937      (BS_ELT)BS_word_count(set3), size5) );
01938   Is_True( BS_word_count(set4) == size5,
01939     ("BS_3_2_Minus_4_Or_5_Or_1_Or_D: set4 size (%d) != set5 size (%d)",
01940      (BS_ELT)BS_word_count(set4), size5) );
01941 
01942   if ( size1 < size5 )
01943     set1 = bs_Realloc(set1,size5,pool);
01944 
01945   for ( i = 0; i < size5; ++i )
01946     BS_word(set1,i) |= (~BS_word(set2,i) & BS_word(set3,i)) |
01947                           BS_word(set4,i) | BS_word(set5,i);
01948 
01949   return set1;
01950 }
01951 
01952 
01953 /* ====================================================================
01954  *
01955  *  BS_2_1_Minus_3_Or_4_And_5_And_6_And_R
01956  *
01957  *  See interface description
01958  *
01959  * ====================================================================
01960  */
01961 
01962 extern BS*
01963 BS_2_1_Minus_3_Or_4_And_5_And_6_And_R(
01964   BS       *result,
01965   const BS *set1,
01966   const BS *set2,
01967   const BS *set3,
01968   const BS *set4,
01969   const BS *set5,
01970   const BS *set6,
01971   MEM_POOL *pool
01972 )
01973 {
01974   BS_ELT i;
01975   BS_ELT rsize = BS_word_count(result);
01976   BS_ELT size3 = BS_word_count(set3);
01977 
01978   Is_True( BS_word_count(set1) == size3,
01979     ("BS_2_1_Minus_3_Or_4_And_5_And_6_And_R: set1 size (%d) < set3 size (%d)",
01980      (BS_ELT)BS_word_count(set1), size3) );
01981   Is_True( BS_word_count(set2) == size3,
01982     ("BS_2_1_Minus_3_Or_4_And_5_And_6_And_R: set2 size (%d) < set3 size (%d)",
01983      (BS_ELT)BS_word_count(set2), size3) );
01984   Is_True( BS_word_count(set4) == size3,
01985     ("BS_2_1_Minus_3_Or_4_And_5_And_6_And_R: set4 size (%d) < set3 size (%d)",
01986      (BS_ELT)BS_word_count(set4), size3) );
01987   Is_True( BS_word_count(set5) == size3,
01988     ("BS_2_1_Minus_3_Or_4_And_5_And_6_And_R: set5 size (%d) < set3 size (%d)",
01989      (BS_ELT)BS_word_count(set4), size3) );
01990   Is_True( BS_word_count(set6) == size3,
01991     ("BS_2_1_Minus_3_Or_4_And_5_And_6_And_R: set6 size (%d) < set3 size (%d)",
01992      (BS_ELT)BS_word_count(set6), size3) );
01993 
01994   if ( rsize < size3 )
01995     result = bs_Realloc(result,size3,pool);
01996 
01997   for ( i = 0; i < size3; ++i )
01998     BS_word(result,i) = ((~BS_word(set1,i) & BS_word(set2,i)) |
01999                          BS_word(set3,i)) &
02000                         BS_word(set4,i) &
02001                         BS_word(set5,i) &
02002                         BS_word(set6,i);
02003 
02004   return result;
02005 }
02006 
02007 /* ====================================================================
02008  *
02009  *  BS_2_1_Minus_3_Or_4_And_R
02010  *
02011  *  See interface description
02012  *
02013  * ====================================================================
02014  */
02015 
02016 extern BS*
02017 BS_2_1_Minus_3_Or_4_And_R(
02018   BS       *result,
02019   const BS *set1,
02020   const BS *set2,
02021   const BS *set3,
02022   const BS *set4,
02023   MEM_POOL *pool
02024 )
02025 {
02026   BS_ELT i;
02027   BS_ELT rsize = BS_word_count(result);
02028   BS_ELT size3 = BS_word_count(set3);
02029 
02030   Is_True( BS_word_count(set1) == size3,
02031     ("BS_2_1_Minus_3_Or_4_And_R: set1 size (%d) < set3 size (%d)",
02032      (BS_ELT)BS_word_count(set1), size3) );
02033   Is_True( BS_word_count(set2) == size3,
02034     ("BS_2_1_Minus_3_Or_4_And_R: set2 size (%d) < set3 size (%d)",
02035      (BS_ELT)BS_word_count(set2), size3) );
02036   Is_True( BS_word_count(set4) == size3,
02037     ("BS_2_1_Minus_3_Or_4_And_R: set4 size (%d) < set3 size (%d)",
02038      (BS_ELT)BS_word_count(set4), size3) );
02039 
02040   if ( rsize < size3 )
02041     result = bs_Realloc(result,size3,pool);
02042 
02043   for ( i = 0; i < size3; ++i )
02044     BS_word(result,i) = ((~BS_word(set1,i) & BS_word(set2,i)) |
02045                          BS_word(set3,i)) &
02046                         BS_word(set4,i);
02047 
02048   return result;
02049 }
02050 
02051 /* ====================================================================
02052  *
02053  *  BS_1_Not_2_Or_3_Minus_4_And_R
02054  *
02055  *  See interface description
02056  *
02057  * ====================================================================
02058  */
02059 
02060 extern BS*
02061 BS_1_Not_2_Or_3_Minus_4_And_R(
02062   BS       *result,
02063   const BS *set1,
02064   const BS *set2,
02065   const BS *set3,
02066   const BS *set4,
02067   MEM_POOL *pool
02068 )
02069 {
02070   BS_ELT i;
02071   BS_ELT rsize = BS_word_count(result);
02072   BS_ELT size2 = BS_word_count(set2);
02073 
02074   Is_True( BS_word_count(set1) == size2,
02075     ("BS_1_Not_2_Or_3_Minus_4_And_R: set1 size (%d) < set2 size (%d)",
02076      (BS_ELT)BS_word_count(set1), size2) );
02077   Is_True( BS_word_count(set3) == size2,
02078     ("BS_1_Not_2_Or_3_Minus_4_And_R: set3 size (%d) < set2 size (%d)",
02079      (BS_ELT)BS_word_count(set3), size2) );
02080   Is_True( BS_word_count(set4) == size2,
02081     ("BS_1_Not_2_Or_3_Minus_4_And_R: set4 size (%d) < set2 size (%d)",
02082      (BS_ELT)BS_word_count(set4), size2) );
02083 
02084   if ( rsize < size2 )
02085     result = bs_Realloc(result,size2,pool);
02086 
02087   for ( i = 0; i < size2; ++i )
02088     BS_word(result,i) = (~BS_word(set1,i) | BS_word(set2,i)) &
02089                         (~BS_word(set3,i)) &
02090                         BS_word(set4,i);
02091   return result;
02092 }
02093 
02094 /* ====================================================================
02095  * 
02096  * BS_2_3_Or_1_Or_D
02097  *
02098  * set1 += (set2 OR set3)
02099  * set1 += (set2 + set3)
02100  * 
02101  * ====================================================================
02102  */
02103 
02104 extern BS*
02105 BS_2_3_Or_1_Or_D(
02106   BS       *set1,
02107   const BS *set2,
02108   const BS *set3,
02109   MEM_POOL *pool )
02110 {
02111   BS_ELT i;
02112   BS_ELT size1 = BS_word_count(set1);
02113   BS_ELT size2  = BS_word_count(set2);
02114   BS_ELT size3  = BS_word_count(set3);
02115 
02116   Is_True( size2 == size3,
02117     ("BS_2_3_Or_1_Or_D: sizes not equal %d, %d",
02118      size2, size3) );
02119 
02120   if ( size1 < size2 )
02121     set1 = bs_Realloc(set1,size2,pool);
02122 
02123   for ( i = 0; i < size2; ++i )
02124     BS_word(set1,i) |= (BS_word(set2,i) | BS_word(set3,i));
02125 
02126   return set1;
02127 }
02128 
02129 /* ====================================================================
02130  *
02131  *  BS_1_2_Or_3_And_R
02132  *
02133  *  See interface description
02134  *
02135  * ====================================================================
02136  */
02137 
02138 extern BS*
02139 BS_1_2_Or_3_And_R(
02140   BS       *result,
02141   const BS *set1,
02142   const BS *set2,
02143   const BS *set3,
02144   MEM_POOL *pool
02145 )
02146 {
02147   BS_ELT i;
02148   BS_ELT rsize = BS_word_count(result);
02149   BS_ELT size1 = BS_word_count(set1);
02150 
02151   Is_True( BS_word_count(set2) == size1,
02152     ("BS_1_2_Or_3_And_R: set2 size (%d) < set1 size (%d)",
02153      (BS_ELT)BS_word_count(set2), size1) );
02154   Is_True( BS_word_count(set3) == size1,
02155     ("BS_1_2_Or_3_And_R: set3 size (%d) < set1 size (%d)",
02156      (BS_ELT)BS_word_count(set3), size1) );
02157 
02158   if ( rsize < size1 )
02159     result = bs_Realloc(result,size1,pool);
02160 
02161   for ( i = 0; i < size1; ++i )
02162     BS_word(result,i) = (BS_word(set1,i) | BS_word(set2,i)) &
02163                         BS_word(set3,i);
02164 
02165   return result;
02166 }
02167 
02168 /* ====================================================================
02169  *
02170  *  BS_2_3_And_1_Or_D
02171  *
02172  *  See interface description
02173  *
02174  * ====================================================================
02175  */
02176 
02177 extern BS*
02178 BS_2_3_And_1_Or_D(
02179   BS *set1,
02180   const BS *set2,
02181   const BS *set3,
02182   MEM_POOL *pool
02183 )
02184 {
02185   BS_ELT i;
02186   BS_ELT size1 = BS_word_count(set1);
02187   BS_ELT size = MIN(BS_word_count(set2),BS_word_count(set3));
02188 
02189   if ( size1 < size )
02190     set1 = bs_Realloc(set1,size,pool);
02191 
02192   for ( i = 0; i < size; ++i )
02193     BS_word(set1,i) |= BS_word(set2,i) & BS_word(set3,i);
02194 
02195   return set1;
02196 }
02197 
02198 /* ====================================================================
02199  *
02200  *  BS_3_Not_4_Or_2_And_1_Or_D
02201  *
02202  *  See interface description
02203  *
02204  * ====================================================================
02205  */
02206 
02207 extern BS*
02208 BS_3_Not_4_Or_2_And_1_Or_D(
02209   BS       *set1,
02210   const BS *set2,
02211   const BS *set3,
02212   const BS *set4,
02213   MEM_POOL *pool
02214 )
02215 {
02216   BS_ELT i;
02217   BS_ELT size1 = BS_word_count(set1);
02218   BS_ELT size2 = BS_word_count(set2);
02219 
02220   Is_True( BS_word_count(set3) == size2,
02221     ("BS_3_Not_4_Or_2_And_1_Or_D: set3 size (%d) < set2 size (%d)",
02222      (BS_ELT)BS_word_count(set3), size2) );
02223   Is_True( BS_word_count(set4) == size2,
02224     ("BS_3_Not_4_Or_2_And_1_Or_D: set4 size (%d) < set2 size (%d)",
02225      (BS_ELT)BS_word_count(set4), size2) );
02226 
02227   if ( size1 < size2 )
02228     set1 = bs_Realloc(set1,size2,pool);
02229 
02230   for ( i = 0; i < size2; ++i )
02231     BS_word(set1,i) |= BS_word(set2,i) &
02232                        (~BS_word(set3,i) | BS_word(set4,i));
02233 
02234   return set1;
02235 }
02236 
02237 /* ====================================================================
02238  *
02239  *  BS_4_3_Minus_2_Not_Or_1_And_D
02240  *
02241  *  See interface description
02242  *
02243  * ====================================================================
02244  */
02245 
02246 extern BS*
02247 BS_4_3_Minus_2_Not_Or_1_And_D(
02248   BS       *set1,
02249   const BS *set2,
02250   const BS *set3,
02251   const BS *set4,
02252   MEM_POOL *pool
02253 )
02254 {
02255   BS_ELT i;
02256   BS_ELT size1 = BS_word_count(set1);
02257   BS_ELT size2 = BS_word_count(set2);
02258 
02259   Is_True( BS_word_count(set3) == size2,
02260     ("BS_4_3_Minus_2_Not_Or_1_And_D: set3 size (%d) < set2 size (%d)",
02261      (BS_ELT)BS_word_count(set3), size2) );
02262   Is_True( BS_word_count(set4) == size2,
02263     ("BS_4_3_Minus_2_Not_Or_1_And_D: set4 size (%d) < set2 size (%d)",
02264      (BS_ELT)BS_word_count(set4), size2) );
02265 
02266   if ( size1 < size2 )
02267     set1 = bs_Realloc(set1,size2,pool);
02268 
02269   for ( i = 0; i < size2; ++i )
02270     BS_word(set1,i) &= ~BS_word(set2,i) |
02271                        (BS_word(set4,i) & ~BS_word(set3,i));
02272 
02273   return set1;
02274 }
02275 
02276 /* ====================================================================
02277  *
02278  *  BS_2_3_Minus_1_Or_D
02279  *
02280  *  See interface description
02281  *
02282  * ====================================================================
02283  */
02284 
02285 extern BS*
02286 BS_2_3_Minus_1_Or_D(
02287   BS       *set1,
02288   const BS *set2,
02289   const BS *set3,
02290   MEM_POOL *pool
02291 )
02292 {
02293   BS_ELT i;
02294   BS_ELT size1 = BS_word_count(set1);
02295   BS_ELT size2 = BS_word_count(set2);
02296   BS_ELT size3 = BS_word_count(set3);
02297 
02298   Is_True( size2 == size3,
02299     ("BS_2_3_Minus_1_Or_D: sizes not equal %d, %d",
02300      size2, size3) );
02301 
02302   if ( size1 < size2 )
02303     set1 = bs_Realloc(set1,size2,pool);
02304 
02305   for ( i = 0; i < size2; ++i )
02306     BS_word(set1,i) |= BS_word(set2,i) & ~BS_word(set3,i);
02307 
02308   return set1;
02309 }
02310 
02311 /* ====================================================================
02312  *
02313  *  BS_2_3_Minus_4_Minus_1_Or_D
02314  *
02315  *  See interface description
02316  *
02317  * ====================================================================
02318  */
02319 
02320 extern BS*
02321 BS_2_3_Minus_4_Minus_1_Or_D(
02322   BS       *set1,
02323   const BS *set2,
02324   const BS *set3,
02325   const BS *set4,
02326   MEM_POOL *pool
02327 )
02328 {
02329   BS_ELT i;
02330   BS_ELT size1 = BS_word_count(set1);
02331   BS_ELT size2 = BS_word_count(set2);
02332   BS_ELT size3 = BS_word_count(set3);
02333   BS_ELT size4 = BS_word_count(set4);
02334 
02335   Is_True( size2 == size3 && size3 == size4,
02336     ("BS_2_3_Minus_4_Minus_1_Or_D: sizes not equal %d, %d, %d",
02337      size2, size3, size4) );
02338 
02339   if ( size1 < size2 )
02340     set1 = bs_Realloc(set1,size2,pool);
02341 
02342   for ( i = 0; i < size2; ++i )
02343     BS_word(set1,i) |= BS_word(set2,i) & ~BS_word(set3,i)
02344                          & ~BS_word(set4,i);
02345 
02346   return set1;
02347 }
02348 
02349 /* ====================================================================
02350  *
02351  *  BS_ContainsP
02352  *
02353  *  See interface description
02354  *
02355  * ====================================================================
02356  */
02357 
02358 extern BOOL
02359 BS_ContainsP(
02360   BS *set1,
02361   BS *set2
02362 )
02363 {
02364   BS_ELT minsize;
02365   BS_ELT i;
02366 
02367   if ( BS_word_count(set1) < BS_word_count(set2) )
02368     minsize = BS_word_count(set1);
02369   else
02370     minsize = BS_word_count(set2);
02371 
02372   /* Check common part.
02373    */
02374   for ( i = 0; i < minsize; ++i ) {
02375     if ( BS_word(set1,i) != (BS_word(set1,i) | BS_word(set2,i)) )
02376       return FALSE;
02377   }
02378 
02379   /* Check excess in set2.
02380    */
02381   for ( ; i < BS_word_count(set2); ++i ) {
02382     if ( BS_word(set2,i) != bs_ZEROS )
02383       return FALSE;
02384   }
02385 
02386   return TRUE;
02387 }
02388 
02389 /* ====================================================================
02390  *
02391  *  BS_EmptyP
02392  *
02393  *  See interface description
02394  *
02395  * ====================================================================
02396  */
02397 
02398 extern BOOL
02399 BS_EmptyP(
02400   BS *set
02401 )
02402 {
02403   BS_ELT i;
02404 
02405   for ( i = 0; i < BS_word_count(set); ++i ) {
02406     if ( BS_word(set,i) != bs_ZEROS )
02407       return FALSE;
02408   }
02409 
02410   return TRUE;
02411 }
02412 
02413 /* ====================================================================
02414  *
02415  *  BS_EqualP
02416  *
02417  *  See interface description
02418  *
02419  * ====================================================================
02420  */
02421 
02422 extern BOOL
02423 BS_EqualP(
02424   BS *set1,
02425   BS *set2
02426 )
02427 {
02428   BS_ELT i;
02429 
02430   /* Normalize so set1 is smaller:
02431    */
02432   if ( BS_word_count(set1) > BS_word_count(set2) ) {
02433     BS *tmp = set1;
02434     set1 = set2;
02435     set2 = tmp;
02436   }
02437 
02438   for ( i = 0; i < BS_word_count(set1); ++i ) {
02439     if ( BS_word(set1,i) != BS_word(set2,i) )
02440       return FALSE;
02441   }
02442 
02443   for ( ; i < BS_word_count(set2); ++i ) {
02444     if ( BS_word(set2,i) != bs_ZEROS )
02445       return FALSE;
02446   }
02447 
02448   return TRUE;
02449 }
02450 
02451 /* ====================================================================
02452  *
02453  *  BS_IntersectsP
02454  *
02455  *  See interface description
02456  *
02457  * ====================================================================
02458  */
02459 
02460 extern BOOL
02461 BS_IntersectsP(
02462   BS *set1,
02463   BS *set2
02464 )
02465 {
02466   BS_ELT minsize, i;
02467 
02468   if ( BS_word_count(set1) < BS_word_count(set2) )
02469     minsize = BS_word_count(set1);
02470   else
02471     minsize = BS_word_count(set2);
02472 
02473   for ( i = 0; i < minsize; ++i ) {
02474     if ( (BS_word(set1,i) & BS_word(set2,i)) != bs_ZEROS )
02475       return TRUE;
02476   }
02477 
02478   return FALSE;
02479 }
02480 
02481 /* ====================================================================
02482  *
02483  *  BS_MemberP
02484  *
02485  *  See interface description
02486  *
02487  * ====================================================================
02488  */
02489 
02490 extern BOOL
02491 BS_MemberP(
02492   BS    *set,
02493   BS_ELT x
02494 )
02495 {
02496   Is_True(x >= 0,("BS_Member called with negative element."));
02497 
02498   if ( bs_QBPW(x) >= BS_word_count(set) )
02499     return FALSE;
02500   else
02501     return    (BS_byte(set,bs_QBPB(x)) & (bs_ONE << bs_RBPB(x)))
02502            != bs_ZEROS;
02503 }
02504 
02505 /* ====================================================================
02506  *
02507  *  BS_Intersection_MemberP
02508  *
02509  *  See interface description
02510  *
02511  * ====================================================================
02512  */
02513 
02514 extern BOOL
02515 BS_Intersection_MemberP(
02516   BS    *set1,
02517   BS    *set2,
02518   BS_ELT x
02519 )
02520 {
02521   Is_True(x >= 0,("BS_Member called with negative element."));
02522 
02523   if (    bs_QBPW(x) >= BS_word_count(set1)
02524        || bs_QBPW(x) >= BS_word_count(set2)
02525   ) {
02526     return FALSE;
02527   }
02528   else {
02529     return    (   BS_byte(set1,bs_QBPB(x))
02530                 & BS_byte(set2,bs_QBPB(x))
02531                 & (bs_ONE << bs_RBPB(x)))
02532            != bs_ZEROS;
02533   }
02534 }
02535 
02536 /* ====================================================================
02537  *
02538  *  PrintRange
02539  *
02540  *  Subroutine for BS_Print to print a range within the set.
02541  *
02542  *  f       - The file to print to
02543  *  low     - First elemnt of range
02544  *  high    - Last element of range
02545  *  first   - First range to be printed.  (Reference argument)
02546  *
02547  * ====================================================================
02548  */
02549 
02550 static void
02551 PrintRange(
02552   FILE  *f,
02553   BS_ELT low,
02554   BS_ELT high,
02555   BOOL  *first
02556 )
02557 {
02558   if ( *first )
02559     *first = FALSE;
02560   else
02561     fprintf(f,",");
02562 
02563   if ( low == high )
02564     fprintf(f,"%d",low);
02565   else
02566     fprintf(f,"%d-%d",low,high);
02567 }
02568 
02569 /* ====================================================================
02570  *
02571  *  BS_Print
02572  *
02573  *  See interface description
02574  *
02575  * ====================================================================
02576  */
02577 
02578 extern void
02579 BS_Print(
02580   BS   *set,
02581   FILE *f
02582 )
02583 {
02584   BS_ELT range_first, last_elt, elt;
02585   BOOL   first = TRUE;
02586 
02587   if ( set == NULL ) {
02588     fprintf ( f, "<NULL>" );
02589     return;
02590   }
02591 
02592   fprintf(f,"{");
02593 
02594   elt = BS_Choose(set);
02595   if ( elt == BS_CHOOSE_FAILURE ) {
02596     fprintf(f,"}");
02597     return;
02598   }
02599   range_first = elt;
02600   last_elt = range_first;
02601 
02602   while ( (elt = BS_Choose_Next(set,elt)) != BS_CHOOSE_FAILURE ) {
02603     if ( elt != last_elt + 1 ) {
02604       /* renge_first .. last_elt was a range
02605        */
02606 
02607       PrintRange(f,range_first,last_elt,&first);
02608       range_first = elt;
02609     }
02610     last_elt = elt;
02611   }
02612 
02613   PrintRange(f,range_first,last_elt,&first);
02614   fprintf(f,"}");
02615 }
02616 
02617 /* ====================================================================
02618  *
02619  *  BS_Print_dbg - same as BS_Print, but to stderr, to be called from
02620  *  dbx
02621  *
02622  * ====================================================================
02623  */
02624 
02625 extern void
02626 BS_Print_dbg(BS   *set)
02627 {
02628   BS_Print(set, stderr);
02629   fprintf(stderr, "\n");
02630 }
02631 
02632 
02633 /* ====================================================================
02634  *
02635  *  FBS_MemberP
02636  *
02637  *  See interface description
02638  *
02639  * ====================================================================
02640  */
02641 
02642 extern BOOL
02643 FBS_MemberP_Validate(
02644   BS    *set,
02645   BS_ELT x
02646 )
02647 {
02648   Is_True(x >= 0,("FBS_Member called with negative element."));
02649   Is_True( bs_QBPW(x) < BS_word_count(set), ("FBS_Member called with out of bound element."));
02650   return  (BS_byte(set,bs_QBPB(x)) & (bs_ONE << bs_RBPB(x))) != bs_ZEROS;
02651 }
02652 
02653 
02654 /* ====================================================================
02655  *
02656  *  FBS_Union1D
02657  *
02658  *  See interface description
02659  *
02660  * ====================================================================
02661  */
02662 
02663 void
02664 FBS_Union1D_Validate(
02665   BS       *set,
02666   BS_ELT    x
02667 )
02668 {
02669   BS_ELT minsize = bs_QBPW(x) + 1;
02670   Is_True(x >= 0,("FBS_Union1D called with negative element."));
02671   Is_True( minsize <= BS_word_count(set), ("FBS_Union1D called with out of bound element."));
02672   BS_byte(set,bs_QBPB(x)) |= bs_ONE << bs_RBPB(x);
02673 }
02674 
02675 
02676 

Generated on Wed Jan 28 04:37:17 2009 for OpenADFortTk (extended to Open64) by  doxygen 1.5.7.1