Skip to content

Commit 6ea08b2

Browse files
committed
Add String.h and update HashTable.h
1 parent 19be423 commit 6ea08b2

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Remember that each data has its own trade-offs. And you need to pay attention mo
2525
* `B` [Array](data-structures/Array)
2626
* `B` Static Array
2727
* `B` Dynamic Array
28+
* `B` String
2829
* `B` [Linked List](data-structures/LinkedList)
2930
* `B` Singly Linked List
3031
* `B` Doubly Linked List

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* `B` [数组](data-structures/Array)
2222
* `B` 静态数组
2323
* `B` 动态数组
24+
* `B` 字符串
2425
* `B` [链表](data-structures/LinkedList)
2526
* `B` 单向链表
2627
* `B` 双向链表

data-structures/Array/__test__/test_Array.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,15 @@ void test_vector(){
200200
std::cout << "*****Vector Test End*****" << std::endl;
201201
}
202202

203+
void test_String(){
204+
205+
}
206+
203207
int main() {
204208
test_array();
205209
std::cout << std::endl;
206210
test_vector();
211+
std::cout << std::endl;
212+
test_String();
207213
return 0;
208214
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef STRING_H
2+
#define STRING_H
3+
4+
#include "./Vector.h"
5+
6+
class String{
7+
public:
8+
String();
9+
~String();
10+
public:
11+
// Capacity
12+
bool empty() const;
13+
size_t size() const;
14+
size_t length() const;
15+
void resize(size_t n);
16+
void clear();
17+
void shrink_to_fit();
18+
// Elements access
19+
char& front();
20+
char& back();
21+
char& at(const size_t index);
22+
char& operator[](const size_t index);
23+
// Modifier
24+
String& operator+=(const String& str);
25+
String& operator+=(const char *s);
26+
String& operator+=(const char c);
27+
private:
28+
Vector<char> _data;
29+
};
30+
31+
32+
// Constructor and Desttructor
33+
String::String(){
34+
35+
}
36+
37+
String::~String(){
38+
39+
}
40+
41+
#endif // STRING_H

data-structures/HashTable/include/HashTable.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class HashTable{
3434
size_t bucket(const Key& key) const;
3535
private:
3636
size_t _next_prime(const size_t n) const;
37+
size_t _bucket_num(const Key& key, size_t n);
38+
size_t _hash(const Key& key);
3739
private:
3840
struct _KVNode{
3941
Key _key;
@@ -47,6 +49,31 @@ class HashTable{
4749
template <typename Key, typename Value>
4850
HashTable<Key, Value>::HashTable() : _buckets(*(_prime_list)), _size(0) {}
4951

52+
// Elements access
53+
template <typename Key, typename Value>
54+
Value& HashTable<Key, Value>::at(const Key &key){
55+
56+
}
57+
58+
// Buckets
59+
template <typename Key, typename Value>
60+
size_t HashTable<Key, Value>::buckets_count() const{
61+
return _buckets.size();
62+
}
63+
64+
template <typename Key, typename Value>
65+
size_t HashTable<Key, Value>::max_buckets_count() const{
66+
return *(_prime_list + _num_primes - 1);
67+
}
68+
69+
template <typename Key, typename Value>
70+
size_t HashTable<Key, Value>::bucket_size(const size_t n) const{
71+
if(n >= _buckets.size()){
72+
throw std::out_of_range("HashTable::bucket_size(): Index out of range");
73+
}
74+
return _buckets[n].size();
75+
}
76+
5077
// Private Functions
5178
template <typename Key, typename Value>
5279
size_t HashTable<Key, Value>::_next_prime(const size_t n) const{
@@ -61,4 +88,14 @@ size_t HashTable<Key, Value>::_next_prime(const size_t n) const{
6188
return pos == last ? *(last - 1) : *pos;
6289
}
6390

64-
#endif // HASHTABLE
91+
template <typename Key, typename Value>
92+
size_t HashTable<Key, Value>::_bucket_num(const Key &key, size_t n){
93+
return _hash(key) % n;
94+
}
95+
96+
template <typename Value>
97+
size_t HashTable<int, Value>::_hash(const int &key){
98+
return key;
99+
}
100+
101+
#endif // HASHTABLE_H

0 commit comments

Comments
 (0)