Re: Fast appending and sorting of alot of short strings

new topic     » goto parent     » topic index » view thread      » older message » newer message

A little trick I found for this kind of dynamically growing sequences,
and that I use a lot in Win4Eu, is storing the number of used elements
in the sequence itself, instead of using a separate variable.
A very good place for this is the last element (seq[length(seq)]).

Here's a piece of code from Win4Eu that deals with collections (my name
for this kind of sequences):
constant GROWSIZE = 64 -- number of elements by which collections grow
constant BUFFER = repeat(0, GROWSIZE)

global function newCollection()
	-- Create a new collection
	-- (the last element of a collection contains the element-count)
	return BUFFER
end function

global function addToCollection(sequence collection, object element)
	-- Add an element to a collection
	integer count, growSize
	count = collection[length(collection)] + 1
	growSize = count - length(collection)
	if growSize > 0 then
		if growSize > GROWSIZE then
			collection &= repeat(0, growSize + GROWSIZE)
		else collection &= BUFFER
		end if
	end if
	collection[count] = element
	collection[length(collection)] = count
	return collection
end function

global function getCollectionCount(sequence collection)
	-- Get the number of elements in the collection
	return collection[length(collection)]
end function

global function findInCollection(object element, sequence collection)
	-- Find an element in a collection
	integer index
	index = find(element, collection)
	if index > collection[length(collection)] then index = 0 end if
	return index
end function


--
tommy online: http://users.telenet.be/tommycarlier
tommy.blog: http://tommycarlier.blogspot.com
Euphoria Message Board: http://uboard.proboards32.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu