Debian Codegraph @

xo/larray.h

raw
1 /* larray.h
2 $Id: larray.h,v 1.2 1997/10/18 04:57:34 elf Exp $
3
4 written by Marc Singer
5 9 October 1997
6
7 This file is part of the project XO. See the file README for
8 more information.
9
10 Copyright (C) 1997 Marc Singer
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 in a file called COPYING along with this program; if not, write to
24 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 02139, USA.
26
27 */
28
29 #ifndef __LARRAY_H__
30 #define __LARRAY_H__
31
32 // ----- Inclusions
33
34 #include <memory.h>
35
36 // ----- Constants
37
38 // ----- Typedefs
39
40 // ----- Classes
41
42 class LArray
43 {
44 protected:
45 int m_cbEach; // Size of each item is bytes
46 int m_cStep; // Allocation increment
47 long m_cItems; // Number of filled items in array
48 long m_cItemMax; // Size of allocation
49 void* m_rgItems; // Item array
50
51 void* _at (long iItem) const {
52 return (void*) ((char*) m_rgItems + iItem*m_cbEach); }
53 int _at (const void* pv) const {
54 return (((const char*) pv) - ((const char*) m_rgItems))/m_cbEach; }
55
56 public:
57 LArray () {
58 zero (); init (sizeof (void*), 4); }
59 LArray (int cbEach) {
60 zero (); init (cbEach); }
61 LArray (int cbEach, int cStep) {
62 zero (); init (cbEach, cStep); }
63 ~LArray () { release (); }
64
65 void init (int cbEach, int cStep = 4);
66 void release (void);
67 void zero (void) { memset (this, 0, sizeof (*this)); }
68
69 bool add (const void* pv);
70 void* add_new (void); // Add and return pointer to it
71 bool alloc (long cItems);
72 bool alloc_to (long cItems);
73 void* at (long iItem) const;
74 long at (const void* pv) const;
75 void attach (void*, long cb);
76 void* base (void) const {
77 return m_rgItems; }
78 int count_each (void) {
79 return m_cbEach; }
80 bool contains (const void* pv);
81 void* enumerate (const void* pv);
82 void* enumerate_reverse (const void* pv);
83 bool get (long iItem, void* pv);
84 long get_count () const { return m_cItems; }
85 void* insert (const void*);
86 void* reference (long iItem);
87 void remove (const void* pv);
88 void remove (long iItem, long cItems = 1);
89 bool set (long iItem, const void* pv);
90 void truncate (long cItems);
91 };
92
93 // ----- Macros
94
95 // ----- Globals / Externals
96
97 // ----- Prototypes
98
99 // ----- Inline
100
101 #endif // __LARRAY_H__