Javascript: hash tables in javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Copyright 2010 Tim Down. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * 源碼下載: http://code.google.com/p/jshashtable/<br><a href="http://www.timdown.co.uk/jshashtable/" rel="noopener nofollow">http://www.timdown.co.uk/jshashtable/</a> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var Hashtable=( function (){ var p= "function" ; var n=( typeof Array.prototype.splice==p)? function (s,r){s.splice(r,1)}: function (u,t){ var s,v,r; if (t===u.length-1){u.length=t} else {s=u.slice(t+1);u.length=t; for (v=0,r=s.length;v<r;++v){u[t+v]=s[v]}}}; function a(t){ var r; if ( typeof t== "string" ){ return t} else { if ( typeof t.hashCode==p){r=t.hashCode(); return ( typeof r== "string" )?r:a(r)} else { if ( typeof t.toString==p){ return t.toString()} else { try { return String(t)} catch (s){ return Object.prototype.toString.call(t)}}}}} function g(r,s){ return r.equals(s)} function e(r,s){ return ( typeof s.equals==p)?s.equals(r):(r===s)} function c(r){ return function (s){ if (s=== null ){ throw new Error( "null is not a valid " +r)} else { if ( typeof s== "undefined" ){ throw new Error(r+ " must not be undefined" )}}}} var q=c( "key" ),l=c( "value" ); function d(u,s,t,r){ this [0]=u; this .entries=[]; this .addEntry(s,t); if (r!== null ){ this .getEqualityFunction= function (){ return r}}} var h=0,j=1,f=2; function o(r){ return function (t){ var s= this .entries.length,v,u= this .getEqualityFunction(t); while (s--){v= this .entries[s]; if (u(t,v[0])){ switch (r){ case h: return true ; case j: return v; case f: return [s,v[1]]}}} return false }} function k(r){ return function (u){ var v=u.length; for ( var t=0,s= this .entries.length;t<s;++t){u[v+t]= this .entries[t][r]}}}d.prototype={getEqualityFunction: function (r){ return ( typeof r.equals==p)?g:e},getEntryForKey:o(j),getEntryAndIndexForKey:o(f),removeEntryForKey: function (s){ var r= this .getEntryAndIndexForKey(s); if (r){n( this .entries,r[0]); return r[1]} return null },addEntry: function (r,s){ this .entries[ this .entries.length]=[r,s]},keys:k(0),values:k(1),getEntries: function (s){ var u=s.length; for ( var t=0,r= this .entries.length;t<r;++t){s[u+t]= this .entries[t].slice(0)}},containsKey:o(h),containsValue: function (s){ var r= this .entries.length; while (r--){ if (s=== this .entries[r][1]){ return true }} return false }}; function m(s,t){ var r=s.length,u; while (r--){u=s[r]; if (t===u[0]){ return r}} return null } function i(r,s){ var t=r[s]; return (t&&(t instanceof d))?t: null } function b(t,r){ var w= this ; var v=[]; var u={}; var x=( typeof t==p)?t:a; var s=( typeof r==p)?r: null ; this .put= function (B,C){q(B);l(C); var D=x(B),E,A,z= null ;E=i(u,D); if (E){A=E.getEntryForKey(B); if (A){z=A[1];A[1]=C} else {E.addEntry(B,C)}} else {E= new d(D,B,C,s);v[v.length]=E;u[D]=E} return z}; this .get= function (A){q(A); var B=x(A); var C=i(u,B); if (C){ var z=C.getEntryForKey(A); if (z){ return z[1]}} return null }; this .containsKey= function (A){q(A); var z=x(A); var B=i(u,z); return B?B.containsKey(A): false }; this .containsValue= function (A){l(A); var z=v.length; while (z--){ if (v[z].containsValue(A)){ return true }} return false }; this .clear= function (){v.length=0;u={}}; this .isEmpty= function (){ return !v.length}; var y= function (z){ return function (){ var A=[],B=v.length; while (B--){v[B][z](A)} return A}}; this .keys=y( "keys" ); this .values=y( "values" ); this .entries=y( "getEntries" ); this .remove= function (B){q(B); var C=x(B),z,A= null ; var D=i(u,C); if (D){A=D.removeEntryForKey(B); if (A!== null ){ if (!D.entries.length){z=m(v,C);n(v,z); delete u[C]}}} return A}; this .size= function (){ var A=0,z=v.length; while (z--){A+=v[z].entries.length} return A}; this .each= function (C){ var z=w.entries(),A=z.length,B; while (A--){B=z[A];C(B[0],B[1])}}; this .putAll= function (H,C){ var B=H.entries(); var E,F,D,z,A=B.length; var G=( typeof C==p); while (A--){E=B[A];F=E[0];D=E[1]; if (G&&(z=w.get(F))){D=C(F,z,D)}w.put(F,D)}}; this .clone= function (){ var z= new b(t,r);z.putAll(w); return z}} return b})(); |
示例用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | < script type="text/javascript" src="js/jshashtable.js"></ script > < script type="text/javascript"> // var key1 = new Object(); var key2 = new Object(); var key3="0704"; var h = new Hashtable(); h.put(key1, "First"); h.put(key2, "Second"); h.put(key3,"今天是塗聚文的生日"); //alert( h.get(key1) ); // Alerts "First" //alert( h.get(key2) ); // Alerts "Second" var tod=new Date(); var sy=tod.getFullYear().toString(); var sm=tod.getMonth().toString(); var sd=tod.getDay().toString(); var nod=Date.parse("2008/03/20"); if(sm.lengh=1) { sm="0"+sm; } if(sd.lengh=1) { sd="0"+sd; } document.write(sy+"-"+sm+"-"+sd+"< br />"); var smd=sm+sd; //alert(); document.write(h.get("0704")); //document.forms[0].elements[0].value="2"; </ script > </ head > < body > < input id="geo" name="geo" class="geo" type="text" value="ge"/> < form name="form1" method="post" action=""> < input type="text" id="geovindu" name="geovindu" class="geovindu" value=""/> < script language="javascript" type="text/javascript"> var s=h.get("0704"); var s1=h.get(key1); document.getElementById("geovindu").value=s+","+s1+h.get(key3); //顯示頁面的所有要素 for (i=0;i< document.form1.elements.length ;i++){ document.write("<br>"); var curname=document.form1.elements[i].name var curvalue=document.form1.elements[i].value document.write(curname+":"+curvalue); } // </ script > </ form > |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Ajax&JavaScript
标签:
JavaScript
, Hashtable
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!