1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef array_collection_h
26 #define array_collection_h
28 ////////////////////////////////////////////////////////////////////////////
29 // ArrayCollection<T,A> is a template for implementing concrete collection
30 // classes that behave like an array.
31 // Suitable concrete classes include
32 // FixArray<T> in <A/contain/fixarray.h>
33 // Array<T> in <A/contain/array.h>
34 // VarArray<T> in <A/contain/vararray.h>
35 // Indexable<T> in <A/contain/idxable.h>
36 ////////////////////////////////////////////////////////////////////////////
38 #include <AD/contain/seqcol.h>
40 ////////////////////////////////////////////////////////////////////////////
41 // Template ArrayCollection<T,A> takes concrete implementation of an
42 // array type and makes an instantiable collection class.
43 ////////////////////////////////////////////////////////////////////////////
44 template <class T
, class A
>
45 class ArrayCollection
: public SequenceableCollection
<T
> {
47 A array
; // implementation
50 //////////////////////////////////////////////////////////////
52 //////////////////////////////////////////////////////////////
53 typedef SequenceableCollection
<T
> Super
;
54 typedef typename
Super::Element Element
;
56 //////////////////////////////////////////////////////////////
57 // Constructor and destructor
58 //////////////////////////////////////////////////////////////
59 ArrayCollection(int size
= Collection_default_size
) : array(size
) {}
60 ArrayCollection(const Collection
<T
>& C
) : array(C
.size()) { *this = C
; }
61 ArrayCollection(const ArrayCollection
& C
) : array(C
.size()) { *this = C
; }
64 //////////////////////////////////////////////////////////////
66 //////////////////////////////////////////////////////////////
67 void operator = (const Collection
<T
>&);
69 //////////////////////////////////////////////////////////////
71 //////////////////////////////////////////////////////////////
72 inline int size() const { return array
.length(); }
73 inline int capacity() const { return array
.capacity(); }
74 // Bool is_empty() const; // inherited
75 // Bool is_full() const; // inherited
76 // Bool contains(const T&) const; // inherited
77 // Ix lookup(const T&) const; // inherited
79 //////////////////////////////////////////////////////////////
81 // Notice that methods clear(), insert() and remove() are
82 // override since they do not make sense in this context
83 //////////////////////////////////////////////////////////////
84 void clear () { should_not_implement("clear"); }
85 inline const T
& operator [] (int i
) const { return array
[i
]; }
86 inline T
& operator [] (int i
) { return array
[i
]; }
87 Ix
insert (const T
&) { should_not_implement("insert"); return 0; }
88 Bool
remove (const T
&) { should_not_implement("remove"); return false; }
89 Bool
del (Ix
) { should_not_implement("del"); return false; }
91 //////////////////////////////////////////////////////////////
93 //////////////////////////////////////////////////////////////
94 inline Ix
first() const { return array
.first(); }
95 inline Ix
next(Ix i
) const { return array
.next(i
); }
96 inline const T
& operator () (Ix i
) const { return array(i
); }
97 inline T
& operator () (Ix i
) { return array(i
); }
99 //////////////////////////////////////////////////////////////
101 //////////////////////////////////////////////////////////////
102 const char * myName() const { return "ArrayCollection"; }
105 ////////////////////////////////////////////////////////////////////////
106 // The default assignment just iterates over all the elements
107 // of a collection and inserts them into the array
108 ////////////////////////////////////////////////////////////////////////
109 template <class T
, class A
>
110 void ArrayCollection
<T
, A
>::operator = (const Collection
<T
>& C
)
112 for (i
= C
.first(), j
= 0; i
; i
= C
.next(i
), j
++) (*this)[j
] = C(i
);