#pragma once /* card.h : This is the header file for DataTypes.cpp author: Edward Middleton-Smith project: Shared technology: Libraries feature: Data Types header file */ #ifndef DATA_TYPES_H #define DATA_TYPES_H // IMPORTS // internal // #include // external #include #include #include #include #include #include #include #include #include #include #include // TEMPLATE OBJECTS template concept Stringable = requires(W w) { { std::to_string(w) } -> std::convertible_to; } || requires(W w) { { w } -> std::convertible_to; } || requires(W w) { { w.ToString() } -> std::convertible_to; }; template concept HasToString = requires(W w) { { w.ToString() } -> std::convertible_to; }; template class FastList { public: FastList() {}; void PushBack(T item) { this->aList.push_back(item); this->index.push_back(std::prev(this->aList.end())); } // T* GetItem(int index) // std::list::iterator GetItem(int index) const T& operator[](std::size_t i) const { return *index[i]; // method 1 /* int szList = this->index.size(); if (szList == 0) { return nullptr; }; index = std::max(0, std::min(szList - 1, index)); return this->index[index]; */ } T& operator[](std::size_t i) { return *index[i]; } /* FastList& operator=(FastList original) { // if (this == &original) { return *this; } size_t n = original.Size(); FastList duplicate; this.aList = duplicate.aList; this.index = duplicate.index; if (n == 0) { return duplicate; } for (size_t i = 0; i < n; i++) { this->PushBack(original[i]); } return &this; } */ void Copy(FastList original) { size_t n = original.Size(); FastList duplicate; this->aList = std::list(); this->index = std::vector::iterator>(); /* this->aList = duplicate->aList; this->index = duplicate->index; */ if (n == 0) { return; } for (size_t i = 0; i < n; i++) { this->PushBack(original[i]); } } void Insert(T item, int i) { int szList = this->index.size(); i = std::max(0, std::min(szList - 1, i)); // std::list::iterator middle = std::next(this->aList.begin(), index); auto middle = std::next(this->aList.begin(), i); this->aList.insert(middle, item); this->index.insert(i, item); } void Remove(int i) { int szList = this->index.size(); if (i < -szList || i >= szList) { return; }; if (i < 0) { i = szList + i; } // std::list::iterator middle = std::next(this->aList.begin(), index); auto middle = std::next(this->aList.begin(), i); this->aList.erase(middle); // this->index.erase(i); // this->index.erase() } std::string ToString() { std::ostringstream oss; oss << "FastList" << std::endl; int szList = this->index.size(); if (szList > 0) { oss << "Items:" << std::endl; for (int i = 0; i < szList; i++) { // oss << &this->GetItem(i) << std::endl; oss << *index[i] << std::endl; } } return oss.str(); } void Swap(int i1, int i2) { int nCard = this->index.size(); i1 = std::min(std::max(0, i1), nCard); i2 = std::min(std::max(0, i2), nCard); // std::swap(this->aList[i1], this->aList[i2]); // std::swap(this->index[i1], this->index[i2]); // std::list tmpList; // std::vector::iterator> tmpI; // std::list::iterator tmpI = this->index.at(i1); /* T* tmp1 = this->GetItem(i1); T* tmp2 = this->GetItem(i2); */ T tmp1 = *this->index[i1]; T tmp2 = *this->index[i2]; this->Remove(i1); this->Insert(tmp2, i1); this->Remove(i2); this->Insert(tmp1, i2); } std::size_t Size() const { return this->index.size(); } // std::vector::iterator> Index() auto Index() { return this->index; } bool operator==(FastList fl2) { int n1 = this->Size(); int n2 = fl2.Size(); if (n1 != n2) { return false; } for (int i = 0; i < n1; i++) { if (this[i] != fl2[i]) { return false; } } return true; } // std::vector::iterator> index; static FastList Default(int nElem, T vDefault) { nElem = std::max(0, nElem); FastList v; if (nElem > 0) { for (int i = 0; i < nElem; i++) { v.PushBack(vDefault); } } return v; } private: std::list aList; std::vector::iterator> index; }; // Base case template std::ostream& operator<<(std::ostream& os, std::vector& vec); template std::ostream& operator<<(std::ostream& os, FastList& vec); /* template std::ostream& operator<<(std::ostream& os, const std::vector>& vec); */ // Base case: Overload for a 0-dimensional vector element template std::ostream& printVector(std::ostream& os, T& var) { os << var; return os; } // Base case: Overload for a 1-dimensional vector template std::ostream& printVector(std::ostream& os, std::vector& vec); // Recursive case: Overload for n-dimensional vectors template std::ostream& printVector(std::ostream& os, std::vector< std::vector>& vec); // Overload the equality operator (==) for std::vector template bool areVectorsEqual(const std::vector& v1, const std::vector& v2) { return false; } template bool areVectorsEqual(const std::vector& v1, const std::vector& v2) { if (v1.size() != v2.size()) { return false; // Vectors have different sizes, so they can't be equal. } for (size_t i = 0; i < v1.size(); ++i) { auto i1 = v1[i]; auto i2 = v2[i]; if (!(i1 == i2)) // if (!(v1[i] == v2[i])) // if (!(v1.at(i) == v2.at(i))) { return false; // Elements at the same index are different, so vectors are not equal. } } return true; // All elements are equal, so vectors are equal. } template bool areVectorsEqual(const std::vector>& v1, const std::vector>& v2) { if (v1.size() != v2.size()) { return false; // Vectors have different sizes, so they can't be equal. } for (size_t i = 0; i < v1.size(); ++i) { /* auto i1 = v1[i]; auto i2 = v2[i]; if (!(i1 == i2)) */ if (!areVectorsEqual(v1[i], v2[i])) // if (!(v1[i] == v2[i])) // if (!(v1.at(i) == v2.at(i))) { return false; // Elements at the same index are different, so vectors are not equal. } } return true; // All elements are equal, so vectors are equal. } template bool operator==(const std::vector& v1, const std::vector& v2) { return areVectorsEqual(v1, v2); } template bool operator==(const std::vector>& v1, const std::vector>& v2) { return areVectorsEqual(v1, v2); } template bool operator==(const std::vector& v1, const std::vector& v2) { return false; } enum PointerType { NEXT, PREVIOUS }; template class LinkedListNode { public: LinkedListNode(T node, T* nodeNext, T* nodePrev) { this->node = node; this->nodeNext = nodeNext; this->nodePrev = nodePrev; } void SetNode(T node) { this->node = node; } void SetPointer(T* ptr, PointerType ptrType) { if (ptrType == PointerType(NEXT)) { this->nodeNext = ptrType; } else { this->nodePrev = ptrType; } } LinkedListNode* GetPointer(PointerType ptrType) { if (ptrType == PointerType(NEXT)) { return &this->nodeNext; } else { return &this->nodePrev; } } private: T node; T* nodeNext; T* nodePrev; }; //template //class LinkedList //{ //public: // LinkedList() : head(nullptr), tail(nullptr) {}; // LinkedList(LinkedListNode* head) : head(head) {}; // void PushBack(T node) // { // if (!head) // { // this->head = &node; // this->tail = &node; // this->length++; // return; // } // if (typeid(head) != typeid(node)) // { // lib::ArgVal::ThrowError(lib::ArgVal::ErrorMessage("node", "LinkedList.PushBack", node, typeid(head).name(), tail, true)); // } // this->tail->SetPointer(&node, PointerType(NEXT)); // this->tail = &node; // this->length++; // return; // } // void Insert(T node, int index) // { // if (!head) // { // this->head = &node; // this->tail = &node; // this->length++; // return; // } // if (typeid(head) != typeid(node)) // { // lib::ArgVal::ThrowError(lib::ArgVal::ErrorMessage("node", "LinkedList.Insert", node, typeid(head).name(), tail, true)); // } // if (index <= 0) // { // this->head->SetPointer(&node, PointerType(PREVIOUS)); // this->head = &node; // this->length++; // return; // } // if (index >= this->length) // { // this->tail->SetPointer(&node, PointerType(NEXT)); // this->tail = &node; // this->length++; // return; // } // LinkedListNode nodeNext = *this->head; // LinkedListNode nodePrev; // for (int n = 1; n < index; n++) // { // nodePrev = nodeNext; // nodeNext = *nodePrev->GetPointer(PointerType(NEXT)); // } // nodePrev.SetPointer(&node, PointerType(NEXT)); // nodeNext.SetPointer(&node, PointerType(PREVIOUS)); // this->length++; // } //private: // LinkedListNode* head; // LinkedListNode* tail; // int length = 0; //}; // NAMESPACE OBJECTS namespace library123 { struct enumPlusPlusItem { public: enumPlusPlusItem(std::string name, int index); std::string Name(); int Index(); std::string to_string(); private: std::string name; int index; }; struct enumPlusPlus { public: enumPlusPlus(std::vector enumNames, int iStart = 0); // , std::string name = "" std::string to_string(); int IStart(); int Size(); enumPlusPlusItem GetBy(int index, bool suppressErrors = false); enumPlusPlusItem GetBy(std::string name, bool suppressErrors = false); private: std::string name; std::vector enums; int iStart; }; enum eWoman { NIPPLE, VAGINA, BREAST }; // template static class ArgVal // { public: template static bool IsString(const W& value); /* template struct IsAllowedType : std::false_type {}; template <> struct IsAllowedType : std::true_type {}; template <> struct IsAllowedType : std::true_type {}; template <> struct IsAllowedType : std::true_type {}; template <> struct IsAllowedType : std::true_type {}; template <> struct IsAllowedType : std::true_type {}; template <> struct IsAllowedType : std::true_type {}; template <> struct IsAllowedType : std::true_type {}; template <> struct IsAllowedType : std::true_type {}; template typename std::enable_if::value, void>::type static std::string ToString(const W& arg); */ /* template static std::string ToString(const std::vector& arg); */ /* template static std::string ToString(S s); template static std::string ToString(std::vector s); */ template struct IsPointer { static constexpr bool value = false; }; template struct IsPointer { static constexpr bool value = true; }; template static Y ExtractValue(const Y& value); template static std::string ErrorHole(const std::vector& v); template static std::string ErrorHole2(const std::vector& v1, const std::vector& v2); template static std::string ErrorMessage(std::string varName, std::string funcName, T& var, std::string typeExpected, U& valueExpected, bool attrNotVar = false); template static std::string ErrorMessage(std::string varName, std::string funcName, std::vector& var, std::string typeExpected, std::vector& valueExpected, bool attrNotVar = false); template static std::string ErrorMessage(std::string varName, std::string funcName, const FastList& var, std::string typeExpected, const FastList& valueExpected, bool attrNotVar = false); static void ThrowError(std::string errorMsg, bool raiseNotThrow = false); static int main(); }; template struct HasMethodToString { template static std::true_type test(decltype(&U::to_string)*); template static std::false_type test(...); static constexpr bool value = decltype(test(nullptr))::value; }; class test_1 { public: int nudes; std::string name; test_1() { this->nudes = 1; this->name = "Tierney"; } std::string ToString() { std::ostringstream oss; oss << "test_1" << std::endl; oss << "name: " << this->name << std::endl; oss << "nudes: " << this->nudes << std::endl; return oss.str(); } }; class test_10 { public: std::list aList1; std::vector::iterator> aIndex1; test_10() { std::list aList1 = std::list(); std::vector::iterator> aIndex1 = std::vector::iterator>(); } std::string ToString() { std::ostringstream oss; oss << "test_1" << std::endl; // oss << "name: " << this->aList1 << std::endl; // oss << "nudes: " << this->aIndex1 << std::endl; return oss.str(); } }; } #endif DATA_TYPES_H