From 44da5fdeb5d7e8985b515921f171966f526057db Mon Sep 17 00:00:00 2001 From: noitongadil Date: Mon, 13 Oct 2025 15:39:58 -0600 Subject: [PATCH 1/6] hw10 api created --- homework/src/CMakeLists.txt | 1 + homework/src/hw10_skip_lists/CMakeLists.txt | 3 + homework/src/hw10_skip_lists/hw10.cpp | 0 homework/src/hw10_skip_lists/hw10.h | 127 ++++++++++++++++++++ homework/tests/CMakeLists.txt | 2 + homework/tests/tests_hw10.cpp | 1 + 6 files changed, 134 insertions(+) create mode 100644 homework/src/hw10_skip_lists/CMakeLists.txt create mode 100644 homework/src/hw10_skip_lists/hw10.cpp create mode 100644 homework/src/hw10_skip_lists/hw10.h create mode 100644 homework/tests/tests_hw10.cpp diff --git a/homework/src/CMakeLists.txt b/homework/src/CMakeLists.txt index f92fac3..1919929 100644 --- a/homework/src/CMakeLists.txt +++ b/homework/src/CMakeLists.txt @@ -7,3 +7,4 @@ add_subdirectory(hw06_strings) add_subdirectory(hw07_structs) add_subdirectory(hw08_linked_lists) add_subdirectory(hw09_binary_search_trees) +add_subdirectory(hw10_skip_lists) diff --git a/homework/src/hw10_skip_lists/CMakeLists.txt b/homework/src/hw10_skip_lists/CMakeLists.txt new file mode 100644 index 0000000..872348a --- /dev/null +++ b/homework/src/hw10_skip_lists/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(hw10 hw10.cpp) + +target_include_directories(hw10 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} "${gtest_SOURCE_DIR}/include") diff --git a/homework/src/hw10_skip_lists/hw10.cpp b/homework/src/hw10_skip_lists/hw10.cpp new file mode 100644 index 0000000..e69de29 diff --git a/homework/src/hw10_skip_lists/hw10.h b/homework/src/hw10_skip_lists/hw10.h new file mode 100644 index 0000000..23a64a9 --- /dev/null +++ b/homework/src/hw10_skip_lists/hw10.h @@ -0,0 +1,127 @@ +#pragma once + +#include // for size_t + +namespace cppclass +{ +template class SkipList +{ +private: + static constexpr int MAX_LEVEL = 20; ///< Maximum number of pointers a Node can have in its array of pointers. + + struct Node + { + T data; ///< The data that the node contains. + Node *levels[MAX_LEVEL] = { nullptr }; ///< The array of pointers. + size_t height; ///< The height of the node (the array). + + /** + * @brief Constructs a node with the given @p. + * + * @param data Reference to the data to be contained. + * @param height The height of the node. + */ + Node(const T &data, const size_t height); + }; + +public: + /** + * @brief Constructs an empty skip list. + */ + SkipList(); + + /** + * @brief Constructs a skip list from an array. + * + * @param arr Pointer to the array. + * @param size Size of the array. + */ + SkipList(const T *arr, const size_t size); + + /** + * @brief Constructs a skip list from an array. + * + * @param other Reference to the skip list to copy from. + */ + SkipList(const SkipList &other); + + /** + * @brief Constructs a skip list from an array. + * + * @param other R-Value reference to the skip list to move from. + */ + SkipList(SkipList &&other); + + /** + * @brief Destroys the skip list. + */ + ~SkipList(); + + /** + * @brief Iterates through the list to find @p index. + * + * @param index Index within the skip list. + * @return Pointer to the node with @p index. + */ + Node *at(size_t index) const; + + /** + * @brief Searches the skip list for the node with @p data. + * + * @param data Data to search for. + * @return If found pointer to the node with @p data, else nullptr. + */ + Node *search(const T &data, Node *node = nullptr) const; + + /** + * @brief Inserts a node with @p data into the skip list. + * + * @param data Data to add to skip list. + * @return True if inserted, otherwise false. + */ + bool insert(const T &data); + + /** + * @brief Removes @p node_to_erase from the list. + * + * @param node_to_erase Pointer to the node to remove. + * @return True if removed, otherwise false. + */ + bool remove(Node *node_to_rem); + + /** + * @brief Removes an element with @p data from the list. + * + * @param data The data of the item to be removed from the list. + * @return True if removed, otherwise false. + */ + bool remove(const T &data); + + /** + * @brief Returns the number of nodes in the list. + * + * @return Number of nodes in the list. + */ + size_t size() const; + + /** + * @brief Checks if two skip lists are equal in values and order. + * + * @param other Reference to other skip list. + * @return True, if values and order are identical. False otherwise. + */ + bool operator==(const SkipList &other) const; + + /** + * @brief Checks if two skip lists are inequal in values and order. + * + * @param other Reference to other skip list. + * @return True, if values and order are not identical. False otherwise. + */ + bool operator!=(const SkipList &other) const; + +private: + size_t m_size; ///< Number of nodes in the skip list. + Node *m_head; ///< Pointer to the first node in the skip list. +}; +} diff --git a/homework/tests/CMakeLists.txt b/homework/tests/CMakeLists.txt index d570801..2731266 100644 --- a/homework/tests/CMakeLists.txt +++ b/homework/tests/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCE_FILES driver.cpp tests_hw07.cpp tests_hw08.cpp tests_hw09.cpp + tests_hw10.cpp ) set(HW_LIBS hw01 hw02 @@ -18,6 +19,7 @@ set(HW_LIBS hw01 hw07 hw08 hw09 + hw10 ) add_executable(test_homework ${SOURCE_FILES}) diff --git a/homework/tests/tests_hw10.cpp b/homework/tests/tests_hw10.cpp new file mode 100644 index 0000000..a093b36 --- /dev/null +++ b/homework/tests/tests_hw10.cpp @@ -0,0 +1 @@ +#include "hw10.h" From 1de191847fd435854f9fc36f09aebf53970d7d91 Mon Sep 17 00:00:00 2001 From: noitongadil Date: Mon, 27 Oct 2025 16:22:06 -0600 Subject: [PATCH 2/6] hw10 api search method change --- homework/src/hw10_skip_lists/hw10.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/src/hw10_skip_lists/hw10.h b/homework/src/hw10_skip_lists/hw10.h index 23a64a9..5dcb739 100644 --- a/homework/src/hw10_skip_lists/hw10.h +++ b/homework/src/hw10_skip_lists/hw10.h @@ -71,7 +71,7 @@ template class SkipList * @param data Data to search for. * @return If found pointer to the node with @p data, else nullptr. */ - Node *search(const T &data, Node *node = nullptr) const; + Node *search(const T &data) const; /** * @brief Inserts a node with @p data into the skip list. From 3d4793d5e8016fb4f1749b4960f397856aa183db Mon Sep 17 00:00:00 2001 From: noitongadil Date: Sat, 1 Nov 2025 21:27:00 -0600 Subject: [PATCH 3/6] hw10 api changes --- homework/src/hw10_skip_lists/hw10.h | 45 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/homework/src/hw10_skip_lists/hw10.h b/homework/src/hw10_skip_lists/hw10.h index 5dcb739..33c3c65 100644 --- a/homework/src/hw10_skip_lists/hw10.h +++ b/homework/src/hw10_skip_lists/hw10.h @@ -4,15 +4,13 @@ namespace cppclass { -template class SkipList +template class SkipList { private: - static constexpr int MAX_LEVEL = 20; ///< Maximum number of pointers a Node can have in its array of pointers. - struct Node { T data; ///< The data that the node contains. - Node *levels[MAX_LEVEL] = { nullptr }; ///< The array of pointers. + Node *levels[]; ///< Pointer to an array of pointers. (heap-allocate) size_t height; ///< The height of the node (the array). /** @@ -22,6 +20,11 @@ template class SkipList * @param height The height of the node. */ Node(const T &data, const size_t height); + + /** + * @brief Destroys a node. + */ + ~Node(); }; public: @@ -39,31 +42,39 @@ template class SkipList SkipList(const T *arr, const size_t size); /** - * @brief Constructs a skip list from an array. + * @brief Copy constructs a skip list from @p other. * * @param other Reference to the skip list to copy from. */ SkipList(const SkipList &other); /** - * @brief Constructs a skip list from an array. + * @brief Copy assigns a skip list from @p other. + * + * @param other Reference to skip list to copy. + * @return Reference to this object. + */ + SkipList &operator=(const SkipList &other); + + /** + * @brief Move contructs a skip list from @p other * * @param other R-Value reference to the skip list to move from. */ SkipList(SkipList &&other); /** - * @brief Destroys the skip list. + * @brief Move assigns a skip list from @p other + * + * @param other R-Value reference to the skip list to move from. + * @return Reference to this object. */ - ~SkipList(); + SkipList &operator=(const SkipList &&other); /** - * @brief Iterates through the list to find @p index. - * - * @param index Index within the skip list. - * @return Pointer to the node with @p index. + * @brief Destroys the skip list. */ - Node *at(size_t index) const; + ~SkipList(); /** * @brief Searches the skip list for the node with @p data. @@ -81,14 +92,6 @@ template class SkipList */ bool insert(const T &data); - /** - * @brief Removes @p node_to_erase from the list. - * - * @param node_to_erase Pointer to the node to remove. - * @return True if removed, otherwise false. - */ - bool remove(Node *node_to_rem); - /** * @brief Removes an element with @p data from the list. * From 5db90837988a8cd21b4c3d050537a6cdb5f7671f Mon Sep 17 00:00:00 2001 From: noitongadil Date: Sat, 1 Nov 2025 21:40:58 -0600 Subject: [PATCH 4/6] node arr of ptrs fixed --- homework/src/hw10_skip_lists/hw10.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/src/hw10_skip_lists/hw10.h b/homework/src/hw10_skip_lists/hw10.h index 33c3c65..9a0e076 100644 --- a/homework/src/hw10_skip_lists/hw10.h +++ b/homework/src/hw10_skip_lists/hw10.h @@ -10,7 +10,7 @@ template class SkipList struct Node { T data; ///< The data that the node contains. - Node *levels[]; ///< Pointer to an array of pointers. (heap-allocate) + Node **levels; ///< Pointer to an array of pointers. (heap-allocate) size_t height; ///< The height of the node (the array). /** From d605ab0a3384d4e98a7a826acbaf4bff398d80d3 Mon Sep 17 00:00:00 2001 From: noitongadil Date: Sat, 1 Nov 2025 21:43:30 -0600 Subject: [PATCH 5/6] move assignment fixed --- homework/src/hw10_skip_lists/hw10.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/src/hw10_skip_lists/hw10.h b/homework/src/hw10_skip_lists/hw10.h index 9a0e076..7d2376f 100644 --- a/homework/src/hw10_skip_lists/hw10.h +++ b/homework/src/hw10_skip_lists/hw10.h @@ -69,7 +69,7 @@ template class SkipList * @param other R-Value reference to the skip list to move from. * @return Reference to this object. */ - SkipList &operator=(const SkipList &&other); + SkipList &operator=(SkipList &&other); /** * @brief Destroys the skip list. From e73cc8993498e95bde51fd2d846e1fd9b12831fd Mon Sep 17 00:00:00 2001 From: noitongadil Date: Sun, 2 Nov 2025 19:26:53 -0700 Subject: [PATCH 6/6] assignment operators removed --- homework/src/hw10_skip_lists/hw10.h | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/homework/src/hw10_skip_lists/hw10.h b/homework/src/hw10_skip_lists/hw10.h index 7d2376f..6b9ef11 100644 --- a/homework/src/hw10_skip_lists/hw10.h +++ b/homework/src/hw10_skip_lists/hw10.h @@ -48,14 +48,6 @@ template class SkipList */ SkipList(const SkipList &other); - /** - * @brief Copy assigns a skip list from @p other. - * - * @param other Reference to skip list to copy. - * @return Reference to this object. - */ - SkipList &operator=(const SkipList &other); - /** * @brief Move contructs a skip list from @p other * @@ -63,14 +55,6 @@ template class SkipList */ SkipList(SkipList &&other); - /** - * @brief Move assigns a skip list from @p other - * - * @param other R-Value reference to the skip list to move from. - * @return Reference to this object. - */ - SkipList &operator=(SkipList &&other); - /** * @brief Destroys the skip list. */