aboutsummaryrefslogtreecommitdiff
path: root/Homework 8/MyStructures/ContactList.hpp
blob: f897b5d55891f2bf8e246728665be43f650b263a (plain) (blame)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#ifndef CONTACT_LIST_HPP
#define CONTACT_LIST_HPP

#include <algorithm>

namespace MyStructures
{
    template<class C>
    class ContactList
    {
    public:
        /**
        * Contains an array of Contacts or othe C class objects
        */
        ContactList() = default;
        ContactList(size_t length);                                                          //Creates contact list of this size
        ContactList(const C* contacts, const size_t& length, const size_t size);             //Makes a copy of another contact list
        ContactList(const C* storage, const size_t& length);
        ~ContactList();

        ContactList(const ContactList& copy);                            //deep copy constructor
        ContactList& operator=(const ContactList& rhs);                  //deep copy assignment

        ContactList(ContactList&& move);                                //reference(move) constructor
        ContactList& operator=(ContactList&& rhs);                      //reference(move) assignment

        void Print() const;                                             //Print the Contact List, calling Print on each element.
        size_t Size() const;                                            //Return the size of the contact list

        void Append(const C& data);                                     //Add contact to back of list
        void Prepend(const C& data);                                    //add contact as first contact
        void RemoveLast();                                              //removes last contact
        void RemoveFirst();                                             //removes first contact
        void Extract(const C& data);                                    //delete this contact: data
        void InsertAfter(const C& data, const C& after);                //insert contact data after 'after' contact
        void InsertBefore(const C& data, const C& before);              //insert contact data before 'before' contact
        void Clear();                                                   //Deletes all contacts, empties the list, nothing left!
        void ContactDelete(const size_t& elementNumber);
     
        C& Last();                                                      //returns last contact reference in list
        C Last() const;                                                 //returns copy of last contact in list
        C& First();                                                     //returns first contact reference in lsit
        C First() const;
                                                                        //returns first contact copy

        C& operator[](const int& index);                                //returns contact reference via [] overload
        C operator[](const int& index) const;                           //returns contact copy via [] overload
        explicit operator bool() const;                                 // overloads ContactList to return to true/false as boolean operator

        bool operator==(const ContactList<C>& rhs) const;               //if (newContact == old Contact)
        bool Empty() const;                                             //is the list empty
        bool Full() const;                                              //returns true if all allocated




    private:
        C* contacts_{ nullptr };
        size_t length_{ 0 }; //entire length of array
        size_t size_{ 0 }; //number of actual numbers

        C* AllocateContactList(const size_t& length);
    };


    template<class C>
    void ContactList<C>::ContactDelete(const size_t& elementNumber)
    {
        size_t index = elementNumber;
        contacts_[index].Delete();
    }

    template <class C>
    ContactList<C>::ContactList(const C* contacts, const size_t& length)
    {
        contacts_ = AllocateContactList(length);
        for (auto i = 0u; i < length_; ++i)
        {
            contacts_[i] = contacts[i];
        }
    }

   
    template<class C>
    inline ContactList<C>::ContactList(size_t length)
    {
    }

    template <class C>
    ContactList<C>::ContactList(const C* contacts, const size_t& length, const size_t size)
    {
        contacts_ = AllocateContactList(length);

        for (auto i = 0u; i < length_; ++i)
        {
            contacts_[i] = contacts[i];
        }
    }

    template <class C>
    ContactList<C>::~ContactList()
    {
        delete[] contacts_;
        contacts_ = nullptr;
    }

    template <class C>
    ContactList<C>::ContactList(const ContactList& copy)
    {
        delete[] contacts_;
        contacts_ = nullptr;

        auto temp = ContactList<C>{ copy.contacts_, copy.length_ };

        *this = std::move(temp);

        size_ = copy.size_;
      
    }

    template <class C>
    ContactList<C>& ContactList<C>::operator=(const ContactList& rhs)
    {
        if (this != &rhs)
        {
            delete[] contacts_;

            contacts_ = nullptr;

            auto temp = ContactList<C>{ rhs.contacts_, rhs.length_ };

            *this = std::move(temp);

            size_ = rhs.size_;
        }
        return *this;
    }

    template <class C>
    ContactList<C>::ContactList(ContactList&& move)
    {
        *this = std::move(move);
    }

