DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=4 sw=4 et tw=99 ft=cpp:
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef InlineMap_h__
#define InlineMap_h__

#include "js/HashTable.h"

namespace js {

/*
 * A type can only be used as an InlineMap key if zero is an invalid key value
 * (and thus may be used as a tombstone value by InlineMap).
 */
template <typename T> struct ZeroIsReserved         { static const bool result = false; };
template <typename T> struct ZeroIsReserved<T *>    { static const bool result = true; };

template <typename K, typename V, size_t InlineElems>
class InlineMap
{
  public:
    typedef HashMap<K, V, DefaultHasher<K>, TempAllocPolicy> WordMap;

    struct InlineElem
    {
        K key;
        V value;
    };

  private:
    typedef typename WordMap::Ptr       WordMapPtr;
    typedef typename WordMap::AddPtr    WordMapAddPtr;
    typedef typename WordMap::Range     WordMapRange;

    size_t          inlNext;
    size_t          inlCount;
    InlineElem      inl[InlineElems];
    WordMap         map;

    void checkStaticInvariants() {
        JS_STATIC_ASSERT(ZeroIsReserved<K>::result);
    }

    bool usingMap() const {
        return inlNext > InlineElems;
    }

    bool switchToMap() {
        JS_ASSERT(inlNext == InlineElems);

        if (map.initialized()) {
            map.clear();
        } else {
            if (!map.init(count()))
                return false;
            JS_ASSERT(map.initialized());
        }

        for (InlineElem *it = inl, *end = inl + inlNext; it != end; ++it) {
            if (it->key && !map.putNew(it->key, it->value))
                return false;
        }

        inlNext = InlineElems + 1;
        JS_ASSERT(map.count() == inlCount);
        JS_ASSERT(usingMap());
        return true;
    }

    JS_NEVER_INLINE
    bool switchAndAdd(const K &key, const V &value) {
        if (!switchToMap())
            return false;

        return map.putNew(key, value);
    }

  public:
    explicit InlineMap(JSContext *cx)
      : inlNext(0), inlCount(0), map(cx) {
        checkStaticInvariants(); /* Force the template to instantiate the static invariants. */
    }

    class Entry
    {
        friend class InlineMap;
        const K &key_;
        V &value_;

        Entry(const K &key, V &value) : key_(key), value_(value) {}

      public:
        const K &key() { return key_; }
        V &value() { return value_; }
    }; /* class Entry */

    class Ptr
    {
        friend class InlineMap;

        WordMapPtr  mapPtr;
        InlineElem  *inlPtr;
        bool        isInlinePtr;

        typedef Ptr ******* ConvertibleToBool;

        explicit Ptr(WordMapPtr p) : mapPtr(p), isInlinePtr(false) {}
        explicit Ptr(InlineElem *ie) : inlPtr(ie), isInlinePtr(true) {}
        void operator==(const Ptr &other);

      public:
        /* Leaves Ptr uninitialized. */
        Ptr() {
#ifdef DEBUG
            inlPtr = (InlineElem *) 0xbad;
            isInlinePtr = true;
#endif
        }

        /* Default copy constructor works for this structure. */

        bool found() const {
            return isInlinePtr ? bool(inlPtr) : mapPtr.found();
        }

        operator ConvertibleToBool() const {
            return ConvertibleToBool(found());
        }

        K &key() {
            JS_ASSERT(found());
            return isInlinePtr ? inlPtr->key : mapPtr->key;
        }

        V &value() {
            JS_ASSERT(found());
            return isInlinePtr ? inlPtr->value : mapPtr->value;
        }
    }; /* class Ptr */

    class AddPtr
    {
        friend class InlineMap;

        WordMapAddPtr   mapAddPtr;
        InlineElem      *inlAddPtr;
        bool            isInlinePtr;
        /* Indicates whether inlAddPtr is a found result or an add pointer. */
        bool            inlPtrFound;

        AddPtr(InlineElem *ptr, bool found)
          : inlAddPtr(ptr), isInlinePtr(true), inlPtrFound(found)
        {}

        AddPtr(const WordMapAddPtr &p) : mapAddPtr(p), isInlinePtr(false) {}

        void operator==(const AddPtr &other);

        typedef AddPtr ******* ConvertibleToBool;

      public:
        AddPtr() {}

        bool found() const {
            return isInlinePtr ? inlPtrFound : mapAddPtr.found();
        }

        operator ConvertibleToBool() const {
            return found() ? ConvertibleToBool(1) : ConvertibleToBool(0);
        }

        V &value() {
            JS_ASSERT(found());
            if (isInlinePtr)
                return inlAddPtr->value;
            return mapAddPtr->value;
        }
    }; /* class AddPtr */

    size_t count() {
        return usingMap() ? map.count() : inlCount;
    }

    bool empty() const {
        return usingMap() ? map.empty() : !inlCount;
    }

