Public Member Functions | Private Attributes
mbl_select_n_from_m Class Reference

A class which returns an N element subset of the integers [0..M-1]. More...

#include <mbl_select_n_from_m.h>

List of all members.

Public Member Functions

 mbl_select_n_from_m ()
 Dflt ctor.
 mbl_select_n_from_m (unsigned int n, unsigned int m)
 Construct to select n from m.
void set_n_m (unsigned int new_n, unsigned int new_m)
 Construct to select n from m.
void use_random (bool flag)
 If true then use random subsets.
void reseed (long)
 Reseed randomiser.
bool reset ()
 Reset to first example. Returns true if there is a valid first example.
bool next ()
 Generate next set. Return true until all combinations exhausted.
bool is_done () const
 Returns true when all sets enumerated.
const vcl_vector< int > & subset () const
 Current subset of n from m.
const vcl_vector< int > & complement ()
 Sub-set not chosen (m-n) from m.

Private Attributes

unsigned int n_
unsigned int m_
bool is_done_
bool use_random_
vcl_vector< int > index_
vcl_vector< int > not_index_
mbl_random_n_from_m random_

Detailed Description

A class which returns an N element subset of the integers [0..M-1].

By default it systematically steps through all combinations of choosing N integers from a set of 0..M-1 Thus when N=1 repeated calls to subset() return 1,2,3,4,...,M When N=2 calls to subset give (0,1), (0,1) .. (0,M-1), (1,2), (1,3),..(M-2,M-1) However, the use_random() function allows random examples to be returned (using the mbl_random_n_from_m class). When N > M, nothing is returned, ie, next() immediately returns false. When N == 0, the empty set is returned. The complement() function returns the integers not in the subset.

The following code snippet runs process_data() on all possible subsets:

  mbl_select_n_from_m selector(3,5);
  if (selector.reset())
  do
  {
    process_data(selector.subset());
  } while selector.next();

Definition at line 30 of file mbl_select_n_from_m.h.


Constructor & Destructor Documentation

mbl_select_n_from_m::mbl_select_n_from_m ( )

Dflt ctor.

Definition at line 10 of file mbl_select_n_from_m.cxx.

mbl_select_n_from_m::mbl_select_n_from_m ( unsigned int  n,
unsigned int  m 
)

Construct to select n from m.

Definition at line 16 of file mbl_select_n_from_m.cxx.


Member Function Documentation

const vcl_vector< int > & mbl_select_n_from_m::complement ( )

Sub-set not chosen (m-n) from m.

is_done() should not be true when this function is being called

Definition at line 132 of file mbl_select_n_from_m.cxx.

bool mbl_select_n_from_m::is_done ( ) const [inline]

Returns true when all sets enumerated.

Never returns true when random subsets being chosen

Definition at line 68 of file mbl_select_n_from_m.h.

bool mbl_select_n_from_m::next ( )

Generate next set. Return true until all combinations exhausted.

Definition at line 75 of file mbl_select_n_from_m.cxx.

void mbl_select_n_from_m::reseed ( long  s)

Reseed randomiser.

Definition at line 24 of file mbl_select_n_from_m.cxx.

bool mbl_select_n_from_m::reset ( )

Reset to first example. Returns true if there is a valid first example.

Definition at line 56 of file mbl_select_n_from_m.cxx.

void mbl_select_n_from_m::set_n_m ( unsigned int  new_n,
unsigned int  new_m 
)

Construct to select n from m.

Definition at line 31 of file mbl_select_n_from_m.cxx.

const vcl_vector< int > & mbl_select_n_from_m::subset ( ) const

Current subset of n from m.

is_done() should not be true when this function is being called

Definition at line 126 of file mbl_select_n_from_m.cxx.

void mbl_select_n_from_m::use_random ( bool  flag)

If true then use random subsets.

Definition at line 46 of file mbl_select_n_from_m.cxx.


Member Data Documentation

vcl_vector<int> mbl_select_n_from_m::index_ [private]

Definition at line 38 of file mbl_select_n_from_m.h.

Definition at line 35 of file mbl_select_n_from_m.h.

unsigned int mbl_select_n_from_m::m_ [private]

Definition at line 34 of file mbl_select_n_from_m.h.

unsigned int mbl_select_n_from_m::n_ [private]

Definition at line 33 of file mbl_select_n_from_m.h.

vcl_vector<int> mbl_select_n_from_m::not_index_ [private]

Definition at line 39 of file mbl_select_n_from_m.h.

Definition at line 41 of file mbl_select_n_from_m.h.

Definition at line 36 of file mbl_select_n_from_m.h.


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