红黑树ACE_RB_Tree,哈希表ACE_Hash_Map_Manager

来源:互联网 发布:php发送大量邮件 编辑:程序博客网 时间:2024/04/30 04:17

红黑树

红黑树是一种平衡二叉树,它在最差的情况下的对元素的查找性能(时间复杂度)优于其他任何一种数据结构。如果你的设计要求高效稳定,在查找的频率较高并且数据量比较大的情况下红黑树这种数据结构是一个较为理想的选择。ACE通过ACE_RB_Tree模板容器实现了红黑树的实现,提供了元素的插入查找删除遍历功能。ACE_RB_Tree通过ACE_Less_Than模板实现在插入树各个键元素之间进行二叉排序(类似hash表容器中的ACE_Equal_To)ACE提供了若干个ACE_Less_Than模板的特化版本,但是大多数情况下你要为自己定义的键元素创建自己的特化版本。

示例代码

RB_Tree_Functors.h

头文件RB_Tree_Functors.h定义了键元素KeyType并且实现了ACE_Less_Than模板对KeyType的特化
/* -*- C++ -*- */
// RB_Tree_Functors.h,v 1.3 2005/12/02 08:45:34 isisbuilds Exp
 
#ifndef __RB_TREE_FUNCTORS_H_
#define __RB_TREE_FUNCTORS_H_
 
#include "ace/Functor.h"
 
// Listing 1 code/ch05
// Same key type.
class KeyType
{
public:
  KeyType () : val_(0) {}
  KeyType (int i) : val_ (i) {}
  KeyType (const KeyType& kt) { this->val_ = kt.val_; }
  operator int() const { return val_; };
 
private:
  int val_;
};
 
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
//特化KeyType
template<>
class ACE_Less_Than<KeyType>
{
public:
  int operator() (const KeyType k1, const KeyType k2)
  { return k1 < k2; }
};
 
ACE_END_VERSIONED_NAMESPACE_DECL
 
// Listing 1
 
#endif /* __RB_TREE_FUNCTORS_H_ */
 

RB_Tree_Functors.cpp

此示例演示了ACE_RB_Tree模板容器对数据的插入、删除、查找、遍历等功能。
// RB_Tree_Functors.cpp,v 1.2 2006/03/30 12:41:43 jwillemsen Exp
 
#include "ace/Log_Msg.h"
#include "DataElement.h"
#include "RB_Tree_Functors.h"
 
// Listing 0 code/ch05
#include "ace/RB_Tree.h"
#include "ace/Synch.h"
 
// Little helper class.
template<class EXT_ID, class INT_ID>
class Tree : public ACE_RB_Tree<EXT_ID, INT_ID,
                                ACE_Less_Than<EXT_ID>,
                                ACE_Null_Mutex>
{};
// Listing 0
 
class Tree_Example
{
public:
  // Illustrate the tree.
  int run (void);
 
private:
  // Use the forward iterator.
  void iterate_forward (void);
 
  // Use the reverse iterator.
  void iterate_reverse (void);
 
  // Remove all elements from the tree.
  int remove_all (void);
 
private:
  Tree<KeyType, DataElement*> tree_;
};
 
 
int Tree_Example::run ()
{
  ACE_TRACE (ACE_TEXT ("Tree_Example::run"));
 
  DataElement *d  = 0;
  for (int i = 0; i < 100; i++)
    {
      ACE_NEW_RETURN (d, DataElement (i), -1);
      int result = tree_.bind(i, d);
      if (result != 0)
        {
          ACE_ERROR_RETURN((LM_ERROR, "%p/n", "Bind"), -1);
        }
    }
 
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using find: /n")));
  for (int j = 0; j < 100; j++)
    {
      DataElement* d = 0;
      int result = tree_.find (j, d);
      if (result != 0)
        {
          ACE_ERROR_RETURN((LM_ERROR, "%p/n", "Find"), -1);
        }
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), d->getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n")));
 
  // Use the forward iterator.
  this->iterate_forward ();
 
  // Use the reverse iterator.
  this->iterate_reverse ();
 
  // Remove all elements from the tree.
  ACE_ASSERT (this->remove_all ()!= -1);
 
  // Iterate through once again.
  this->iterate_forward ();
 
  return 0;
}
 
