diff options
Diffstat (limited to 'Homework8/MyStructures/ContactList.hpp')
| -rw-r--r-- | Homework8/MyStructures/ContactList.hpp | 162 |
1 files changed, 73 insertions, 89 deletions
diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp index a2d1efa..f84016b 100644 --- a/Homework8/MyStructures/ContactList.hpp +++ b/Homework8/MyStructures/ContactList.hpp @@ -11,48 +11,48 @@ namespace MyStructures { public: ContactList() = default; - ContactList(size_t size); //Creates contact list of this size - ContactList(const C* contacts, const size_t& length); //Makes a copy of another contact list + ContactList(size_t size); + ContactList(const C* contacts, const size_t& length); ~ContactList(); - ContactList(const ContactList& copy); //deep copy constructor - ContactList& operator=(const ContactList& rhs); //deep copy assignment + ContactList(const ContactList& copy); + ContactList& operator=(const ContactList& rhs); - ContactList(ContactList&& move); //reference(move) constructor - ContactList& operator=(ContactList&& rhs); //reference(move) assignment + ContactList(ContactList&& move); + ContactList& operator=(ContactList&& rhs); - void PrintList() const; //Print the Contact List, calling print on each element - size_t Size() const; //Return the size of the contact list + void PrintList() const; + size_t Size() const; - void Append(const C& data); //add contact to the back of the list - void Prepend(const C& data); //add contact as the first item - void RemoveLast(); //remove last - void RemoveFirst(); //remove first - void Extract(const C& data); //delete this data, slash collecting its data ;;return data - void InsertAfter(const C& data, const C& after); //Insert contact data after 'after' - void InsertBefore(const C& data, const C& before); //Insert contact data before 'before' - void Clear(); //deletes all contacts, empties list, nothing left + void Append(const C& data); + void Prepend(const C& data); + void RemoveLast(); + void RemoveFirst(); + void Extract(const C& data); + void InsertAfter(const C& data, const C& after); + void InsertBefore(const C& data, const C& before); + void Clear(); void ContactDeleter(const size_t& size); void DeleteContact(const size_t& choice); - C& Last(); //returning a reference to contact, it gives last contact in list - C Last() const; //const version of this, unchanging because its const and its a copy - C& First(); //returning first contact in list - C First() const; //returning first contact copy + C& Last(); + C Last() const; + C& First(); + C First() const; void ContactDoubler(); - C& operator[](const int& index); //like a getter/ returns contact reference via squarebracket overload - C operator[](const int& index) const; //returns a contact copy via squarebracket overload - explicit operator bool() const; //overloading class name to ask if its true or false; this allows you to use your contactlist in boolean statements - //operator. also explicit tells the compiler not to make it implic + C& operator[](const int& index); + C operator[](const int& index) const; + explicit operator bool() const; - bool operator==(const ContactList<C>& rhs) const; //overloading == e.g if(newcontactlist == oldcontactlist) - bool Empty() const; //is the list empty - bool Full() const; //returns true if allocated + + bool operator==(const ContactList<C>& rhs) const; + bool Empty() const; + bool Full() const; private: C* contacts_{ nullptr }; - size_t length_{ 0 }; //entire length of array - size_t size_{ 0 }; //number of actual elements + size_t length_{ 0 }; + size_t size_{ 0 }; C* AllocateContactList(const size_t& length); }; @@ -63,6 +63,7 @@ namespace MyStructures { contacts_ = AllocateContactList(size); } + template<class C> ContactList<C>::ContactList(const C* contacts, const size_t& length) { @@ -72,27 +73,17 @@ namespace MyStructures contacts_[i] = contacts[i]; } } + template<class C> ContactList<C>::~ContactList() { delete[] contacts_; contacts_ = nullptr; - - } + template<class C> ContactList<C>::ContactList(const ContactList& copy) { - //length_ = copy.length_; - //size_ = copy.size_; - //contacts_ = AllocateContactList(copy.length_); - - //contacts_ = AllocateContactList(length_); - //for (auto i = 0u; i < length_; ++i) - //{ - // contacts_[i] = copy[i]; - //} - delete[] contacts_; contacts_ = nullptr; @@ -100,8 +91,6 @@ namespace MyStructures *this = std::move(temp); size_ = copy.size_; - - } template<class C> ContactList<C>&ContactList<C>::operator=(const ContactList& rhs) @@ -132,8 +121,9 @@ namespace MyStructures template<class C> ContactList<C>::ContactList(ContactList&& move) { - *this = std::move(move); //move shifts addresses + *this = std::move(move); } + template<class C> ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) { @@ -147,7 +137,7 @@ namespace MyStructures length_ = rhs.length_; size_ = rhs.size_; - rhs.contacts_ = nullptr; //Invalidate the other object + rhs.contacts_ = nullptr; } @@ -160,7 +150,7 @@ namespace MyStructures for (auto i = 0u; i < size_; ++i) { - contacts_[i].Print(); + cout << i + 1 << ": "; contacts_[i].Print(); } } @@ -186,12 +176,12 @@ namespace MyStructures { size_t index_locate = 0; - //what if its the beginning + if (size_ == 0) { index_locate = 0; } - //what if the middle + if (size_ > 0 && size_ < length_) { index_locate = size_; @@ -210,27 +200,22 @@ namespace MyStructures contacts_ = nullptr; *this = std::move(temp); - size_+=4; + size_ += 4; return; } - //Add to end of the array contacts_[index_locate] = data; ++size_; } template<class C> void ContactList<C>::Prepend(const C& data) { - //data goes to element zero - if (Empty()) { Append(data); size_++; return; } - //move all elements +1 element - //check for sizing - //check for length + if (!Full()) { for (auto i = 0; i<size_; ++i) @@ -257,14 +242,13 @@ namespace MyStructures } size_++; - } template<class C> void ContactList<C>::RemoveLast() { if (Empty()) return; - //clear the last member + ContactDeleter(size_ - 1); size_--; @@ -275,14 +259,14 @@ namespace MyStructures { if (Empty()) return; - //clear the first member + ContactDeleter(0); - //shift so the program doesn't throw in the print function + for (auto i = 0u; i < size_; i++) { contacts_[i] = contacts_[i + 1]; } - //just to eliminate the last cell from the copy assignment above^^^^ + ContactDeleter(size_ - 1); size_--; @@ -297,7 +281,7 @@ namespace MyStructures { if (contacts_[i] == data) { - tempo.Append(data); //the extraction + tempo.Append(data); //the extraction ContactDeleter(i); for (auto j = size_; j > i; j--) { @@ -308,12 +292,6 @@ namespace MyStructures } size_--; - //find where data is in the contacts - //extract data - //remove data - //decrement size after - //if there are elements above data, shift down after - } template<class C> @@ -322,18 +300,18 @@ namespace MyStructures if (Empty()) Append(data); return; if (Full()) ContactDoubler(); - if (after == contacts_[0]) //checks if after is the first contact + if (after == contacts_[0]) { for (auto i = 1u; i < size_; i++) { - contacts_[i + 1] = contacts_[i]; // shift the contacts up to make space for the incoming data + contacts_[i + 1] = contacts_[i]; } contacts_[1] = data; size_++; return; } - if (after == contacts_[size_ - 1]) //checks if it is after the last contact + if (after == contacts_[size_ - 1]) { contacts_[size_] = data; size_++; @@ -342,7 +320,7 @@ namespace MyStructures else { - for (auto i = size_-1; i>0; i--) //shifts the content after "after" + for (auto i = size_-1; i>0; i--) { if (contacts_[i] != after) { @@ -350,7 +328,7 @@ namespace MyStructures } } - for (auto j = 0u; j < size_; j++) //assigns the data after "after" + for (auto j = 0u; j < size_; j++) { if (contacts_[j] == after) { @@ -408,7 +386,7 @@ namespace MyStructures } template<class C> - inline void ContactList<C>::ContactDeleter(const size_t& size) + void ContactList<C>::ContactDeleter(const size_t& size) { contacts_[size].SetFirstName(nullptr); contacts_[size].SetLastName(nullptr); @@ -420,47 +398,48 @@ namespace MyStructures } template<class C> - inline void ContactList<C>::DeleteContact(const size_t& choice) + void ContactList<C>::DeleteContact(const size_t& choice) { + ContactDeleter(choice); - for (auto i = size_; i >= choice; i--) + for (auto i = choice+1; i <size_; i++) { - contacts_[i+1] = contacts_[i]; + contacts_[i-1] = contacts_[i]; } size_--; } - - template<class C> - inline C& ContactList<C>::Last() + C& ContactList<C>::Last() { return contacts_[size_-1]; } template<class C> - inline C ContactList<C>::Last() const + C ContactList<C>::Last() const { return contacts_[size_-1]; } template<class C> - inline C& ContactList<C>::First() + C& ContactList<C>::First() { return contacts_[0]; } + template<class C> - inline C ContactList<C>::First() const + C ContactList<C>::First() const { return contacts_[0]; } + template<class C> - inline void ContactList<C>::ContactDoubler() + void ContactList<C>::ContactDoubler() { - auto temp = ContactList<C>{ length_ * 2 }; //doubles the length + auto temp = ContactList<C>{ length_ * 2 }; for (auto i = 0u; i <= size_; ++i) { temp.Append(contacts_[i]); @@ -471,8 +450,9 @@ namespace MyStructures *this = std::move(temp); size_--; } + template<class C> - inline C& ContactList<C>::operator[](const int& index) + C& ContactList<C>::operator[](const int& index) { if (index < 0 || index >= static_cast<int>(length_)) { @@ -483,8 +463,9 @@ namespace MyStructures return contacts_[index]; } + template<class C> - inline C ContactList<C>::operator[](const int& index) const + C ContactList<C>::operator[](const int& index) const { if (index < 0 || index >= static_cast<int>(size_)) { @@ -494,14 +475,16 @@ namespace MyStructures return contacts_[index]; } + template<class C> - inline ContactList<C>::operator bool() const + ContactList<C>::operator bool() const { bool var = contacts_ != nullptr; return (var); } + template<class C> - inline bool ContactList<C>::operator==(const ContactList<C>& rhs) const + bool ContactList<C>::operator==(const ContactList<C>& rhs) const { if (&contacts_ == &rhs.contacts_) return true; @@ -517,10 +500,11 @@ namespace MyStructures } template<class C> - inline bool ContactList<C>::Empty() const + bool ContactList<C>::Empty() const { return size_ == 0; } + template<class C> bool ContactList<C>::Full() const { |