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
use Strings, Math, Editor
group HashTableTest
type MyRecord is record
utf8 Name
end
(1)use template Structures.HashTable for HashTableType, MyRecord, Name, 10 with test
design Tester is
cell Out
HashTableType HashTable
MyRecord.ptr Entry
int Count=0
constructor is
Out = create Editor.MenuWindow("HashTable test")
HashTable.Out = Out
HashTable.Init()
ansi alfabet[] = []
Out <- print("\nNr of entries in this test will be : [ nel alfabet ^ 3] ([nel alfabet ^ 3 * sizeof MyRecord] bytes)")
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
Out <- print("\n\n*** Reading back the rows in the table\n\n")
for Key = 0 to 1023 do
HashTable.Print( Key )
end
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