void Tree_Example::iterate_forward (void)
{
  ACE_TRACE (ACE_TEXT ("Tree_Example::iterate_forward"));
 
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Forward Iteration /n")));
  for (Tree<KeyType, DataElement*>::iterator iter = tree_.begin ();
       iter != tree_.end (); iter++)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).item ()->getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n")));
}
 
void Tree_Example::iterate_reverse (void)
{
  ACE_TRACE (ACE_TEXT ("Tree_Example::iterate_reverse"));
 
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reverse Iteration /n")));
  for (Tree<KeyType, DataElement*>::reverse_iterator iter = tree_.rbegin ();
       iter != tree_.rend (); iter++)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).item ()->getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n")));
}
 
int Tree_Example::remove_all (void)
{
  ACE_TRACE (ACE_TEXT ("Tree_Example::remove_all"));
 
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Removing elements/n")));
  for (int i = 0; i < 100; i++)
    {
      DataElement * d = 0;
      int result = tree_.unbind (i, d);
      if (result != 0)
        {
          ACE_ERROR_RETURN((LM_ERROR, "%p/n", "Unbind"), -1);
        }
      ACE_ASSERT (d != 0);
      delete d;
    }
 
  return 0;
}
 
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  Tree_Example te;
  return te.run ();
}
 
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_RB_Tree <KeyType, DataElement*,
ACE_Less_Than<KeyType>, ACE_Null_Mutex>
;
template class Tree <KeyType, DataElement*>
;
template class ACE_RB_Tree_Iterator_Base<KeyType, DataElement*, ACE_Less_Than<KeyType>, ACE_Null_Mutex>
;
template class ACE_RB_Tree_Iterator<KeyType, DataElement*, ACE_Less_Than<KeyType>, ACE_Null_Mutex>
;
template class ACE_RB_Tree_Reverse_Iterator<KeyType, DataElement*, ACE_Less_Than<KeyType>, ACE_Null_Mutex>
;
template class ACE_RB_Tree_Node<KeyType, DataElement*>
;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_RB_Tree <KeyType, DataElement*,ACE_Less_Than<KeyType>, ACE_Null_Mutex>
#pragma instantiate Tree <KeyType, DataElement*>
#pragma instantiate ACE_RB_Tree_Iterator_Base<KeyType, DataElement*, ACE_Less_Than<int>, ACE_Null_Mutex>
#pragma instantiate ACE_RB_Tree_Iterator<KeyType, DataElement*, ACE_Less_Than<KeyType>, ACE_Null_Mutex>
#pragma instantiate ACE_RB_Tree_Reverse_Iterator<KeyType, DataElement*, ACE_Less_Than<KeyType>, ACE_Null_Mutex>
#pragma instantiate ACE_RB_Tree_Node<KeyType, DataElement*>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION*/

执行结果

D:/project/ACE_wrappers/examples/APG/Containers>RB_Tree
Using find:
0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:
30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56
:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:8
3:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:
Forward Iteration:
0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:
30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56
:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:8
3:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:
Reverse Iteration:
99:98:97:96:95:94:93:92:91:90:89:88:87:86:85:84:83:82:81:80:79:78:77:76:75:74:73
:72:71:70:69:68:67:66:65:64:63:62:61:60:59:58:57:56:55:54:53:52:51:50:49:48:47:4
6:45:44:43:42:41:40:39:38:37:36:35:34:33:32:31:30:29:28:27:26:25:24:23:22:21:20:
19:18:17:16:15:14:13:12:11:10:9:8:7:6:5:4:3:2:1:0:
Removing elements
Forward Iteration:

哈希表(Hash)

简单的映射表数据元素的排列在容器内部是线性的所以读取的效率会差一些,要想或得更快的读取操作我们可以用Hash表。ACE通过ACE_Hash_Map_Manager类模板实现对Hash表的支持。Hash表能否达到最佳效率取决于设定的hash函数以及为hash创建的桶的数目,ACE_Hash_Map_Manager通过设定哈希函数对其进行微调。

示例代码

Hash_Map.cpp

此示例演示了ACE_Hash_Map_Manager的使用方法,包括数据元素的插入、查找、正向遍历(迭代)、反向遍历。

// Hash_Map.cpp,v 1.1 2004/01/01 21:01:00 shuston Exp
 
