mFrame
載入中...
搜尋中...
無符合項目
ufm::util::Map< V > 結構 樣版 參考文件abstract

映射表介面,定義鍵值對的容器結構。 更多...

#include <Map.h>

類別ufm::util::Map< V >的繼承圖:
ufm::util::Container

複合項目

struct  Entry
 Map的條目介面,代表一個鍵值對 更多...
 

公開方法(Public Methods)

virtual bool containsKey (ufm::lang::Interface &key) const override
 檢查map是否包含指定的鍵
 
virtual bool containsValue (V *value) const override
 檢查map是否包含指定的值
 
virtual V * get (ufm::lang::Interface &key) const override
 獲取指定鍵對應的值
 
virtual V * put (ufm::lang::Interface &key, V *value) override
 將指定的值與指定的鍵相關聯
 
virtual V * remove (ufm::lang::Interface &key) override
 從map中移除指定鍵的映射
 
virtual V * replace (ufm::lang::Interface &key, V *value) override
 替換指定鍵的值
 
- 公開方法(Public Methods) 繼承自 ufm::util::Container
virtual void clear (void) override
 從此集合中刪除所有元素(可選操作)
 
virtual bool isEmpty (void) const override
 檢查此集合是否不包含任何元素
 
virtual int size (void) const override
 返回此集合中的元素數量
 

詳細描述

template<typename V = void>
struct ufm::util::Map< V >

映射表介面,定義鍵值對的容器結構。

映射表介面,將鍵映射到值的對象容器。

樣版參數
V值的類型,預設為 void

Map 介面定義了鍵值對映射容器的標準操作。映射表將唯一的鍵 映射到對應的值,每個鍵最多只能對應一個值,不允許重複的鍵。 提供了完整的增刪查改功能和條目管理機制。

主要特性:
  • 鍵的唯一性:每個鍵只能出現一次
  • 鍵值關聯:建立鍵與值之間的對應關係
  • 動態管理:支援運行時的增刪改查操作
  • 條目介面:提供 Entry 子介面管理單一鍵值對
使用範例:
// 實作一個簡單的 Map 類別
template<typename V>
class SimpleMap : public ufm::util::Map<V> {
private:
struct MapEntry : public ufm::util::Map<V>::Entry {
int key;
V* value;
SimpleMap<V>* parent;
MapEntry(int k, V* v, SimpleMap<V>* p) : key(k), value(v), parent(p) {}
int getKey(void) override { return key; }
V* getValue(void) override { return value; }
V* setValue(V* newValue) override {
V* oldValue = value;
value = newValue;
return oldValue;
}
void remove(void) override {
if (parent) {
parent->removeByKey(key);
}
}
};
// 使用動態陣列替代 std::vector 和 std::unique_ptr
MapEntry** entries;
int capacity;
int entryCount;
public:
// 實作 Container 介面
void clear(void) override {
// 清除所有條目
for (int i = 0; i < entryCount; ++i) {
delete entries[i];
}
entryCount = 0;
}
bool isEmpty(void) const override {
return entryCount == 0;
}
int size(void) const override {
return entryCount;
}
// 實作 Map 介面
bool containsKey(ufm::lang::Interface& key) const override {
// 簡化實作,假設 key 可以轉換為整數
int intKey = reinterpret_cast<intptr_t>(&key);
return findEntry(intKey) != nullptr;
}
bool containsValue(V* value) const override {
for (const auto& entry : entries) {
if (entry->getValue() == value) {
return true;
}
}
return false;
}
V* get(ufm::lang::Interface& key) const override {
int intKey = reinterpret_cast<intptr_t>(&key);
MapEntry* entry = findEntry(intKey);
return entry ? entry->getValue() : nullptr;
}
V* put(ufm::lang::Interface& key, V* value) override {
int intKey = reinterpret_cast<intptr_t>(&key);
MapEntry* existing = findEntry(intKey);
if (existing) {
return existing->setValue(value);
} else {
// 檢查容量並擴展陣列
if (entryCount >= capacity) {
// 需要擴展容量的邏輯
return nullptr; // 簡化處理
}
entries[entryCount] = new MapEntry(intKey, value, this);
++entryCount;
return nullptr;
}
}
V* remove(ufm::lang::Interface& key) override {
int intKey = reinterpret_cast<intptr_t>(&key);
return removeByKey(intKey);
}
V* replace(ufm::lang::Interface& key, V* value) override {
int intKey = reinterpret_cast<intptr_t>(&key);
MapEntry* entry = findEntry(intKey);
if (entry) {
return entry->setValue(value);
}
return nullptr;
}
// 輔助方法
void printAll() const {
ufm::lang::System::out().print("Map 內容 (大小: ");
for (const auto& entry : entries) {
ufm::lang::System::out().print(entry->getKey());
if (entry->getValue()) {
ufm::lang::System::out().println(reinterpret_cast<intptr_t>(entry->getValue()));
} else {
}
}
}
private:
MapEntry* findEntry(int key) const {
for (const auto& entry : entries) {
if (entry->getKey() == key) {
return entry.get();
}
}
return nullptr;
}
V* removeByKey(int key) {
for (int i = 0; i < entryCount; ++i) {
if (entries[i]->getKey() == key) {
V* value = entries[i]->getValue();
delete entries[i];
// 移動後續元素向前
for (int j = i; j < entryCount - 1; ++j) {
entries[j] = entries[j + 1];
}
--entryCount;
return value;
}
}
return nullptr;
}
};
// 使用 Map 的基本操作
SimpleMap<ufm::lang::Strings> stringMap;
// 建立一些測試用的鍵和值
int key1 = 100, key2 = 200, key3 = 300;
ufm::lang::Interface* keyInterface1 = reinterpret_cast<ufm::lang::Interface*>(&key1);
ufm::lang::Interface* keyInterface2 = reinterpret_cast<ufm::lang::Interface*>(&key2);
ufm::lang::Interface* keyInterface3 = reinterpret_cast<ufm::lang::Interface*>(&key3);
ufm::lang::Strings value1("First");
ufm::lang::Strings value2("Second");
ufm::lang::Strings value3("Third");
ufm::lang::System::out().println("=== Map 基本操作測試 ===");
// 測試 put 操作
ufm::lang::System::out().println("添加鍵值對:");
stringMap.put(*keyInterface1, &value1);
stringMap.put(*keyInterface2, &value2);
stringMap.put(*keyInterface3, &value3);
stringMap.printAll();
// 測試 get 操作
ufm::lang::System::out().println("\n取得值:");
ufm::lang::Strings* retrieved = stringMap.get(*keyInterface1);
if (retrieved) {
ufm::lang::System::out().print(" 對應的值: ");
// 由於 ufm::lang::Strings 可能沒有直接的 << 操作,使用其字串內容
ufm::lang::System::out().println("字串值"); // 簡化處理
}
// 測試 containsKey 和 containsValue
ufm::lang::System::out().println("\n存在性檢查:");
ufm::lang::System::out().print("包含鍵 ");
ufm::lang::System::out().println(stringMap.containsKey(*keyInterface1) ? "是" : "否");
ufm::lang::System::out().print("包含值 '");
ufm::lang::System::out().print("Second值"); // 簡化字串處理
ufm::lang::System::out().println(stringMap.containsValue(&value2) ? "是" : "否");
// 測試 replace 操作
std::cout << "\n替換操作:" << std::endl;
std::string newValue = "Updated Second";
std::string* oldValue = stringMap.replace(*keyInterface2, &newValue);
if (oldValue) {
ufm::lang::System::out().println("替換成功,舊值: (字串值)"); // 簡化處理
}
stringMap.printAll();
// 測試 remove 操作
std::cout << "\n移除操作:" << std::endl;
std::string* removedValue = stringMap.remove(*keyInterface1);
if (removedValue) {
ufm::lang::System::out().println("移除成功,移除的值: (字串值)"); // 簡化處理
}
stringMap.printAll();
// Entry 操作範例
class MapEntryDemo {
public:
template<typename V>
static void demonstrateEntryOperations(SimpleMap<V>& map) {
ufm::lang::System::out().println("\n=== Entry 操作演示 ===");
if (!map.isEmpty()) {
ufm::lang::System::out().println("透過 Entry 修改值...");
// 注意:這裡簡化了 Entry 的獲取過程
// 實際使用中可能需要提供迭代器或其他方式來獲取 Entry
map.printAll();
}
}
};
MapEntryDemo::demonstrateEntryOperations(stringMap);
// 設定檔管理範例
class ConfigurationMap : public SimpleMap<ufm::lang::Strings> {
public:
void setConfig(const ufm::lang::Strings& configName, const ufm::lang::Strings& configValue) {
// 使用簡單的 hash 代替 std::hash
int key = configName.hashCode(); // 假設 ufm::lang::Strings 有 hashCode 方法
ufm::lang::Interface* keyInterface = reinterpret_cast<ufm::lang::Interface*>(&key);
ufm::lang::Strings* value = new ufm::lang::Strings(configValue);
put(*keyInterface, value);
ufm::lang::System::out().print("配置名稱"); // 簡化字串處理
}
ufm::lang::Strings getConfig(const ufm::lang::Strings& configName, const ufm::lang::Strings& defaultValue = ufm::lang::Strings("")) {
// 使用簡單的 hash 代替 std::hash
int key = configName.hashCode(); // 假設 ufm::lang::Strings 有 hashCode 方法
ufm::lang::Interface* keyInterface = reinterpret_cast<ufm::lang::Interface*>(&key);
ufm::lang::Strings* value = get(*keyInterface);
return value ? *value : defaultValue;
}
bool hasConfig(const ufm::lang::Strings& configName) {
// 使用簡單的 hash 代替 std::hash
int key = configName.hashCode(); // 假設 ufm::lang::Strings 有 hashCode 方法
ufm::lang::Interface* keyInterface = reinterpret_cast<ufm::lang::Interface*>(&key);
return containsKey(*keyInterface);
}
void printAllConfigs() {
std::cout << "所有設定項目:" << std::endl;
printAll();
}
};
// 使用設定檔管理
std::cout << "\n=== 設定檔管理範例 ===" << std::endl;
ConfigurationMap config;
config.setConfig("server_port", "8080");
config.setConfig("database_url", "localhost:3306");
config.setConfig("debug_mode", "true");
std::cout << "伺服器端口: " << config.getConfig("server_port") << std::endl;
std::cout << "資料庫網址: " << config.getConfig("database_url") << std::endl;
std::cout << "除錯模式: " << config.getConfig("debug_mode") << std::endl;
std::cout << "快取大小: " << config.getConfig("cache_size", "1024") << std::endl; // 使用預設值
config.printAllConfigs();
PrintStream & println(void)
印出換行字元。
PrintStream & print(bool b, bool newLine=false)
印出布林值,可選擇是否換行。
字串類別,提供字串操作和記憶體管理功能
Definition Strings.h:33
static ufm::io::PrintStream & out(void)
獲取系統輸出緩衝區
Definition System.h:127
類別共用基礎介面
Definition Interface.h:152
映射表介面,定義鍵值對的容器結構。
樣版參數
V值的類型,預設為 void

