1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  stats.h
  5  *
  6  *    Description:  simple distribution execise
  7  *
  8  *        Version:  1.0
  9  *        Created:  04/15/2011 06:12:42 PM
 10  *       Revision:  none
 11  *       Compiler:  gcc
 12  *
 13  *         Author:  Ruben Safir,
 14  *        Company:  
 15  *
 16  * =====================================================================================
 17  */
 18 #ifndef STATS_H
 19 #define STATS_H
 20 #include        <iostream>
 21 #include        <fstream>
 22 #include        <climits>
 23 #include        <stdlib.h>
 24 #include        <iomanip>
 25 #include        "linklist.h"
 26
 27 /*
 28  * =====================================================================================
 29  *        Class:  Distribution
 30  *  Description:  Keeps Track of Distribution of 6's (or anything else) in a series of List
 31  * =====================================================================================
 32  */
 33
 34
 35 namespace stats{
 36
 37    template<class T>
 38       class Distribution {
 39          template<T> friend std::ostream & operator<<(std::ostream &, const Distribution<T>&);
 40          public:
 41          /* ====================  LIFECYCLE     ======================================= */
 42          Distribution (T descr, int occurances = 0);
 43          Distribution():freq(NULL),occurances(NULL){};
 44          //    Distribution ( const Distribution &other );   /* copy constructor */
 45          //            ~Distribution ();                            /* destructor       */
 46          /* ====================  ACCESSORS     ======================================= */
 47          T description()const{ return freq;}
 48          int population()const { return occurances; }  
 49          /* ====================  MUTATORS      ======================================= */
 50          void increase_occ(){ ++occurances; std::cout << "description " << freq << " occurances " << occurances << std::endl; }
 51          void descrease_occ(){ --occurances; }
 52          /* ====================  OPERATORS     ======================================= */
 53          //Distribution& operator = ( const Distribution &other ); /* assignment operator */
 54          int operator()(){
 55             return freq;
 56          }
 57          bool operator==(Distribution &tmp){
 58             if(this->freq == tmp.freq)
 59                return true;
 60             return false;
 61          }
 62
 63          bool operator<(Distribution &tmp){
 64             if(freq < tmp.freq)
 65                return true;
 66             return false;
 67          }
 68          chainlist::List< stats::Distribution<T> > * tally;  //a list of distribution talleys
 69
 70          float stddev(chainlist::List<stats::Distribution<T> > *);
 71
 72
 73
 74
 75          protected:
 76          /* ====================  DATA MEMBERS  ======================================= */
 77          private:
 78          /* ====================  DATA MEMBERS  ======================================= */
 79          T freq;  //description of how many times found in a List
 80          int occurances; //description of how many times a frequency was found in a list
 81       }; /* -----  end of class Distribution  ----- */
 82
 83
 84    template<typename T>
 85       std::ostream & operator << ( std::ostream & os, const Distribution<T> & obj )
 86       {
 87          T desc = obj.description();
 88          int pop  = obj.population();
 89          os << "The Identification of " << desc << " was seen " << pop ;
 90          return os;
 91       }         /* -----  end of function operator <<  ----- */
 92
 93    template<typename T>
 94       Distribution<T>::Distribution(T descr, int occ): occurances(occ){
 95          freq = descr;
 96       }
 97
 98
 99    //calculation standard deviation of distribution list
100    //
101    template<typename T>
102       float Distribution<T>::stddev(chainlist::List<stats::Distribution<T> > * tally){
103          float dev;
104
105
106
107          return dev;
108       }
109
110    /*  Routine to go though a single list and add it to an existing distribution table */
111    template<typename T>
112       void mount_individual_data_point(chainlist::List<T> * tabulate, chainlist::List<stats::Distribution<T> > * table);
113
114    /* Routine to find all the occurances of a type in a list of lists */
115    template<typename T>
116       void take_tally(chainlist::List<T> *,chainlist::List<stats::Distribution<T> > *);
117
118
119
120
121
122
123    template<typename T>
124       void take_tally(chainlist::List<T> * tabulate, chainlist::List<stats::Distribution<T> > * table){
125          for(tabulate->cursor()=tabulate->front();tabulate->cursor() != tabulate->endd(); tabulate->cursor( tabulate->cursor()->next() ) ){ //build distribution list
126             mount_individual_data_point(tabulate, table);
127          }
128          //we are at the end of tabulate
129          mount_individual_data_point(tabulate, table);
130          table->sort(*table);
131       }
132
133    template<typename T>
134       void mount_individual_data_point(chainlist::List<T> * tabulate, chainlist::List<stats::Distribution<T> > * table){
135          int val;
136          stats::Distribution<T> * j;
137          val = *(tabulate->cursor()->value()); //get a value
138          table->cursor()= table->front(); //check to see if the distribution list exists
139          if(!table->cursor()){ // if not add a distribution table to the List of distributions
140             j = new stats::Distribution<T> (val);
141             table->insert(*j ); //now we have at least one
142             delete j;
143             j=table->cursor()->value();//and increased its population
144             j->increase_occ();
145          }else{
146             //otherwise search for a distribution node described as value
147             table->find_value(val);
148             if( table->cursor() ){
149                j=table->cursor()->value();//and increase its population
150                j->increase_occ();
151             }else{//otherwise add a new node
152                j = new stats::Distribution<T> (val);
153                table->insert( *j ); //now we have one for that value
154                delete j;
155                j=table->cursor()->value();//and increased its population
156                j->increase_occ();
157             }
158          }
159       }
160
161
162
163
164    template <typename T>
165       float mean_list(chainlist::List< Distribution<T> > * tally){
166          if(tally->endd() == 0){
167             std::cout << "Empty List" << std::endl;
168             return;
169          }
170
171          int sum = 0;
172
173          tally->cursor() = tally->front();
174          while(tally->cursor() != tally->endd() ){
175             sum +=  tally->cursor()->value()->population() ;
176             tally->cursor(tally->cursor()->next());
177          }
178          sum +=  tally->cursor()->value()->population() ;
179
180
181          sum += tally->curosor->value()->population();
182
183          return sum/(tally->size());
184       }
185
186
187
188
189
190 }
191 #endif /* STATS_H */