• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Vec.cc

Go to the documentation of this file.
00001 /* -*-Mode: c++;-*-
00002   Various vector related code.
00003 
00004   @section license License
00005 
00006   Licensed to the Apache Software Foundation (ASF) under one
00007   or more contributor license agreements.  See the NOTICE file
00008   distributed with this work for additional information
00009   regarding copyright ownership.  The ASF licenses this file
00010   to you under the Apache License, Version 2.0 (the
00011   "License"); you may not use this file except in compliance
00012   with the License.  You may obtain a copy of the License at
00013 
00014       http://www.apache.org/licenses/LICENSE-2.0
00015 
00016   Unless required by applicable law or agreed to in writing, software
00017   distributed under the License is distributed on an "AS IS" BASIS,
00018   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019   See the License for the specific language governing permissions and
00020   limitations under the License.
00021 */
00022 
00023 /* UnionFind after Tarjan */
00024 
00025 #include <stdint.h>
00026 #include "Vec.h"
00027 
00028 const uintptr_t prime2[] = {
00029   1, 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191,
00030   16381, 32749, 65521, 131071, 262139, 524287, 1048573, 2097143,
00031   4194301, 8388593, 16777213, 33554393, 67108859, 134217689,
00032   268435399, 536870909
00033 };
00034   
00035 // primes generated with map_mult.c
00036 const uintptr_t open_hash_primes[256] = {
00037 0x02D4AF27, 0x1865DFC7, 0x47C62B43, 0x35B4889B, 0x210459A1, 0x3CC51CC7, 0x02ADD945, 0x0607C4D7, 
00038 0x558E6035, 0x0554224F, 0x5A281657, 0x1C458C7F, 0x7F8BE723, 0x20B9BA99, 0x7218AA35, 0x64B10C2B, 
00039 0x548E8983, 0x5951218F, 0x7AADC871, 0x695FA5B1, 0x40D40FCB, 0x20E03CC9, 0x55E9920F, 0x554CE08B, 
00040 0x7E78B1D7, 0x7D965DF9, 0x36A520A1, 0x1B0C6C11, 0x33385667, 0x2B0A7B9B, 0x0F35AE23, 0x0BD608FB, 
00041 0x2284ADA3, 0x6E6C0687, 0x129B3EED, 0x7E86289D, 0x1143C24B, 0x1B6C7711, 0x1D87BB41, 0x4C7E635D, 
00042 0x67577999, 0x0A0113C5, 0x6CF085B5, 0x14A4D0FB, 0x4E93E3A7, 0x5C87672B, 0x67F3CA17, 0x5F944339, 
00043 0x4C16DFD7, 0x5310C0E3, 0x2FAD1447, 0x4AFB3187, 0x08468B7F, 0x49E56C51, 0x6280012F, 0x097D1A85, 
00044 0x34CC9403, 0x71028BD7, 0x6DEDC7E9, 0x64093291, 0x6D78BB0B, 0x7A03B465, 0x2E044A43, 0x1AE58515, 
00045 0x23E495CD, 0x46102A83, 0x51B78A59, 0x051D8181, 0x5352CAC9, 0x57D1312B, 0x2726ED57, 0x2E6BC515, 
00046 0x70736281, 0x5938B619, 0x0D4B6ACB, 0x44AB5E2B, 0x0029A485, 0x002CE54F, 0x075B0591, 0x3EACFDA9, 
00047 0x0AC03411, 0x53B00F73, 0x2066992D, 0x76E72223, 0x55F62A8D, 0x3FF92EE1, 0x17EE0EB3, 0x5E470AF1, 
00048 0x7193EB7F, 0x37A2CCD3, 0x7B44F7AF, 0x0FED8B3F, 0x4CC05805, 0x7352BF79, 0x3B61F755, 0x523CF9A3, 
00049 0x1AAFD219, 0x76035415, 0x5BE84287, 0x6D598909, 0x456537E9, 0x407EA83F, 0x23F6FFD5, 0x60256F39, 
00050 0x5D8EE59F, 0x35265CEB, 0x1D4AD4EF, 0x676E2E0F, 0x2D47932D, 0x776BB33B, 0x6DE1902B, 0x2C3F8741, 
00051 0x5B2DE8EF, 0x686DDB3B, 0x1D7C61C7, 0x1B061633, 0x3229EA51, 0x7FCB0E63, 0x5F22F4C9, 0x517A7199, 
00052 0x2A8D7973, 0x10DCD257, 0x41D59B27, 0x2C61CA67, 0x2020174F, 0x71653B01, 0x2FE464DD, 0x3E7ED6C7, 
00053 0x164D2A71, 0x5D4F3141, 0x5F7BABA7, 0x50E1C011, 0x140F5D77, 0x34E80809, 0x04AAC6B3, 0x29C42BAB, 
00054 0x08F9B6F7, 0x461E62FD, 0x45C2660B, 0x08BF25A7, 0x5494EA7B, 0x0225EBB7, 0x3C5A47CF, 0x2701C333, 
00055 0x457ED05B, 0x48CDDE55, 0x14083099, 0x7C69BDAB, 0x7BF163C9, 0x41EE1DAB, 0x258B1307, 0x0FFAD43B, 
00056 0x6601D767, 0x214DBEC7, 0x2852CCF5, 0x0009B471, 0x190AC89D, 0x5BDFB907, 0x15D4E331, 0x15D22375, 
00057 0x13F388D5, 0x12ACEDA5, 0x3835EA5D, 0x2587CA35, 0x06756643, 0x487C6F55, 0x65C295EB, 0x1029F2E1, 
00058 0x10CEF39D, 0x14C2E415, 0x444825BB, 0x24BE0A2F, 0x1D2B7C01, 0x64AE3235, 0x5D2896E5, 0x61BBBD87, 
00059 0x4A49E86D, 0x12C277FF, 0x72C81289, 0x5CF42A3D, 0x332FF177, 0x0DAECD23, 0x6000ED1D, 0x203CDDE1, 
00060 0x40C62CAD, 0x19B9A855, 0x782020C3, 0x6127D5BB, 0x719889A7, 0x40E4FCCF, 0x2A3C8FF9, 0x07411C7F, 
00061 0x3113306B, 0x4D7CA03F, 0x76119841, 0x54CEFBDF, 0x11548AB9, 0x4B0748EB, 0x569966B1, 0x45BC721B, 
00062 0x3D5A376B, 0x0D8923E9, 0x6D95514D, 0x0F39A367, 0x2FDAD92F, 0x721F972F, 0x42D0E21D, 0x5C5952DB, 
00063 0x7394D007, 0x02692C55, 0x7F92772F, 0x025F8025, 0x34347113, 0x560EA689, 0x0DCC21DF, 0x09ECC7F5, 
00064 0x091F3993, 0x0E0B52AB, 0x497CAA55, 0x0A040A49, 0x6D8F0CC5, 0x54F41609, 0x6E0CB8DF, 0x3DCB64C3, 
00065 0x16C365CD, 0x6D6B9FB5, 0x02B9382B, 0x6A5BFAF1, 0x1669D75F, 0x13CFD4FD, 0x0FDF316F, 0x21F3C463, 
00066 0x6FC58ABF, 0x04E45BE7, 0x1911225B, 0x28CD1355, 0x222084E9, 0x672AD54B, 0x476FC267, 0x6864E16D, 
00067 0x20AEF4FB, 0x603C5FB9, 0x55090595, 0x1113B705, 0x24E38493, 0x5291AF97, 0x5F5446D9, 0x13A6F639, 
00068 0x3D501313, 0x37E02017, 0x236B0ED3, 0x60F246BF, 0x01E02501, 0x2D2F66BD, 0x6BF23609, 0x16729BAF
00069 };
00070 
00071 // binary search over intervals
00072 static int
00073 i_find(const Intervals *i, int x) {
00074   ink_assert(i->n);
00075   int l = 0, h = i->n;
00076  Lrecurse:
00077   if (h <= l + 2) {
00078     if (h <= l)
00079       return -(l + 1);
00080     if (x < i->v[l] || x > i->v[l + 1])
00081       return -(l + 1);
00082     return h;
00083   }
00084   int m = (((h - l) / 4) * 2) + l;
00085   if (x > i->v[m + 1]) {
00086     l = m;
00087     goto Lrecurse;
00088   }
00089   if (x < i->v[m]) {    
00090     h = m;
00091     goto Lrecurse;
00092   }
00093   return (l + 1);
00094 }
00095 
00096 bool
00097 Intervals::in(int x) const {
00098   if (!n)
00099     return false;
00100   if (i_find(this, x) > 0)
00101     return true;
00102   return false;
00103 }
00104 
00105 // insert into interval with merge
00106 void 
00107 Intervals::insert(int x) {
00108   if (!n) {
00109     add(x);
00110     add(x);
00111     return;
00112   }
00113   int l = i_find(this, x);
00114   if (l > 0)
00115     return;
00116   l = -l - 1;
00117 
00118   if (x > v[l + 1]) {
00119     if (x == v[l + 1] + 1) {
00120       v[l + 1]++;
00121       goto Lmerge;
00122     }
00123     l += 2;
00124     if (l < (int)n) {
00125       if (x == v[l] - 1) {
00126         v[l]--;
00127         goto Lmerge;
00128       }
00129     }
00130     goto Lmore;
00131   } else {
00132     ink_assert(x < v[l]);
00133     if (x == v[l] - 1) {
00134       v[l]--;
00135       goto Lmerge;
00136     }
00137     if (!l)
00138       goto Lmore;
00139     l -= 2;
00140     if (x == v[l + 1] + 1) {
00141       v[l + 1]++;
00142       goto Lmerge;
00143     }
00144   }
00145  Lmore:
00146   fill(n + 2);
00147   if (n - 2 - l > 0)
00148     memmove(v + l + 2, v + l, sizeof(int) * (n - 2 - l));
00149   v[l] = x;
00150   v[l+1] = x;
00151   return;
00152  Lmerge:
00153   if (l) {
00154     if (v[l] - v[l-1] < 2) {
00155       l -= 2;
00156       goto Ldomerge;
00157     }
00158   }
00159   if (l < (int)(n - 2)) {
00160     if (v[l + 2] - v[l + 1] < 2)
00161       goto Ldomerge;
00162   }
00163   return;
00164  Ldomerge:
00165   memmove(v + l + 1, v + l + 3, sizeof(int) * (n - 3 - l));
00166   n -= 2;
00167   goto Lmerge;
00168 }
00169 
00170 void
00171 UnionFind::size(int s) {
00172   size_t nn = n;
00173   fill(s);
00174   for (size_t i = nn; i < n; i++)
00175     v[i] = -1;
00176 }
00177 
00178 int
00179 UnionFind::find(int n) {
00180   int i, t;
00181   for (i = n; v[i] >= 0; i = v[i]);
00182   while (v[n] >= 0) {
00183     t = n;
00184     n = v[n];
00185     v[t] = i;
00186   }
00187   return i;
00188 }
00189 
00190 void 
00191 UnionFind::unify(int n, int m) {
00192   n = find(n);
00193   m = find(m);
00194   if (n != m) {
00195     if (v[m] < v[n]) {
00196       v[m] += (v[n] - 1);
00197       v[n] = m;
00198     } else {
00199       v[n] += (v[m] - 1);
00200       v[m] = n;
00201     }
00202   }
00203 }

Generated by  doxygen 1.7.1