tools/jprof/intcnt.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/jprof/intcnt.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "intcnt.h"
     1.9 +
    1.10 +IntCount::IntCount() : numInts(0), iPair(nullptr) { }
    1.11 +IntCount::~IntCount() { delete [] iPair;}
    1.12 +int IntCount::getSize() {return numInts;}
    1.13 +int IntCount::getCount(int pos) {return iPair[pos].cnt;}
    1.14 +int IntCount::getIndex(int pos) {return iPair[pos].idx;}
    1.15 +
    1.16 +void IntCount::clear()
    1.17 +{
    1.18 +    delete[] iPair;
    1.19 +    iPair = new IntPair[0];
    1.20 +    numInts = 0;
    1.21 +}
    1.22 +
    1.23 +int IntCount::countAdd(int index, int increment)
    1.24 +{
    1.25 +  if(numInts) {
    1.26 +    // Do a binary search to find the element
    1.27 +    int divPoint = 0;
    1.28 +
    1.29 +    if(index>iPair[numInts-1].idx) {
    1.30 +	    divPoint = numInts;
    1.31 +    } else if(index<iPair[0].idx) {
    1.32 +	    divPoint = 0;
    1.33 +    } else {
    1.34 +	    int low=0, high=numInts-1;
    1.35 +	    int mid = (low+high)/2;
    1.36 +	    while(1) {
    1.37 +        mid = (low+high)/2;
    1.38 +
    1.39 +        if(index<iPair[mid].idx) {
    1.40 +          high = mid;
    1.41 +        } else if(index>iPair[mid].idx) {
    1.42 +          if(mid<numInts-1 && index<iPair[mid+1].idx) {
    1.43 +            divPoint = mid+1;
    1.44 +            break;
    1.45 +          } else {
    1.46 +            low = mid+1;
    1.47 +          }
    1.48 +        } else if(index==iPair[mid].idx) {
    1.49 +          return iPair[mid].cnt += increment;
    1.50 +        }
    1.51 +	    }
    1.52 +    }
    1.53 +
    1.54 +    int i;
    1.55 +    IntPair *tpair = new IntPair[numInts+1];
    1.56 +    for(i=0; i<divPoint; i++) {
    1.57 +      tpair[i] = iPair[i];
    1.58 +    }
    1.59 +    for(i=divPoint; i<numInts; i++) {
    1.60 +      tpair[i+1] = iPair[i];
    1.61 +    }
    1.62 +    ++numInts;
    1.63 +    delete [] iPair;
    1.64 +    iPair = tpair;
    1.65 +    iPair[divPoint].idx = index;
    1.66 +    iPair[divPoint].cnt = increment;
    1.67 +    return increment;
    1.68 +  } else {
    1.69 +    iPair = new IntPair[1];
    1.70 +    numInts = 1;
    1.71 +    iPair[0].idx = index;
    1.72 +    return iPair[0].cnt = increment;
    1.73 +  }
    1.74 +}

mercurial