integrator.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1996 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the MASH Research
00017  *  Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #ifndef lint
00036 static const char rcsid[] =
00037     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/tools/integrator.cc,v 1.8 1998/06/27 01:23:57 gnguyen Exp $";
00038 #endif
00039 
00040 #include <stdlib.h>
00041 #include "integrator.h"
00042 
00043 static class IntegratorClass : public TclClass {
00044  public:
00045     IntegratorClass() : TclClass("Integrator") {}
00046     TclObject* create(int, const char*const*) {
00047         return (new Integrator);
00048     }
00049 } integrator_class;
00050 
00051 Integrator::Integrator() : lastx_(0.), lasty_(0.), sum_(0.)
00052 {
00053     bind("lastx_", &lastx_);
00054     bind("lasty_", &lasty_);
00055     bind("sum_", &sum_);
00056 }
00057 
00058 void Integrator::set(double x, double y)
00059 {
00060     lastx_ = x;
00061     lasty_ = y;
00062     sum_ = 0.;
00063 }
00064 
00065 void Integrator::newPoint(double x, double y)
00066 {
00067     sum_ += (x - lastx_) * lasty_;
00068     lastx_ = x;
00069     lasty_ = y;
00070 }
00071 
00072 int Integrator::command(int argc, const char*const* argv)
00073 {
00074     if (argc == 2) {
00075         if (strcmp(argv[1], "reset") == 0) {
00076             set(0., 0.);
00077             return (TCL_OK);
00078         }
00079     } else if (argc == 4) {
00080         if (strcmp(argv[1], "newpoint") == 0) {
00081             double x = atof(argv[2]);
00082             double y = atof(argv[3]);
00083             newPoint(x, y);
00084             return (TCL_OK);
00085         }
00086     }
00087     return (TclObject::command(argc, argv));
00088 }
00089 
00090 /*
00091  * interface for the 'Samples' class, will probably want to move
00092  * to some sort of "stats" file at some point
00093  */
00094 static class SamplesClass : public TclClass {
00095  public:
00096     SamplesClass() : TclClass("Samples") {}
00097     TclObject* create(int, const char*const*) {
00098         return (new Samples);
00099     }
00100 } samples_class;
00101 
00102 int Samples::command(int argc, const char*const* argv)
00103 {
00104     if (argc == 2) {
00105         if (strcmp(argv[1], "mean") == 0) {
00106             if (cnt_ > 0) {
00107                 Tcl::instance().resultf("%g", mean());
00108                 return (TCL_OK);
00109             }
00110             Tcl::instance().resultf("tried to take mean with no sample points");
00111             return (TCL_ERROR);
00112         }
00113         if (strcmp(argv[1], "cnt") == 0) {
00114             Tcl::instance().resultf("%u", cnt());
00115             return (TCL_OK);
00116         }
00117         if (strcmp(argv[1], "variance") == 0) {
00118             if (cnt_ == 1) {
00119                 Tcl::instance().resultf("0.0");
00120                 return (TCL_OK);
00121             }
00122             if (cnt_ > 2) {
00123                 Tcl::instance().resultf("%g", variance());
00124                 return (TCL_OK);
00125             }
00126             return (TCL_ERROR);
00127         }
00128         if (strcmp(argv[1], "reset") == 0) {
00129             reset();
00130             return (TCL_OK);
00131         }
00132     } else if ( argc == 3 ) {
00133         if ( strcmp(argv[1],"newpoint") == 0 ) {
00134             double x = atof(argv[2]);
00135             newPoint(x);
00136             return (TCL_OK);
00137         }
00138     }
00139     return (TclObject::command(argc, argv));
00140 }

Generated on Tue Mar 6 16:47:46 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6