Examples

Using Templates

In this example we use one of the templates of the Structures group. Templates allow for writing code that is generic with respect to the type(s) used in the templates.

A template is defined for a number of parameters - that can be types, but also constants etc - and is instantiated by specifying the actual template parameters in a 'use template' directive.

A template can also contain conditional code (eg for testing purposes), that can be included or excluded as required.









(1) That is actually all there is to it. The rest of the this example is a test of the HashTableType that has been created.

07 Using Templates.celsrc
-.-
	HashTable
			
	In the following example we use a template to create a hashtable. The template 
	for the hashtable can be found in Structures.cellsrc.
	
	The test code in the example below is in the constructor, so any allocations
	(in particular the strings that are constructed as parameters), are on the 
	scratchpad and released automatically. The hashtable itself is at the global scope of
	the design and thus allocated on the heap. It has to be deleted explicitely (or 
	also automatically when the cell is destroyed.)			
-.-

use Strings, Math, Editor

group HashTableTest

-- The testrecord has one field, the name
type MyRecord is record
	utf8 	Name
end

-- Create a hashtable (see Structures.cellsrc for details)  - include the test code
-- The name of the hash table type is HashTableType, the records stored are 'MyRecord', the
-- field for which a hash key is calculated is the 'Name' field and the size of 
-- the primary table is 2^10. We also include the parts of the template labeled 'test'.
(1)use template Structures.HashTable for HashTableType, MyRecord, Name, 10 with test

-- The design for a test cell
design Tester is 

	cell 			Out			-- The output cell
	HashTableType	HashTable	-- The table 
	MyRecord.ptr 	Entry		-- A record 
	int 			Count=0		-- Counter
	
	constructor is
	
		-- Make an output cell
		Out = create Editor.MenuWindow("HashTable test")	
		
		-- In the test version there is an output cell
		HashTable.Out = Out

		-- Init the table
		HashTable.Init()
		
		-- The alphabet (here declared as a series of strings of 1 byte)	
		ansi alfabet[] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]		

		-- print the size of the table(s): primary table and secondary tables.
		Out <- print("\nNr of entries in this test will be : [ nel alfabet ^ 3]  ([nel alfabet ^ 3 * sizeof MyRecord] bytes)")

		-- First we fill up the table
		Out <- print("\n\n*** Filling up the tables\n\n")

		for each a in alfabet do
			for each b in alfabet do
				for each c in alfabet do
					Entry := HashTable.Add( "[a][b][c]" )	
				end
			end
		end
		
		-- print a few complete elements from the list
		Out <- print("\n\n*** Reading back the rows in the table\n\n")
		for Key = 0 to 1023 do
			HashTable.Print( Key )
		end

		-- Now we look up every 50 th element in the table
		Out <- print("\n\n*** Looking up names in the table\n\n")
	
		Count = 0

		for each a in alfabet do 
			for each b in alfabet do 
				for each c in alfabet do
					Count += 1					
					if Count % 50 is 0 then
						Entry := HashTable.Find( "[a][b][c]" )
						Entry is null ? Out <- print(".. not found")
					end
				end
			end
		end
		
		Out <- print("\n\ndone")
	
	end
	
end