Map 不能包含重複的鍵,每個鍵最多只能映射到一個值。 提供完整的鍵值對管理功能,包括增加、刪除、查詢和修改操作。

參閱
ufm::util::Container
1.0.0

函式成員說明文件

◆ containsKey()

template<typename V = void>
virtual bool ufm::util::Map< V >::containsKey ( ufm::lang::Interface & key) const
pure virtual

檢查map是否包含指定的鍵

參數
key- 要檢查的鍵
傳回值
true - 如果map包含指定的鍵
false - 如果map不包含指定的鍵

◆ containsValue()

template<typename V = void>
virtual bool ufm::util::Map< V >::containsValue ( V * value) const
pure virtual

檢查map是否包含指定的值

參數
value- 要檢查的值
傳回值
true - 如果map包含指定的值
false - 如果map不包含指定的值

◆ get()

template<typename V = void>
virtual V * ufm::util::Map< V >::get ( ufm::lang::Interface & key) const
pure virtual

獲取指定鍵對應的值

參數
key- 要查找的鍵
傳回值
V* - 對應的值,如果鍵不存在則返回null

◆ put()

template<typename V = void>
virtual V * ufm::util::Map< V >::put ( ufm::lang::Interface & key,
V * value )
pure virtual

將指定的值與指定的鍵相關聯

參數
key- 要關聯的鍵
value- 要關聯的值
傳回值
V* - 先前與鍵關聯的值,如果沒有則返回null

◆ remove()

template<typename V = void>
virtual V * ufm::util::Map< V >::remove ( ufm::lang::Interface & key)
pure virtual

從map中移除指定鍵的映射

參數
key- 要移除的鍵
傳回值
V* - 先前與鍵關聯的值,如果沒有則返回null

◆ replace()

template<typename V = void>
virtual V * ufm::util::Map< V >::replace ( ufm::lang::Interface & key,
V * value )
pure virtual

替換指定鍵的值

參數
key- 要替換值的鍵
value- 新的值
傳回值
V* - 先前與鍵關聯的值,如果沒有則返回null

此結構(structure) 文件是由下列檔案中產生: