abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
extraUtilBitMatrix.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [extraUtilBitMatrix.c]
4 
5  PackageName [extra]
6 
7  Synopsis [Various reusable software utilities.]
8 
9  Author [Alan Mishchenko]
10 
11  Affiliation [UC Berkeley]
12 
13  Date [Ver. 1.0. Started - September 1, 2003.]
14 
15  Revision [$Id: extraUtilBitMatrix.c,v 1.0 2003/09/01 00:00:00 alanmi Exp $]
16 
17 ***********************************************************************/
18 
19 #include "extra.h"
20 
22 
23 
24 /*---------------------------------------------------------------------------*/
25 /* Constant declarations */
26 /*---------------------------------------------------------------------------*/
27 
28 /*---------------------------------------------------------------------------*/
29 /* Stucture declarations */
30 /*---------------------------------------------------------------------------*/
31 
33 {
34  unsigned ** ppData; // bit data
35  int nSize; // the number of bits in one dimension
36  int nWords; // the number of words in one dimension
37  int nBitShift; // the number of bits to shift to get words
38  unsigned uMask; // the mask to get the number of bits in the word
39  int nLookups; // the number of lookups
40  int nInserts; // the number of inserts
41  int nDeletes; // the number of deletions
42 };
43 
44 /*---------------------------------------------------------------------------*/
45 /* Type declarations */
46 /*---------------------------------------------------------------------------*/
47 
48 /*---------------------------------------------------------------------------*/
49 /* Variable declarations */
50 /*---------------------------------------------------------------------------*/
51 
52 /*---------------------------------------------------------------------------*/
53 /* Macro declarations */
54 /*---------------------------------------------------------------------------*/
55 
56 
57 /**AutomaticStart*************************************************************/
58 
59 /*---------------------------------------------------------------------------*/
60 /* Static function prototypes */
61 /*---------------------------------------------------------------------------*/
62 
63 /**AutomaticEnd***************************************************************/
64 
65 
66 /*---------------------------------------------------------------------------*/
67 /* Definition of exported functions */
68 /*---------------------------------------------------------------------------*/
69 
70 /**Function*************************************************************
71 
72  Synopsis [Starts the bit matrix.]
73 
74  Description []
75 
76  SideEffects []
77 
78  SeeAlso []
79 
80 ***********************************************************************/
82 {
83  Extra_BitMat_t * p;
84  int i;
85  p = ABC_ALLOC( Extra_BitMat_t, 1 );
86  memset( p, 0, sizeof(Extra_BitMat_t) );
87  p->nSize = nSize;
88  p->nBitShift = (sizeof(unsigned) == 4) ? 5: 6;
89  p->uMask = (sizeof(unsigned) == 4) ? 31: 63;
90  p->nWords = nSize / (8 * sizeof(unsigned)) + ((nSize % (8 * sizeof(unsigned))) > 0);
91  p->ppData = ABC_ALLOC( unsigned *, nSize );
92  p->ppData[0] = ABC_ALLOC( unsigned, nSize * p->nWords );
93  memset( p->ppData[0], 0, sizeof(unsigned) * nSize * p->nWords );
94  for ( i = 1; i < nSize; i++ )
95  p->ppData[i] = p->ppData[i-1] + p->nWords;
96  return p;
97 }
98 
99 /**Function*************************************************************
100 
101  Synopsis [Stops the bit matrix.]
102 
103  Description []
104 
105  SideEffects []
106 
107  SeeAlso []
108 
109 ***********************************************************************/
111 {
112  memset( p->ppData[0], 0, sizeof(unsigned) * p->nSize * p->nWords );
113 }
114 
115 /**Function*************************************************************
116 
117  Synopsis [Stops the bit matrix.]
118 
119  Description []
120 
121  SideEffects []
122 
123  SeeAlso []
124 
125 ***********************************************************************/
127 {
128  ABC_FREE( p->ppData[0] );
129  ABC_FREE( p->ppData );
130  ABC_FREE( p );
131 }
132 
133 /**Function*************************************************************
134 
135  Synopsis [Prints the bit-matrix.]
136 
137  Description []
138 
139  SideEffects []
140 
141  SeeAlso []
142 
143 ***********************************************************************/
145 {
146  int i, k, nVars;
147  printf( "\n" );
148  nVars = Extra_BitMatrixReadSize( pMat );
149  for ( i = 0; i < nVars; i++ )
150  {
151  for ( k = 0; k <= i; k++ )
152  printf( " " );
153  for ( k = i+1; k < nVars; k++ )
154  if ( Extra_BitMatrixLookup1( pMat, i, k ) )
155  printf( "1" );
156  else
157  printf( "." );
158  printf( "\n" );
159  }
160 }
161 
162 
163 /**Function*************************************************************
164 
165  Synopsis [Reads the matrix size.]
166 
167  Description []
168 
169  SideEffects []
170 
171  SeeAlso []
172 
173 ***********************************************************************/
175 {
176  return p->nSize;
177 }
178 
179 /**Function*************************************************************
180 
181  Synopsis [Inserts the element into the upper part.]
182 
183  Description []
184 
185  SideEffects []
186 
187  SeeAlso []
188 
189 ***********************************************************************/
190 void Extra_BitMatrixInsert1( Extra_BitMat_t * p, int i, int k )
191 {
192  p->nInserts++;
193  if ( i < k )
194  p->ppData[i][k>>p->nBitShift] |= (1<<(k & p->uMask));
195  else
196  p->ppData[k][i>>p->nBitShift] |= (1<<(i & p->uMask));
197 }
198 
199 /**Function*************************************************************
200 
201  Synopsis [Inserts the element into the upper part.]
202 
203  Description []
204 
205  SideEffects []
206 
207  SeeAlso []
208 
209 ***********************************************************************/
211 {
212  p->nLookups++;
213  if ( i < k )
214  return ((p->ppData[i][k>>p->nBitShift] & (1<<(k & p->uMask))) > 0);
215  else
216  return ((p->ppData[k][i>>p->nBitShift] & (1<<(i & p->uMask))) > 0);
217 }
218 
219 /**Function*************************************************************
220 
221  Synopsis [Inserts the element into the upper part.]
222 
223  Description []
224 
225  SideEffects []
226 
227  SeeAlso []
228 
229 ***********************************************************************/
230 void Extra_BitMatrixDelete1( Extra_BitMat_t * p, int i, int k )
231 {
232  p->nDeletes++;
233  if ( i < k )
234  p->ppData[i][k>>p->nBitShift] &= ~(1<<(k & p->uMask));
235  else
236  p->ppData[k][i>>p->nBitShift] &= ~(1<<(i & p->uMask));
237 }
238 
239 
240 
241 /**Function*************************************************************
242 
243  Synopsis [Inserts the element into the upper part.]
244 
245  Description []
246 
247  SideEffects []
248 
249  SeeAlso []
250 
251 ***********************************************************************/
252 void Extra_BitMatrixInsert2( Extra_BitMat_t * p, int i, int k )
253 {
254  p->nInserts++;
255  if ( i > k )
256  p->ppData[i][k>>p->nBitShift] |= (1<<(k & p->uMask));
257  else
258  p->ppData[k][i>>p->nBitShift] |= (1<<(i & p->uMask));
259 }
260 
261 /**Function*************************************************************
262 
263  Synopsis [Inserts the element into the upper part.]
264 
265  Description []
266 
267  SideEffects []
268 
269  SeeAlso []
270 
271 ***********************************************************************/
273 {
274  p->nLookups++;
275  if ( i > k )
276  return ((p->ppData[i][k>>p->nBitShift] & (1<<(k & p->uMask))) > 0);
277  else
278  return ((p->ppData[k][i>>p->nBitShift] & (1<<(i & p->uMask))) > 0);
279 }
280 
281 /**Function*************************************************************
282 
283  Synopsis [Inserts the element into the upper part.]
284 
285  Description []
286 
287  SideEffects []
288 
289  SeeAlso []
290 
291 ***********************************************************************/
292 void Extra_BitMatrixDelete2( Extra_BitMat_t * p, int i, int k )
293 {
294  p->nDeletes++;
295  if ( i > k )
296  p->ppData[i][k>>p->nBitShift] &= ~(1<<(k & p->uMask));
297  else
298  p->ppData[k][i>>p->nBitShift] &= ~(1<<(i & p->uMask));
299 }
300 
301 
302 /**Function*************************************************************
303 
304  Synopsis [Inserts the element into the upper part.]
305 
306  Description []
307 
308  SideEffects []
309 
310  SeeAlso []
311 
312 ***********************************************************************/
313 void Extra_BitMatrixOr( Extra_BitMat_t * p, int i, unsigned * pInfo )
314 {
315  int w;
316  for ( w = 0; w < p->nWords; w++ )
317  p->ppData[i][w] |= pInfo[w];
318 }
319 
320 /**Function*************************************************************
321 
322  Synopsis [Inserts the element into the upper part.]
323 
324  Description []
325 
326  SideEffects []
327 
328  SeeAlso []
329 
330 ***********************************************************************/
331 void Extra_BitMatrixOrTwo( Extra_BitMat_t * p, int i, int j )
332 {
333  int w;
334  for ( w = 0; w < p->nWords; w++ )
335  p->ppData[i][w] = p->ppData[j][w] = (p->ppData[i][w] | p->ppData[j][w]);
336 }
337 
338 /**Function*************************************************************
339 
340  Synopsis [Counts the number of 1's in the upper rectangle.]
341 
342  Description []
343 
344  SideEffects []
345 
346  SeeAlso []
347 
348 ***********************************************************************/
350 {
351  int i, k, nTotal = 0;
352  for ( i = 0; i < p->nSize; i++ )
353  for ( k = i + 1; k < p->nSize; k++ )
354  nTotal += ( (p->ppData[i][k>>5] & (1 << (k&31))) > 0 );
355  return nTotal;
356 }
357 
358 /**Function*************************************************************
359 
360  Synopsis [Returns 1 if the matrices have no entries in common.]
361 
362  Description []
363 
364  SideEffects []
365 
366  SeeAlso []
367 
368 ***********************************************************************/
370 {
371  int i, w;
372  assert( p1->nSize == p2->nSize );
373  for ( i = 0; i < p1->nSize; i++ )
374  for ( w = 0; w < p1->nWords; w++ )
375  if ( p1->ppData[i][w] & p2->ppData[i][w] )
376  return 0;
377  return 1;
378 }
379 
380 /**Function*************************************************************
381 
382  Synopsis [Returns 1 if the matrix is a set of cliques.]
383 
384  Description [For example pairwise symmetry info should satisfy this property.]
385 
386  SideEffects []
387 
388  SeeAlso []
389 
390 ***********************************************************************/
392 {
393  int v, u, i;
394  for ( v = 0; v < pMat->nSize; v++ )
395  for ( u = v+1; u < pMat->nSize; u++ )
396  {
397  if ( !Extra_BitMatrixLookup1( pMat, v, u ) )
398  continue;
399  // v and u are symmetric
400  for ( i = 0; i < pMat->nSize; i++ )
401  {
402  if ( i == v || i == u )
403  continue;
404  // i is neither v nor u
405  // the symmetry status of i is the same w.r.t. to v and u
406  if ( Extra_BitMatrixLookup1( pMat, i, v ) != Extra_BitMatrixLookup1( pMat, i, u ) )
407  return 0;
408  }
409  }
410  return 1;
411 }
412 
413 
414 ////////////////////////////////////////////////////////////////////////
415 /// END OF FILE ///
416 ////////////////////////////////////////////////////////////////////////
417 
418 
420 
char * memset()
int Extra_BitMatrixReadSize(Extra_BitMat_t *p)
void Extra_BitMatrixOr(Extra_BitMat_t *p, int i, unsigned *pInfo)
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int Extra_BitMatrixLookup1(Extra_BitMat_t *p, int i, int k)
void Extra_BitMatrixDelete2(Extra_BitMat_t *p, int i, int k)
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
void Extra_BitMatrixClean(Extra_BitMat_t *p)
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
int Extra_BitMatrixIsClique(Extra_BitMat_t *pMat)
int Extra_BitMatrixCountOnesUpper(Extra_BitMat_t *p)
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void Extra_BitMatrixInsert1(Extra_BitMat_t *p, int i, int k)
void Extra_BitMatrixStop(Extra_BitMat_t *p)
int Extra_BitMatrixIsDisjoint(Extra_BitMat_t *p1, Extra_BitMat_t *p2)
#define ABC_FREE(obj)
Definition: abc_global.h:232
void Extra_BitMatrixDelete1(Extra_BitMat_t *p, int i, int k)
#define assert(ex)
Definition: util_old.h:213
void Extra_BitMatrixInsert2(Extra_BitMat_t *p, int i, int k)
int nTotal
DECLARATIONS ///.
Definition: cutTruth.c:37
Extra_BitMat_t * Extra_BitMatrixStart(int nSize)
int Extra_BitMatrixLookup2(Extra_BitMat_t *p, int i, int k)
void Extra_BitMatrixPrint(Extra_BitMat_t *pMat)
void Extra_BitMatrixOrTwo(Extra_BitMat_t *p, int i, int j)