docs: update containers API docs
This commit is contained in:
@@ -10,7 +10,11 @@ void Clear();
|
||||
|
||||
**返回:** 无
|
||||
|
||||
**复杂度:** O(m_bucketCount),需要清空所有桶
|
||||
**复杂度:** O(bucketCount),需要清空所有桶
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
|
||||
@@ -15,6 +15,10 @@ explicit HashMap(size_t bucketCount, Memory::IAllocator* allocator = nullptr);
|
||||
|
||||
**复杂度:** O(bucketCount),需要初始化所有桶
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
@@ -22,8 +26,7 @@ XCEngine::Containers::HashMap<int, const char*> map1;
|
||||
|
||||
XCEngine::Containers::HashMap<int, const char*> map2(32);
|
||||
|
||||
auto customAllocator = XCEngine::Memory::GetDefaultAllocator();
|
||||
XCEngine::Containers::HashMap<int, const char*> map3(64, customAllocator);
|
||||
XCEngine::Containers::HashMap<int, const char*> map3(64, nullptr);
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
@@ -13,6 +13,10 @@ bool Contains(const Key& key) const;
|
||||
|
||||
**复杂度:** O(1) 平均,最坏 O(n)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -16,6 +16,10 @@ HashMap(HashMap&& other) noexcept;
|
||||
- 拷贝构造:O(m_bucketCount + other.m_size)
|
||||
- 移动构造:O(m_bucketCount),移动构造需要遍历所有桶以重新建立桶的指针关系
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
|
||||
**复杂度:** O(n),需要清空所有桶中的元素
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -11,7 +11,13 @@ bool Erase(const Key& key);
|
||||
|
||||
**返回:** 如果元素被删除返回 `true`,如果键不存在返回 `false`。
|
||||
|
||||
**复杂度:** O(1) 平均,最坏 O(n)
|
||||
**复杂度:** O(1) 平均,最坏 O(n)(同一桶中有多个键发生哈希冲突)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**实现备注:** 使用 swap-with-last 策略,将待删除元素与桶内最后一个元素交换,然后 popBack,避免数组元素批量移动。
|
||||
|
||||
**示例:**
|
||||
|
||||
|
||||
@@ -14,6 +14,10 @@ const Value* Find(const Key& key) const;
|
||||
|
||||
**复杂度:** O(1) 平均,最坏 O(n)(同一桶中有多个键发生哈希冲突)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -4,11 +4,22 @@
|
||||
|
||||
**类型**: `class` (template)
|
||||
|
||||
**头文件**: `XCEngine/Containers/HashMap.h`
|
||||
|
||||
**描述**: 模板哈希表容器,提供键值对存储和快速查找。
|
||||
|
||||
## 概述
|
||||
|
||||
`HashMap<Key, Value>` 是一个模板哈希表容器,使用动态数组作为桶来解决哈希冲突,支持键值对的插入、查找和删除操作。
|
||||
`HashMap<Key, Value>` 是一个模板哈希表容器,使用分离链接法(separate chaining)解决哈希冲突,每个桶是一个动态数组。
|
||||
|
||||
## 实现细节
|
||||
|
||||
| 参数 | 值 | 描述 |
|
||||
|------|-----|------|
|
||||
| 默认桶数 | 16 | `DefaultBucketCount` |
|
||||
| 负载因子 | 0.75 | 当 `size / bucketCount > 0.75` 时触发扩容 |
|
||||
| 扩容策略 | ×2 | 每次扩容桶数翻倍 |
|
||||
| Erase 策略 | swap-with-last | 将待删除元素与最后一个元素交换,避免数组元素移动 |
|
||||
|
||||
## 公共类型
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@ bool Insert(Pair&& pair);
|
||||
|
||||
**复杂度:** O(1) 平均,最坏 O(n)(包括可能的 rehash)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -15,6 +15,10 @@ ConstIterator end() const;
|
||||
|
||||
**复杂度:** O(1)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -16,6 +16,10 @@ HashMap& operator=(HashMap&& other) noexcept;
|
||||
- 拷贝赋值:O(m_bucketCount + other.m_size)
|
||||
- 移动赋值:O(m_size),需要先清空当前内容
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -13,6 +13,10 @@ Value& operator[](const Key& key);
|
||||
|
||||
**复杂度:** O(1) 平均,最坏 O(n)(发生 rehash 时)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -13,6 +13,10 @@ void SetAllocator(Memory::IAllocator* allocator);
|
||||
|
||||
**复杂度:** O(1)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -15,6 +15,10 @@ bool Empty() const;
|
||||
|
||||
**复杂度:** O(1)
|
||||
|
||||
**线程安全:** ❌ 非线程安全
|
||||
|
||||
**异常:** 无
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
|
||||
Reference in New Issue
Block a user