Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | // ========== Copyright Header Begin ========================================== |
2 | // | |
3 | // OpenSPARC T2 Processor File: Hash.cc | |
4 | // Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved | |
5 | // 4150 Network Circle, Santa Clara, California 95054, U.S.A. | |
6 | // | |
7 | // * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
8 | // | |
9 | // This program is free software; you can redistribute it and/or modify | |
10 | // it under the terms of the GNU General Public License as published by | |
11 | // the Free Software Foundation; version 2 of the License. | |
12 | // | |
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | // | |
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | // | |
22 | // For the avoidance of doubt, and except that if any non-GPL license | |
23 | // choice is available it will apply instead, Sun elects to use only | |
24 | // the General Public License version 2 (GPLv2) at this time for any | |
25 | // software where a choice of GPL license versions is made | |
26 | // available with the language indicating that GPLv2 or any later version | |
27 | // may be used, or where a choice of which version of the GPL is applied is | |
28 | // otherwise unspecified. | |
29 | // | |
30 | // Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
31 | // CA 95054 USA or visit www.sun.com if you need additional information or | |
32 | // have any questions. | |
33 | // | |
34 | // ========== Copyright Header End ============================================ | |
35 | ||
36 | #include <stdio.h> | |
37 | #include <string.h> | |
38 | #include "cReport.h" | |
39 | #include "Hash.h" | |
40 | ||
41 | ||
42 | // HashTable::insert | |
43 | // | |
44 | // Inserts an item into the hash. | |
45 | // | |
46 | void | |
47 | HashTable::insert(HashValueListNode *node) { | |
48 | unsigned long hash = node->Hash() % _HASH_SIZE; | |
49 | HashValueListNode *table_node = valueNodes[hash]; | |
50 | ||
51 | while (table_node != NULL) { | |
52 | // is this a duplicate? | |
53 | if (0 == strcmp(table_node->mip_name, node->mip_name)) { | |
54 | // done. entry already exists | |
55 | return; | |
56 | } else { | |
57 | table_node = table_node->next; | |
58 | } | |
59 | } | |
60 | ||
61 | //fprintf(stderr, "inserting: %s\n", node->mip_name); | |
62 | ||
63 | // This is a new entry. Prepend to the bucket. | |
64 | table_node = valueNodes[hash]; | |
65 | valueNodes[hash] = node; | |
66 | valueNodes[hash]->next = table_node; | |
67 | valueNodes[hash]->next_overall = most_recent_inserted; | |
68 | most_recent_inserted = valueNodes[hash]; | |
69 | ||
70 | } // HashTable::insert() | |
71 | ||
72 | ||
73 | // HashTable::lookup() | |
74 | // | |
75 | // Return a pointer to the value hashed to by key node. | |
76 | // | |
77 | const HashValueListNode* | |
78 | HashTable::lookup(const HashValueListNode& node) { | |
79 | unsigned long hash = node.Hash() % _HASH_SIZE; | |
80 | HashValueListNode *table_node = valueNodes[hash]; | |
81 | ||
82 | while (table_node != NULL) { | |
83 | // search the bucket | |
84 | if (0 == strcmp(table_node->mip_name, node.mip_name)) { | |
85 | // match. | |
86 | break; | |
87 | } else { | |
88 | table_node = table_node->next; | |
89 | } | |
90 | } | |
91 | return table_node; | |
92 | } | |
93 | ||
94 | ||
95 | // HashTable::next_element | |
96 | // | |
97 | // Returns the `next' element from the hash. Call start_walk() to start at | |
98 | // the beginning. | |
99 | // | |
100 | // NOTE! The order of elements returned by this function is essentially | |
101 | // arbitrary. This is sufficient for traversing the table, but not much | |
102 | // else. | |
103 | // | |
104 | const HashValueListNode* | |
105 | HashTable::next_element() { | |
106 | HashValueListNode* table_node = next_walk; | |
107 | if (table_node != NULL) { | |
108 | next_walk = table_node->next_overall; | |
109 | } | |
110 | return table_node; | |
111 | } | |
112 | ||
113 |