| 1 | /* |
| 2 | * Copyright (C) 2015-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <wtf/IndexKeyType.h> |
| 29 | #include <wtf/Vector.h> |
| 30 | |
| 31 | namespace WTF { |
| 32 | |
| 33 | // This is a map for keys that have an index(). It's super efficient for BasicBlocks. It's only |
| 34 | // efficient for Values if you don't create too many of these maps, since Values can have very |
| 35 | // sparse indices and there are a lot of Values. |
| 36 | |
| 37 | template<typename Key, typename Value> |
| 38 | class IndexMap { |
| 39 | public: |
| 40 | IndexMap() = default; |
| 41 | IndexMap(IndexMap&&) = default; |
| 42 | IndexMap& operator=(IndexMap&&) = default; |
| 43 | IndexMap(const IndexMap&) = default; |
| 44 | IndexMap& operator=(const IndexMap&) = default; |
| 45 | |
| 46 | template<typename... Args> |
| 47 | explicit IndexMap(size_t size, Args&&... args) |
| 48 | { |
| 49 | m_vector.fill(Value(std::forward<Args>(args)...), size); |
| 50 | } |
| 51 | |
| 52 | template<typename... Args> |
| 53 | void resize(size_t size, Args&&... args) |
| 54 | { |
| 55 | m_vector.fill(Value(std::forward<Args>(args)...), size); |
| 56 | } |
| 57 | |
| 58 | template<typename... Args> |
| 59 | void clear(Args&&... args) |
| 60 | { |
| 61 | m_vector.fill(Value(std::forward<Args>(args)...), m_vector.size()); |
| 62 | } |
| 63 | |
| 64 | size_t size() const { return m_vector.size(); } |
| 65 | |
| 66 | Value& at(const Key& key) |
| 67 | { |
| 68 | return m_vector[IndexKeyType<Key>::index(key)]; |
| 69 | } |
| 70 | |
| 71 | const Value& at(const Key& key) const |
| 72 | { |
| 73 | return m_vector[IndexKeyType<Key>::index(key)]; |
| 74 | } |
| 75 | |
| 76 | Value& at(size_t index) |
| 77 | { |
| 78 | return m_vector[index]; |
| 79 | } |
| 80 | |
| 81 | const Value& at(size_t index) const |
| 82 | { |
| 83 | return m_vector[index]; |
| 84 | } |
| 85 | |
| 86 | Value& operator[](size_t index) { return at(index); } |
| 87 | const Value& operator[](size_t index) const { return at(index); } |
| 88 | Value& operator[](const Key& key) { return at(key); } |
| 89 | const Value& operator[](const Key& key) const { return at(key); } |
| 90 | |
| 91 | template<typename PassedValue> |
| 92 | void append(const Key& key, PassedValue&& value) |
| 93 | { |
| 94 | RELEASE_ASSERT(IndexKeyType<Key>::index(key) == m_vector.size()); |
| 95 | m_vector.append(std::forward<PassedValue>(value)); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | Vector<Value, 0, UnsafeVectorOverflow> m_vector; |
| 100 | }; |
| 101 | |
| 102 | } // namespace WTF |
| 103 | |
| 104 | using WTF::IndexMap; |
| 105 | |