uLib  User mode C/C++ extended API library for Win32 programmers.
WordList Class Reference

#include <uLib/StrFunc.h>

Inheritance diagram for WordList:
StringCache

Detailed Description

WordList is a sorted list of unique strings (duplicates are excluded).

It adds sorting, duplicate culling, and indexed access to StringCache.
Memory is chunk-wise allocated on demand by AddString(), and freed by the d'tor.
WordList is intended for memory-efficient string lists, f.ex. keyword tables,
and therefore doesn't support Unicode strings.
It is designed to be fast, by utilizing iterative binary search and pre-allocation.

Note: You may use the underlying Storage for multi-string operations,
however, the Storage cache is not sorted, only the Strings array is.

Example: Using bsearch() to search a WordList..

WordList list;
list.AddString( "Xylophone" );
list.AddString( "Saxophone" );
list.AddString( "Telephone" );
list.AddString( "Phoneme" );
list.AddString( "Allophone" );
int __cdecl _matchItem( const void* _pattern, const void* _element )
{
char* pattern = (char*)_pattern; // Key..
char* element = *(char**)_element; // Ptr to the Strings[ix] ptr..
int len = strlen( pattern ); // Accept partial match..
return strncmp( pattern, element, len );
}
// You'll recieve a ptr to the Strings[ix] ptr to the matching element..
char* item = *(char**) bsearch(
"Tele", list.Strings, list.Count, sizeof(char*), _matchItem
);
// item --> Telephone

Definition at line 778 of file StrFunc.h.

Public Data

char ** Strings
 
UINT Count
 
- Public Data inherited from StringCache
char * Storage
 

Public Functions

 WordList (UINT InitListDim=128, UINT InitStgSize=4096, UINT ListGrowBy=128, UINT StgGrowBy=4096)
 
 ~WordList ()
 
bool AddString (char *Str, bool caseSens=true)
 
char * Search (char *Key, int(__stdcall *Compare)(char *Key, char *Str, void *Ctx), void *Context)
 
void Reset ()
 
void Free ()
 
- Public Functions inherited from StringCache
 StringCache (UINT InitSize=4096, UINT ChunkSize=4096)
 
 ~StringCache ()
 
char * DupStr (char *Str)
 
void Reset ()
 
void Free ()
 
int StorageSize ()
 
int StorageUsed ()
 

Protected Member Functions

bool Grow ()
 
- Protected Member Functions inherited from StringCache
bool GrowStorage ()
 

Protected Attributes

UINT Dim
 
UINT ListChunk
 
- Protected Attributes inherited from StringCache
UINT StgSize
 
UINT StgUsed
 
UINT StgChunk
 

Constructor & Destructor Documentation

◆ WordList()

WordList::WordList ( UINT  InitListDim = 128,
UINT  InitStgSize = 4096,
UINT  ListGrowBy = 128,
UINT  StgGrowBy = 4096 
)

Use the constuctor parameters to optimize your memory use.

Parameters
InitListDimNr of string ptrs in initial allocation.
ListGrowByNr of string ptrs per additional allocation.
InitStgSizeBlock size of initial StringCache allocation.
StgGrowByStringCache chunk size to add per reallocation.

Definition at line 1758 of file StrFunc.cpp.

◆ ~WordList()

WordList::~WordList ( )

The d'tor deallocates Strings and Storage.

Definition at line 1768 of file StrFunc.cpp.

Member Function Documentation

◆ AddString()

bool WordList::AddString ( char *  Str,
bool  caseSens = true 
)

AddString() adds a string to the list if it doesn't exist already.
It returns true if Str was added, or false if it was already present.
Use caseSens to specify if the comparator should be case sensitive or not.

Definition at line 1803 of file StrFunc.cpp.

◆ Search()

char * WordList::Search ( char *  Key,
int(__stdcall *Compare)(char *Key, char *Str, void *Ctx)  ,
void *  Context 
)

Search does a binary search of the WordList content.
Return the string matching Key, or NULL on failure.
Note: This function avoids the problems in bsearch() implementation.

Definition at line 1875 of file StrFunc.cpp.

◆ Reset()

void WordList::Reset ( )

Un-use all memory.. No deallocation.

Definition at line 1773 of file StrFunc.cpp.

◆ Free()

void WordList::Free ( )

Deallocate all memory.

Definition at line 1779 of file StrFunc.cpp.

◆ Grow()

bool WordList::Grow ( )
protected

Definition at line 1786 of file StrFunc.cpp.

Member Data Documentation

◆ Strings

char** WordList::Strings

Sorted string list (pointer array). Treat as R/O.

Definition at line 780 of file StrFunc.h.

◆ Count

UINT WordList::Count

Nr of Strings in the list. Treat as R/O.

Definition at line 785 of file StrFunc.h.

◆ Dim

UINT WordList::Dim
protected

Definition at line 826 of file StrFunc.h.

◆ ListChunk

UINT WordList::ListChunk
protected

Definition at line 827 of file StrFunc.h.


The documentation for this class was generated from the following files: