2 libfmail: Generic Search Tree implementation
4 Copyright (C) 2007 Carlos Daniel Ruvalcaba Valenzuela <clsdaniel@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 typedef int (*HASH_FUNC
)(char s
);
23 class SearchNotFound
{};
25 template <class T
, int max
> class TreeNode
{
28 TreeNode
<T
, max
> *tree
[max
];
31 for (int i
= 0; i
< max
; i
++){
37 template <class T
, int max
, HASH_FUNC hash_func
> class SearchTree
{
39 TreeNode
<T
, max
> *rootNode
;
41 int getNodeSize(TreeNode
<T
, max
> *node
){
46 size
+= sizeof(T
); //Size of value
47 size
+= sizeof(void*) * max
;
48 for (i
= 0; i
< max
; i
++){
49 if (node
->tree
[i
] != NULL
)
50 size
+= getNodeSize(node
->tree
[i
]);
57 rootNode
= new TreeNode
<T
, max
>();
59 int Insert(char *key
, T value
){
66 for (i
= 0; i
< l
; i
++){
67 tk
= hash_func(key
[i
]);
72 if (c
->tree
[tk
] == NULL
)
73 c
->tree
[tk
] = new TreeNode
<T
, max
>();
86 for (i
= 0; i
< l
; i
++){
87 tk
= hash_func(key
[i
]);
89 throw SearchNotFound();
94 if (c
->tree
[tk
] == NULL
)
95 throw SearchNotFound();
104 return getNodeSize(rootNode
);
107 int HasKey(char *key
){
112 for (i
= 0; i
< l
; i
++){
113 tk
= hash_func(key
[i
]);
121 if (c
->tree
[tk
] == NULL
)