From a4b27831693eed988bcdf957bbe9ad7609e7bdf7 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 22 Oct 2012 03:25:53 +0000 Subject: small optimization for BLI_heap(), give some speedup in decimeter. - use unsigned ints only (where mixing signed/unsigned) - turn heap_swap into an inline function, add SWAP_TVAL macro to swap values using a temp value as storage. - added type checking SHIFT3/4 macros also style cleanup for CTR_Map --- intern/container/CTR_HashedPtr.h | 17 ++++++--- intern/container/CTR_Map.h | 76 +++++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 42 deletions(-) (limited to 'intern') diff --git a/intern/container/CTR_HashedPtr.h b/intern/container/CTR_HashedPtr.h index 8d0ad2bb2a3..b7ac460f270 100644 --- a/intern/container/CTR_HashedPtr.h +++ b/intern/container/CTR_HashedPtr.h @@ -43,12 +43,19 @@ inline unsigned int CTR_Hash(void *inDWord) class CTR_HashedPtr { - void* m_valptr; + void *m_valptr; public: - CTR_HashedPtr(void* val) : m_valptr(val) {}; - unsigned int hash() const { return CTR_Hash(m_valptr);}; - inline friend bool operator ==(const CTR_HashedPtr & rhs, const CTR_HashedPtr & lhs) { return rhs.m_valptr == lhs.m_valptr;}; - void *getValue() const { return m_valptr; } + CTR_HashedPtr(void *val) : m_valptr(val) { + }; + unsigned int hash() const { + return CTR_Hash(m_valptr); + }; + inline friend bool operator ==(const CTR_HashedPtr & rhs, const CTR_HashedPtr & lhs) { + return rhs.m_valptr == lhs.m_valptr; + }; + void *getValue() const { + return m_valptr; + } }; #endif /* __CTR_HASHEDPTR_H__ */ diff --git a/intern/container/CTR_Map.h b/intern/container/CTR_Map.h index d729f2a1b06..c278fe5330c 100644 --- a/intern/container/CTR_Map.h +++ b/intern/container/CTR_Map.h @@ -37,13 +37,14 @@ class CTR_Map { private: struct Entry { Entry (Entry *next, Key key, Value value) : - m_next(next), - m_key(key), - m_value(value) {} + m_next(next), + m_key(key), + m_value(value) { + } Entry *m_next; - Key m_key; - Value m_value; + Key m_key; + Value m_value; }; public: @@ -62,18 +63,18 @@ public: for (int i = 0; i < m_num_buckets; ++i) { m_buckets[i] = 0; - for (Entry *entry = map.m_buckets[i]; entry; entry=entry->m_next) + for (Entry *entry = map.m_buckets[i]; entry; entry = entry->m_next) { insert(entry->m_key, entry->m_value); + } } } - int size() { - int count=0; - for (int i=0;im_next; count++; } @@ -81,15 +82,13 @@ public: return count; } - Value* at(int index) { - int count=0; - for (int i=0;im_value; } bucket = bucket->m_next; @@ -99,15 +98,13 @@ public: return 0; } - Key* getKey(int index) { - int count=0; - for (int i=0;im_key; } bucket = bucket->m_next; @@ -117,7 +114,8 @@ public: return 0; } - void clear() { + void clear() + { for (int i = 0; i < m_num_buckets; ++i) { Entry *entry_ptr = m_buckets[i]; @@ -130,12 +128,14 @@ public: } } - ~CTR_Map() { + ~CTR_Map() + { clear(); - delete [] m_buckets; + delete[] m_buckets; } - void insert(const Key& key, const Value& value) { + void insert(const Key& key, const Value& value) + { Entry *entry_ptr = m_buckets[key.hash() % m_num_buckets]; while ((entry_ptr != 0) && !(key == entry_ptr->m_key)) { entry_ptr = entry_ptr->m_next; @@ -150,7 +150,8 @@ public: } } - void remove(const Key& key) { + void remove(const Key& key) + { Entry **entry_ptr = &m_buckets[key.hash() % m_num_buckets]; while ((*entry_ptr != 0) && !(key == (*entry_ptr)->m_key)) { entry_ptr = &(*entry_ptr)->m_next; @@ -163,7 +164,8 @@ public: } } - Value *operator[](Key key) { + Value *operator[](Key key) + { Entry *bucket = m_buckets[key.hash() % m_num_buckets]; while ((bucket != 0) && !(key == bucket->m_key)) { bucket = bucket->m_next; -- cgit v1.2.3