📄 map.h
字号:
#ifndef __map_h
#define __map_h
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/map.h 5 8/28/02 6:27a Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk <andrei@remenchuk.com>
//-----------------------------------------------------------------------------
// map.cpp: simple map with string keys
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "array.h"
#include "cstring.h"
#include "utils.h"
template<class T> class mapitem
{
public:
string m_key;
T m_value;
mapitem(const char* key) : m_key(key)
{
memset(&m_value, 0, sizeof(T));
}
T* addr() { return &m_value; }
~mapitem()
{
}
};
template<class T, bool auto_delete = false> class map
{
protected:
array< mapitem<T> > m_items;
mapitem<T>* locate(const char* key) const
{
for(int i=0,n=m_items.size(); i<n; i++)
{
mapitem<T>* item = m_items.get(i);
if (item != NULL && stricmp(item->m_key, key) == 0)
return item;
}
return NULL;
}
mapitem<T>* create(const char* key)
{
mapitem<T> *item = new mapitem<T>(key);
m_items.add(item);
return item;
}
mapitem<T>* provide(const char* key)
{
mapitem<T>* item = locate(key);
if (item == NULL)
item = create(key);
return item;
}
public:
map()
{
}
int size() const { return m_items.size(); }
const char* key_at(int index, T& value) const
{
mapitem<T>* item = m_items.get(index);
value = item->m_value;
return item->m_key;
}
T* get(const char* key) const
{
mapitem<T>* item = locate(key);
T* value;
if (item)
value = item->addr();
else
value = NULL;
return value;
}
void put(const char* key, T value)
{
mapitem<T>* item = provide(key);
item->m_value = value;
}
bool key_exists(const char* key) const
{
return (locate(key) != 0);
}
~map()
{
}
};
typedef map<const char*,false> cchar_map;
#endif // __map_h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -