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/. */
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() {
15 iPair
= new IntPair
[0];
19 int IntCount::countAdd(int index
, int increment
) {
21 // Do a binary search to find the element
24 if (index
> iPair
[numInts
- 1].idx
) {
26 } else if (index
< iPair
[0].idx
) {
29 int low
= 0, high
= numInts
- 1;
30 int mid
= (low
+ high
) / 2;
32 mid
= (low
+ high
) / 2;
34 if (index
< iPair
[mid
].idx
) {
36 } else if (index
> iPair
[mid
].idx
) {
37 if (mid
< numInts
- 1 && index
< iPair
[mid
+ 1].idx
) {
43 } else if (index
== iPair
[mid
].idx
) {
44 return iPair
[mid
].cnt
+= increment
;
50 IntPair
* tpair
= new IntPair
[numInts
+ 1];
51 for (i
= 0; i
< divPoint
; i
++) {
54 for (i
= divPoint
; i
< numInts
; i
++) {
55 tpair
[i
+ 1] = iPair
[i
];
60 iPair
[divPoint
].idx
= index
;
61 iPair
[divPoint
].cnt
= increment
;
64 iPair
= new IntPair
[1];
67 return iPair
[0].cnt
= increment
;