/usr/include/js/Containers.h is in libmozjs185-dev 1.8.5-1.0.0-0ubuntu8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
/* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is [Open Source Virtual Machine].
*
* The Initial Developer of the Original Code is
* Adobe System Incorporated.
* Portions created by the Initial Developer are Copyright (C) 2004-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Adobe AS3 Team
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef __nanojit_Containers__
#define __nanojit_Containers__
namespace nanojit
{
/** simple linear bit array, memory taken from Allocator
* warning: when bit array grows, old memory is wasted since it
* was allocated from Allocator. pre-size the bitmap when possible
* by passing nbits to the constructor. */
class BitSet {
Allocator &allocator;
int cap;
int64_t *bits;
static const int64_t ONE = 1;
static const int SHIFT = 6;
inline int bitnum2word(int i) {
return i >> 6;
}
inline int64_t bitnum2mask(int i) {
return ONE << (i & 63);
}
/** keep doubling array to fit at least w words */
void grow(int w);
public:
BitSet(Allocator& allocator, int nbits=128);
/** clear all bits */
void reset();
/** perform a bitwise or with BitSet other, return true if
* this bitset was modified */
bool setFrom(BitSet& other);
/** return bit i as a bool */
bool get(int i) {
NanoAssert(i >= 0);
int w = bitnum2word(i);
if (w < cap)
return (bits[w] & bitnum2mask(i)) != 0;
return false;
}
/** set bit i */
void set(int i) {
NanoAssert(i >= 0);
int w = bitnum2word(i);
if (w >= cap)
grow(w);
bits[w] |= bitnum2mask(i);
}
/** clear bit i */
void clear(int i) {
NanoAssert(i >= 0);
int w = bitnum2word(i);
if (w < cap)
bits[w] &= ~bitnum2mask(i);
}
};
/** Seq is a single node in a linked list */
template<class T> class Seq {
public:
Seq(T head, Seq<T>* tail=NULL) : head(head), tail(tail) {}
T head;
Seq<T>* tail;
};
/** SeqBuilder is used to create a linked list of Seq<T> by inserting
* nodes either at the beginning, with insert(), or at the end, with
* add(). Once built, the actual list can be retained while this
* SeqBuilder can be discarded. */
template<class T> class SeqBuilder {
public:
SeqBuilder(Allocator& allocator)
: allocator(allocator)
, items(NULL)
, last(NULL)
{ }
/** add item to beginning of list */
void insert(T item) {
Seq<T>* e = new (allocator) Seq<T>(item, items);
if (last == NULL)
last = e;
items = e;
}
/** add item to end of list */
void add(T item) {
Seq<T>* e = new (allocator) Seq<T>(item);
if (last == NULL)
items = e;
else
last->tail = e;
last = e;
}
/** return first item in sequence */
Seq<T>* get() const {
return items;
}
/** self explanitory */
bool isEmpty() const {
return items == NULL;
}
/** de-reference all items */
void clear() {
items = last = NULL;
}
private:
Allocator& allocator;
Seq<T>* items;
Seq<T>* last;
};
#ifdef NANOJIT_64BIT
static inline size_t murmurhash(const void *key, size_t len) {
const uint64_t m = 0xc6a4a7935bd1e995;
const int r = 47;
uint64_t h = 0;
const uint64_t *data = (const uint64_t*)key;
const uint64_t *end = data + (len/8);
while(data != end)
{
uint64_t k = *data++;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
}
const unsigned char *data2 = (const unsigned char*)data;
switch(len & 7) {
case 7: h ^= uint64_t(data2[6]) << 48;
case 6: h ^= uint64_t(data2[5]) << 40;
case 5: h ^= uint64_t(data2[4]) << 32;
case 4: h ^= uint64_t(data2[3]) << 24;
case 3: h ^= uint64_t(data2[2]) << 16;
case 2: h ^= uint64_t(data2[1]) << 8;
case 1: h ^= uint64_t(data2[0]);
h *= m;
};
h ^= h >> r;
h *= m;
h ^= h >> r;
return (size_t)h;
}
#else
static inline size_t murmurhash(const void * key, size_t len) {
const uint32_t m = 0x5bd1e995;
const int r = 24;
uint32_t h = 0;
const unsigned char * data = (const unsigned char *)key;
while(len >= 4) {
uint32_t k = *(size_t *)(void*)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
switch(len) {
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return (size_t)h;
}
#endif
template<class K> struct DefaultHash {
static size_t hash(const K &k) {
// (const void*) cast is required by ARM RVCT 2.2
return murmurhash((const void*) &k, sizeof(K));
}
};
template<class K> struct DefaultHash<K*> {
static size_t hash(K* k) {
uintptr_t h = (uintptr_t) k;
// move the low 3 bits higher up since they're often 0
h = (h>>3) ^ (h<<((sizeof(uintptr_t) * 8) - 3));
return (size_t) h;
}
};
/** Bucket hashtable with a fixed # of buckets (never rehash)
* Intended for use when a reasonable # of buckets can be estimated ahead of time.
* Note that operator== is used to compare keys.
*/
template<class K, class T, class H=DefaultHash<K> > class HashMap {
Allocator& allocator;
size_t nbuckets;
class Node {
public:
K key;
T value;
Node(K k, T v) : key(k), value(v) { }
};
Seq<Node>** buckets;
/** return the node containing K, and the bucket index, or NULL if not found */
Node* find(K k, size_t &i) {
i = H::hash(k) % nbuckets;
for (Seq<Node>* p = buckets[i]; p != NULL; p = p->tail) {
if (p->head.key == k)
return &p->head;
}
return NULL;
}
public:
HashMap(Allocator& a, size_t nbuckets = 16)
: allocator(a)
, nbuckets(nbuckets)
, buckets(new (a) Seq<Node>*[nbuckets])
{
NanoAssert(nbuckets > 0);
clear();
}
/** clear all buckets. Since we allocate all memory from Allocator,
* nothing needs to be freed. */
void clear() {
VMPI_memset(buckets, 0, sizeof(Seq<Node>*) * nbuckets);
}
/** add (k,v) to the map. If k is already in the map, replace the value */
void put(const K& k, const T& v) {
size_t i;
Node* n = find(k, i);
if (n) {
n->value = v;
return;
}
buckets[i] = new (allocator) Seq<Node>(Node(k,v), buckets[i]);
}
/** return v for element k, or T(0) if k is not present */
T get(const K& k) {
size_t i;
Node* n = find(k, i);
return n ? n->value : 0;
}
/** returns true if k is in the map. */
bool containsKey(const K& k) {
size_t i;
return find(k, i) != 0;
}
/** remove k from the map, if it is present. if not, remove()
* silently returns */
void remove(const K& k) {
size_t i = H::hash(k) % nbuckets;
Seq<Node>** prev = &buckets[i];
for (Seq<Node>* p = buckets[i]; p != NULL; p = p->tail) {
if (p->head.key == k) {
(*prev) = p->tail;
return;
}
prev = &p->tail;
}
}
/** Iter is an iterator for HashMap, intended to be instantiated on
* the stack. Iteration order is undefined. Mutating the hashmap
* while iteration is in progress gives undefined results. All iteration
* state is in class Iter, so multiple iterations can be in progress
* at the same time. for example:
*
* HashMap<K,T>::Iter iter(map);
* while (iter.next()) {
* K *k = iter.key();
* T *t = iter.value();
* }
*/
class Iter {
friend class HashMap;
const HashMap<K,T,H> ↦
int bucket;
const Seq<Node>* current;
public:
Iter(HashMap<K,T,H>& map) : map(map), bucket((int)map.nbuckets-1), current(NULL)
{ }
/** return true if more (k,v) remain to be visited */
bool next() {
if (current)
current = current->tail;
while (bucket >= 0 && !current)
current = map.buckets[bucket--];
return current != NULL;
}
/** return the current key */
const K& key() const {
NanoAssert(current != NULL);
return current->head.key;
}
/** return the current value */
const T& value() const {
NanoAssert(current != NULL);
return current->head.value;
}
};
/** return true if the hashmap has no elements */
bool isEmpty() {
Iter iter(*this);
return !iter.next();
}
};
/**
* Simple binary tree. No balancing is performed under the assumption
* that the only users of this structure are not performance critical.
*/
template<class K, class T> class TreeMap {
Allocator& alloc;
class Node {
public:
Node* left;
Node* right;
K key;
T value;
Node(K k, T v) : left(NULL), right(NULL), key(k), value(v)
{ }
};
Node* root;
/**
* helper method to recursively insert (k,v) below Node n or a child
* of n so that the binary search tree remains well formed.
*/
void insert(Node* &n, K k, T v) {
if (!n)
n = new (alloc) Node(k, v);
else if (k == n->key)
n->value = v;
else if (k < n->key)
insert(n->left, k, v);
else
insert(n->right, k, v);
}
/**
* search for key k below Node n and return n if found, or the
* closest parent n where k should be inserted.
*/
Node* find(Node* n, K k) {
if (!n)
return NULL;
if (k == n->key)
return n;
if (k < n->key)
return find(n->left, k);
if (n->right)
return find(n->right, k);
return n;
}
public:
TreeMap(Allocator& alloc) : alloc(alloc), root(NULL)
{ }
/** set k = v in the map. if k already exists, replace its value */
void put(K k, T v) {
insert(root, k, v);
}
/** return the closest key that is <= k, or NULL if k
is smaller than every key in the Map. */
K findNear(K k) {
Node* n = find(root, k);
return n ? n->key : 0;
}
/** returns the value for k or NULL */
T get(K k) {
Node* n = find(root, k);
return (n && n->key == k) ? n->value : 0;
}
/** returns true iff k is in the Map. */
bool containsKey(K k) {
Node* n = find(root, k);
return n && n->key == k;
}
/** make the tree empty. trivial since we dont manage elements */
void clear() {
root = NULL;
}
};
}
#endif // __nanojit_Containers__
|