dt.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2009 1&1 Internet AG
  3. *
  4. * This file is part of sip-router, a free SIP server.
  5. *
  6. * sip-router 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
  10. *
  11. * sip-router 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.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef _DT_H_
  21. #define _DT_H_
  22. #include "common.h"
  23. struct dt_node_t {
  24. struct dt_node_t *child[10];
  25. carrier_t carrier;
  26. };
  27. /*
  28. Allocates memory for the root node and initializes it.
  29. Returns a pointer to an initialized root node on success, NULL otherwise.
  30. */
  31. struct dt_node_t *dt_init();
  32. /*
  33. Deletes a subtree and frees memory including node.
  34. Memory for the root node is not freed.
  35. */
  36. void dt_delete(struct dt_node_t *root, struct dt_node_t *node);
  37. /*
  38. Deletes the whole tree and frees the memory including the root node.
  39. */
  40. void dt_destroy(struct dt_node_t **root);
  41. /*
  42. Deletes everything but the root node.
  43. The root node is initialized.
  44. This will make an empty tree like after dt_init.
  45. */
  46. void dt_clear(struct dt_node_t *root);
  47. /*
  48. Inserts a number with a corresponding carrier id.
  49. Nodes are created if necessary and the node after the last
  50. digit is marked with the given carrier id.
  51. Returns 0 on success, -1 otherwise.
  52. */
  53. int dt_insert(struct dt_node_t *root, const char *number, int numberlen, carrier_t carrier);
  54. /*
  55. Returns the number of nodes in the given tree.
  56. */
  57. int dt_size(struct dt_node_t *root);
  58. /*
  59. Returns the number of nodes in the given tree that are marked
  60. with a carrier id (carrier>0).
  61. */
  62. int dt_loaded_nodes(struct dt_node_t *root);
  63. /*
  64. Returns the number of leaf nodes in the given tree.
  65. On leaf nodes the leaf flag is set, on other nodes it is cleared.
  66. */
  67. int dt_leaves(struct dt_node_t *root);
  68. /*
  69. Finds the longest prefix match of number in root.
  70. Sets *carrier according to value in dtree.
  71. Return the number of matched digits.
  72. In case no (partial) match is found, return -1 (i.e,
  73. not even the very first digit could be prefixed).
  74. */
  75. int dt_longest_match(struct dt_node_t *root, const char *number, int numberlen, carrier_t *carrier);
  76. /*
  77. Returns 1 if number is found in root and set *carrier
  78. according to value in dtree.
  79. Returns 0 if the number is not found.
  80. */
  81. int dt_contains(struct dt_node_t *root, const char *number, int numberlen, carrier_t *carrier);
  82. /*
  83. Optimizes the tree by means of compression. Effectively,
  84. this reduces the tree to a set of nodes representing shortest
  85. prefixes (as opposed to [likely complete] phone numbers).
  86. */
  87. void dt_optimize(struct dt_node_t *root);
  88. #endif