MESSAGE
DATE | 2011-05-09 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] C++ Workshop - Notes
|
Within the workshop we have played with a rudimentary statistical module within out test_del.cpp program. It's been tested fairly rigerously and i think it is probably useful to remove the module from the parent program and to put it into it's own header file. As such, this is the program file, which i want to review for a minute
* ===================================================================================== * * Filename: stats.h * * Description: simple distribution execise * * Version: 1.0 * Created: 04/15/2011 06:12:42 PM * Revision: none * Compiler: gcc * * Author: Ruben Safir, * Company: * * ===================================================================================== */ #ifndef STATS_H #define STATS_H #include #include #include #include #include #include "linklist.h"
/* * ===================================================================================== * Class: Distribution * Description: Keeps Track of Distribution of 6's (or anything else) * in a series of List * ===================================================================================== */
namespace stats{
template class Distribution { template friend std::ostream & operator<<(std::ostream &, const Distribution&); public: /* ==================== LIFECYCLE * ======================================= */ Distribution (T descr, int occurances = 0); Distribution():freq(NULL),occurances(NULL){}; // Distribution ( const Distribution &other ); /* copy // constructor */ // ~Distribution (); /* // destructor */ /* ==================== ACCESSORS * ======================================= */ T description()const{ return freq;} int population()const { return occurances; } /* ==================== MUTATORS * ======================================= */ void increase_occ(){ ++occurances; std::cout << "description " << freq << " occurances " << occurances << std::endl; } void descrease_occ(){ --occurances; } /* ==================== OPERATORS * ======================================= */ //Distribution& operator = ( const Distribution &other //); /* assignment operator */ int operator()(){ return freq; } bool operator==(Distribution &tmp){ if(this->freq == tmp.freq) return true; return false; }
bool operator<(Distribution &tmp){ if(freq < tmp.freq) return true; return false; } chainlist::List< stats::Distribution > * tally; //a list of distribution talleys
protected: /* ==================== DATA MEMBERS * ======================================= */ private: /* ==================== DATA MEMBERS * ======================================= */ T freq; //description of how many times found in a List int occurances; //description of how many times a frequency was found in a list }; /* ----- end of class Distribution ----- */
template std::ostream & operator << ( std::ostream & os, const Distribution & obj ) { T desc = obj.description(); int pop = obj.population(); os << "The Identification of " << desc << " was seen " << pop ; return os; } /* ----- end of function operator << ----- */
template Distribution::Distribution(T descr, int occ): occurances(occ){ freq = descr; }
/* Routine to go though a single list and add it to an existing * distribution table */ template void mount_individual_data_point(chainlist::List * tabulate, chainlist::List > * table);
/* Routine to find all the occurances of a type in a list of lists */ template void take_tally(chainlist::List *,chainlist::List > *);
template void take_tally(chainlist::List * tabulate, chainlist::List > * table){ for(tabulate->cursor()=tabulate->front();tabulate->cursor() != tabulate->endd(); tabulate->cursor( tabulate->cursor()->next() ) ){ //build distribution list mount_individual_data_point(tabulate, table); } //we are at the end of tabulate mount_individual_data_point(tabulate, table); table->sort(*table); }
template void mount_individual_data_point(chainlist::List * tabulate, chainlist::List > * table){ int val; stats::Distribution * j; val = *(tabulate->cursor()->value()); //get a value table->cursor()= table->front(); //check to see if the distribution list exists if(!table->cursor()){ // if not add a distribution table to the List of distributions j = new stats::Distribution (val); table->insert(*j ); //now we have at least one delete j; j=table->cursor()->value();//and increased its population j->increase_occ(); }else{ //otherwise search for a distribution node described as value table->find_value(val); if( table->cursor() ){ j=table->cursor()->value();//and increase its population j->increase_occ(); }else{//otherwise add a new node j = new stats::Distribution (val); table->insert( *j ); //now we have one for that value delete j; j=table->cursor()->value();//and increased its population j->increase_occ(); } } }
} #endif /* STATS_H */
In total it is about `50 lines in the namespace stats
The class Distribution has 2 basic data members, freq, which is an object of type T according to the template, generally a specific data point among a set of data, and occurances of type int which holds how many times data point freq occurs within a set of data.
so if you have a set of data, chars for example
{ a, f, d, r ,t w, d, i, d, k, l, g }
you can create a single distribution object who might hold, for example, the letter d and the occurances of d as 3 (there are 3 'd''s in our set.
There is a member function to class Distribution called take_tally defined as follows
template void take_tally(chainlist::List *,chainlist::List > *);
it takes as arguments a pointer to one of our custom lists within the chainlist namespace. That needs to have a set of nodes with values of type T in each node. That list will be searched for values in order to record their frequencies of occurances, unique value recorded in a Distribution object, of which a list of those will be stored in the chainlist::List of type Distribution which is being passed as the second argument, each Distribution Object a value of a node.
The member function mount_individual_data_point actually constructs and builds a chainlist::List adding new nodes as needed, and increasing the occurance counts for each unique data type.
As it stands, the module is useful, but not nearly as useful as it can be. Seperating out this namespace will allow us to greatly develop our statistical analysis capabilities of Lists in a uniform API
Ruben
|
|