You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB

  1. /* Copyright (C) 2003 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. #include <my_sys.h>
  14. /*
  15. A typesafe wrapper around DYNAMIC_ARRAY
  16. */
  17. template <class Elem> class Dynamic_array
  18. {
  19. DYNAMIC_ARRAY array;
  20. public:
  21. Dynamic_array(uint prealloc=16, uint increment=16)
  22. {
  23. my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment);
  24. }
  25. Elem& at(int idx)
  26. {
  27. return *(((Elem*)array.buffer) + idx);
  28. }
  29. Elem *front()
  30. {
  31. return (Elem*)array.buffer;
  32. }
  33. Elem *back()
  34. {
  35. return ((Elem*)array.buffer) + array.elements;
  36. }
  37. bool append(Elem &el)
  38. {
  39. return (insert_dynamic(&array, (gptr)&el));
  40. }
  41. int elements()
  42. {
  43. return array.elements;
  44. }
  45. ~Dynamic_array()
  46. {
  47. delete_dynamic(&array);
  48. }
  49. typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2);
  50. void sort(CMP_FUNC cmp_func)
  51. {
  52. qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func);
  53. }
  54. };