copy constructor & op= for my HashTable

Dec 3, 2011 at 1:32am
I made a adjustable HashTable with an array of STL list(s). I can get every thing to work except the copy constructor and the operator=. It will compile it eerrors out during run time

If anyone can help me or give me some tips I would be greatful.

Thanks for your time in advance.

DynamList.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

#include <algorithm>
#include <assert.h> 
#include <list>

#ifndef DynamList_h
#define DynamList_h


#define HASHTABLE_CAPACITY 1009

template <class DataType>
class DynamList
{
public:
  DynamList();
  DynamList(const list<DataType>*);
  ~DynamList();
  list<DataType>* operator=(const list<DataType>*);
  void insert(const DataType&);
  bool first(DataType& getData);
  bool getNext(DataType&);
  bool retrieve(DataType&);
  bool remove(DataType&);
  bool replace(DataType&);
  int getSize() const; // getter
  double getLoadFactor() const; // getter

private:
  list<DataType>* aList; 
  typename list<DataType>::iterator it;
  typename list<DataType>::const_iterator itRe; // const_ for retrieve only!
  void reSize();
  bool isPrime(int);
  void setWrappedIndex(int); // setter
  int wrappedIndex;
  int size, current;
  int capacity;

};
template <class DataType>
DynamList<DataType>::DynamList()
: size(0), wrappedIndex(0), current(-1), capacity(HASHTABLE_CAPACITY), aList(new list<DataType>[HASHTABLE_CAPACITY])
{ }

// copy constructor, need to fix
template <class DataType>
DynamList<DataType>::DynamList(const list<DataType>* a)
: size(a.size), current(a.current), capacity(a.capacity)
{
  list<DataType>* newList = new list<DataType>[capacity];

  DataType data;

  first(data);

      do
      { 
       
        setWrappedIndex(data.hashCode());
        newList[wrappedIndex].push_back(data); 
       

      }while(getNext(data));

    // delete array  
    delete [] aList;
    
  aList = newList; // put elements back into the re-sized array   
  current = wrappedIndex;

  /*
 // aList = a.aList;
 
  aList = 0;

  //if(capacity > 0)
    aList = new list<DataType>[capacity];

  for (int i = 0; i < capacity; i++)
    aList[i] = a.aList[i];
    */
}

// operator= need to fix
// This was the last stuff tried but way off from my original
/*
template <class DataType>
list<DataType>* DynamList<DataType>::operator=(const list<DataType>* a)
{


  
  if (this != &a) // if true
  {
    size = a.size; // set length
    current = a.current; // set current
    capacity = a.capacity;
    
    
    list<DataType>* newList = new list<DataType>[capacity]


  
 // } // close if

  //return *this; // return *this

}*/

template <class DataType>
DynamList<DataType>::~DynamList()
{

  delete [] aList;

}

template <class DataType>
 void DynamList<DataType>::insert(const DataType& newData)
 {
  setWrappedIndex(newData.hashCode());
  
  it = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), newData); 
  
  if (it != aList[wrappedIndex].end()) *it = newData; // replace 

  else 
  {
    aList[wrappedIndex].push_back(newData); 
    current = wrappedIndex;
    size++;

  } // close else

  if (getLoadFactor() > .8)
      reSize();
  
}

 template <class DataType>
void DynamList<DataType>::setWrappedIndex(int DI)
{
  if (DI > capacity)
  {
    wrappedIndex = DI % capacity;

    if (wrappedIndex > capacity)
    {
      while (wrappedIndex > capacity)
        wrappedIndex = wrappedIndex % capacity;

    } // close if

  } // close if

  else if (DI < 0)
  {
    while (DI < 0 && DI < capacity)
      DI += capacity;
   

    wrappedIndex = DI;

  } // close else-if

  else
    wrappedIndex = DI;

} // close setWrappedIndex

template <class DataType>
bool DynamList<DataType>::isPrime(int aNum)
{
  int max = static_cast <int> (aNum); /* Cast it to remove warnings */
        
  if (aNum <= 0 || aNum == 1 || (aNum % 2 == 0 && aNum != 2)) return false;
        
  for (int i = 3; i < max; i += 2)
    if (aNum % i == 0)
      return false;
      
  return true;

}

template <class DataType>
void DynamList<DataType>::reSize()
{
  int i = 0;

   while(!isPrime((capacity * 2) + i))
     i++;

  int newCapacity = (capacity * 2) + i;
  int oldCapacity = capacity;
  DataType data;

  list<DataType>* newList = new list<DataType>[newCapacity];
  
      first(data);

      do
      { 
        capacity = newCapacity;
        setWrappedIndex(data.hashCode());
        newList[wrappedIndex].push_back(data); 
        capacity = oldCapacity;

      }while(getNext(data));

    // delete array  
    delete [] aList;
    
  aList = newList; // put elements back into the re-sized array
  capacity = newCapacity;   
  current = wrappedIndex;  

}

