Bug 1890277: part 4) Add CSPParser support for the `trusted-types` directive, guarded...
[gecko.git] / tools / jprof / intcnt.cpp
bloba87b6ccf745062ffb577aff5fe5b71fb754d9c99
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "intcnt.h"
7 IntCount::IntCount() : numInts(0), iPair(nullptr) {}
8 IntCount::~IntCount() { delete[] iPair; }
9 int IntCount::getSize() { return numInts; }
10 int IntCount::getCount(int pos) { return iPair[pos].cnt; }
11 int IntCount::getIndex(int pos) { return iPair[pos].idx; }
13 void IntCount::clear() {
14 delete[] iPair;
15 iPair = new IntPair[0];
16 numInts = 0;
19 int IntCount::countAdd(int index, int increment) {
20 if (numInts) {
21 // Do a binary search to find the element
22 int divPoint = 0;
24 if (index > iPair[numInts - 1].idx) {
25 divPoint = numInts;
26 } else if (index < iPair[0].idx) {
27 divPoint = 0;
28 } else {
29 int low = 0, high = numInts - 1;
30 int mid = (low + high) / 2;
31 while (1) {
32 mid = (low + high) / 2;
34 if (index < iPair[mid].idx) {
35 high = mid;
36 } else if (index > iPair[mid].idx) {
37 if (mid < numInts - 1 && index < iPair[mid + 1].idx) {
38 divPoint = mid + 1;
39 break;
40 } else {
41 low = mid + 1;
43 } else if (index == iPair[mid].idx) {
44 return iPair[mid].cnt += increment;
49 int i;
50 IntPair* tpair = new IntPair[numInts + 1];
51 for (i = 0; i < divPoint; i++) {
52 tpair[i] = iPair[i];
54 for (i = divPoint; i < numInts; i++) {
55 tpair[i + 1] = iPair[i];
57 ++numInts;
58 delete[] iPair;
59 iPair = tpair;
60 iPair[divPoint].idx = index;
61 iPair[divPoint].cnt = increment;
62 return increment;
63 } else {
64 iPair = new IntPair[1];
65 numInts = 1;
66 iPair[0].idx = index;
67 return iPair[0].cnt = increment;