copy constructor & op= for my HashTable

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

#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 - Changed from original post
template <class DataType>
DynamList<DataType>::DynamList(const list<DataType>& a)
: size(a.size), current(a.current), capacity(a.capacity)
{
 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 


Last edited on
This is not a copy constructor but is that what you have problem with?
DynamList(const list<DataType>*);
Ya I used this DynamList(const list<DataType>*) instead of DynamList(const list<DataType>&) b/c it's a dynamic array of list so i assumed it's like passing a dynamic array to a function. if not thats why im learning.

Thanks for any help and your time.

but that is my problem and the op=, I can't get them to work Ive tried a lot of different ways.

Last edited on
Hi there,

I believe I'm in the same class(CMSC-210 at DVC?) as you because this is exactly the assignment I am looking for help on as well. If so, I think we are supposed to use a static array of lists for this assignment, and in that case I believe we won't need to worry about the destructor, copy constructor, or the overloaded operator=.
Last edited on
thats funny. and yes I am in the class. Ya I realized that we need a static array and finished the lab w/o the dynamic array. I was just trying to do this so I could change the size.

Thanks for your help.

on to the next lab!
Haha, no problem. I'm a bit stuck getting the desired index from the hashCode() function inside the struct to use in the getWrappedIndex function. How did you go about this?
The copy constructor receives an object of the same class (by constant reference).
1
2
3
4
5
6
7
8
9
template <class DataType>
DynamList<DataType>::DynamList(const list<DataType>& a) //this is not a copy constructor
: size(a.size), current(a.current), capacity(a.capacity)
{
 aList = new list<DataType>[capacity];

  for (int i = 0; i < capacity; i++)
    aList[i] = a.aList[i]; //there isn't 'aList' in a std::list
}
You could use std::vector and there will be no need to code the copy constructor, assignment operator or destructor.

¿what errors are you getting?
Last edited on
No compile errors it just shuts down durring runtime.

to class mate :
you can see above how I called the function hashCode() in the h file
and my struct looks like this in my driver

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
// struct definition
struct Schedule
{
  // declare strings
  string term, course, instructor;

  // value returning function to get hashCode
  int hashCode() const // by getting the sum of the term & course string
  {
    // declare variables
    int hashCodeInt = 0, i;

    // for loop to traverse the term string and add the ascii value of each char
    for (i = 0; i < term.length(); i++)
      hashCodeInt += term[i]; // add ascii value of char to hashCodeInt 

    // for loop to traverse the course string and add the ascii value of each char
    for (i = 0; i < course.length(); i++)
      hashCodeInt += course[i]; // add ascii value of char to hashCodeInt

    return hashCodeInt; // return value

  } // close hashCode

}; // close Schedule 


hope that helps my fellow class mate
Topic archived. No new replies allowed.