~stewart/drizzle/embedded-innodb-create-select-transaction-arrgh

« back to all changes in this revision

Viewing changes to sql/sql_array.h

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <my_sys.h>
 
17
 
 
18
/*
 
19
  A typesafe wrapper around DYNAMIC_ARRAY
 
20
*/
 
21
 
 
22
template <class Elem> class Dynamic_array
 
23
{
 
24
  DYNAMIC_ARRAY  array;
 
25
public:
 
26
  Dynamic_array(uint prealloc=16, uint increment=16)
 
27
  {
 
28
    init(prealloc, increment);
 
29
  }
 
30
 
 
31
  void init(uint prealloc=16, uint increment=16)
 
32
  {
 
33
    my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment);
 
34
  }
 
35
 
 
36
  Elem& at(int idx)
 
37
  {
 
38
    return *(((Elem*)array.buffer) + idx);
 
39
  }
 
40
 
 
41
  Elem *front()
 
42
  {
 
43
    return (Elem*)array.buffer;
 
44
  }
 
45
 
 
46
  Elem *back()
 
47
  {
 
48
    return ((Elem*)array.buffer) + array.elements;
 
49
  }
 
50
 
 
51
  bool append(Elem &el)
 
52
  {
 
53
    return (insert_dynamic(&array, (uchar*)&el));
 
54
  }
 
55
 
 
56
  int elements()
 
57
  {
 
58
    return array.elements;
 
59
  }
 
60
 
 
61
  void clear()
 
62
  {
 
63
    array.elements= 0;
 
64
  }
 
65
 
 
66
  ~Dynamic_array()
 
67
  {
 
68
    delete_dynamic(&array);
 
69
  }
 
70
 
 
71
  typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2);
 
72
 
 
73
  void sort(CMP_FUNC cmp_func)
 
74
  {
 
75
    my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func);
 
76
  }
 
77
};
 
78
 
 
79
 
 
80
/* 
 
81
  Array of pointers to Elem that uses memory from MEM_ROOT
 
82
 
 
83
  MEM_ROOT has no realloc() so this is supposed to be used for cases when
 
84
  reallocations are rare.
 
85
*/
 
86
 
 
87
template <class Elem> class Array
 
88
{
 
89
  enum {alloc_increment = 16};
 
90
  Elem **buffer;
 
91
  uint n_elements, max_element;
 
92
public:
 
93
  Array(MEM_ROOT *mem_root, uint prealloc=16)
 
94
  {
 
95
    buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
 
96
    max_element = buffer? prealloc : 0;
 
97
    n_elements= 0;
 
98
  }
 
99
 
 
100
  Elem& at(int idx)
 
101
  {
 
102
    return *(((Elem*)buffer) + idx);
 
103
  }
 
104
 
 
105
  Elem **front()
 
106
  {
 
107
    return buffer;
 
108
  }
 
109
 
 
110
  Elem **back()
 
111
  {
 
112
    return buffer + n_elements;
 
113
  }
 
114
 
 
115
  bool append(MEM_ROOT *mem_root, Elem *el)
 
116
  {
 
117
    if (n_elements == max_element)
 
118
    {
 
119
      Elem **newbuf;
 
120
      if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
 
121
                                                  sizeof(Elem**))))
 
122
      {
 
123
        return FALSE;
 
124
      }
 
125
      memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
 
126
      buffer= newbuf;
 
127
    }
 
128
    buffer[n_elements++]= el;
 
129
    return FALSE;
 
130
  }
 
131
 
 
132
  int elements()
 
133
  {
 
134
    return n_elements;
 
135
  }
 
136
 
 
137
  void clear()
 
138
  {
 
139
    n_elements= 0;
 
140
  }
 
141
 
 
142
  typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
 
143
 
 
144
  void sort(CMP_FUNC cmp_func)
 
145
  {
 
146
    my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);
 
147
  }
 
148
};
 
149