Gray Code Iterator


Detailed Description

Here we define an implementation of knuth's looples gray code iterator for binary strings as defined in ``The Art of Computer Programming, Chapter 7.2.1.1''.


Files

file  eg_gcit.h

Data Structures

struct  EGgcIt_t
 Structure to store the information relevant to the binary gray code iterator. More...

Defines

#define EG_GCIT_DBG   0
 Debug level for the heap.
#define EGgcItClear(__gc)
 free all internally allocated memory for the given EGgcIt_t structure.
#define EGgcItGetChange(__gc)   ((const int)((__gc)->changed_pos))
 return which position in the binary string changed in the last iteration.
#define EGgcItGetSize(__gc)   ((const int)((__gc)->sz))
 Return the number of bits for the given gray code iterator.
#define EGgcItGetTuple(__gc)   ((const int*const)((__gc)->tuple))
 Return a constant pointer to the current tuple.
#define EGgcItInit(__gc, __sz)
 Initialize a binary gray code iterator, and set the iterator to the zero position.
#define EGgcItNext(__gc)
 move to the next binary string, if no next string exists (i.e. we finish the loop), return 0, otherwise return 1.
#define EGgcItReset(__gc)
 Reset an initialized gray code iterator to the zero position.


Define Documentation

#define EG_GCIT_DBG   0
 

Debug level for the heap.

Definition at line 45 of file eg_gcit.h.

#define EGgcItClear __gc   ) 
 

Value:

do{\
  EGgcIt_t*const __EGgcit = (__gc);\
  EGfree(__EGgcit->tuple);\
  EGfree(__EGgcit->focus);\
  memset(__EGgcit,0,sizeof(EGgcIt_t));}while(0)
free all internally allocated memory for the given EGgcIt_t structure.

Parameters:
__gc pointer to a gray-code iterator structure (EGgcIt_t)

Definition at line 89 of file eg_gcit.h.

#define EGgcItGetChange __gc   )     ((const int)((__gc)->changed_pos))
 

return which position in the binary string changed in the last iteration.

Parameters:
__gc pointer to a gray-code iterator structure (EGgcIt_t)

Definition at line 113 of file eg_gcit.h.

#define EGgcItGetSize __gc   )     ((const int)((__gc)->sz))
 

Return the number of bits for the given gray code iterator.

Parameters:
__gc pointer to a gray-code iterator structure (EGgcIt_t)
Returns:
number of bits for the iterator

Definition at line 100 of file eg_gcit.h.

#define EGgcItGetTuple __gc   )     ((const int*const)((__gc)->tuple))
 

Return a constant pointer to the current tuple.

Parameters:
__gc pointer to a gray-code iterator structure (EGgcIt_t)
Returns:
pointer to an array containing the current tuple

Definition at line 106 of file eg_gcit.h.

#define EGgcItInit __gc,
__sz   ) 
 

Value:

do{\
  EGgcIt_t*const __EGgcit = (__gc);\
  const int __EGgcsz = __EGgcit->sz = ((int)(__sz));\
  __EGgcit->tuple = EGsMalloc(int,__EGgcsz);\
  __EGgcit->focus = EGsMalloc(int,__EGgcsz+1);\
  EGgcItReset(__EGgcit);}while(0)
Initialize a binary gray code iterator, and set the iterator to the zero position.

Parameters:
__gc pointer to a gray-code iterator structure (EGgcIt_t)
__sz number of bits to be used in the iterator.

Definition at line 77 of file eg_gcit.h.

#define EGgcItNext __gc   ) 
 

Value:

({\
  EGgcIt_t*const __EGgcit = (__gc);\
  const int __EGgccp = __EGgcit->changed_pos = __EGgcit->focus[0];\
  const int __EGgcrval=(__EGgccp!=__EGgcit->sz);\
  if(__EGgcrval){\
  __EGgcit->focus[0]=0;\
  __EGgcit->focus[__EGgccp]=__EGgcit->focus[__EGgccp+1];\
  __EGgcit->focus[__EGgccp+1]=__EGgccp+1;\
  __EGgcit->tuple[__EGgccp] = 1-__EGgcit->tuple[__EGgccp];}\
  __EGgcrval;})
move to the next binary string, if no next string exists (i.e. we finish the loop), return 0, otherwise return 1.

Parameters:
__gc pointer to a gray-code iterator structure (EGgcIt_t)
Returns:
zero if no next string exists, otherwise 1.

Definition at line 121 of file eg_gcit.h.

#define EGgcItReset __gc   ) 
 

Value:

do{\
  EGgcIt_t*const __EGgcit2 = (__gc);\
  register int __EGgci = __EGgcit2->sz;\
  __EGgcit2->focus[__EGgci]=__EGgci;\
  while(__EGgci--){__EGgcit2->tuple[__EGgci] = 0;\
  __EGgcit2->focus[__EGgci]=__EGgci;}\
  __EGgcit2->changed_pos=0;}while(0)
Reset an initialized gray code iterator to the zero position.

Parameters:
__gc pointer to a gray-code iterator structure (EGgcIt_t)

Definition at line 63 of file eg_gcit.h.


Generated on Wed Nov 21 09:38:35 2007 for MTgomory by  doxygen 1.4.6