dt.h

00001 /*
00002  * Copyright (C) 2009 1&1 Internet AG
00003  *
00004  * This file is part of sip-router, a free SIP server.
00005  *
00006  * sip-router is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version
00010  *
00011  * sip-router is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License 
00017  * along with this program; if not, write to the Free Software 
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #ifndef _DT_H_
00022 #define _DT_H_
00023 
00024 
00025 
00026 
00027 #include "common.h" 
00028 
00029 
00030 
00031 
00032 struct dt_node_t {
00033         struct dt_node_t *child[10];
00034         carrier_t carrier;
00035 };
00036 
00037 
00038 
00039 
00040 /*
00041  Allocates memory for the root node and initializes it.
00042  Returns a pointer to an initialized root node on success, NULL otherwise.
00043 */
00044 struct dt_node_t *dt_init();
00045 
00046 /*
00047  Deletes a subtree and frees memory including node.
00048  Memory for the root node is not freed.
00049 */
00050 void dt_delete(struct dt_node_t *root, struct dt_node_t *node);
00051 
00052 /*
00053  Deletes the whole tree and frees the memory including the root node.
00054 */
00055 void dt_destroy(struct dt_node_t **root);
00056 
00057 /*
00058  Deletes everything but the root node.
00059  The root node is initialized.
00060  This will make an empty tree like after dt_init.
00061 */
00062 void dt_clear(struct dt_node_t *root);
00063 
00064 /*
00065  Inserts a number with a corresponding carrier id.
00066  Nodes are created if necessary and the node after the last
00067  digit is marked with the given carrier id.
00068  Returns 0 on success, -1 otherwise.
00069 */
00070 int dt_insert(struct dt_node_t *root, const char *number, int numberlen, carrier_t carrier);
00071 
00072 /*
00073  Returns the number of nodes in the given tree.
00074 */
00075 int dt_size(struct dt_node_t *root);
00076 
00077 /*
00078  Returns the number of nodes in the given tree that are marked
00079  with a carrier id (carrier>0).
00080 */
00081 int dt_loaded_nodes(struct dt_node_t *root);
00082 
00083 /*
00084  Returns the number of leaf nodes in the given tree.
00085  On leaf nodes the leaf flag is set, on other nodes it is cleared.
00086 */
00087 int dt_leaves(struct dt_node_t *root);
00088 
00089 /*
00090  Finds the longest prefix match of number in root.
00091  Sets *carrier according to value in dtree.
00092  Return the number of matched digits.
00093  In case no (partial) match is found, return -1 (i.e,
00094  not even the very first digit could be prefixed).
00095 */
00096 int dt_longest_match(struct dt_node_t *root, const char *number, int numberlen, carrier_t *carrier);
00097 
00098 /*
00099  Returns 1 if number is found in root and  set *carrier
00100  according to value in dtree.
00101  Returns 0 if the number is not found.
00102 */
00103 int dt_contains(struct dt_node_t *root, const char *number, int numberlen, carrier_t *carrier);
00104 
00105 /*
00106  Optimizes the tree by means of compression. Effectively,
00107  this reduces the tree to a set of nodes representing shortest
00108  prefixes (as opposed to [likely complete] phone numbers).
00109 */
00110 void dt_optimize(struct dt_node_t *root);
00111 
00112 
00113 
00114 
00115 #endif