Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "intcnt.h" |
michael@0 | 6 | |
michael@0 | 7 | IntCount::IntCount() : numInts(0), iPair(nullptr) { } |
michael@0 | 8 | IntCount::~IntCount() { delete [] iPair;} |
michael@0 | 9 | int IntCount::getSize() {return numInts;} |
michael@0 | 10 | int IntCount::getCount(int pos) {return iPair[pos].cnt;} |
michael@0 | 11 | int IntCount::getIndex(int pos) {return iPair[pos].idx;} |
michael@0 | 12 | |
michael@0 | 13 | void IntCount::clear() |
michael@0 | 14 | { |
michael@0 | 15 | delete[] iPair; |
michael@0 | 16 | iPair = new IntPair[0]; |
michael@0 | 17 | numInts = 0; |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | int IntCount::countAdd(int index, int increment) |
michael@0 | 21 | { |
michael@0 | 22 | if(numInts) { |
michael@0 | 23 | // Do a binary search to find the element |
michael@0 | 24 | int divPoint = 0; |
michael@0 | 25 | |
michael@0 | 26 | if(index>iPair[numInts-1].idx) { |
michael@0 | 27 | divPoint = numInts; |
michael@0 | 28 | } else if(index<iPair[0].idx) { |
michael@0 | 29 | divPoint = 0; |
michael@0 | 30 | } else { |
michael@0 | 31 | int low=0, high=numInts-1; |
michael@0 | 32 | int mid = (low+high)/2; |
michael@0 | 33 | while(1) { |
michael@0 | 34 | mid = (low+high)/2; |
michael@0 | 35 | |
michael@0 | 36 | if(index<iPair[mid].idx) { |
michael@0 | 37 | high = mid; |
michael@0 | 38 | } else if(index>iPair[mid].idx) { |
michael@0 | 39 | if(mid<numInts-1 && index<iPair[mid+1].idx) { |
michael@0 | 40 | divPoint = mid+1; |
michael@0 | 41 | break; |
michael@0 | 42 | } else { |
michael@0 | 43 | low = mid+1; |
michael@0 | 44 | } |
michael@0 | 45 | } else if(index==iPair[mid].idx) { |
michael@0 | 46 | return iPair[mid].cnt += increment; |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | int i; |
michael@0 | 52 | IntPair *tpair = new IntPair[numInts+1]; |
michael@0 | 53 | for(i=0; i<divPoint; i++) { |
michael@0 | 54 | tpair[i] = iPair[i]; |
michael@0 | 55 | } |
michael@0 | 56 | for(i=divPoint; i<numInts; i++) { |
michael@0 | 57 | tpair[i+1] = iPair[i]; |
michael@0 | 58 | } |
michael@0 | 59 | ++numInts; |
michael@0 | 60 | delete [] iPair; |
michael@0 | 61 | iPair = tpair; |
michael@0 | 62 | iPair[divPoint].idx = index; |
michael@0 | 63 | iPair[divPoint].cnt = increment; |
michael@0 | 64 | return increment; |
michael@0 | 65 | } else { |
michael@0 | 66 | iPair = new IntPair[1]; |
michael@0 | 67 | numInts = 1; |
michael@0 | 68 | iPair[0].idx = index; |
michael@0 | 69 | return iPair[0].cnt = increment; |
michael@0 | 70 | } |
michael@0 | 71 | } |