Java iterators for C++:
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Public Member Functions | Private Attributes | List of all members
subset< N > Class Template Reference

Iterator/generator that returns all subsets for an N element set. More...

#include <subset.h>

Public Member Functions

 subset ()
 Constructor. DIM() == N More...
 
 ~subset ()
 Destructor. More...
 
bool hasNext () const
 Returns true while there are more subsets to generate. More...
 
const bool * next ()
 Generate next subset. More...
 
int DIM () const
 Returns the size of the complete set. More...
 

Private Attributes

bool * m_mask
 Marks which elements are in the current set. More...
 
int m_cont
 

Detailed Description

template<int N>
class subset< N >

Iterator/generator that returns all subsets for an N element set.

/* class subset: program example.

{ }
{ 0 }
{ 1 }
{ 0 , 1 }
{ 2 }
{ 0 , 2 }
{ 1 , 2 }
{ 0 , 1 , 2 }
*/
int main() {{
subset<3> iter; // iter.DIM() == 3
while ( iter.hasNext() ) {
const bool *SS = iter.next();
bool didOne = false;
std::cout << "{ ";
for ( int i=0; i<iter.DIM(); ++i ) {
if ( SS[i] ) {
if ( didOne ) {
std::cout << " , ";
}
std::cout << i;
didOne = true;
}
}
std::cout << " }" << std::endl;
}
}}

See Also
http://compprog.wordpress.com/2007/10/10/generating-subsets/

Definition at line 21 of file subset.h.

Constructor & Destructor Documentation

template<int N>
subset< N >::subset ( )

Constructor. DIM() == N

Constructor. Set has
elements.

Definition at line 53 of file subset.h.

template<int N>
subset< N >::~subset ( )
inline

Destructor.

Definition at line 27 of file subset.h.

Member Function Documentation

template<int N>
subset< N >::hasNext ( ) const
inline

Returns true while there are more subsets to generate.

Definition at line 28 of file subset.h.

template<int N>
subset< N >::next ( )

Generate next subset.

Definition at line 36 of file subset.h.

template<int N>
subset< N >::DIM ( ) const
inline

Returns the size of the complete set.

Definition at line 32 of file subset.h.

Member Data Documentation

template<int N>
subset< N >::m_mask
private

Marks which elements are in the current set.

Goes from 2^n down to 0.

Definition at line 23 of file subset.h.

template<int N>
subset< N >::m_cont
private

Definition at line 24 of file subset.h.


The documentation for this class was generated from the following file: