//====================================================================
//class Hashtable:散列
Hashtable = function ()
{
this.hash = new Array();
Hashtable.prototype.Clear = function()
{
this.hash = new Array();
}
Hashtable.prototype.ContainsKey = function(key)
{
var exists = false;
if (this.hash[key] != null)
{
exists = true;
}
return exists;
}
Hashtable.prototype.ContainsValue = function(value)
{
var contains = false;
if (value != null)
{
for (var i in this.hash)
{
if (this.hash[i] == value)
{
contains = true;
break;
}
}
}
return contains;
}
Hashtable.prototype.Get = function(key)
{
return this.hash[key];
}
Hashtable.prototype.IsEmpty = function()
{
return (parseInt(this.size()) == 0) ? true : false;
}
Hashtable.prototype.GetKeys = function()
{
var keys = new Array();
for (var i in this.hash)
{
if (this.hash[i] != null)
keys.push(i);
}
return keys;
}
Hashtable.prototype.Put = function(key, value)
{
if (key == null || value == null)
{
return;
}
else
{
this.hash[key] = value;
}
}
Hashtable.prototype.Remove = function(key)
{
var rtn = this.hash[key];
this.hash[key] = null;
return rtn;
}
Hashtable.prototype.GetSize = function()
{
var size = 0;
for (var i in this.hash)
{
if (this.hash[i] != null)
{
size ++;
}
}
return size;
}
Hashtable.prototype.ToString = function()
{
var result = "";
for (var i in this.hash)
{
if (this.hash[i] != null)
result += "{" + i + "},{" + this.hash[i] + "}/n";
}
return result;
}
Hashtable.prototype.GetValues = function()
{
var values = new Array();
for (var i in this.hash)
{
if (this.hash[i] != null)
values.push(this.hash[i]);
}
return values;
}
Hashtable.prototype.EntrySet = function()
{
return this.hash;
}
}
//====================================================================
//class Queue:队列
Queue = function()
{
this.queue = [];
this.queueSpace = 0;
Queue.prototype.GetSize = function()
{
return this.queue.length - this.queueSpace;
}
Queue.prototype.IsEmpty = function()
{
return (this.queue.length == 0);
}
Queue.prototype.Enqueue = function(element)
{
this.queue.push(element);
}
Queue.prototype.Dequeue = function()
{
var element = undefined;
if (this.queue.length)
{
element = this.queue[this.queueSpace];
if (++this.queueSpace * 2 >= this.queue.length)
{
this.queue = this.queue.slice(this.queueSpace);
this.queueSpace=0;
}
}
return element;
}
Queue.prototype.GetOldestElement = function()
{
var element = undefined;
if (this.queue.length)
{
element = this.queue[this.queueSpace];
}
return element;
}
}
//====================================================================
//class ArrayList
ArrayList = function ()
{
this.elems = [];
this.elementCount = 0;
ArrayList.prototype.GetSize = function ()
{
return this.elementCount;
}
ArrayList.prototype.Add = function (element)
{
this.EnsureCapacity(this.elementCount + 1);
this.elems[this.elementCount++] = element;
return true;
}
ArrayList.prototype.AddElementAt = function (index, element)
{
if (index > this.elementCount || index < 0)
{
return;
}
this.EnsureCapacity(this.elementCount + 1);
for (var i = this.elementCount + 1; i > index; i--)
{
this.elems[i] = this.elems[i - 1];
}
this.elems[index] = element;
this.elementCount++;
}
ArrayList.prototype.SetElementAt = function (index, element)
{
if (index > this.elementCount || index < 0)
{
return;
}
this.elems[index] = element;
}
ArrayList.prototype.ToString = function ()
{
var str = "";
for (var i = 0; i < this.elementCount; i++)
{
if (i > 0)
{
str += "|";
}
str += this.elems[i];
}
str += "";
return str;
}
ArrayList.prototype.Get = function (index)
{
if (index >= this.elementCount)
{
return;
}
return this.elems[index];
}
ArrayList.prototype.Remove = function (index)
{
if (index >= this.elementCount)
{
return null;
}
var oldData = this.elems[index];
for (var i = index; i < this.elementCount - 1; i++)
{
this.elems[i] = this.elems[i + 1];
}
this.elems[this.elementCount - 1] = null;
this.elementCount--;
return oldData;
}
ArrayList.prototype.IsEmpty = function ()
{
return elementCount == 0;
}
ArrayList.prototype.IndexOf = function (elem)
{
for (var i = 0; i < this.elementCount; i++)
{
if (this.elems[i] == elem)
{
return i;
}
}
return -1;
}
ArrayList.prototype.LastIndexOf = function (elem)
{
for (var i = this.elementCount - 1; i >= 0; i--)
{
if (this.elems[i] == elem)
{
return i;
}
}
return -1;
}
ArrayList.prototype.Contains = function (elem)
{
return this.IndexOf(elem) >= 0;
}
ArrayList.prototype.EnsureCapacity = function (minCapacity)
{
var oldCapacity = this.elems.length;
if (minCapacity > oldCapacity)
{
var oldData = this.elems;
var newCapacity = parseInt((oldCapacity * 3) / 2 + 1);
if (newCapacity < minCapacity)
{
newCapacity = minCapacity;
}
this.elems = new Array(newCapacity);
for (var i = 0; i < oldCapacity; i++)
{
this.elems[i] = oldData[i];
}
}
}
}
JS Collection--javascript 集合类( 在使用Comet来进行数据交互时根据网上的资源写了几个集合类。自我感觉还比较好用,呵呵! )
最新推荐文章于 2023-02-14 21:47:31 发布
本文介绍了三种常用的数据结构:散列表实现高效查找与存储,队列实现先进先出的数据操作原则,以及动态数组实现灵活的元素增删。这些数据结构在程序设计中扮演着重要角色,能够有效提升程序效率。
744

被折叠的 条评论
为什么被折叠?



