algorithms.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*!
  2. Temelia - generic algorithms interface.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu, Bercaru Cristian
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef ALGORITHMS_H_
  18. #define ALGORITHMS_H_
  19. #include "platform.h"
  20. /*!
  21. * @brief For an indexable collection X = {x[i]} calls iterate_function
  22. * for-each key x[i]. The initial collections may be modified only in iterate_function.
  23. * O(n) complexity
  24. *
  25. * @param Pointer to indexable collection
  26. * @param Collection's size
  27. * @param Iterate function
  28. * @param Context
  29. */
  30. DECLSPEC void map(void *data, int size, void *key_at(void *data, int index),
  31. void iterate_function(void *key, void *context), void *context);
  32. /*!
  33. * @brief For an indexable collection X = {x[i]} calls filter_function
  34. * for-each key in context0. If filter function returns true then applies
  35. * then_function to * x[i] in context1; else applies else_function to x[i] in
  36. * context2.
  37. * O(n) complexity
  38. *
  39. * @param Pointer to indexable collection
  40. * @param Collection's size
  41. * @param Iterate function
  42. * @param Elements indexing function
  43. * @param Then function
  44. * @param Else function
  45. * @param Context for filter function
  46. * @param Context for then function
  47. * @param Context for else function
  48. */
  49. DECLSPEC void filter(void *data, int size, void *key_at(void *data, int index),
  50. int filter_function(void *key, void *context), void then_function(
  51. void *key, void *context), void else_function(void *key,
  52. void *context), void *filter_context, void *then_context,
  53. void *else_context);
  54. /*!
  55. * @brief Searches key in a sorted collection using binary search algorithm and
  56. * returns the index if key is found and -1 otherwise.
  57. * O(log(n)) complexity
  58. *
  59. * @param Pointer to indexable collection
  60. * @param Collection's size
  61. * @param Key to find
  62. * @param Index function
  63. * @param Comparison function
  64. */
  65. DECLSPEC int
  66. binary_search(void *data, int size, void *key, void *key_at(
  67. void *data, int index), int compare(void *x, void *y,
  68. void *context), void *context);
  69. #endif /* ALGORITHMS_H_ */