src/share/vm/utilities/numberSeq.hpp

changeset 777
37f87013dfd8
child 1521
89f1b9ae8991
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/numberSeq.hpp	Thu Jun 05 15:57:56 2008 -0700
     1.3 @@ -0,0 +1,117 @@
     1.4 +/*
     1.5 + * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/**
    1.29 + **  This file contains a few classes that represent number sequence,
    1.30 + **  x1, x2, x3, ..., xN, and can calculate their avg, max, and sd.
    1.31 + **
    1.32 + **  Here's a quick description of the classes:
    1.33 + **
    1.34 + **    AbsSeq: abstract superclass
    1.35 + **    NumberSeq: the sequence is assumed to be very long and the
    1.36 + **      maximum, avg, sd, davg, and dsd are calculated over all its elements
    1.37 + **    TruncatedSeq: this class keeps track of the last L elements
    1.38 + **      of the sequence and calculates avg, max, and sd only over them
    1.39 + **/
    1.40 +
    1.41 +#define DEFAULT_ALPHA_VALUE 0.7
    1.42 +
    1.43 +class AbsSeq {
    1.44 +private:
    1.45 +  void init(double alpha);
    1.46 +
    1.47 +protected:
    1.48 +  int    _num; // the number of elements in the sequence
    1.49 +  double _sum; // the sum of the elements in the sequence
    1.50 +  double _sum_of_squares; // the sum of squares of the elements in the sequence
    1.51 +
    1.52 +  double _davg; // decaying average
    1.53 +  double _dvariance; // decaying variance
    1.54 +  double _alpha; // factor for the decaying average / variance
    1.55 +
    1.56 +  // This is what we divide with to get the average. In a standard
    1.57 +  // number sequence, this should just be the number of elements in it.
    1.58 +  virtual double total() const { return (double) _num; };
    1.59 +
    1.60 +public:
    1.61 +  AbsSeq(double alpha = DEFAULT_ALPHA_VALUE);
    1.62 +
    1.63 +  virtual void add(double val); // adds a new element to the sequence
    1.64 +  void add(unsigned val) { add((double) val); }
    1.65 +  virtual double maximum() const = 0; // maximum element in the sequence
    1.66 +  virtual double last() const = 0; // last element added in the sequence
    1.67 +
    1.68 +  // the number of elements in the sequence
    1.69 +  int num() const { return _num; }
    1.70 +  // the sum of the elements in the sequence
    1.71 +  double sum() const { return _sum; }
    1.72 +
    1.73 +  double avg() const; // the average of the sequence
    1.74 +  double variance() const; // the variance of the sequence
    1.75 +  double sd() const; // the standard deviation of the sequence
    1.76 +
    1.77 +  double davg() const; // decaying average
    1.78 +  double dvariance() const; // decaying variance
    1.79 +  double dsd() const; // decaying "standard deviation"
    1.80 +};
    1.81 +
    1.82 +class NumberSeq: public AbsSeq {
    1.83 +private:
    1.84 +  bool check_nums(NumberSeq* total, int n, NumberSeq** parts);
    1.85 +
    1.86 +protected:
    1.87 +  double _last;
    1.88 +  double _maximum; // keep track of maximum value
    1.89 +
    1.90 +public:
    1.91 +  NumberSeq(double alpha = DEFAULT_ALPHA_VALUE);
    1.92 +  NumberSeq(NumberSeq* total, int n_parts, NumberSeq** parts);
    1.93 +
    1.94 +  virtual void add(double val);
    1.95 +  virtual double maximum() const { return _maximum; }
    1.96 +  virtual double last() const { return _last; }
    1.97 +};
    1.98 +
    1.99 +class TruncatedSeq: public AbsSeq {
   1.100 +private:
   1.101 +  enum PrivateConstants {
   1.102 +    DefaultSeqLength = 10
   1.103 +  };
   1.104 +  void init();
   1.105 +protected:
   1.106 +  double *_sequence; // buffers the last L elements in the sequence
   1.107 +  int     _length; // this is L
   1.108 +  int     _next;   // oldest slot in the array, i.e. next to be overwritten
   1.109 +
   1.110 +public:
   1.111 +  // accepts a value for L
   1.112 +  TruncatedSeq(int length = DefaultSeqLength,
   1.113 +               double alpha = DEFAULT_ALPHA_VALUE);
   1.114 +  virtual void add(double val);
   1.115 +  virtual double maximum() const;
   1.116 +  virtual double last() const; // the last value added to the sequence
   1.117 +
   1.118 +  double oldest() const; // the oldest valid value in the sequence
   1.119 +  double predict_next() const; // prediction based on linear regression
   1.120 +};

mercurial