File: apvector.cpp

package info (click to toggle)
wing 0.7-33
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,744 kB
  • sloc: cpp: 2,526; sh: 65; makefile: 16
file content (178 lines) | stat: -rw-r--r-- 4,777 bytes parent folder | download | duplicates (4)
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
171
172
173
174
175
176
177
178
//****************************************************************
//
//  apvector class  implementation -- continuation of apvector.h
//
//****************************************************************

#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include "apvector.h"

//****************************************************************
//********           Constructors / destructor         ***********
//****************************************************************

template <class itemType>
apvector<itemType>::apvector()

// Constructor: builds an empty vector of size 0.

    : mSize(0), mList(0)      
{}

//****************************************************************

template <class itemType>
apvector<itemType>::apvector(int size)

// Constructor: builds a vector of a given size.
// size must be >= 0.

   : mSize(size), mList(new itemType[size])
{}

//****************************************************************

template <class itemType>
apvector<itemType>::apvector(int size, const itemType &fillValue)

// Constructor: builds a vector of a given size with all elements
//   set to fillValue.
// size must be >= 0.

    : mSize(size), mList(new itemType[size])
{
    int k;

    for (k = 0;   k < size;   k++)
        mList[k] = fillValue;
}

//****************************************************************

template <class itemType>
apvector<itemType>::apvector(const apvector<itemType> &v)

// Copy constructor: builds a vector equal to a given vector v.

    : mSize(v.length()), mList(new itemType[mSize])
{
    int k;

    for (k = 0;   k < mSize;   k++)
        mList[k] = v.mList[k];
}

//****************************************************************

template <class itemType>
apvector<itemType>::~apvector ()

// Destructor.

{
    delete [] mList;
}

//****************************************************************
//********                 Assignment                  ***********
//****************************************************************

template <class itemType>
const apvector<itemType> &
apvector<itemType>::operator= (const apvector<itemType> &v)

// Assignment operator.  Resizes the vector and copies elements
//   from v to the vector.

{
    if (this != &v) {          // Avoid assignment to itself
        delete [] mList;
        mSize = v.mSize;
        mList = new itemType[mSize];

        int k;

        for (k = 0;   k < mSize;   k++)
            mList[k] = v.mList[k];
    }
    return *this;              // Return a reference to itself to support
                               //   chained assignments a = b = c
}

//****************************************************************
//********                  Subscripting               ***********
//****************************************************************

template <class itemType>
itemType &apvector<itemType>::operator[] (int k)

// Overloaded subscript operator.  Verifies that 0 <= k <= mSize - 1.

{
    if (k < 0 || k >= mSize) {
        std::cerr << "Illegal vector index: " << k << " max index = "
                  << (mSize-1) << std::endl;
        abort();
    }
    return mList[k]; 
}

//****************************************************************

template <class itemType>
const itemType &apvector<itemType>::operator[] (int k) const

// Overloaded subscript operator for const (read-only) vectors.
// Verifies that 0 <= k <= mSize - 1.

{
    if (k < 0 || k >= mSize) {
        std::cerr << "Illegal vector index: " << k << " max index = "
                  << (mSize-1) << std::endl;
        abort();
    }
    return mList[k]; 
}

//****************************************************************
//********           length() and resize(...)          ***********
//****************************************************************

template <class itemType>
int apvector<itemType>::length() const

// Returns the current size (number of elements) in the vector.

{
    return mSize; 
}

template <class itemType>
void apvector<itemType>::resize(int newSize)

// Resizes the vector to a specified size newSize.
//   The tail of the vector may be chopped off and the values lost
//   if newSize is less than the current size.
//   Other elements remain unchanged.

{
    int k;
    int numToCopy = newSize;

    if (numToCopy > mSize)
          numToCopy = mSize;

    itemType *newList = new itemType[newSize];
    for (k = 0;   k < numToCopy;   k++)
        newList[k] = mList[k];

    delete [] mList;
    mSize = newSize;
    mList = newList;
}

//****************************************************************