CMS 3D CMS Logo

PartitionGenerator Class Reference

Class to compute the possible partitions of a collection of 'collectionSize' elements. More...

#include <CommonTools/Statistics/interface/PartitionGenerator.h>

List of all members.

Public Types

typedef std::vector< intPartition

Public Member Functions

std::vector< Partitionpartitions (int collectionSize, int minCollectionSize=1) const
 Return partitions in a row.
std::vector< std::vector
< Partition > > 
sortedPartitions (int collectionSize, int minCollectionSize=1) const
 Return partitions ordered according to the number of subcollections they have.

Classes

class  LessCollections
 a private class just defining the () operator More...


Detailed Description

Class to compute the possible partitions of a collection of 'collectionSize' elements.

A Partition is a list of subcollection sizes that add up to 'collectionSize', ordered in decreasing order of subcollection size. No subcollection size is less than 'minCollectionSize'.

Definition at line 13 of file PartitionGenerator.h.


Member Typedef Documentation

typedef std::vector<int> PartitionGenerator::Partition

Definition at line 17 of file PartitionGenerator.h.


Member Function Documentation

vector< PartitionGenerator::Partition > PartitionGenerator::partitions ( int  collectionSize,
int  minCollectionSize = 1 
) const

Return partitions in a row.

Definition at line 8 of file PartitionGenerator.cc.

References edmNew::copy(), first, and edm::second().

Referenced by CombinationGenerator< T >::combinations(), and sortedPartitions().

00009       {
00010 
00011   vector<Partition> partitions;
00012 
00013   // at the very least, we have a single bag of size 'collectionSize'
00014   partitions.push_back( Partition(1, collectionSize) );
00015 
00016   int first = collectionSize - minCollectionSize, second = minCollectionSize;
00017   while( first >= second ) {
00018     // try to further divide the first
00019     vector<Partition> subPartitions = this->partitions(first, second);
00020     vector<Partition>::iterator isub;
00021     for( isub = subPartitions.begin(); isub != subPartitions.end(); isub++ ) {
00022       const Partition& sub = *isub;
00023       // reject subPartitions of first with a last element smaller than second
00024       if( sub.back() < second ) continue;
00025       Partition partition( sub.size()+1 );
00026       copy( sub.begin(), sub.end(), partition.begin() );
00027       partition[ partition.size()-1 ] = second;
00028       partitions.push_back( partition );
00029     }
00030     first--; second++;
00031   }
00032 
00033   return partitions;
00034 }

vector< vector< PartitionGenerator::Partition > > PartitionGenerator::sortedPartitions ( int  collectionSize,
int  minCollectionSize = 1 
) const

Return partitions ordered according to the number of subcollections they have.

'sortedPartitions[N]' = list of partitions with N subcollections.

Definition at line 38 of file PartitionGenerator.cc.

References i, partitions(), and python::multivaluedict::sort().

Referenced by CombinationGenerator< T >::combinations().

00039                                                                   {
00040 
00041   vector<Partition> partitions 
00042     = this->partitions(collectionSize, minCollectionSize);
00043   sort (partitions.begin(), partitions.end(), LessCollections());
00044   
00045   vector< vector<Partition> > sortedPartitions;
00046   sortedPartitions.resize(partitions.rbegin()->size());
00047 
00048   for (vector<Partition>::const_iterator i = partitions.begin();
00049        i != partitions.end(); i++) {
00050     sortedPartitions[(*i).size() - 1].push_back(*i);
00051   }
00052 
00053   return sortedPartitions;
00054 }


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:29:27 2009 for CMSSW by  doxygen 1.5.4