Browse Source

fix issue with reference counting

Kolja Strohm 2 years ago
parent
commit
6eb73b9a8d
2 changed files with 13 additions and 11 deletions
  1. 5 5
      HashMap.h
  2. 8 6
      RCPointer.h

+ 5 - 5
HashMap.h

@@ -66,7 +66,7 @@ namespace Framework
 
         void put( K key, V value )
         {
-            int index = hash( key ) % bucketCount;
+            int index = abs( hash( key ) ) % bucketCount;
             if( !buckets[ index ] )
                 buckets[ index ] = new Array< MapEntry<K, V>>();
             for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )
@@ -82,7 +82,7 @@ namespace Framework
 
         void remove( K key )
         {
-            int index = hash( key ) % bucketCount;
+            int index = abs( hash( key ) ) % bucketCount;
             if( !buckets[ index ] )
                 return;
             int listIndex = 0;
@@ -98,7 +98,7 @@ namespace Framework
 
         V get( K key ) const
         {
-            int index = hash( key ) % bucketCount;
+            int index = abs( hash( key ) ) % bucketCount;
             if( !buckets[ index ] )
                 throw "element not found";
             for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )
@@ -111,7 +111,7 @@ namespace Framework
 
         V safeGet( K key, V fallback ) const
         {
-            int index = hash( key ) % bucketCount;
+            int index = abs( hash( key ) ) % bucketCount;
             if( !buckets[ index ] )
                 return fallback;
             for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )
@@ -124,7 +124,7 @@ namespace Framework
 
         bool has( K key ) const
         {
-            int index = hash( key ) % bucketCount;
+            int index = abs( hash( key ) ) % bucketCount;
             if( !buckets[ index ] )
                 return 0;
             for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )

+ 8 - 6
RCPointer.h

@@ -9,14 +9,14 @@ namespace Framework
         T *pointer;
 
     public:
-        RCPointer()
+        static RCPointer<T> of( T *pointer )
         {
-            pointer = 0;
+            return RCPointer<T>() = pointer;
         }
 
-        RCPointer( T *pointer )
+        RCPointer()
         {
-            this->pointer = pointer;
+            pointer = 0;
         }
 
         RCPointer( RCPointer &ptr )
@@ -30,18 +30,20 @@ namespace Framework
                 pointer->release();
         }
 
-        RCPointer &operator=( RCPointer &ptr )
+        RCPointer<T> &operator=( RCPointer<T> &ptr )
         {
             if( this->pointer )
                 this->pointer->release();
             this->pointer = ptr.pointer ? dynamic_cast<T *>( ptr.pointer->getThis() ) : 0;
+            return *this;
         }
 
-        RCPointer &operator=( T *ptr )
+        RCPointer<T> &operator=( T *ptr )
         {
             if( this->pointer )
                 this->pointer->release();
             this->pointer = ptr;
+            return *this;
         }
 
         T *operator->()