1 /*********************************************************************
3 * Copyright 2006 Keith Packard and Carl Worth
4 * Copyright 2012 Intel Corporation
5 * Copyright 2012 VMware, Inc.
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use, copy,
12 * modify, merge, publish, distribute, sublicense, and/or sell copies
13 * of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 *********************************************************************/
34 #include "trim_callset.hpp"
40 CallRange::CallRange(CallNo first, CallNo last, int level)
46 next = new CallRange*[level];
50 CallRange::contains(CallNo call_no)
52 return (first <= call_no && last >= call_no);
55 CallSet::CallSet(): head(0, 0, MAX_LEVEL)
57 head.first = std::numeric_limits<CallNo>::max();
58 head.last = std::numeric_limits<CallNo>::min();
60 for (int i = 0; i < MAX_LEVEL; i++)
67 * Generate a random level number, distributed
68 * so that each level is 1/4 as likely as the one before
70 * Note that level numbers run 1 <= level < MAX_LEVEL
75 /* tricky bit -- each bit is '1' 75% of the time */
76 long int bits = random() | random();
79 while (level < MAX_LEVEL)
91 CallSet::add(CallNo first, CallNo last)
93 CallRange **update[MAX_LEVEL];
94 CallRange *node, *next;
97 /* Find node immediately before insertion point. */
99 for (i = max_level - 1; i >= 0; i--) {
100 while (node->next[i] && first > node->next[i]->last) {
101 node = node->next[i];
103 update[i] = &node->next[i];
106 /* Can we contain first by expanding tail of current range by 1? */
107 if (node != &head && node->last == first - 1) {
110 goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
114 /* Current range could not contain first, look at next. */
116 node = node->next[0];
119 /* Do nothing if new range is already entirely contained. */
120 if (node->first <= first && node->last >= last) {
124 /* If last is already included, we can simply expand
125 * node->first to fully include the range. */
126 if (node->first <= last && node->last >= last) {
131 /* This is our candidate node if first is contained */
132 if (node->first <= first && node->last >= first) {
134 goto MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES;
138 /* Not possible to expand any existing node, so create a new one. */
140 level = random_level();
142 /* Handle first node of this level. */
143 if (level > max_level) {
144 level = max_level + 1;
145 update[max_level] = &head.next[max_level];
149 node = new CallRange(first, last, level);
151 /* Perform insertion into all lists. */
152 for (i = 0; i < level; i++) {
153 node->next[i] = *update[i];
157 MERGE_NODE_WITH_SUBSEQUENT_COVERED_NODES:
158 next = node->next[0];
159 while (next && next->first <= node->last + 1) {
160 if (next->last > node->last)
161 node->last = next->last;
163 /* Delete node 'next' */
164 for (i = 0; i < node->level && i < next->level; i++) {
165 node->next[i] = next->next[i];
168 for (; i < next->level; i++) {
169 *update[i] = next->next[i];
174 next = node->next[0];
179 CallSet::add(CallNo call_no)
181 this->add(call_no, call_no);
185 CallSet::contains(CallNo call_no)
191 for (i = max_level - 1; i >= 0; i--) {
192 while (node->next[i] && call_no > node->next[i]->last) {
193 node = node->next[i];
197 node = node->next[0];
202 return node->contains(call_no);