Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

ogdf::SubsetEnumerator< T > Class Template Reference

Enumerator for k-subsets of a given type. More...

#include <ogdf/basic/SubsetEnumerator.h>

Public Member Functions

 SubsetEnumerator (const List< T > &set)
 Constructor. More...
 
void array (Array< T > &array) const
 Obtain an array of the elements in the subset. More...
 
void begin (int low, int high)
 Initialize the SubsetEnumerator to enumerate subsets of cardinalities from low to high. More...
 
void begin (int card)
 Initialize the SubsetEnumerator to enumerate subsets of given cardinality. More...
 
void begin ()
 Initialize the SubsetEnumerator to enumerate all subsets. More...
 
bool hasMember (const T &element) const
 Check in O(subset cardinality) whether the argument is a member of the subset. More...
 
void list (List< T > &list) const
 Obtain (append) a list of the elements in the subset. More...
 
void list (List< T > &list, List< T > &complement) const
 Obtain (append) a list of the elements in the subset and a list of the other elements of the set. More...
 
void next ()
 Obtain the next subset if possible. The result should be checked using the valid() method. More...
 
T operator[] (int i) const
 Get element of subset by index (starting from 0). More...
 
int size () const
 Return the cardinality of the subset. More...
 
bool valid () const
 Is the current subset valid? If not, all subsets have already been enumerated. More...
 

Protected Member Functions

void initSubset (int card)
 

Protected Attributes

Array< int > m_index
 
int m_maxCard
 
const List< T > & m_set
 
Array< T > m_subset
 
bool m_valid
 

Detailed Description

template<typename T>
class ogdf::SubsetEnumerator< T >

Enumerator for k-subsets of a given type.

Usage examples

  • Enumerate all subsets of edges with cardinality 3:
    List<edge> edges;
    do_something_eg_fill_edges();
    SubsetEnumerator<edge> subset(edges);
    for (subset.begin(3); subset.valid(); subset.next()) {
    do_something_with(subset[0], subset[1], subset[2]);
    }
  • Enumerate all subsets of edges:
    SubsetEnumerator<edge> subset(edges);
    for (subset.begin(); subset.valid(); subset.next()) {
    for (int i = 0; i < subset.size(); ++i) {
    do_something_with(subset[i]);
    }
    do_stuff();
    }
  • Do something with element lists and complement lists of all 2-, 3-, and 4-element subsets
    SubsetEnumerator<edge> subset(edges);
    for (subset.begin(2, 4); subset.valid(); subset.next()) {
    List<edge> list1, list2;
    subset.list(list1, list2);
    // if subset = { 1, 3, 4 } of { 1, 2, 3, 4, 5 },
    // then list1 = 1 3 4 and list2 = 2 5
    do_something_with(list1);
    do_another_things_with(list2);
    }

Please note that the internal data structures of SubsetEnumerator do not use references of the type T. Hence, T should either be a simple type or a pointer to a complex type (which is also only sane for Lists, too). Otherwise the data structure will slow down due to extensive implicit copying.

Definition at line 88 of file SubsetEnumerator.h.

Constructor & Destructor Documentation

◆ SubsetEnumerator()

template<typename T>
ogdf::SubsetEnumerator< T >::SubsetEnumerator ( const List< T > &  set)
inlineexplicit

Constructor.

Parameters
setThe list of elements we want to enumerate subsets for.

Definition at line 110 of file SubsetEnumerator.h.

Member Function Documentation

◆ array()

template<typename T>
void ogdf::SubsetEnumerator< T >::array ( Array< T > &  array) const
inline

Obtain an array of the elements in the subset.

Definition at line 211 of file SubsetEnumerator.h.

◆ begin() [1/3]

template<typename T>
void ogdf::SubsetEnumerator< T >::begin ( int  low,
int  high 
)
inline

Initialize the SubsetEnumerator to enumerate subsets of cardinalities from low to high.

Definition at line 122 of file SubsetEnumerator.h.

◆ begin() [2/3]

template<typename T>
void ogdf::SubsetEnumerator< T >::begin ( int  card)
inline

Initialize the SubsetEnumerator to enumerate subsets of given cardinality.

Definition at line 129 of file SubsetEnumerator.h.

◆ begin() [3/3]

template<typename T>
void ogdf::SubsetEnumerator< T >::begin ( )
inline

Initialize the SubsetEnumerator to enumerate all subsets.

Definition at line 135 of file SubsetEnumerator.h.

◆ hasMember()

template<typename T>
bool ogdf::SubsetEnumerator< T >::hasMember ( const T &  element) const
inline

Check in O(subset cardinality) whether the argument is a member of the subset.

Definition at line 153 of file SubsetEnumerator.h.

◆ initSubset()

template<typename T>
void ogdf::SubsetEnumerator< T >::initSubset ( int  card)
inlineprotected

Definition at line 96 of file SubsetEnumerator.h.

◆ list() [1/2]

template<typename T>
void ogdf::SubsetEnumerator< T >::list ( List< T > &  list) const
inline

Obtain (append) a list of the elements in the subset.

Definition at line 203 of file SubsetEnumerator.h.

◆ list() [2/2]

template<typename T>
void ogdf::SubsetEnumerator< T >::list ( List< T > &  list,
List< T > &  complement 
) const
inline

Obtain (append) a list of the elements in the subset and a list of the other elements of the set.

Definition at line 220 of file SubsetEnumerator.h.

◆ next()

template<typename T>
void ogdf::SubsetEnumerator< T >::next ( )
inline

Obtain the next subset if possible. The result should be checked using the valid() method.

Definition at line 172 of file SubsetEnumerator.h.

◆ operator[]()

template<typename T>
T ogdf::SubsetEnumerator< T >::operator[] ( int  i) const
inline

Get element of subset by index (starting from 0).

Definition at line 164 of file SubsetEnumerator.h.

◆ size()

template<typename T>
int ogdf::SubsetEnumerator< T >::size ( ) const
inline

Return the cardinality of the subset.

Definition at line 141 of file SubsetEnumerator.h.

◆ valid()

template<typename T>
bool ogdf::SubsetEnumerator< T >::valid ( ) const
inline

Is the current subset valid? If not, all subsets have already been enumerated.

Definition at line 147 of file SubsetEnumerator.h.

Member Data Documentation

◆ m_index

template<typename T>
Array<int> ogdf::SubsetEnumerator< T >::m_index
protected

Definition at line 94 of file SubsetEnumerator.h.

◆ m_maxCard

template<typename T>
int ogdf::SubsetEnumerator< T >::m_maxCard
protected

Definition at line 92 of file SubsetEnumerator.h.

◆ m_set

template<typename T>
const List<T>& ogdf::SubsetEnumerator< T >::m_set
protected

Definition at line 90 of file SubsetEnumerator.h.

◆ m_subset

template<typename T>
Array<T> ogdf::SubsetEnumerator< T >::m_subset
protected

Definition at line 93 of file SubsetEnumerator.h.

◆ m_valid

template<typename T>
bool ogdf::SubsetEnumerator< T >::m_valid
protected

Definition at line 91 of file SubsetEnumerator.h.


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