1/* Copyright (C) 2021 Free Software Foundation, Inc.
2   Contributed by Oracle.
3
4   This file is part of GNU Binutils.
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; either version 3, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, 51 Franklin Street - Fifth Floor, Boston,
19   MA 02110-1301, USA.  */
20
21#ifndef _DbeArray_H
22#define _DbeArray_H
23
24template <typename ITEM> class DbeArray
25{
26public:
27
28  DbeArray (long sz)
29  {
30    count = 0;
31    limit = sz;
32    data = new ITEM[limit];
33  };
34
35  virtual
36  ~DbeArray ()
37  {
38    delete[] data;
39  }
40
41  int
42  append (const ITEM &item)
43  {
44    int n = allocate (1);
45    ITEM *p = get (n);
46    *p = item;
47    return n;
48  };
49
50  ITEM *
51  get (long index)
52  {
53    return (index < count && index >= 0) ? data + index : (ITEM *) NULL;
54  };
55
56  int
57  allocate (int cnt)
58  {
59    count += cnt;
60    resize (count);
61    return count - cnt;
62  };
63
64  int
65  size ()
66  {
67    return (int) count;
68  };
69
70  void
71  reset ()
72  {
73    count = 0;
74  };
75
76private:
77
78  void
79  resize (long cnt)
80  {
81    if (limit <= cnt)
82      {
83	limit *= 2;
84	if (limit < cnt)
85	  limit = cnt + 1;
86	ITEM *d = new ITEM[limit];
87	if (count > 0)
88	  memcpy (d, data, sizeof (ITEM) * count);
89	delete[] data;
90	data = d;
91      }
92  };
93
94  ITEM *data;   // Pointer to data vector
95  long count;   // Number of items
96  long limit;   // Array length
97};
98
99#endif /* _DbeArray_H */
100