Skip to content

Commit

Permalink
Cleanup container.h/inl with value_type, key_type and mapped_type.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenliang committed May 16, 2011
1 parent 5c41d5b commit a9066d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
32 changes: 17 additions & 15 deletions util/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,31 @@ namespace util
#define UTIL_CONST_FOREACH(i, container, type) \
for( type::const_iterator i = (container).begin(); i != (container).end(); ++i )

// 容器约定:
// 元素的类型为 Container::value_type
// 元素键值的类型为 Container::key_type ,即 Container::value_type::first_type
// 元素映射值的类型为 Container::mapped_type ,即 Container::value_type::second_type

// 遍历容器各个元素,对各个元素调用 Functor::operator()
template <class Container, class Functor>
inline void Vist(Container& container, Functor& functor);

// 判断容器是否存在某个 Key
template <class Container, class Key>
inline bool Contain(Container& container, Key& key);
// 判断容器是否存在一个以 key 为键值的元素
template <class Container>
inline bool Contain(Container& container, const typename Container::key_type& key);

// 删除容器的某个键为 Key 的元素
template <class Container, class Key>
inline bool Remove(Container& container, Key& key);
// 删除容器的键值为 key 的元素
template <class Container>
inline bool Remove(Container& container, const typename Container::key_type& key);

// 向容器插入一个新元素
template <class Container, class ValueType>
inline bool Insert(Container& container, ValueType& value);

// TODO: 如何把两个 Get 合并?

template <class Key, class Value>
inline Value& Get(std::map<Key, Value>& container, const Key& key);
template <class Container>
inline bool Insert(Container& container, const typename Container::value_type& value);

template <class Key, class Value>
inline Value& Get(std::multimap<Key, Value>& container, const Key& key);
// 获取第一个以 key 为键值的元素的映射值
template <class Container>
inline typename Container::mapped_type& Get(
Container& container, const typename Container::key_type& key);

#include "container.inl"
}
33 changes: 10 additions & 23 deletions util/container.inl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ inline void Vist(Container& container, Functor& functor)
std::for_each(container.begin(), container.end(), functor);
}

template <class Container, class Key>
inline bool Contain(Container& container, Key& key)
template <class Container>
inline bool Contain(Container& container, const typename Container::key_type& key)
{
return container.find(key) != container.end();
}

template <class Container, class Key>
inline bool Remove(Container& container, Key& key)
template <class Container>
inline bool Remove(Container& container, const typename Container::key_type& key)
{
if (Contain(container, key))
{
Expand All @@ -23,18 +23,17 @@ inline bool Remove(Container& container, Key& key)
return false;
}

template <class Container, class ValueType>
inline bool Insert(Container& container, ValueType& value)
template <class Container>
inline bool Insert(Container& container, const typename Container::value_type& value)
{
return container.insert(container.begin(), value) != container.end();
}

// TODO: ÈçºÎ°ÑÁ½¸ö Get ºÏ²¢£¿

template <class Key, class Value>
inline Value& Get(std::map<Key, Value>& container, const Key& key)
template <class Container>
inline typename Container::mapped_type& Get(
Container& container, const typename Container::key_type& key)
{
static Value value;
static Container::mapped_type value;

if (Contain(container, key))
return container.find(key)->second;
Expand All @@ -44,15 +43,3 @@ inline Value& Get(std::map<Key, Value>& container, const Key& key)
return value;
}

template <class Key, class Value>
inline Value& Get(std::multimap<Key, Value>& container, const Key& key)
{
static Value value;

if (Contain(container, key))
return container.find(key)->second;
else
assert(false);

return value;
}

0 comments on commit a9066d8

Please sign in to comment.