template <class DataType>
bool DynamList<DataType>::first(DataType& getData)
{
  assert(current >= -1 && current < capacity);

  for (current = 0; current < capacity; current++)
  {
    if (!aList[current].empty())
    {
      it = aList[current].begin();
      break;
    } 
  }

  if (current == capacity) current = -1;

  if (current == -1) return false;

  getData = *it;

  return true;
}


template <class DataType>
bool DynamList<DataType>::getNext(DataType& getData)
{
  assert(current >= -1 && current < capacity);

  if (current == -1) return false;

  assert(!aList[current].empty());

  getData = *it++;

  if (it == aList[current].end())
  {
    for (current = current + 1; current < capacity; current++)
    {
      if (!aList[current].empty())
      {
        it = aList[current].begin();
        break;
     
      } 
    } 
  }
  
  if (current == capacity) current = -1;
  
  return current >= 0;
}


template <class DataType>
double DynamList<DataType>::getLoadFactor() const
{
  return double(size) / capacity;

}


template <class DataType>
int DynamList<DataType>::getSize() const
{
  return size;

} // close get size 


template <class DataType>
bool DynamList<DataType>::retrieve(DataType& getData)
{
  setWrappedIndex(getData.hashCode());

  itRe = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), getData); 

  if (itRe == aList[wrappedIndex].end()) return false; 
 
  getData = *itRe; 
  current = wrappedIndex;

  return true;

}

template <class DataType>
bool DynamList<DataType>::remove(DataType& data)
{
  setWrappedIndex(data.hashCode());

  it = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), data); 

  if (it == aList[wrappedIndex].end()) return false; 
 
  aList[wrappedIndex].erase(it);  
  size--;
  current = wrappedIndex;

  return true;

} // close remove

template <class DataType>
bool DynamList<DataType>::replace(DataType& data)
{
  setWrappedIndex(data.hashCode());

  it = find(aList[wrappedIndex].begin(), aList[wrappedIndex].end(), data); 

  if (it == aList[wrappedIndex].end()) return false; 

  *it = data;
  current = wrappedIndex;

  return true;

}

#endif 


Dec 3, 2011 at 12:56pm
Ok, Im not gonna read through all that code but i will show you the conventions.

Examine this:

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
class Example { 
public:
	 Example() {
               z = NULL;   // set all pointers to NULL in every constructor!!!
        }
      
        Example(const Example& ex) {  
		z = NULL;		
		CopyIntoThis(ex);      
	}		

	Example& operator = (const Example& ex) {
		if (this != &ex)  
			CopyIntoThis(ex);
		
		return *this;
	}
	
	
private:
	CopyIntoThis(const Example& ex) {
		if (ex.z) {
	           delete z;
		   z = new int;
	           z = ex.z;
                }
	}

        int* z;
};
		



Edit- don't you want list<DataType>* newList = new list<DataType>[capacity];

to be

aList = new list<DataType>[capacity]; ? Why use a temp in a copy constructor? Just check properly.

I would set aList to NULL, then check if capacity is greater than 0 before I copy anything.


Last edited on Dec 3, 2011 at 1:26pm
Dec 3, 2011 at 1:27pm
Your copy constructor has the wrong parameter type to be a copy constructor. As IceThatJaw's example shows, the parameter type of a copy constructor is the same class as what which the method is being defined for. So I'de expect your class declaration to start off:

1
2
3
4
5
6
7
8
9
10
template <class DataType>
class DynamList
{
public:
  DynamList();
  DynamList(const DynamList&); // copy construtor
  ~DynamList();
  DynamList& operator=(const DynamList&); // assignent operator

  ...


It's ok to have a constuctor which takes a pointer to a list, but it's not a copy constructor.

And I'm uneasy about

list<DataType>* operator=(const list<DataType>*);

as you're setting a non-pointer type to equal a pointer type. If you need this functionality, I would consider using something along the lines of a setList(const list<DataType>*) method -- doing a copy -- or even attachList(list<DataType>*) -- which does what it says.

Andy
Last edited on Dec 3, 2011 at 1:29pm
Dec 4, 2011 at 12:12am
Thanks for all the tips and help I will try these and see if I can get things to work properly. I used the * instead of the & b/c I was thinking it was like passing a dynamic array to a function. I know normally the & is used but that didn't work for me so I was just trying anything I know which isn't a lot Im a advance beginner I guess you could say.

Thanks again for your time.
Last edited on Dec 4, 2011 at 12:16am
Topic archived. No new replies allowed.