Click here to Skip to main content
13,287,834 members (35,773 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 1 Mar 2004

Fast Textual Data Processing using Hashed Strings

, 6 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Managing a textual numbering system for hashing words into numbers


Strings are considered heavy nesting loop generators. Comparisons, searching, and sorting strings are considered time consuming operations. Indexing textual data is a good step, however setting searching key as string yields bad performance for search algorithms.

In this article we introduce an ADT abstracting strings with hash-codes. Comparisons will no more be done with byte comparisons. Each words in the alphabet will be assigned an invertible hash-code.

Hashing Function

Hashing functions aim to represent a collection of data in a single code. Codes may overlap, so a good hash function aims to minimise overlapping between codes. Systems using hashing should also manage overflow problems. What we represent here could be called as hashing. However, it is invertible and assures uniquness. Actually, it manages a numbering system for textual data.

Consider the word "baby", calculating hash-code is simply as converting from binary to decimal numbering systems. In this case, we convert from 26th numbring system to decimal, where 26 is the number of english alphbet characters.



This means that "baba" is less than "babb". Comparison is done with numbers. No more nesting loops is needed for comparisons.

Class hashedstring

class hashedstring


    hashedstring():_pdata(0), _hashkey(0), _size(0){}
    hashedstring(const hashedstring& hstr):_pdata(strdup(hstr._pdata)), _
        hashkey(hstr._hashkey), _size(hstr._size){}
    hashedstring(const char* pdata):_pdata(strdup(pdata)), 
        _hashkey(0), _size(strlen(pdata))
    hashedstring operator+(const hashedstring& s) const 
        hashedstring rtn=*this;
        strcat(rtn._pdata, s._pdata);
        rtn._hashkey+=_hashkey*pow(26, s._size);
        return rtn;

    hashedstring operator+=(const hashedstring& s)
        strcat(_pdata, s._pdata);
        _hashkey=_hashkey*pow(26, s._size)+s._hashkey;
        return *this;
    hashedstring operator=(const hashedstring& s) 
        return *this;

    void clear()
            delete []_pdata;

    operator const double() const {return _hashkey;}

    double _computehashkey()
        unsigned int i=0;
        for(i=_size-1; i!=-1; i--)
            _hashkey+=(tolower(_pdata[i])-'a')*pow(26, _size-1-i);
        return _hashkey;
    char* _pdata;
    double _hashkey;
    unsigned int _size;



Maintaining alphabet numbering system provides faster string processing. Calculating hash codes, an offline process, should be added in indexing huge textual data. Then the system will enjoy fast comparisons, search, and sort routines.

hashedstring s1("baby");
hashedstring s2("babybaba");
hashedstring s3("babybabb");







s1= 17626
s1= 8054676578
s2= 8054676578
s3= 8054676579
s1<s2= 0
s2<s3= 1
s2==s3= 0
s1==s2= 1


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Mo Hossny
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralVERY Very bad article Pin
SB200329-Dec-09 6:07
memberSB200329-Dec-09 6:07 
GeneralRe: VERY Very bad article Pin
Mohammed Hossny28-Sep-10 16:49
memberMohammed Hossny28-Sep-10 16:49 
GeneralTurkmenistana = Turkmenistanb Pin
Zegee12-Jul-07 2:05
memberZegee12-Jul-07 2:05 
QuestionWhere can I find a working implementation? Pin
Robert F.23-Sep-04 1:13
sussRobert F.23-Sep-04 1:13 
AnswerRe: Where can I find a working implementation? Pin
Mohammed Hossny23-Sep-04 1:22
memberMohammed Hossny23-Sep-04 1:22 
GeneralRe: Where can I find a working implementation? Pin
Robert F23-Sep-04 1:38
sussRobert F23-Sep-04 1:38 
GeneralRe: Where can I find a working implementation? Pin
Mohammed Hossny23-Sep-04 2:15
memberMohammed Hossny23-Sep-04 2:15 
GeneralRe: Where can I find a working implementation? Pin
Robert F.23-Sep-04 4:23
sussRobert F.23-Sep-04 4:23 
GeneralRe: Where can I find a working implementation? Pin
Mohammed Hossny23-Sep-04 13:16
memberMohammed Hossny23-Sep-04 13:16 
GeneralOptimizator : "Does it speaks to you ?" Pin
Kochise18-Mar-04 22:20
memberKochise18-Mar-04 22:20 
GeneralRe: Optimizator : &quot;Does it speaks to you ?&quot; Pin
Mohammed Hossny20-Mar-04 21:56
memberMohammed Hossny20-Mar-04 21:56 
GeneralYes, that's it, hashing at compile time... Pin
Kochise21-Mar-04 5:32
memberKochise21-Mar-04 5:32 
Generalaaa = aa = a = 0 Pin
Chris Roberts8-Mar-04 8:49
memberChris Roberts8-Mar-04 8:49 
GeneralRe: aaa = aa = a = 0 Pin
Mohammed Hossny9-Mar-04 0:27
memberMohammed Hossny9-Mar-04 0:27 
QuestionCould you fix the horizontal scrolling annoyance? Pin
WREY3-Mar-04 11:35
memberWREY3-Mar-04 11:35 
AnswerRe: Could you fix the horizontal scrolling annoyance? Pin
Mohammed Hossny7-Mar-04 12:45
memberMohammed Hossny7-Mar-04 12:45 
GeneralRe: Could you fix the horizontal scrolling annoyance? Pin
jongman7-Mar-04 18:09
memberjongman7-Mar-04 18:09 
GeneralNice, but... it does not work. Pin
anickol2-Mar-04 22:57
memberanickol2-Mar-04 22:57 
GeneralRe: Nice, but... it does not work. Pin
Jon3-Mar-04 0:02
memberJon3-Mar-04 0:02 
GeneralRe: Nice, but... it does not work. Pin
Mohammed Hossny5-Mar-04 10:40
memberMohammed Hossny5-Mar-04 10:40 
QuestionCollision space? Pin
sdoyle2-Mar-04 7:25
membersdoyle2-Mar-04 7:25 
AnswerRe: Collision space? Pin
Mohammed Hossny2-Mar-04 8:20
memberMohammed Hossny2-Mar-04 8:20 
GeneralNice concept... not practical.. Pin
Peter Mares2-Mar-04 3:53
memberPeter Mares2-Mar-04 3:53 
Hey there...

Very nice concept... and I think it go somewhere with a little more thought on how to handle longer strings, or even paragraphs of text. Sooner or later you are going to overflow.

Any thoughts on that?
[Glossary Manager] [AfterThought Backup Lite]

99 little bugs in the code, 99 little bugs,
Fix 1 bug, recompile....
101 little bugs in the code...
GeneralRe: Nice concept... not practical.. Pin
Mohammed Hossny2-Mar-04 4:04
memberMohammed Hossny2-Mar-04 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 7 Mar 2004
Article Copyright 2004 by Mo Hossny
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid