diff options
| author | Yana Blashchishina <[email protected]> | 2024-03-10 14:49:57 -0700 |
|---|---|---|
| committer | Yana Blashchishina <[email protected]> | 2024-03-10 14:49:57 -0700 |
| commit | a6a5a946428e192a873bbc94fa1b3d5e8f7696ad (patch) | |
| tree | c1ceb23d0309e855ebc7b5de5a4042b9de804a66 /Homework 8/MyStructures/ContactList.hpp | |
| parent | errors :( (diff) | |
| download | homework-8-yanablash-a6a5a946428e192a873bbc94fa1b3d5e8f7696ad.tar.xz homework-8-yanablash-a6a5a946428e192a873bbc94fa1b3d5e8f7696ad.zip | |
defining functions
Diffstat (limited to 'Homework 8/MyStructures/ContactList.hpp')
| -rw-r--r-- | Homework 8/MyStructures/ContactList.hpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/Homework 8/MyStructures/ContactList.hpp b/Homework 8/MyStructures/ContactList.hpp new file mode 100644 index 0000000..b8e981f --- /dev/null +++ b/Homework 8/MyStructures/ContactList.hpp @@ -0,0 +1,149 @@ +#ifndef CONTACT_LIST +#define CONTACT_LIST + +#include <algorithm> + +namespace MySStructures +{ + template<class C> + class ContactList + { + ContactList() = default; + ContactList(size_t length); //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 + + 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! + + 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() cosnt; //returns first contact copy + + C& operator[](const int& index); + C operator[](const int& index) const; + explicit operator bool() const; + + bool operator==(const ContactList<C>& rhs) const; + bool Empty() const; + + + + + + 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> + ContactList<C>::ContactList(size_t length) + { + contacts_ = AllocateContactList(length); + } + + 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> + 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_); + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = copy[i]; + } + } + + template <class C> + ContactList<C>& ContactList<C>::operator=(const ContactList& rhs) + { + if (this != &rhs) + { + delete[] contacts_; + contacts_ = nullptr; + + contacts_ = AllocateContactsList(rhs.length_); + size_ = rhs.size_; + + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = copy[i]; + } + } + return *this; + } + + template <class C> + ContactList<C>::ContactList(ContactList&& move) + { + *this = std::move(move); + } + + template <class C> + ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) + { + if (this != &rhs) + { + delete[] contacts_; + contacts_ = nullptr; + + contacts_ = rhs.contacts_; + length_ = rhs.length_; + size_ = rhs.size_; + + rhs.contacts_ = nullptr; + + } + } + template <class C> + C* ContactList<C>::AllocateContactList(const size_t& length) + { + C* storage = nullptr; + + length_ = length; + + storage = new C[length]{}; + + return storage; + } +} + + +#endif
\ No newline at end of file |