~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_array.h

  • Committer: Monty Taylor
  • Date: 2008-07-05 16:23:40 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705162340-an09yicpupdtwo2m
Fixed simple signdedness problem.

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
 
 */
19
 
 
20
 
#ifndef DRIZZLED_SQL_ARRAY_H
21
 
#define DRIZZLED_SQL_ARRAY_H
22
 
 
23
 
#include <mysys/my_sys.h>
 
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
 
24
79
 
25
80
/* 
26
81
  Array of pointers to Elem that uses memory from MEM_ROOT
33
88
{
34
89
  enum {alloc_increment = 16};
35
90
  Elem **buffer;
36
 
  uint32_t n_elements, max_element;
 
91
  uint n_elements, max_element;
37
92
public:
38
 
  Array(MEM_ROOT *mem_root, uint32_t prealloc=16)
 
93
  Array(MEM_ROOT *mem_root, uint prealloc=16)
39
94
  {
40
95
    buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
41
96
    max_element = buffer? prealloc : 0;
65
120
      if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
66
121
                                                  sizeof(Elem**))))
67
122
      {
68
 
        return false;
 
123
        return FALSE;
69
124
      }
70
125
      memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
71
126
      buffer= newbuf;
72
127
    }
73
128
    buffer[n_elements++]= el;
74
 
    return false;
 
129
    return FALSE;
75
130
  }
76
131
 
77
132
  int elements()
92
147
  }
93
148
};
94
149
 
95
 
#endif /* DRIZZLED_SQL_ARRAY_H */