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.

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