#include "ace/Hash_Map_Manager.h"
#include "ace/Synch.h" // needed for the lock
#include "ace/Functor.h"
#include "DataElement.h"
 
// Listing 1 code/ch05
// Little helper class.
template<class EXT_ID, class INT_ID>
//为了方便使用ACE_Hash_Map_Manager_Ex进行简单的封装
class Hash_Map :
      public ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID,
      ACE_Hash<EXT_ID>, ACE_Equal_To<EXT_ID>, ACE_Null_Mutex>
{};
// Listing 1
 
class Hash_Map_Example
{
public:
  // Constructor
  Hash_Map_Example ();
 
  // Illustrate the hash map.
  int run (void);
 
  // Use the forward iterator.
  void iterate_forward (void);
 
  // Use the reverse iterator.
  void iterate_reverse (void);
 
  // Remove all the elements from the map.
  void remove_all (void);
 
private:
  Hash_Map<int, DataElement> map_;
};
 
// Listing 2 code/ch05
Hash_Map_Example::Hash_Map_Example()
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::Hash_Map_Example"));
 
  map_.open (100);
}
// Listing 2
 
int Hash_Map_Example::run (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::run"));
 
  for (int i = 0; i < 100; i++)
    {
      map_.bind (i, DataElement(i));
    }
 
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Map has /n")));
  for (int j = 0; j < 100; j++)
    {
      DataElement d;
      map_.find (j,d);
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), d.getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n")));
 
  // Use the forward iterator.
  this->iterate_forward ();
 
  // Use the reverse iterator.
  this->iterate_reverse ();
 
  // Remove all the elements from the map.
  this->remove_all ();
 
  // Iterate through the map again.
  this->iterate_forward ();
 
  return 0;
}
//正向遍历
void Hash_Map_Example::iterate_forward (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::iterate_forward"));
 
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Forward Iteration /n")));
  for (Hash_Map<int, DataElement>::iterator iter = map_.begin ();
       iter != map_.end (); iter++)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).int_id_.getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n")));
}
//反向遍历
void Hash_Map_Example::iterate_reverse (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::iterate_reverse"));
 
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reverse Iteration /n")));
  for (Hash_Map<int, DataElement>::reverse_iterator iter = map_.rbegin ();
       iter != map_.rend (); iter++)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).int_id_.getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n")));
}
 
void Hash_Map_Example::remove_all (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::remove_all"));
  map_.unbind_all ();
}
 
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  Hash_Map_Example me;
  return me.run ();
}
 
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class Hash_Map <int, DataElement>
;
template class ACE_Hash_Map_Manager_Ex<int, DataElement, ACE_Hash<int>, ACE_Equal_To<int>, ACE_Null_Mutex>
;
template class ACE_Hash_Map_Entry<int, DataElement>
;
template class ACE_Hash_Map_Iterator_Base_Ex<int, DataElement, ACE_Hash<int>, ACE_Equal_To<int>, ACE_Null_Mutex>
;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate Hash_Map <int, DataElement*>
#pragma instantiate ACE_Hash_Map_Manager_Ex<int, DataElement, ACE_Hash<int>, ACE_Equal_To<int>, ACE_Null_Mutex>;
#pragma instantiate ACE_Hash_Map_Entry<int, DataElement>;
#pragma instantiate ACE_Hash_Map_Iterator_Base_Ex<int, DataElement, ACE_Hash<int>, ACE_Equal_To<int>, ACE_Null_Mutex>;
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION*/
 

执行结果

D:/project/ACE_wrappers/examples/APG/Containers>Hash_Map
Map has
0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:
30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56
:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:8
3:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:
Forward Iteration
0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:
30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56
:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:8
3:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:
Reverse Iteration
99:98:97:96:95:94:93:92:91:90:89:88:87:86:85:84:83:82:81:80:79:78:77:76:75:74:73
:72:71:70:69:68:67:66:65:64:63:62:61:60:59:58:57:56:55:54:53:52:51:50:49:48:47:4
6:45:44:43:42:41:40:39:38:37:36:35:34:33:32:31:30:29:28:27:26:25:24:23:22:21:20:
19:18:17:16:15:14:13:12:11:10:9:8:7:6:5:4:3:2:1:0:
Forward Iteration
 
原创粉丝点击