From 6ea08b24f5bc8400726a0e4343ffe2d5d2a6583d Mon Sep 17 00:00:00 2001 From: MasonCodingHere <38603431+MasonCodingHere@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:50:09 +0800 Subject: [PATCH] Add String.h and update HashTable.h --- README.md | 1 + README.zh-CN.md | 1 + data-structures/Array/__test__/test_Array.cpp | 6 +++ data-structures/Array/include/String.h | 41 +++++++++++++++++++ data-structures/HashTable/include/HashTable.h | 39 +++++++++++++++++- 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 data-structures/Array/include/String.h diff --git a/README.md b/README.md index 8aee6b6..6a2d474 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Remember that each data has its own trade-offs. And you need to pay attention mo * `B` [Array](data-structures/Array) * `B` Static Array * `B` Dynamic Array + * `B` String * `B` [Linked List](data-structures/LinkedList) * `B` Singly Linked List * `B` Doubly Linked List diff --git a/README.zh-CN.md b/README.zh-CN.md index da4f47f..8606d50 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -21,6 +21,7 @@ * `B` [数组](data-structures/Array) * `B` 静态数组 * `B` 动态数组 + * `B` 字符串 * `B` [链表](data-structures/LinkedList) * `B` 单向链表 * `B` 双向链表 diff --git a/data-structures/Array/__test__/test_Array.cpp b/data-structures/Array/__test__/test_Array.cpp index d021276..b3d1c8d 100644 --- a/data-structures/Array/__test__/test_Array.cpp +++ b/data-structures/Array/__test__/test_Array.cpp @@ -200,9 +200,15 @@ void test_vector(){ std::cout << "*****Vector Test End*****" << std::endl; } +void test_String(){ + +} + int main() { test_array(); std::cout << std::endl; test_vector(); + std::cout << std::endl; + test_String(); return 0; } \ No newline at end of file diff --git a/data-structures/Array/include/String.h b/data-structures/Array/include/String.h new file mode 100644 index 0000000..067c400 --- /dev/null +++ b/data-structures/Array/include/String.h @@ -0,0 +1,41 @@ +#ifndef STRING_H +#define STRING_H + +#include "./Vector.h" + +class String{ + public: + String(); + ~String(); + public: + // Capacity + bool empty() const; + size_t size() const; + size_t length() const; + void resize(size_t n); + void clear(); + void shrink_to_fit(); + // Elements access + char& front(); + char& back(); + char& at(const size_t index); + char& operator[](const size_t index); + // Modifier + String& operator+=(const String& str); + String& operator+=(const char *s); + String& operator+=(const char c); + private: + Vector _data; +}; + + +// Constructor and Desttructor +String::String(){ + +} + +String::~String(){ + +} + +#endif // STRING_H \ No newline at end of file diff --git a/data-structures/HashTable/include/HashTable.h b/data-structures/HashTable/include/HashTable.h index 33f9564..c74e407 100644 --- a/data-structures/HashTable/include/HashTable.h +++ b/data-structures/HashTable/include/HashTable.h @@ -34,6 +34,8 @@ class HashTable{ size_t bucket(const Key& key) const; private: size_t _next_prime(const size_t n) const; + size_t _bucket_num(const Key& key, size_t n); + size_t _hash(const Key& key); private: struct _KVNode{ Key _key; @@ -47,6 +49,31 @@ class HashTable{ template HashTable::HashTable() : _buckets(*(_prime_list)), _size(0) {} +// Elements access +template +Value& HashTable::at(const Key &key){ + +} + +// Buckets +template +size_t HashTable::buckets_count() const{ + return _buckets.size(); +} + +template +size_t HashTable::max_buckets_count() const{ + return *(_prime_list + _num_primes - 1); +} + +template +size_t HashTable::bucket_size(const size_t n) const{ + if(n >= _buckets.size()){ + throw std::out_of_range("HashTable::bucket_size(): Index out of range"); + } + return _buckets[n].size(); +} + // Private Functions template size_t HashTable::_next_prime(const size_t n) const{ @@ -61,4 +88,14 @@ size_t HashTable::_next_prime(const size_t n) const{ return pos == last ? *(last - 1) : *pos; } -#endif // HASHTABLE \ No newline at end of file +template +size_t HashTable::_bucket_num(const Key &key, size_t n){ + return _hash(key) % n; +} + +template +size_t HashTable::_hash(const int &key){ + return key; +} + +#endif // HASHTABLE_H \ No newline at end of file