[sword-devel] Counting Keys in a Module

DM Smith dmsmith555 at yahoo.com
Mon Apr 21 13:09:28 MST 2008

Troy A. Griffitts wrote:
> Theoretically, it is not always a good thing for a container object to 
> provide a count method.  The problem is more easily seen when thinking 
> about a tree container.  A count method can traverse the tree to sum all 
> nodes, but typically there is no efficient way to obtain this 
> information-- without storing 'accounting' information about the 
> container, and this has the danger of getting out of sync.
> In any case, can you provide the use case for needing the count of 
> entries in a module?  Maybe we can think of an alternative route.  One 
> place we've needed them is for providing search status % information in 
> the status update callback.  We can't determine the % if we don't know 
> the total count of entries.
The performance improvement that I got for JSword on the display of the 
dictionaries was due in part to knowing the number of entries in a 

Many modern GUIs have a list widget that does not need to be fully 
populated but merely need to know how to get the rows by index it needs 
to display. These also need a total size in order to make the scrollbar 
work. If I recall correctly, GnomeSword has a performance problem with 
dictionaries and this might help that.

For a dictionary, one can take the size of the dat file and divide it by 
the size of a dat file entry to determine the number of dictionary 
entries. I looked to see if there were any way for a dictionary to have 
dat entries that did not point to a valid place and I did not find any. 
It looks like the strict ordering of the keys requires this to work. I 
tested this against all the dictionary modules at CrossWire and this is 
true for them.

In Him,
> We can always provide a brute force count method in SWModule with 
> something like:
> long SWModule::entryCount() {
> 	SWKey *saveKey = getKey().clone();
> 	long count = 0;
> 	for (*this = TOP; !this->Error(); (*this)++)
> 		count++;
> 	setKey(saveKey);
> 	delete saveKey;
> 	return count;
> }
> mmital wrote:
>> Hello,
>> I was just wondering if you managed to solve your problem. I had the same
>> question. Thanks,
>> Jeremy Erickson wrote:
>>> Hi,
>>> 	What is the proper way in Sword to quickly determine the number of keys
>>> in a 
>>> module?  Currently in BibleTime we use the following code to determine the 
>>> starting and ending indices:
>>>     *m_module = sword::TOP;
>>>     unsigned long verseLowIndex = m_module->Index();
>>>     *m_module = sword::BOTTOM;
>>>     unsigned long verseHighIndex = m_module->Index();
>>> This code works as expected when the module is a Bible, but it does not
>>> work 
>>> when the module is a Lexicon.  In this case, the Index() function always 
>>> returns 0.  Is this because StrKey does not reimplement the Index()
>>> function?  
>>> In any case, what would the cleanest way for me to count the keys in a
>>> module 
>>> be, so that it works with all types of modules?
>>> -Jeremy Erickson

More information about the sword-devel mailing list