[sword-devel] Counting Keys in a Module

Troy A. Griffitts scribe at crosswire.org
Mon Apr 21 12:25:41 MST 2008


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.

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
>>
>> _______________________________________________
>> sword-devel mailing list: sword-devel at crosswire.org
>> http://www.crosswire.org/mailman/listinfo/sword-devel
>> Instructions to unsubscribe/change your settings at above page
>>
>>
> 






More information about the sword-devel mailing list