📄 map
字号:
// map stl/clr header
#ifndef _CLI_MAP_
#define _CLI_MAP_
#include <cliext/xtree>
#include <cliext/utility>
namespace cliext {
namespace impl {
//
// TEMPLATE CLASS map_traits
//
template<typename _Key_t, // key type
typename _Mapped_t, // mapped type
bool _Mflag, // true if multiple equivalent keys are permitted
bool _Is_ref_key, // true if key field is allocated
bool _Is_ref_mapped> // true if mapped field is allocated
ref class map_traits
{ // traits required to make tree behave like a map
public:
typedef map_traits<_Key_t, _Mapped_t, _Mflag,
_Is_ref_key, _Is_ref_mapped> _Mytype_t;
typedef _Key_t key_type;
typedef _Mapped_t mapped_type;
typedef _STLCLR GenericPair<_Key_t, _Mapped_t>^ value_type;
typedef _STLCLR BinaryDelegate<key_type, key_type, bool>
key_compare;
typedef _STLCLR BinaryDelegate<value_type, value_type, bool>
value_compare;
typedef _Key_t generic_key;
typedef _Mapped_t generic_mapped;
map_traits()
: comp(gcnew key_compare(&_Key_compare)),
_Multi(_Mflag)
{ // construct with default comparator
}
map_traits(key_compare^ _Pred)
: comp(_Pred),
_Multi(_Mflag)
{ // construct with specified comparator
}
key_compare^ key_comp()
{ // return object for comparing keys
return (comp);
}
value_compare^ value_comp()
{ // return object for comparing values
return (gcnew value_compare(this, &_Mytype_t::_Value_compare));
}
static key_type get_key(value_type% _Val)
{ // extract key from element value
return (_Val->first);
}
static mapped_type get_mapped(value_type% _Val)
{ // extract mapped from element value
return (_Val->second);
}
static void unmake_value(value_type% _Val)
{ // free allocated fields as needed
_Cont_make_value<key_type, _Is_ref_key>::unmake_value(
_Val->first);
_Cont_make_value<mapped_type, _Is_ref_mapped>::unmake_value(
_Val->second);
}
_STLCLR_FIELD_ACCESS:
bool _Value_compare(value_type _Left, value_type _Right)
{ // test if _Left ordered before _Right
return (comp(_Left->first, _Right->first));
}
// data members
key_compare^ comp; // the comparator predicate for keys: < or >
bool _Multi; // true if multiple equivalents keys are permitted
};
//
// TEMPLATE REF CLASS TreeKVPEnumerator
//
template<typename TKey,
typename TMapped,
typename TValue>
ref class TreeKVPEnumerator
: public _STLCLR TreeEnumerator<TKey, TValue>,
System::Collections::Generic::IEnumerator<
System::Collections::Generic::KeyValuePair<
TKey, TMapped>>
{ // typed enumerator for a tree-based dictionary
public:
typedef TreeKVPEnumerator<TKey, TMapped, TValue> _Mytype_t;
typedef _STLCLR TreeEnumerator<TKey, TValue> _Mybase_t;
typedef _STLCLR Generic::INode<TValue> _Mynode_it;
typedef System::Collections::Generic::KeyValuePair<TKey, TMapped>
_Mykvpair_t;
TreeKVPEnumerator(_Mynode_it^ _First)
: _Mybase_t(_First)
{ // construct from initial tree node
}
~TreeKVPEnumerator()
{ // destroy the object
}
property _Mykvpair_t Current
{ // get or set next element
virtual _Mykvpair_t get() new
{ // get next element
TValue _Myval = _Mybase_t::Current;
return (_Mykvpair_t(_Myval->first, _Myval->second));
}
virtual void set(_Mykvpair_t)
{ // set next element
throw gcnew System::InvalidOperationException();
}
};
};
//
// TEMPLATE CLASS map_base0
//
template<typename _Key_t,
typename _Mapped_t,
bool _Is_ref_key,
bool _Is_ref_mapped>
ref class map_base0
: public tree<
map_traits<_Key_t, _Mapped_t, false,
_Is_ref_key, _Is_ref_mapped> >,
System::Collections::Generic::IDictionary<_Key_t, _Mapped_t>
{ // ordered red-black tree of unique keys + values
public:
// types
typedef map_base0<_Key_t, _Mapped_t, _Is_ref_key, _Is_ref_mapped>
_Mytype_t;
typedef tree<
map_traits<_Key_t, _Mapped_t, false,
_Is_ref_key, _Is_ref_mapped> > _Mybase_t;
typedef _STLCLR GenericPair<_Key_t, _Mapped_t> _Object_t;
typedef _Key_t key_type;
typedef _Mapped_t mapped_type;
typedef System::Collections::Generic::KeyValuePair<_Key_t, _Mapped_t>
_Mykvpair_t;
typedef cli::array<_Mykvpair_t> _Mykvarray_t;
// basics
map_base0()
: _Mybase_t()
{ // construct empty map from defaults
}
map_base0(map_base0% _Right)
: _Mybase_t((_Mybase_t%)_Right)
{ // construct by copying a list
}
map_base0% operator=(map_base0% _Right)
{ // assign
_Mybase_t::operator=(_Right);
return (*this);
}
// constructors
explicit map_base0(key_compare^ _Pred)
: _Mybase_t(_Pred)
{ // construct empty map from comparator
}
template<typename _Iter_t>
map_base0(_Iter_t _First, _Iter_t _Last)
: _Mybase_t()
{ // construct map from [_First, _Last), default comparator
for (; _First != _Last; ++_First)
insert((value_type)*_First);
}
template<typename _Iter_t>
map_base0(_Iter_t _First, _Iter_t _Last, key_compare^ _Pred)
: _Mybase_t(_Pred)
{ // construct map from [_First, _Last), comparator
for (; _First != _Last; ++_First)
insert((value_type)*_First);
}
property mapped_type default[key_type]
{ // get or set subscripted element
virtual mapped_type get(key_type _Key)
{ // get _Key element
_Pairnb _Ans = insert_node(
gcnew _Object_t(_Key));
return (_Ans.first->_Value->second);
}
virtual void set(key_type _Key, mapped_type _Val)
{ // set _Key element
node_type^ _Node = insert_node(
gcnew _Object_t(_Key)).first;
_Node->_Value->second = _Val;
}
};
// interfaces
private:
property size_type Count_kvpair
{ // element count
virtual size_type get() sealed
= System::Collections::Generic::ICollection<_Mykvpair_t>
::Count::get
{ // get element count
return (size());
}
};
property bool IsReadOnly_kvpair
{ // test if read only
virtual bool get() sealed
= System::Collections::Generic::ICollection<_Mykvpair_t>
::IsReadOnly::get
{ // test if read only
return (false);
}
};
virtual void CopyTo(_Mykvarray_t^ _Dest, int _First) sealed
= System::Collections::Generic::ICollection<_Mykvpair_t>::CopyTo
{ // copy to _Dest, beginning at _First
node_type^ _Node = head_node();
for (int _Idx = size(); 0 <= --_Idx; )
{ // copy back to front
_Node = _Node->prev_node();
_Dest[_First + _Idx] = _Mykvpair_t(_Node->_Value->first,
_Node->_Value->second);
}
}
virtual System::Collections::Generic::IEnumerator<_Mykvpair_t>^
GetEnumerator() sealed
= System::Collections::Generic::IEnumerable<_Mykvpair_t>
::GetEnumerator
{ // get enumerator for the container
return (gcnew TreeKVPEnumerator<
_Key_t, _Mapped_t, _Object_t^>(front_node()));
}
virtual void Add(_Mykvpair_t _Kvpair) sealed
= System::Collections::Generic::ICollection<_Mykvpair_t>::Add
{ // add element with value _Kvpair
insert_node(gcnew _Object_t(_Kvpair.Key, _Kvpair.Value));
}
virtual void Clear_dictionary() sealed
= System::Collections::Generic::ICollection<_Mykvpair_t>::Clear
{ // erase all elements
clear();
}
virtual bool Contains(_Mykvpair_t _Kvpair) sealed
= System::Collections::Generic::ICollection<_Mykvpair_t>::Contains
{ // search for element matching value _Kvpair
_Object_t^ _Val = gcnew _Object_t(_Kvpair.Key, _Kvpair.Value);
for (node_type^ _Node = front_node(); _Node != head_node();
_Node = _Node->next_node())
if (((System::Object^)_Val)->Equals(
(System::Object^)_Node->_Value))
return (true);
return (false);
}
virtual bool Remove(_Mykvpair_t _Kvpair) sealed
= System::Collections::Generic::ICollection<_Mykvpair_t>::Remove
{ // remove first element matching key _Keypair
for (node_type^ _Node = front_node(); _Node != head_node();
_Node = _Node->next_node())
if (((System::Object^)_Kvpair.Key)->Equals(
(System::Object^)_Node->_Value->first)
&& ((System::Object^)_Kvpair.Value)->Equals(
(System::Object^)_Node->_Value->second))
{ // found a match, remove it
erase_node(_Node);
return (true);
}
return (false);
}
property System::Collections::Generic::ICollection<_Key_t>^ Keys
{ // get or set collection of keys
virtual System::Collections::Generic::ICollection<_Key_t>^
get() sealed
= System::Collections::Generic::IDictionary<_Key_t, _Mapped_t>
::Keys::get
{ // get key elements
System::Collections::Generic::List<_Key_t>^
_List = gcnew System::Collections::Generic::List<_Key_t>;
for (iterator _It = begin(); _It != end(); ++_It)
_List->Add(_It->first);
return (_List);
}
};
property System::Collections::Generic::ICollection<_Mapped_t>^ Values
{ // get or set collection of mapped values
virtual System::Collections::Generic::ICollection<_Mapped_t>^
get() sealed
= System::Collections::Generic::IDictionary<_Key_t, _Mapped_t>
::Values::get
{ // get mapped elements
System::Collections::Generic::List<_Mapped_t>^
_List = gcnew System::Collections::Generic::List<_Mapped_t>;
for (iterator _It = begin(); _It != end(); ++_It)
_List->Add(_It->second);
return (_List);
}
};
virtual void Add(key_type _Keyval, mapped_type _Mappedval) sealed
= System::Collections::Generic::IDictionary<_Key_t, _Mapped_t>
::Add
{ // add element with value (_Keyval, _Mappedval)
insert_node(gcnew _Object_t(_Keyval, _Mappedval));
}
virtual bool ContainsKey(key_type _Keyval) sealed
= System::Collections::Generic::IDictionary<_Key_t, _Mapped_t>
::ContainsKey
{ // search for element matching key _Keyval
return (_Mybase_t::count(_Keyval) != 0);
}
virtual bool Remove(key_type _Keyval) sealed
= System::Collections::Generic::IDictionary<_Key_t, _Mapped_t>
::Remove
{ // remove first element matching key _Keyval
return (_Mybase_t::erase(_Keyval) != 0);
}
virtual bool TryGetValue(key_type _Keyval, mapped_type% _Mappedval) sealed
= System::Collections::Generic::IDictionary<_Key_t, _Mapped_t>
::TryGetValue
{ // search for element matching key _Keyval and copy mapped value
iterator _Iter = _Mybase_t::find(_Keyval);
if (_Iter == end())
return (false);
else
{ // found, copy mapped
_Mappedval = _Iter->second;
return (true);
}
}
};
//
// TEMPLATE CLASS map_base
//
template<typename _Key_t,
typename _Mapped_t,
bool _Is_ref_key,
bool _Is_ref_mapped>
ref class map_base
: public map_base0<_Key_t, _Mapped_t, _Is_ref_key, _Is_ref_mapped>,
System::Collections::Generic::ICollection<
_STLCLR GenericPair<_Key_t, _Mapped_t>^>,
System::Collections::Generic::IEnumerable<
_STLCLR GenericPair<_Key_t, _Mapped_t>^>
{ // ordered red-black tree of unique keys + values
public:
// types
typedef map_base<_Key_t, _Mapped_t, _Is_ref_key, _Is_ref_mapped>
_Mytype_t;
typedef map_base0<_Key_t, _Mapped_t, _Is_ref_key, _Is_ref_mapped>
_Mybase_t;
typedef _STLCLR GenericPair<_Key_t, _Mapped_t> _Object_t;
typedef _Key_t key_type;
typedef _Mapped_t mapped_type;
// typedef typename _Traits_t::value_type value_type;
// typedef typename _Traits_t::key_compare key_compare;
// typedef typename _Traits_t::value_compare value_compare;
// typedef int size_type;
// typedef int difference_type;
// typedef _Value_t value_type;
// typedef value_type% reference;
// typedef value_type% const_reference;
// typedef _Mycont_it generic_container;
// typedef value_type generic_value;
// typedef _STLCLR GenericPair<iterator^, bool> pair_iter_bool;
// typedef _STLCLR GenericPair<iterator^, iterator^> pair_iter_iter;
// basics
map_base()
: _Mybase_t()
{ // construct empty map from defaults
}
map_base(map_base% _Right)
: _Mybase_t((_Mybase_t%)_Right)
{ // construct by copying a list
}
map_base% operator=(map_base% _Right)
{ // assign
_Mybase_t::operator=(_Right);
return (*this);
}
// constructors
explicit map_base(key_compare^ _Pred)
: _Mybase_t(_Pred)
{ // construct empty map from comparator
}
template<typename _Iter_t>
map_base(_Iter_t _First, _Iter_t _Last)
: _Mybase_t()
{ // construct map from [_First, _Last), default comparator
for (; _First != _Last; ++_First)
insert((value_type)*_First);
}
template<typename _Iter_t>
map_base(_Iter_t _First, _Iter_t _Last, key_compare^ _Pred)
: _Mybase_t(_Pred)
{ // construct map from [_First, _Last), comparator
for (; _First != _Last; ++_First)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -