summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/ArrayList.h
blob: c0985c3a7a517655cb58539a203069690bf52b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*  Project FoundationEx
    Destroyer Studios LLC
    Copyright © 1997-2004. All Rights Reserved.

    SUBSYSTEM:    FoundationEx
    FILE:         ArrayList.h
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Simple untyped array list

	The E: That is going to be removed very very soon. For now, it has been aliased to std::vector<DWORD>
	(or std::vector<float> for FloatList). Full conversion to std::vector is still needed.
*/

#ifndef ArrayList_h
#define ArrayList_h

#ifdef WIN32
#include <windows.h>
#include <windowsx.h>
#endif

#include <vector>

// +-------------------------------------------------------------------+

class ArrayList
{
public:
   ArrayList() { array.clear(); }
   ArrayList(const ArrayList& l);
   ~ArrayList()  { }

   DWORD    operator[](int i) const;
   DWORD&   operator[](int i);
   DWORD    at(int i) const;
   DWORD&   at(int i);

   void     append(const ArrayList& list);
   void     append(const DWORD val);
   void     insert(const DWORD val, int index=0);
   void     insertSort(DWORD val);

   DWORD    first()   const      { return *array.begin();       }
   DWORD    last()    const      { return array.back(); }
   void     remove(DWORD val);
   void     removeIndex(int index);

   void     clear();

   int      size()    const      { return array.size();  }
   bool     isEmpty() const      { return array.size() == 0; }

   bool     contains(DWORD val) const;
   int      count(DWORD val)    const;
   int      index(DWORD val)    const;

   void     sort();
   void     shuffle();

   bool     check(int& index) const;

private:
   void     swap(int i, int j);
   void     resize(int newsize);
   

   std::vector<DWORD>   array;

   friend class ArrayListIter;
};

// +-------------------------------------------------------------------+

class ArrayListIter
{
public:
   ArrayListIter()                       : list(0),      step(-1)      { }
   ArrayListIter(const ArrayListIter& i) : list(i.list), step(i.step)  { }
   ArrayListIter(ArrayList& l)           : list(&l),     step(-1)      { }

   int   operator++() { return next() != 0; }
   int   operator--() { return prev() != 0; }

   void        reset() { step = -1;          }
   DWORD       next();
   DWORD       prev();
   DWORD       value();
   void        removeItem();

   void        attach(ArrayList& l);
   ArrayList&  container();
   int         size();
   int         index() { return step; }

private:
   ArrayList*  list;
   int         step;
};


// +-------------------------------------------------------------------+
// +-------------------------------------------------------------------+
// +-------------------------------------------------------------------+

class FloatList : public ArrayList
{
public:
   FloatList() { array.clear(); }
   FloatList(const FloatList& l);
   ~FloatList()  {}

   float    operator[](int i) const;
   float&   operator[](int i);
   float    at(int i) const;
   float&   at(int i);

   void     append(const float val);
   void		append(const FloatList& list);
   void     insert(const float val, int index=0);
   void     insertSort(float val);

   float    first()   const      { return *array.begin();       }
   float    last()    const      { return array.back(); }
   void     remove(float val);

    bool     contains(float val) const;
   int      count(float val)    const;
   int      index(float val)    const;
  
private:

   std::vector<float>   array;

   friend class FloatListIter;
};

// +-------------------------------------------------------------------+

class FloatListIter
{
public:
   FloatListIter()                       : list(0),      step(-1)      { }
   FloatListIter(const FloatListIter& i) : list(i.list), step(i.step)  { }
   FloatListIter(FloatList& l)           : list(&l),     step(-1)      { }

   int   operator++() { return next() != 0; }
   int   operator--() { return prev() != 0; }

   void        reset() { step = -1;          }
   float       next();
   float       prev();
   float       value();
   void        removeItem();

   void        attach(FloatList& l);
   FloatList&  container();
   int         size();
   int         index() { return step; }

private:
   FloatList*  list;
   int         step;
};

#endif ArrayList_h