Browse Source

add hashCode function to Text class

Kolja Strohm 2 years ago
parent
commit
28d33d78ce
5 changed files with 70 additions and 1 deletions
  1. 1 0
      Framework Linux.vcxproj
  2. 3 0
      Framework Linux.vcxproj.filters
  3. 51 0
      RCPointer.h
  4. 11 0
      Text.cpp
  5. 4 1
      Text.h

+ 1 - 0
Framework Linux.vcxproj

@@ -142,6 +142,7 @@
     <ClCompile Include="Punkt.cpp" />
     <ClCompile Include="Rahmen.cpp" />
     <ClCompile Include="Random.cpp" />
+    <ClCompile Include="RCPointer.h" />
     <ClCompile Include="ReferenceCounter.cpp" />
     <ClCompile Include="Schrift.cpp" />
     <ClCompile Include="Scroll.cpp" />

+ 3 - 0
Framework Linux.vcxproj.filters

@@ -482,5 +482,8 @@
     <ClCompile Include="InMemoryBuffer.cpp">
       <Filter>Quelldateien\Framework\IO</Filter>
     </ClCompile>
+    <ClCompile Include="RCPointer.h">
+      <Filter>Headerdateien\Framework\Data</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 51 - 0
RCPointer.h

@@ -0,0 +1,51 @@
+#include "Betriebssystem.h"
+
+namespace Framework
+{
+    template<typename T>
+    class RCPointer
+    {
+    private:
+        T *pointer;
+
+    public:
+        RCPointer( T *pointer )
+        {
+            this->pointer = pointer;
+        }
+
+        RCPointer( RCPointer &ptr )
+        {
+            this->pointer = ptr.pointer ? dynamic_cast<T *>( ptr.pointer->getThis() ) : 0;
+        }
+
+        ~RCPointer()
+        {
+            this->pointer->release();
+        }
+
+        RCPointer &operator=( RCPointer &ptr )
+        {
+            if( this->pointer )
+                this->pointer->release();
+            this->pointer = ptr.pointer ? dynamic_cast<T *>( ptr.pointer->getThis() ) : 0;
+        }
+
+        RCPointer &operator=( T *ptr )
+        {
+            if( this->pointer )
+                this->pointer->release();
+            this->pointer = ptr;
+        }
+
+        T *operator->()
+        {
+            return pointer;
+        }
+
+        operator T *( )
+        {
+            return pointer;
+        }
+    };
+}

+ 11 - 0
Text.cpp

@@ -2,6 +2,7 @@
 #include "Text.h"
 #include <sstream>
 #include <iomanip>
+#include <math.h>
 #ifndef WIN32
 #include <string.h>
 #endif
@@ -1082,6 +1083,16 @@ Text *Text::getTeilText( int p ) const // gibt den Text von p bis zum Ende zur
     return getTeilText( p, getLength() ); // Text zurückgeben
 }
 
+// berechnet den Hash code des textes
+int Text::hashCode() const
+{
+    int result = 0;
+    int len = getLength();
+    for( int i = 0; i < len; i++ )
+        result += (int)pow( txt[ i ] * 31, len - 1 - i );
+    return result;
+}
+
 // Operatoren
 Text &Text::operator+=( const int num )
 {

+ 4 - 1
Text.h

@@ -288,6 +288,8 @@ namespace Framework
         //! Gibt einen Text zurück, der eine Kopie eines bestimmten Textabschnittes enthält
         //!  p1: Die Startposition des Textabschnittes (Der Textabschnitt geht bis ans Ende des Textes)
         DLLEXPORT Text *getTeilText( int p ) const;
+        // berechnet den Hash code des textes
+        DLLEXPORT int hashCode() const;
         //! Hängt eine Zahl ans Ende des Textes an
         DLLEXPORT Text &operator+=( const int num );
         //! Hängt eine Zahl ans Ende des Textes an
@@ -311,7 +313,7 @@ namespace Framework
         //! Setzt den Inhalt des Textes gleich einer Kopie des Inhalts eines anderen Textes
         DLLEXPORT Text &operator=( const Text &txt );
         //! Gibt den Inhalt des Textes als Zeichenkette zurück
-        DLLEXPORT operator char*( ) const;
+        DLLEXPORT operator char *( ) const;
         //! Konviertiert den Inhalt des Textes zu einer Zahl
         DLLEXPORT operator int() const;
         //! Konviertiert den Inhalt des Textes zu einer Zahl
@@ -336,6 +338,7 @@ namespace Framework
         DLLEXPORT Text operator+( const double num ) const;
         //! Erstellt einen neuen Text bestehend aus diesem und num
         DLLEXPORT Text operator+( const float num ) const;
+
     };
 
     class TextReader : public Reader, public virtual ReferenceCounter