[dasher] Fast-cased Alphabet{, Map} for unicode characters which are single chars in utf8.



commit bd9871878c90820df8496638a0a729bb0796785a
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Mon Aug 10 10:06:20 2009 +0200

    Fast-cased Alphabet{,Map} for unicode characters which are single chars in utf8.
    (23-Jul-2009)

 ChangeLog                               |    3 ++-
 Src/DasherCore/Alphabet/Alphabet.cpp    |   14 ++++++++++----
 Src/DasherCore/Alphabet/Alphabet.h      |   22 +++++++++++-----------
 Src/DasherCore/Alphabet/AlphabetMap.cpp |   20 ++++++++++++++++++--
 Src/DasherCore/Alphabet/AlphabetMap.h   |    3 +++
 5 files changed, 44 insertions(+), 18 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 8e0e151..34844aa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,8 @@
 2009-08-10  Alan Lawrence <acl33 inf phy cam ac uk>
 
 	* Remove unused GetRenderCount methods
-	* Cleanup Alphabet(Map) e.g. g/c KeyIsPrefix.
+	* Cleanup Alphabet{,Map} e.g. g/c KeyIsPrefix.
+	* Alphabet{,Map}: Optimise single byte UTF8 case.
 
 2009-08-08  Alan Lawrence <acl33 inf phy cam ac uk>
 
diff --git a/Src/DasherCore/Alphabet/Alphabet.cpp b/Src/DasherCore/Alphabet/Alphabet.cpp
index ddf1472..fc70415 100644
--- a/Src/DasherCore/Alphabet/Alphabet.cpp
+++ b/Src/DasherCore/Alphabet/Alphabet.cpp
@@ -167,10 +167,16 @@ void CAlphabet::GetSymbols(std::vector<symbol> &symbols, std::istream &in) const
           in >> skip;
         }
       else
-        {
-          in.read(utfchar, len);
-          utfchar[len] = '\0';
-          sym = TextMap.Get(string(utfchar));
+	  {
+		  if (len == 1) {
+			in.ignore(1);
+			sym = TextMap.GetSingleChar(ch);
+		  }
+		  else {
+            in.read(utfchar, len);
+			utfchar[len] = '\0';
+			sym = TextMap.Get(string(utfchar));
+		  }
           symbols.push_back(sym);
         }
       ch = in.peek();
diff --git a/Src/DasherCore/Alphabet/Alphabet.h b/Src/DasherCore/Alphabet/Alphabet.h
index e14396f..8919e67 100644
--- a/Src/DasherCore/Alphabet/Alphabet.h
+++ b/Src/DasherCore/Alphabet/Alphabet.h
@@ -125,16 +125,6 @@ namespace Dasher {
 
     void Trace() const;         // diagnostic
 
-    // Add the characters that can appear in Nodes
-    void AddChar(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);    // add single char to the alphabet
-
-    // Alphabet language parameters
-    void AddParagraphSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
-    void AddSpaceSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
-    void AddControlSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
-    void AddStartConversionSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
-    void AddEndConversionSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
-
     void SetOrientation(Opts::ScreenOrientations Orientation) {
       m_Orientation = Orientation;
     }
@@ -156,9 +146,19 @@ namespace Dasher {
     }
 
     SGroupInfo *m_pBaseGroup;
-    
+
   private:
 
+    // Add the characters that can appear in Nodes
+    void AddChar(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);    // add single char to the alphabet
+
+    // Alphabet language parameters
+    void AddParagraphSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
+    void AddSpaceSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
+    void AddControlSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
+    void AddStartConversionSymbol(std::string NewCharacter, std::string Display , int Colour, std::string Foreground);
+    void AddEndConversionSymbol(std::string NewCharacter, std::string Display, int Colour, std::string Foreground);
+
     Opts::AlphabetTypes m_DefaultEncoding;
     Opts::ScreenOrientations m_Orientation;
     symbol m_ParagraphSymbol;
diff --git a/Src/DasherCore/Alphabet/AlphabetMap.cpp b/Src/DasherCore/Alphabet/AlphabetMap.cpp
index c4299fd..89b707a 100644
--- a/Src/DasherCore/Alphabet/AlphabetMap.cpp
+++ b/Src/DasherCore/Alphabet/AlphabetMap.cpp
@@ -9,7 +9,7 @@
 #include "../../Common/Common.h"
 
 #include "AlphabetMap.h"
-
+#include <limits>
 
 using namespace Dasher;
 using namespace std;
@@ -27,9 +27,21 @@ static char THIS_FILE[] = __FILE__;
 alphabet_map::alphabet_map(unsigned int InitialTableSize)
 :HashTable(InitialTableSize <<1), Undefined(0) {
   Entries.reserve(InitialTableSize);
+
+  const int numChars = numeric_limits<char>::max() + 1;
+  m_pSingleChars = new symbol[numChars];
+  for (int i = 0; i<numChars; i++) m_pSingleChars[i] = Undefined;
+}
+
+alphabet_map::~alphabet_map() {
+  delete m_pSingleChars;
 }
 
 void alphabet_map::Add(const std::string &Key, symbol Value) {
+  if (Key.length() == 1) {
+    m_pSingleChars[Key[0]] = Value;
+    return;
+  }
   Entry *&HashEntry = HashTable[Hash(Key)];
 
   // Loop through Entries with the correct Hash value.
@@ -65,7 +77,9 @@ void alphabet_map::Add(const std::string &Key, symbol Value) {
 }
 
 symbol alphabet_map::Get(const std::string &Key) const {
-
+  if (Key.length() == 1) {
+	return GetSingleChar(Key[0]);
+  }
   // Loop through Entries with the correct Hash value.
   for(Entry * i = HashTable[Hash(Key)]; i; i = i->Next) {
     if(i->Key == Key) {
@@ -75,3 +89,5 @@ symbol alphabet_map::Get(const std::string &Key) const {
 
   return Undefined;
 }
+
+symbol alphabet_map::GetSingleChar(char key) const {return m_pSingleChars[key];}
diff --git a/Src/DasherCore/Alphabet/AlphabetMap.h b/Src/DasherCore/Alphabet/AlphabetMap.h
index c0b7d02..b018e40 100644
--- a/Src/DasherCore/Alphabet/AlphabetMap.h
+++ b/Src/DasherCore/Alphabet/AlphabetMap.h
@@ -65,10 +65,12 @@ class Dasher::alphabet_map {
 
 public:
   alphabet_map(unsigned int InitialTableSize = 255);
+  ~alphabet_map();
   void Add(const std::string & Key, symbol Value);
 
   // Return the symbol associated with Key or Undefined.
   symbol Get(const std::string & Key) const;
+  symbol GetSingleChar(char key) const;
 
 private:
   class Entry {
@@ -105,6 +107,7 @@ private:
   } std::vector < Entry > Entries;
   std::vector < Entry * >HashTable;
   const symbol Undefined;
+  symbol *m_pSingleChars;
 };
 /// \}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]