New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / test / OOPDemos / sysdep / hashed_strings.c
blob7ca542ac88f6e3aa5924f3e6374774bb6e828e01
1 /*
2 (C) 1997-98 AROS - The Amiga Research OS
3 $Id$
5 Desc: Demo of new OOP system.
6 Lang: english
7 */
9 #include "oop.h"
10 #include "hash.h"
11 #include "sysdep/hashed_strings.h"
12 #include <stdlib.h>
14 #define SDEBUG 0
15 #define DEBUG 0
16 #include "debug.h"
18 #define ClassID ClassNode.ln_Name
20 static struct Bucket *CopyBucket(struct Bucket *old_b);
21 static VOID FreeBucket(struct Bucket *b);
23 /**********************
24 ** NumNewMethods() **
25 **********************/
27 static ULONG NumNewMethods(Class *cl, struct InterfaceDescr *ifDescr)
29 struct Bucket **ht;
30 ULONG numnewmethods = 0;
32 EnterFunc(bug("NumNewMethods(cl=%s, ifDescr=%p)\n", cl->ClassID, ifDescr));
34 if (cl->SuperClass)
36 ht = (struct Bucket **)cl->SuperClass->HashTable;
37 for (; ifDescr->MethodTable; ifDescr ++)
39 ULONG i;
41 for (i = 0; i < ifDescr->NumMethods; i ++)
44 if (!HashLookupStr(ht, (IPTR)ifDescr->MethodTable[i].MethodID))
46 numnewmethods ++;
54 else
56 /* This is rootclass, count the methods */
57 ht = (struct Bucket **)cl->HashTable;
58 for (; ifDescr->MethodTable; ifDescr ++)
60 numnewmethods += ifDescr->NumMethods;
63 ReturnInt ("NumNewMethods", ULONG, numnewmethods);
66 /***************************
67 ** AllocDispatchTales() **
68 ***************************/
70 BOOL AllocDispatchTables(Class *cl, struct InterfaceDescr *ifDescr)
72 ULONG entries;
74 EnterFunc(bug("AllocDispatchTables(cl=%s,ifDescr=%p)\n",
75 cl->ClassID, ifDescr));
77 entries = NumNewMethods(cl, ifDescr);
79 cl->HashTable = (struct MethodBucket **)NewHash(entries);
80 if (cl->HashTable)
82 /* Save hashtable mask for speed */
83 cl->HashMask = HashMask(cl->HashTable);
85 if (cl->SuperClass)
87 /* Copy the superclass hash table */
88 if ( !CopyHash((struct Bucket **)cl->HashTable
89 ,(struct Bucket **)cl->SuperClass->HashTable
90 ,CopyBucket
91 ,NULL))
93 goto failure;
97 /* Insert our own methods */
98 for (; ifDescr->MethodTable; ifDescr ++)
100 struct MethodDescr *mtab= ifDescr->MethodTable;
101 ULONG i;
103 for (i = 0; i < ifDescr->NumMethods; i ++)
105 struct MethodBucket *b;
107 /* Method existed in superclass ? */
108 b = (struct MethodBucket *)HashLookupStr((struct Bucket **)cl->HashTable
109 ,(IPTR)mtab[i].MethodID);
110 if (b)
112 b->MethodFunc = mtab[i].MethodFunc;
113 b->mClass = cl;
115 else
117 /* Must allocate new bucket */
118 struct MethodBucket *new_b;
120 D(bug("Inserting method %s\n", mtab[i].MethodID));
122 new_b = (struct MethodBucket *)malloc( sizeof (struct MethodBucket) );
123 if (!new_b)
125 goto failure;
127 /* Initialize bucket */
128 new_b->MethodID = mtab[i].MethodID;
129 new_b->MethodFunc = mtab[i].MethodFunc;
130 new_b->mClass = cl;
132 /* Add bucket to hashtable */
133 InsertBucket((struct Bucket **)cl->HashTable, (struct Bucket *)new_b);
136 } /* for (each method in methodtable) */
138 } /* for (each interface) */
141 ReturnBool ("AllocDispatchTables", TRUE);
142 failure:
143 FreeHash((struct Bucket **)cl->HashTable, FreeBucket);
144 ReturnBool ("AllocDispatchTables", FALSE);
148 VOID FreeDispatchTables(Class *cl)
150 FreeHash((struct Bucket **)cl->HashTable, FreeBucket);
152 return;
156 /**************************
157 ** Hash handling hooks **
158 **************************/
159 #define MB(x) ((struct MethodBucket *)x)
160 static struct Bucket *CopyBucket(struct Bucket *old_b)
162 struct MethodBucket *new_b;
164 new_b = (struct MethodBucket *)malloc(sizeof (struct MethodBucket) );
165 if (new_b)
167 new_b->MethodID = MB(old_b)->MethodID;
168 new_b->MethodFunc = MB(old_b)->MethodFunc;
169 new_b->mClass = MB(old_b)->mClass;
170 return ((struct Bucket *)new_b);
172 return (NULL);
175 static VOID FreeBucket(struct Bucket *b)
177 free(b);