Skip to content

Commit

Permalink
Add String.h and update HashTable.h
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdove committed Oct 1, 2024
1 parent 19be423 commit 6ea08b2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* `B` [数组](data-structures/Array)
* `B` 静态数组
* `B` 动态数组
* `B` 字符串
* `B` [链表](data-structures/LinkedList)
* `B` 单向链表
* `B` 双向链表
Expand Down
6 changes: 6 additions & 0 deletions data-structures/Array/__test__/test_Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
41 changes: 41 additions & 0 deletions data-structures/Array/include/String.h
Original file line number Diff line number Diff line change
@@ -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<char> _data;
};


// Constructor and Desttructor
String::String(){

}

String::~String(){

}

#endif // STRING_H
39 changes: 38 additions & 1 deletion data-structures/HashTable/include/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +49,31 @@ class HashTable{
template <typename Key, typename Value>
HashTable<Key, Value>::HashTable() : _buckets(*(_prime_list)), _size(0) {}

// Elements access
template <typename Key, typename Value>
Value& HashTable<Key, Value>::at(const Key &key){

}

// Buckets
template <typename Key, typename Value>
size_t HashTable<Key, Value>::buckets_count() const{
return _buckets.size();
}

template <typename Key, typename Value>
size_t HashTable<Key, Value>::max_buckets_count() const{
return *(_prime_list + _num_primes - 1);
}

template <typename Key, typename Value>
size_t HashTable<Key, Value>::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 <typename Key, typename Value>
size_t HashTable<Key, Value>::_next_prime(const size_t n) const{
Expand All @@ -61,4 +88,14 @@ size_t HashTable<Key, Value>::_next_prime(const size_t n) const{
return pos == last ? *(last - 1) : *pos;
}

#endif // HASHTABLE
template <typename Key, typename Value>
size_t HashTable<Key, Value>::_bucket_num(const Key &key, size_t n){
return _hash(key) % n;
}

template <typename Value>
size_t HashTable<int, Value>::_hash(const int &key){
return key;
}

#endif // HASHTABLE_H

0 comments on commit 6ea08b2

Please sign in to comment.