~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_array.h

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
 
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 */
19
15
 
20
16
#include <mysys/my_sys.h>
21
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
 
22
80
/* 
23
81
  Array of pointers to Elem that uses memory from MEM_ROOT
24
82
 
30
88
{
31
89
  enum {alloc_increment = 16};
32
90
  Elem **buffer;
33
 
  uint32_t n_elements, max_element;
 
91
  uint n_elements, max_element;
34
92
public:
35
 
  Array(MEM_ROOT *mem_root, uint32_t prealloc=16)
 
93
  Array(MEM_ROOT *mem_root, uint prealloc=16)
36
94
  {
37
95
    buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
38
96
    max_element = buffer? prealloc : 0;