OpenCores
URL https://opencores.org/ocsvn/async_sdm_noc/async_sdm_noc/trunk

Subversion Repositories async_sdm_noc

[/] [async_sdm_noc/] [branches/] [init/] [common/] [tb/] [hash_table.h] - Blame information for rev 70

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 wsong0210
/*
2
 Asynchronous SDM NoC
3
 (C)2011 Wei Song
4
 Advanced Processor Technologies Group
5
 Computer Science, the Univ. of Manchester, UK
6
 
7
 Authors:
8
 Wei Song     wsong83@gmail.com
9
 
10
 License: LGPL 3.0 or later
11
 
12
 A hash table container to store the frames under transmission.
13
 ?? Using STL may be better?
14
 
15
 Sorry, I have no intention to explain the codes below as it is unlikely to modify it.
16
 If there are bugs here, please email me.
17
 
18
 History:
19
 29/06/2010  Initial version. <wsong83@gmail.com>
20
 27/05/2011  Clean up for opensource. <wsong83@gmail.com>
21
 
22
*/
23
 
24
#ifndef HASH_TABLE_H_
25
#define HASH_TABLE_H_
26
 
27
#include <ostream>
28
 
29
using namespace std;
30
 
31
template<typename T, unsigned int HSIZE>
32
class hash_table;
33
 
34
 
35
template<typename T, unsigned int HSIZE>
36
ostream& operator<< (ostream& os, const hash_table<T,HSIZE>& HB) {
37
  T * item;
38
  os << "Records in hash table:" << endl;
39
  for(unsigned int i=0; i<HSIZE; i++) {
40
    os << "vector " << i << ": ";
41
    item = HB.dat[i][0];
42
    while(item != NULL) {
43
      os << *item << "| ";
44
      item = item->next;
45
    }
46
    os << endl;
47
  }
48
  return os;
49
}
50
 
51
template<typename T, unsigned int HSIZE>
52
class hash_table {
53
 
54
 public:
55
  T * dat [HSIZE][2];
56
 
57
  hash_table() {
58
    for(unsigned int i=0; i<HSIZE; i++) {
59
      dat[i][0] = NULL;
60
      dat[i][1] = NULL;
61
    }
62
  }
63
 
64
  void insert ( T& );    /* insert an item into the hash table */
65
  T * find ( long );     /* find an item */
66
  T * find ( const T& ); /* find an item by reading an existing item */
67
  void clear ( T * );    /* delete an item in the hash table */
68
 
69
  ~hash_table() {
70
    for(unsigned int i=0; i<HSIZE; i++) {
71
      if(dat[i][0] != NULL) {
72
        delete dat[i][0];
73
        dat[i][0] = NULL;
74
        dat[i][1] = NULL;
75
      }
76
    }
77
  }
78
 
79
  friend ostream& operator<< <T,HSIZE> (ostream&, const hash_table<T,HSIZE>&);
80
 
81
 private:
82
  T* search (T*, long);
83
};
84
 
85
template<typename T, unsigned int HSIZE>
86
  void hash_table<T,HSIZE>::insert ( T& RD) {
87
  long key = RD.key;
88
  unsigned int vcn = key%HSIZE;
89
  T * entry = dat[vcn][1];
90
 
91
  if(entry == NULL) {        /* the whole vector is empty right now */
92
    dat[vcn][0] = &RD;
93
    dat[vcn][1] = &RD;
94
  } else {                      /* non-empty vector */
95
    entry->next = &RD;
96
    RD.pre = entry;
97
    dat[vcn][1] = &RD;
98
  }
99
}
100
 
101
template<typename T, unsigned int HSIZE>
102
  T * hash_table<T,HSIZE>::find ( long mkey) {
103
  unsigned int vcn = mkey%HSIZE;
104
  return search(dat[vcn][0], mkey);
105
}
106
 
107
template<typename T, unsigned int HSIZE>
108
  T * hash_table<T,HSIZE>::find ( const T& RD) {
109
  return find((long)(RD));
110
}
111
 
112
template<typename T, unsigned int HSIZE>
113
  void hash_table<T,HSIZE>::clear ( T * RD) {
114
  unsigned int vcn = ((long)(*RD)) % HSIZE;
115
 
116
  if(RD->pre == NULL)           /* head of a vector */
117
    dat[vcn][0] = RD->next;
118
 
119
  if(RD->next == NULL)          /* tail of a vector */
120
    dat[vcn][1] = RD->pre;
121
 
122
  if(RD->pre != NULL)
123
    (RD->pre)->next = RD->next;
124
 
125
  if(RD->next != NULL)
126
    (RD->next)->pre = RD->pre;
127
 
128
  RD->pre = NULL;
129
  RD->next = NULL;
130
 
131
  delete RD;
132
}
133
 
134
template<typename T, unsigned int HSIZE>
135
  T* hash_table<T,HSIZE>::search (T* entry, long mkey) {
136
  while(entry != NULL) {
137
    if((long)(*entry) == mkey)
138
      return entry;
139
    else
140
      entry = entry->next;
141
  }
142
  return NULL;
143
}
144
 
145
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.