diff options
| author | rPatrickWarner <[email protected]> | 2024-03-04 17:11:54 -0800 |
|---|---|---|
| committer | rPatrickWarner <[email protected]> | 2024-03-04 17:11:54 -0800 |
| commit | e5eb6f396d9d7a959c45abf34a095a57b2295a24 (patch) | |
| tree | cd09a47418711839b533b90299f1b5cf11eb8e0f /Homework8/MyStructures/ContactList.hpp | |
| parent | init (diff) | |
| download | homework-8-reecepwarner-e5eb6f396d9d7a959c45abf34a095a57b2295a24.tar.xz homework-8-reecepwarner-e5eb6f396d9d7a959c45abf34a095a57b2295a24.zip | |
changes
Diffstat (limited to 'Homework8/MyStructures/ContactList.hpp')
| -rw-r--r-- | Homework8/MyStructures/ContactList.hpp | 226 |
1 files changed, 196 insertions, 30 deletions
diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp index dd0f4ee..e7959a8 100644 --- a/Homework8/MyStructures/ContactList.hpp +++ b/Homework8/MyStructures/ContactList.hpp @@ -1,6 +1,9 @@ #ifndef CONTACT_LIST_HPP #define CONTACT_LIST_HPP +#include "Contact.hpp" + + namespace MyStructures { template<class C> @@ -8,23 +11,52 @@ 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); //Creates contact list of this size + ContactList(const C* contacts, const size_t& length); //Makes a copy of another contact list ~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 + ContactList(const ContactList& copy); //deep copy constructor + ContactList& operator=(const ContactList& rhs); //deep copy assignment - private: - C* contacts_{ nullptr }; - size_t length_{ 0 }; //entire length of array - size_t size_{ 0 }; //number of actual elements + ContactList(ContactList&& move); //reference(move) constructor + ContactList& operator=(ContactList&& rhs); //reference(move) assignment + + void PrintList() 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 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 + 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 + + + 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& 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 + + bool operator==(const ContactList<C>& rhs) const; //overloading == e.g if(newcontactlist == oldcontactlist) + bool Empty() const; //is the list empty + + private: + C* contacts_{ nullptr }; + size_t length_{ 0 }; //entire length of array + size_t size_{ 0 }; //number of actual elements C* AllocateContactList(const size_t& length); }; + + template<class C> ContactList<C>::ContactList(size_t size) { @@ -33,7 +65,7 @@ namespace MyStructures template<class C> ContactList<C>::ContactList(const C* contacts, const size_t& length) { - contacts_ AllocateContactList(length); + contacts_ = AllocateContactList(length); for (auto i = 0u; i < length; ++i) { contacts_[i] = contacts[i]; @@ -52,7 +84,7 @@ namespace MyStructures size_ = copy.size_; contacts_ = AllocateContactList(copy.length_); - contacts_ AllocateContactList(length); + contacts_ = AllocateContactList(length_); for (auto i = 0u; i < length_; ++i) { contacts_[i] = copy[i]; @@ -62,54 +94,188 @@ namespace MyStructures template<class C> ContactList<C>&ContactList<C>::operator=(const ContactList& rhs) { - if (this != &rhs) //asking if they're the same object + if (this != &rhs) //asking if they're the same object { delete[] contacts_; contacts_ = nullptr; - contacts_ = AllocateContactList(rhs.length_); - size_ = rhs.size_; - contacts_ AllocateContactList(length); - - for (auto i = 0u; i < length_; ++i) + if (rhs.contacts_ != nullptr) { - contacts_[i] = rhs[i]; + + contacts_ = AllocateContactList(rhs.length_); + size_ = rhs.size_; + contacts_ = AllocateContactList(length_); + + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = rhs[i]; + } } + else + contacts_ = nullptr; + } return *this; } template<class C> ContactList<C>::ContactList(ContactList&& move) { - *this = std::move(move); //move shifts addresses + *this = std::move(move); //move shifts addresses } template<class C> ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) { - if(this != &rhs) + if (this != &rhs) { delete[] contacts_; - contacts = nullptr; + contacts_ = nullptr; contacts_ = rhs.contacts_; - + length_ = rhs.length_; size_ = rhs.size_; - rhs.contacts_ = nullptr; //Invalidate the other object + rhs.contacts_ = nullptr; //Invalidate the other object + + } + + return *this; } + template<class C> - C* ContactList<C>::AllocateContactList(const size_t& length) + void ContactList<C>::PrintList() const { - C* storage = nullptr - length_ - length; + for (auto i = 0u; i < length_; ++i) + { + contacts_[i].Print(); + } + } - storage = newC[length]{}; + template<class C> + size_t ContactList<C>::Size() const + { + return size_t(); + } + + template<class C> + C* ContactList<C>::AllocateContactList(const size_t& length) + { + C* storage = nullptr; + length_ = length; + storage = new C[length]{}; return storage; } + template<class C> + void ContactList<C>::Append(const C& data) + { + 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_; + } + + if (size_ >= length_) + { + *this = ContactList(contacts_, length_ * 2); + } + //Add to end of the array + contacts_[index_locate] = data; + size_++; + } + template<class C> + void ContactList<C>::Prepend(const C& data) + { + + } + template<class C> + void ContactList<C>::RemoveLast() + { + } + template<class C> + void ContactList<C>::RemoveFirst() + { + } + template<class C> + void ContactList<C>::Extract(const C& data) + { + } + template<class C> + void ContactList<C>::InsertAfter(const C& data, const C& after) + { + } + template<class C> + void ContactList<C>::InsertBefore(const C& data, const C& before) + { + } + template<class C> + void ContactList<C>::Clear() + { + } + template<class C> + inline C& ContactList<C>::Last() + { + // TODO: insert return statement here + } + template<class C> + inline C ContactList<C>::Last() const + { + return C(); + } + template<class C> + inline C& ContactList<C>::First() + { + // TODO: insert return statement here + } + template<class C> + inline C ContactList<C>::First() const + { + return C(); + } + template<class C> + inline C& ContactList<C>::operator[](const int& index) + { + // TODO: insert return statement here + } + template<class C> + inline C ContactList<C>::operator[](const int& index) const + { + return index; + } + template<class C> + inline ContactList<C>::operator bool() const + { + bool var = contacts_ != nullptr; + return (var); + } + template<class C> + inline bool ContactList<C>::operator==(const ContactList<C>& rhs) const + { + if (contacts_ == rhs.contacts_) return true; + + if (size_ != rhs.size_) return false; + + if (length_ != rhs.length_) return false; + + for (auto i = 0u; i < size_; ++i) + { + if (contacts_[i] != rhs.contacts_[i]) return false; + } + return true; + } + template<class C> + inline bool ContactList<C>::Empty() const + { + return size_ == 0; + } }; -} |