2 Pitchfork Music Player Daemon Client
3 Copyright (C) 2007 Roger Bystrøm
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /* HashMap implementation, guess it's more of a key-lookup-linked-list though */
21 function HashNode(key, val) {
31 * Usage: var it = hashmap.iterator();
33 * while(tmp = it.next());
35 function HashMapIterator(first) {
39 HashMapIterator.prototype.next = function() {
48 /* get an entry from the list */
49 HashMap.prototype.get = function(key) {
59 /* Removes an entry from the list and returns it */
60 HashMap.prototype.remove = function(key) {
62 debug("wops, header null");
63 if(key==this.header.key) {
64 var tmp = this.header;
65 this.header = this.header.next;
70 while(n.next != null) {
71 if(n.next.key == key) {
82 /* put a new entry in the list */
83 HashMap.prototype.put = function(key, value) {
84 var node = new HashNode(key,value);
85 var next = this.header;
90 while(next.next!=null)
96 HashMap.prototype.insert = function(key, value) {
97 var node = new HashNode(key,value);
98 node.next = this.header;
102 HashMap.prototype.iterator = function() {
103 return new HashMapIterator(this.header);
107 /* A proper hashtable implementation
109 function Hashtable() {
110 this.data = new Object();
114 /* clears the hashtable */
115 Hashtable.prototype.clear = function() {
116 for(var key in this.data)
117 delete this.data[key];
121 /* tests whether the specified value is in this hashtable
122 returns true if it does, falseotherwise */
123 Hashtable.prototype.contains = function(obj) {
124 if(obj==null) return false;
126 for(var opt in this.data) {
127 if(obj == this.data[opt])
133 /* Tests whether the specified key is in this hashtable
134 returns true if it does, false otherwise*/
135 Hashtable.prototype.containsKey = function(key) {
136 return typeof this.data[key] != "undefined" && this.data[key] != null;
139 Hashtable.prototype.put = function (key, value) {
140 this.data[key] = value;
144 /** puts all the in to this hashtable */
145 Hashtable.prototype.putAll = function(map) {
146 for(var key in map) {
147 this.data[key] = map[key];
152 Hashtable.prototype.get = function (key) {
153 return this.data[key];
156 Hashtable.prototype.isEmpty = function() {
157 return this._size == 0;
160 remove the object and key from this hashtable,
161 returns the object if it's there, null otherwise
163 Hashtable.prototype.remove = function(key) {
164 if(!this.containsKey(key))
166 var ret = this.data[key];
167 delete this.data[key];
171 /** Returns the number of keys in this hashtable */
172 Hashtable.prototype.size = function() {
176 /** Returns an array with all the values in this hashtable */
177 Hashtable.prototype.elements = function() {
178 var ret = new Array();
179 for (var key in this.data) {
180 ret.push(this.data[key]);
185 /** Returns an array with all the keys in this hashtable */
186 Hashtable.prototype.keys = function() {
187 var ret = new Array();
188 for (var key in this.data) {
194 /** Returns a string with the keys and values */
195 Hashtable.prototype.toString = function() {
197 for(var key in this.data) {
198 ret +="{" + key + "," + value + "}";