/* AngelCode Scripting Library Copyright (c) 2012-2021 Andreas Jonsson This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. The original version of this library can be located at: http://www.angelcode.com/angelscript/ Andreas Jonsson andreas@angelcode.com */ // // as_symboltable.h // // Created on: Jun 19, 2012 // Author: Markus Lenger, a.k.a. mlengerx // // This class is used for fast symbol lookups while parsing or loading bytecode // #ifndef AS_SYMBOLTABLE_H #define AS_SYMBOLTABLE_H #include "as_config.h" #include "as_memory.h" #include "as_string.h" #include "as_map.h" #include "as_datatype.h" #include "as_namespace.h" BEGIN_AS_NAMESPACE // Interface to avoid nested templates which is not well supported by older compilers, e.g. MSVC6 struct asIFilter { virtual bool operator()(const void*) const = 0; virtual ~asIFilter() {}; }; // forward declaration template class asCSymbolTable; // Iterator that allows iterating in index order template class asCSymbolTableIterator { public: T2* operator*() const; T2* operator->() const; asCSymbolTableIterator& operator++(int); asCSymbolTableIterator& operator--(int); operator bool() const; int GetIndex() const { return m_idx; } private: friend class asCSymbolTable; asCSymbolTableIterator(asCSymbolTable *table); void Next(); void Previous(); asCSymbolTable* m_table; unsigned int m_idx; }; // Symbol table mapping namespace + name to symbols // The structure keeps the entries indexed in an array so the indices will not change // There is also a map for a quick lookup. The map supports multiple entries with the same name template class asCSymbolTable { public: typedef asCSymbolTableIterator iterator; typedef asCSymbolTableIterator const_iterator; asCSymbolTable(asUINT initialCapacity = 0); int GetFirstIndex(const asSNameSpace *ns, const asCString &name, const asIFilter &comparator) const; int GetFirstIndex(const asSNameSpace *ns, const asCString &name) const; int GetLastIndex() const; int GetIndex(const T*) const; T* GetFirst(const asSNameSpace *ns, const asCString &name, const asIFilter &comparator) const; T* GetFirst(const asSNameSpace *ns, const asCString &name); const T* GetFirst(const asSNameSpace *ns, const asCString &name) const; T* Get(asUINT index); const T* Get(asUINT index) const; T* GetLast(); const T* GetLast() const; const asCArray &GetIndexes(const asSNameSpace *ns, const asCString &name) const; asUINT Put(T* entry); asUINT GetSize() const; void SwapWith(asCSymbolTable &other); void Clear(); bool Erase(asUINT idx); void Allocate(asUINT elem_cnt, bool keep_data); iterator List(); const_iterator List() const; private: // Don't allow assignment asCSymbolTable& operator=(const asCSymbolTable &other) { return *this; } friend class asCSymbolTableIterator; friend class asCSymbolTableIterator; void GetKey(const T *entry, asSNameSpaceNamePair &key) const; bool CheckIdx(asUINT idx) const; asCMap > m_map; asCArray m_entries; unsigned int m_size; }; template void asCSymbolTable::SwapWith(asCSymbolTable &other) { m_map.SwapWith(other.m_map); m_entries.SwapWith(other.m_entries); asUINT tmp = m_size; m_size = other.m_size; other.m_size = tmp; } // Constructor // initialCapacity gives the number of entries to allocate in advance template asCSymbolTable::asCSymbolTable(asUINT initialCapacity) : m_entries(initialCapacity) { m_size = 0; } template int asCSymbolTable::GetFirstIndex( const asSNameSpace *ns, const asCString &name, const asIFilter &filter) const { asSNameSpaceNamePair key(ns, name); asSMapNode > *cursor; if( m_map.MoveTo(&cursor, key) ) { const asCArray &arr = m_map.GetValue(cursor); for( asUINT n = 0; n < arr.GetLength(); n++ ) { T *entry = m_entries[arr[n]]; if( entry && filter(entry) ) return arr[n]; } } return -1; } template const asCArray &asCSymbolTable::GetIndexes(const asSNameSpace *ns, const asCString &name) const { asSNameSpaceNamePair key(ns, name); asSMapNode > *cursor; if( m_map.MoveTo(&cursor, key) ) return m_map.GetValue(cursor); static asCArray dummy; return dummy; } template T* asCSymbolTable::GetFirst(const asSNameSpace *ns, const asCString &name, const asIFilter &comp) const { int idx = GetFirstIndex(ns, name, comp); if (idx != -1) return m_entries[idx]; return 0; } template int asCSymbolTable::GetFirstIndex(const asSNameSpace *ns, const asCString &name) const { asSNameSpaceNamePair key(ns, name); asSMapNode > *cursor; if( m_map.MoveTo(&cursor, key) ) return m_map.GetValue(cursor)[0]; return -1; } // Find the index of a certain symbol // ATTENTION: this function has linear runtime complexity O(n)!! template int asCSymbolTable::GetIndex(const T* entry) const { for( asUINT n = 0; n < m_entries.GetLength(); n++ ) if( m_entries[n] == entry ) return n; return -1; } template T* asCSymbolTable::Get(asUINT idx) { if( !CheckIdx(idx) ) return 0; return m_entries[idx]; } template const T* asCSymbolTable::Get(asUINT idx) const { return const_cast< asCSymbolTable* >(this)->Get(idx); } template T* asCSymbolTable::GetFirst(const asSNameSpace *ns, const asCString &name) { int idx = GetFirstIndex(ns, name); return Get(idx); } template const T* asCSymbolTable::GetFirst(const asSNameSpace *ns, const asCString &name) const { return const_cast< asCSymbolTable* >(this)->GetFirst(ns, name); } template T* asCSymbolTable::GetLast() { return Get(GetLastIndex()); } template const T* asCSymbolTable::GetLast() const { return const_cast< asCSymbolTable* >(this)->GetLast(); } // Clear the symbol table // ATTENTION: The contained symbols are not rleased. This is up to the client template void asCSymbolTable::Clear() { m_entries.SetLength(0); m_map.EraseAll(); m_size = 0; } // Pre-allocate slots for elemCnt entries template void asCSymbolTable::Allocate(asUINT elemCnt, bool keepData) { asASSERT( elemCnt >= m_entries.GetLength() ); m_entries.Allocate(elemCnt, keepData); if( !keepData ) m_map.EraseAll(); } template bool asCSymbolTable::Erase(asUINT idx) { if( !CheckIdx(idx) ) { asASSERT(false); return false; } T *entry = m_entries[idx]; asASSERT(entry); if( !entry ) return false; // Remove the symbol from the lookup map asSNameSpaceNamePair key; GetKey(entry, key); asSMapNode > *cursor; if( m_map.MoveTo(&cursor, key) ) { asCArray &arr = m_map.GetValue(cursor); arr.RemoveValue(idx); if( arr.GetLength() == 0 ) m_map.Erase(cursor); } else asASSERT(false); // Remove the symbol from the indexed array if( idx == m_entries.GetLength() - 1 ) m_entries.PopLast(); else { // Must keep the array packed int prevIdx = int(m_entries.GetLength()-1); m_entries[idx] = m_entries.PopLast(); // Update the index in the lookup map entry = m_entries[idx]; GetKey(entry, key); if( m_map.MoveTo(&cursor, key) ) { asCArray &arr = m_map.GetValue(cursor); arr[arr.IndexOf(prevIdx)] = idx; } else asASSERT(false); } m_size--; return true; } template asUINT asCSymbolTable::Put(T *entry) { asUINT idx = m_entries.GetLength(); asSNameSpaceNamePair key; GetKey(entry, key); asSMapNode > *cursor; if( m_map.MoveTo(&cursor, key) ) m_map.GetValue(cursor).PushLast(idx); else { asCArray arr(1); arr.PushLast(idx); m_map.Insert(key, arr); } m_entries.PushLast(entry); m_size++; return idx; } // Return key for specified symbol (namespace and name are used to generate the key) template void asCSymbolTable::GetKey(const T *entry, asSNameSpaceNamePair &key) const { key = asSNameSpaceNamePair(entry->nameSpace, entry->name); } template asUINT asCSymbolTable::GetSize() const { return m_size; } template bool asCSymbolTable::CheckIdx(asUINT idx) const { return idx < m_entries.GetLength(); } template int asCSymbolTable::GetLastIndex() const { int idx = int(m_entries.GetLength()) - 1; asASSERT( idx == -1 || m_entries[idx] ); return idx; } template asCSymbolTableIterator asCSymbolTable::List() { return asCSymbolTableIterator(this); } template typename asCSymbolTable::const_iterator asCSymbolTable::List() const { return asCSymbolTableIterator(const_cast< asCSymbolTable *>(this)); } ///////////////////////////////////////////////////////////////////////////////////////////////// // Iterator template asCSymbolTableIterator::asCSymbolTableIterator(asCSymbolTable *table) : m_table(table), m_idx(0) { asUINT sz = m_table->m_entries.GetLength(); while( m_idx < sz && m_table->m_entries[m_idx] == 0 ) m_idx++; } template T2* asCSymbolTableIterator::operator*() const { asASSERT(m_table->CheckIdx(m_idx)); return m_table->m_entries[m_idx]; } template T2* asCSymbolTableIterator::operator->() const { asASSERT(m_table->CheckIdx(m_idx)); return m_table->m_entries[m_idx]; } template asCSymbolTableIterator& asCSymbolTableIterator::operator++(int) { Next(); return *this; } // Return true if more elements are following // ATTENTION: When deleting the object currently pointed to by this iterator this // method returns false even though there might be more elements in the list template asCSymbolTableIterator::operator bool() const { return m_idx < m_table->m_entries.GetLength() && m_table->m_entries[m_idx] != 0; } template void asCSymbolTableIterator::Next() { asUINT sz = m_table->m_entries.GetLength(); m_idx++; while( m_idx < sz && m_table->m_entries[m_idx] == 0 ) m_idx++; } template void asCSymbolTableIterator::Previous() { // overflow on stepping over first element asUINT sz = m_table->m_entries.GetLength(); m_idx--; while( m_idx < sz && m_table->m_entries[m_idx] == 0 ) m_idx--; } template asCSymbolTableIterator& asCSymbolTableIterator::operator--(int) { Previous(); return *this; } END_AS_NAMESPACE #endif // AS_SYMBOLTABLE_H