    void clear() {
        inlNext = 0;
        inlCount = 0;
    }

    bool isMap() const {
        return usingMap();
    }

    const WordMap &asMap() const {
        JS_ASSERT(isMap());
        return map;
    }

    const InlineElem *asInline() const {
        JS_ASSERT(!isMap());
        return inl;
    }

    const InlineElem *inlineEnd() const {
        JS_ASSERT(!isMap());
        return inl + inlNext;
    }

    JS_ALWAYS_INLINE
    Ptr lookup(const K &key) {
        if (usingMap())
            return Ptr(map.lookup(key));

        for (InlineElem *it = inl, *end = inl + inlNext; it != end; ++it) {
            if (it->key == key)
                return Ptr(it);
        }

        return Ptr(NULL);
    }

    JS_ALWAYS_INLINE
    AddPtr lookupForAdd(const K &key) {
        if (usingMap())
            return AddPtr(map.lookupForAdd(key));

        for (InlineElem *it = inl, *end = inl + inlNext; it != end; ++it) {
            if (it->key == key)
                return AddPtr(it, true);
        }

        /*
         * The add pointer that's returned here may indicate the limit entry of
         * the linear space, in which case the |add| operation will initialize
         * the map if necessary and add the entry there.
         */
        return AddPtr(inl + inlNext, false);
    }

    JS_ALWAYS_INLINE
    bool add(AddPtr &p, const K &key, const V &value) {
        JS_ASSERT(!p);

        if (p.isInlinePtr) {
            InlineElem *addPtr = p.inlAddPtr;
            JS_ASSERT(addPtr == inl + inlNext);

            /* Switching to map mode before we add this pointer. */
            if (addPtr == inl + InlineElems)
                return switchAndAdd(key, value);

            JS_ASSERT(!p.found());
            JS_ASSERT(uintptr_t(inl + inlNext) == uintptr_t(p.inlAddPtr));
            p.inlAddPtr->key = key;
            p.inlAddPtr->value = value;
            ++inlCount;
            ++inlNext;
            return true;
        }

        return map.add(p.mapAddPtr, key, value);
    }

    JS_ALWAYS_INLINE
    bool put(const K &key, const V &value) {
        AddPtr p = lookupForAdd(key);
        if (p) {
            p.value() = value;
            return true;
        }
        return add(p, key, value);
    }

    void remove(Ptr p) {
        JS_ASSERT(p);
        if (p.isInlinePtr) {
            JS_ASSERT(inlCount > 0);
            JS_ASSERT(p.inlPtr->key != NULL);
            p.inlPtr->key = NULL;
            --inlCount;
            return;
        }
        JS_ASSERT(map.initialized() && usingMap());
        map.remove(p.mapPtr);
    }

    void remove(const K &key) {
        if (Ptr p = lookup(key))
            remove(p);
    }

    class Range
    {
        friend class InlineMap;

        WordMapRange    mapRange;
        InlineElem      *cur;
        InlineElem      *end;
        bool            isInline;

        explicit Range(WordMapRange r)
          : cur(NULL), end(NULL), /* Avoid GCC 4.3.3 over-warning. */
            isInline(false) {
            mapRange = r;
            JS_ASSERT(!isInlineRange());
        }

        Range(const InlineElem *begin, const InlineElem *end_)
          : cur(const_cast<InlineElem *>(begin)),
            end(const_cast<InlineElem *>(end_)),
            isInline(true) {
            advancePastNulls(cur);
            JS_ASSERT(isInlineRange());
        }

        bool checkInlineRangeInvariants() const {
            JS_ASSERT(uintptr_t(cur) <= uintptr_t(end));
            JS_ASSERT_IF(cur != end, cur->key != NULL);
            return true;
        }

        bool isInlineRange() const {
            JS_ASSERT_IF(isInline, checkInlineRangeInvariants());
            return isInline;
        }

        void advancePastNulls(InlineElem *begin) {
            InlineElem *newCur = begin;
            while (newCur < end && NULL == newCur->key)
                ++newCur;
            JS_ASSERT(uintptr_t(newCur) <= uintptr_t(end));
            cur = newCur;
        }

        void bumpCurPtr() {
            JS_ASSERT(isInlineRange());
            advancePastNulls(cur + 1);
        }

        void operator==(const Range &other);

      public:
        bool empty() const {
            return isInlineRange() ? cur == end : mapRange.empty();
        }

        Entry front() {
            JS_ASSERT(!empty());
            if (isInlineRange())
                return Entry(cur->key, cur->value);
            return Entry(mapRange.front().key, mapRange.front().value);
        }

        void popFront() {
            JS_ASSERT(!empty());
            if (isInlineRange())
                bumpCurPtr();
            else
                mapRange.popFront();
        }
    }; /* class Range */

    Range all() const {
        return usingMap() ? Range(map.all()) : Range(inl, inl + inlNext);
    }
}; /* class InlineMap */

} /* namespace js */

#endif