    template <class C>
    ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) noexcept
    {
        if (this != &rhs)
        {
            delete[] contacts_;
            contacts_ = nullptr;

            contacts_ = rhs.contacts_;
            length_ = rhs.length_;
            size_ = rhs.size_;

            rhs.contacts_ = nullptr;

        }
        return *this;
    }



    template <class C>
    void ContactList<C>::Print() const
    {
        for (auto i = 0u; i < length_; ++i)
        {
            contacts_[i].Print();
        }
    }

    template <class C>
    size_t ContactList<C>::Size() const
    {
        return size_;
    }

    template <class C>
    void ContactList<C>::Append(const C& data)
    {

        index = size_;

        contacts_[index] = data;
        size_++;
        return;


        // whatifs;
           //whatif: end of the array is the beginning 
           //whatif: in the body of the array
        if (size_ >= length_)
        {
            index = size_;
        }

        if (size_ == 0)
        {
            contacts_[index] = data;
            size_++;
            return;
        }
        //whatif: end of the array
        //length_ == 10;
        // size_ ==10
        // contacts_[10] is out of bounds!
       //whatif: array is full

        if (size_ >= length_)
        {
            //increase size of array
            ContactList newContacts(contacts_, length_ * 2);
            *this = newContacts
        }


        // Add to end of the array
        contacts_[index] = data;
        size_++;
        return;
    }

    template <class C>
    void ContactList<C>::Prepend(const C& data)
    {
        if (Empty())
        {
            if(contacts_ == nullptr)
            {

            }
            Append(data);
            return;
        }


        if (!Full())
        {
            for (int i = static_cast<int> (size_)-1; i >= 0; i--)
            {
                contacts_[i + 1] = contacts_[i];
            }
            contacts_[0] = data;
            return;
       }

        auto temp = ContactList<C>(length_);
        temp.Append(data);
        for (auto i = 0; i < size_ ++i)
        {
            temp.Append(contacts_[i]);
        }
        delete[] contacts_;
        contacts_ = nullptr;

        *this = std::move(temp);


    }

    template <class C>
    void ContactList<C>::RemoveLast()
    {
        if (Empty()) return;

        ContactDelete(size_ - 1);
        size_--;
    }

    template <class C>
    void ContactList<C>::RemoveFirst()
    {
        if (Empty()) return;

        ContactDelete(0);
        size_--;
    }

    template <class C>
    void ContactList<C>::Extract(const C& data)
    {
        for (size_t i = 0; i < size_; ++i;)
        {
            if (contacts_[i] == data) 
            {
                for (size_t j = i; j < size_ - 1; ++j)
                {

                }
                ContactDelete(size_t - 1);
                size_--;
                return;
            }
        }
    }

    template<class C>
    inline void ContactList<C>::InsertAfter(const C& data, const C& after)
    {
        for (size_t i = 0; i < size_; ++i) {
            if (contacts_[i] == after) {
                if (size_ >= length_) {
                    
                    ContactList newContacts(length_ * 2);
                    newContacts.size_ = size_;
                    for (size_t j = 0; j < i + 1; ++j) {
                        newContacts.contacts_[j] = contacts_[j];
                    }
                    newContacts.contacts_[i + 1] = data;
                    for (size_t j = i + 2; j < newContacts.size_; ++j) {
                        newContacts.contacts_[j] = contacts_[j - 1];
                    }
                    *this = std::move(newContacts);
                }
                else {
                    for (size_t j = size_; j > i + 1; --j) {
                        contacts_[j] = contacts_[j - 1];
                    }
                    contacts_[i + 1] = data;
                    size_++;
                }
                return;
            }
        }
    }

    template<class C>
    inline void ContactList<C>::InsertBefore(const C& data, const C& before)
    {
        for (size_t i = 0; i < size_; ++i) {
            if (contacts_[i] == before) {
                if (size_ >= length_) {
                  
                    ContactList newContacts(length_ * 2);
                    newContacts.size_ = size_;
                    for (size_t j = 0; j < i; ++j) {
                        newContacts.contacts_[j] = contacts_[j];
                    }
                    newContacts.contacts_[i] = data;
                    for (size_t j = i + 1; j < newContacts.size_; ++j) {
                        newContacts.contacts_[j] = contacts_[j - 1];
                    }
                    *this = std::move(newContacts);
                }
                else {
                    for (size_t j = size_; j > i; --j) {
                        contacts_[j] = contacts_[j - 1];
                    }
                    contacts_[i] = data;
                    size_++;
                }
                return;
            }
        }
    }

    template <class C>
    void ContactList<C>::Clear()
    {
        size_ = 0;
        length_ = 0;
        delete[] contacts_;
        contacts_ = nullptr;
    }

    template <class C>
    C& ContactList<C>::Last()
    {
        return contacts_[size_];
    }

    template <class C>
    C ContactList<C>::Last() const
    {
        return contacts_[size_ - 1];
    }

    template <class C>
    C& ContactList<C>::First()
    {
        return contacts_[size_ - 1];
    }

    template <class C>
    C ContactList<C>::First() const
    {
        return contacts_[0];
    }

    template<class C>
    C& ContactList<C>::operator[](const int& index)
    {
        if (index < 0 || index >= static_cast<int>(length_))
        {
            Contact emptyContact;
            std::cerr << "The index exceeds the array length" << endl;
            return emptyContact;
        }
       
        return contacts_[index];
    }

    template <class C>
    C ContactList<C>::operator[](const int& index) const
    {
        if (index < 0 || index >= static_cast<int>(size_))
        {
            cerr << "The index exceeds the array length" << endl;
            return NULL;;
        }
        return contacts_[index];
    }

    template <class C>
    ContactList<C>::operator bool() const
    {
        bool var = contacts_ != nullptr;
        return (var); //if(addressBook)
    }

    template <class C>
    bool ContactList<C>::operator==(const ContactList<C>& rhs) const
    {
        
        //Performance
            //shortcut easy ways of knowing the same
        if (contacts_ == rhs.contacts_)return true;
        if (size_ != rhs.size_)return false;
        if (length_ != rhs.length_)return false;

        //if those fields are equivalent, then check every value
        for (auto i = 0u; i < size_; ++i)
        {
            if (contacts_[i] != rhs.contacts_[i]) return false;
        }
        return true;
    }

    template <class C>
    bool ContactList<C>::Empty() const
    {
        return size_ == 0;
    }

    template <class C>
    bool ContactList<C>::Full() const
    {
        if (size_ < length_) return false;

        return true;
    }

    template <class C>
    C* ContactList<C>::AllocateContactList(const size_t& length)
    {
        C* storage = nullptr;

        length_ = length;

        storage = new C[length]{};

        return storage;
    }

};

#endif