hash_table.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * hash_table.cc
  3.  * Copyright (C) 2000 by the University of Southern California
  4.  * $Id: hash_table.cc,v 1.5 2005/08/25 18:58:04 johnh Exp $
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License,
  8.  * version 2, as published by the Free Software Foundation.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18.  *
  19.  *
  20.  * The copyright of this module includes the following
  21.  * linking-with-specific-other-licenses addition:
  22.  *
  23.  * In addition, as a special exception, the copyright holders of
  24.  * this module give you permission to combine (via static or
  25.  * dynamic linking) this module with free software programs or
  26.  * libraries that are released under the GNU LGPL and with code
  27.  * included in the standard release of ns-2 under the Apache 2.0
  28.  * license or under otherwise-compatible licenses with advertising
  29.  * requirements (or modified versions of such code, with unchanged
  30.  * license).  You may copy and distribute such a system following the
  31.  * terms of the GNU GPL for this module and the licenses of the
  32.  * other code concerned, provided that you include the source code of
  33.  * that other code when and as the GNU GPL requires distribution of
  34.  * source code.
  35.  *
  36.  * Note that people who make modified versions of this module
  37.  * are not obligated to grant this special exception for their
  38.  * modified versions; it is their choice whether to do so.  The GNU
  39.  * General Public License gives permission to release a modified
  40.  * version without this exception; this exception also makes it
  41.  * possible to release a modified version which carries forward this
  42.  * exception.
  43.  *
  44.  */
  45. // Written by Chalermek Intanagonwiwat
  46. #include "tclcl.h"
  47. #include "diff_header.h"
  48. #include "hash_table.h"
  49. #include "diff_prob.h"
  50. Pkt_Hash_Entry::~Pkt_Hash_Entry() {
  51.     clear_fromagent(from_agent);
  52.     if (timer != NULL)
  53.       delete timer;
  54. }
  55. void Pkt_Hash_Entry::clear_fromagent(From_List *list)
  56. {
  57.   From_List *cur=list;
  58.   From_List *temp = NULL;
  59.   while (cur != NULL) {
  60.     temp = FROM_NEXT(cur);
  61.     delete cur;
  62.     cur = temp;
  63.   }
  64. }
  65. void Pkt_Hash_Table::reset()
  66. {
  67.   Pkt_Hash_Entry *hashPtr;
  68.   Tcl_HashEntry *entryPtr;
  69.   Tcl_HashSearch searchPtr;
  70.   entryPtr = Tcl_FirstHashEntry(&htable, &searchPtr);
  71.   while (entryPtr != NULL) {
  72.     hashPtr = (Pkt_Hash_Entry *)Tcl_GetHashValue(entryPtr);
  73.     delete hashPtr;
  74.     Tcl_DeleteHashEntry(entryPtr);
  75.     entryPtr = Tcl_NextHashEntry(&searchPtr);
  76.   }
  77. }
  78. Pkt_Hash_Entry *Pkt_Hash_Table::GetHash(ns_addr_t sender_id, 
  79. unsigned int pk_num)
  80. {
  81.   unsigned int key[3];
  82.   key[0] = sender_id.addr_;
  83.   key[1] = sender_id.port_;
  84.   key[2] = pk_num;
  85.   Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(&htable, (char *)key);
  86.   if (entryPtr == NULL )
  87.      return NULL;
  88.   return (Pkt_Hash_Entry *)Tcl_GetHashValue(entryPtr);
  89. }
  90. void Pkt_Hash_Table::put_in_hash(hdr_cdiff *dfh)
  91. {
  92.     Tcl_HashEntry *entryPtr;
  93.     Pkt_Hash_Entry    *hashPtr;
  94.     unsigned int key[3];
  95.     int newPtr;
  96.     key[0]=(dfh->sender_id).addr_;
  97.     key[1]=(dfh->sender_id).port_;
  98.     key[2]=dfh->pk_num;
  99.     entryPtr = Tcl_CreateHashEntry(&htable, (char *)key, &newPtr);
  100.     if (!newPtr)
  101.       return;
  102.     hashPtr = new Pkt_Hash_Entry;
  103.     hashPtr->forwarder_id = dfh->forward_agent_id;
  104.     hashPtr->from_agent = NULL;
  105.     hashPtr->is_forwarded = false;
  106.     hashPtr->num_from = 0;
  107.     hashPtr->timer = NULL;
  108.     Tcl_SetHashValue(entryPtr, hashPtr);
  109. }
  110. void Data_Hash_Table::reset()
  111. {
  112.   Tcl_HashEntry *entryPtr;
  113.   Tcl_HashSearch searchPtr;
  114.   entryPtr = Tcl_FirstHashEntry(&htable, &searchPtr);
  115.   while (entryPtr != NULL) {
  116.     Tcl_DeleteHashEntry(entryPtr);
  117.     entryPtr = Tcl_NextHashEntry(&searchPtr);
  118.   }
  119. }
  120. Tcl_HashEntry *Data_Hash_Table::GetHash(int *attr)
  121. {
  122.   return Tcl_FindHashEntry(&htable, (char *)attr);
  123. }
  124. void Data_Hash_Table::PutInHash(int *attr)
  125. {
  126.     int newPtr;
  127.     Tcl_CreateHashEntry(&htable, (char *)attr, &newPtr);
  128. }