2 * Copyright (C) 2006, 2008, 2012 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #ifndef PropertyNameArray_h
22 #define PropertyNameArray_h
24 #include "CallFrame.h"
25 #include "Identifier.h"
26 #include <wtf/HashSet.h>
27 #include <wtf/Vector.h>
31 // FIXME: Rename to PropertyNameArray.
32 class PropertyNameArrayData : public RefCounted<PropertyNameArrayData> {
34 typedef Vector<Identifier, 20> PropertyNameVector;
36 static PassRefPtr<PropertyNameArrayData> create() { return adoptRef(new PropertyNameArrayData); }
38 PropertyNameVector& propertyNameVector() { return m_propertyNameVector; }
41 PropertyNameArrayData()
45 PropertyNameVector m_propertyNameVector;
48 // FIXME: Rename to PropertyNameArrayBuilder.
49 class PropertyNameArray {
51 PropertyNameArray(VM* vm)
52 : m_data(PropertyNameArrayData::create())
57 PropertyNameArray(ExecState* exec)
58 : m_data(PropertyNameArrayData::create())
63 VM* vm() { return m_vm; }
65 void add(uint32_t index)
67 add(Identifier::from(m_vm, index));
70 void add(const Identifier&);
71 void add(AtomicStringImpl*);
72 void addKnownUnique(AtomicStringImpl*);
74 Identifier& operator[](unsigned i) { return m_data->propertyNameVector()[i]; }
75 const Identifier& operator[](unsigned i) const { return m_data->propertyNameVector()[i]; }
77 void setData(PassRefPtr<PropertyNameArrayData> data) { m_data = data; }
78 PropertyNameArrayData* data() { return m_data.get(); }
79 PassRefPtr<PropertyNameArrayData> releaseData() { return m_data.release(); }
81 // FIXME: Remove these functions.
82 bool canAddKnownUniqueForStructure() const { return m_data->propertyNameVector().isEmpty(); }
83 typedef PropertyNameArrayData::PropertyNameVector::const_iterator const_iterator;
84 size_t size() const { return m_data->propertyNameVector().size(); }
85 const_iterator begin() const { return m_data->propertyNameVector().begin(); }
86 const_iterator end() const { return m_data->propertyNameVector().end(); }
89 RefPtr<PropertyNameArrayData> m_data;
90 HashSet<AtomicStringImpl*> m_set;
94 ALWAYS_INLINE void PropertyNameArray::add(const Identifier& identifier)
96 add(identifier.impl());
99 ALWAYS_INLINE void PropertyNameArray::addKnownUnique(AtomicStringImpl* identifier)
101 m_data->propertyNameVector().append(Identifier::fromUid(m_vm, identifier));
104 ALWAYS_INLINE void PropertyNameArray::add(AtomicStringImpl* identifier)
106 static const unsigned setThreshold = 20;
110 if (size() < setThreshold) {
111 if (m_data->propertyNameVector().contains(identifier))
114 if (m_set.isEmpty()) {
115 for (Identifier& name : m_data->propertyNameVector())
116 m_set.add(name.impl());
118 if (!m_set.add(identifier).isNewEntry)
122 addKnownUnique(identifier);
127 #endif // PropertyNameArray_h