template<typename V>
private:
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);
}
}
};
MapEntry** entries;
int capacity;
int entryCount;
public:
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;
}
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;
}
int intKey = reinterpret_cast<intptr_t>(&key);
MapEntry* entry = findEntry(intKey);
return entry ? entry->getValue() : nullptr;
}
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;
}
}
int intKey = reinterpret_cast<intptr_t>(&key);
return removeByKey(intKey);
}
int intKey = reinterpret_cast<intptr_t>(&key);
MapEntry* entry = findEntry(intKey);
if (entry) {
return entry->setValue(value);
}
return nullptr;
}
void printAll() const {
for (const auto& entry : entries) {
if (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;
}
};
SimpleMap<ufm::lang::Strings> stringMap;
int key1 = 100, key2 = 200, key3 = 300;
stringMap.put(*keyInterface1, &value1);
stringMap.put(*keyInterface2, &value2);
stringMap.put(*keyInterface3, &value3);
stringMap.printAll();
if (retrieved) {
}
std::cout << "\n替換操作:" << std::endl;
std::string newValue = "Updated Second";
std::string* oldValue = stringMap.replace(*keyInterface2, &newValue);
if (oldValue) {
}
stringMap.printAll();
std::cout << "\n移除操作:" << std::endl;
std::string* removedValue = stringMap.remove(*keyInterface1);
if (removedValue) {
}
stringMap.printAll();
class MapEntryDemo {
public:
template<typename V>
static void demonstrateEntryOperations(SimpleMap<V>& map) {
if (!map.isEmpty()) {
map.printAll();
}
}
};
MapEntryDemo::demonstrateEntryOperations(stringMap);
class ConfigurationMap : public SimpleMap<ufm::lang::Strings> {
public:
int key = configName.hashCode();
put(*keyInterface, value);
}
int key = configName.hashCode();
return value ? *value : defaultValue;
}
int key = configName.hashCode();
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