The world's most popular open source database
00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #include <my_sys.h> 00018 00019 /* 00020 A typesafe wrapper around DYNAMIC_ARRAY 00021 */ 00022 00023 template <class Elem> class Dynamic_array 00024 { 00025 DYNAMIC_ARRAY array; 00026 public: 00027 Dynamic_array(uint prealloc=16, uint increment=16) 00028 { 00029 my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment); 00030 } 00031 00032 Elem& at(int idx) 00033 { 00034 return *(((Elem*)array.buffer) + idx); 00035 } 00036 00037 Elem *front() 00038 { 00039 return (Elem*)array.buffer; 00040 } 00041 00042 Elem *back() 00043 { 00044 return ((Elem*)array.buffer) + array.elements; 00045 } 00046 00047 bool append(Elem &el) 00048 { 00049 return (insert_dynamic(&array, (gptr)&el)); 00050 } 00051 00052 int elements() 00053 { 00054 return array.elements; 00055 } 00056 00057 ~Dynamic_array() 00058 { 00059 delete_dynamic(&array); 00060 } 00061 00062 typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2); 00063 00064 void sort(CMP_FUNC cmp_func) 00065 { 00066 qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func); 00067 } 00068 }; 00069
1.4.7

