1.c++ splice 使ç¨
2.ç®å说说ConcurrentSkipListMap
c++ splice 使ç¨
å¦æä½ æ³éªè¯çè¯å°±æä½ ç¬¬ä¸æ¬¡è°ç¨spliceçè¯å¥ç¨//注éè°ï¼ç¶åæ第äºæ¬¡è°ç¨çè¯å¥æ¹ææç»ä½ åçé£æ ·ï¼ç¶åä½ å°±ä¼å¾å°ä½ è¦ççæ¡äº
ç®å说说ConcurrentSkipListMap
åºæ¬ä»ç»
è·³è·è¡¨çæ§è´¨å¦ä¸ï¼
æåºå±çæ°æ®èç¹æç §å ³é®åkeyååºæåï¼
å å«å¤çº§ç´¢å¼ï¼æ¯ä¸ªçº§å«çç´¢å¼èç¹æç §å ¶å ³èçæ°æ®èç¹çå ³é®åkeyååºæåï¼
é«çº§å«ç´¢å¼æ¯å ¶ä½çº§å«ç´¢å¼çåéï¼
å¦æå ³é®åkeyå¨çº§å«level=içç´¢å¼ä¸åºç°ï¼å级å«level<=içææç´¢å¼é½å å«è¯¥keyã
è·³è·è¡¨ConcurrentSkipListMapçæ°æ®ç»æå¦ä¸å¾æ示ï¼ä¸å¾ä¸å ±æä¸å±ç´¢å¼ï¼æåºä¸ä¸ºæ°æ®èç¹ï¼åä¸å±ç´¢å¼ä¸ï¼ç´¢å¼èç¹ä¹é´ä½¿ç¨rightæéç¸è¿ï¼ä¸å±ç´¢å¼èç¹çdownæéæåä¸å±çç´¢å¼èç¹ã
æºç åææ ¸å¿å段åæhead æå node(BASE_HEADER) ç顶å±ç´¢å¼ã
/***The源码saas云建站源码topmostheadindexoftheskiplist.*/privatetransientvolatileHeadIndex<K,V>head;BASE_HEADER 头ç»ç¹ï¼å³æ顶å±ç´¢å¼ç头èç¹çvalueå¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()Node éæå é¨ç±»ï¼å³æ°æ®èç¹
/***æ°æ®èç¹*/staticfinalclassNode<K,V>{ finalKkey;//æ°æ®èç¹çkeyvolatileObjectvalue;//æ°æ®èç¹çvaluevolatileNode<K,V>next;//æåä¸ä¸ä¸ªæ°æ®èç¹/***Createsanewregularnode.*/Node(Kkey,Objectvalue,Node<K,V>next){ this.key=key;this.value=value;this.next=next;}}Index éæå é¨ç±»ï¼å³æ®éç´¢å¼èç¹
/***æ®éç´¢å¼èç¹*/staticclassIndex<K,V>{ finalNode<K,V>node;//ç´¢å¼èç¹æåçæ°æ®èç¹finalIndex<K,V>down;//å½åç´¢å¼èç¹çæ£ä¸æ¹ç´¢å¼èç¹volatileIndex<K,V>right;//å½åç´¢å¼èç¹çå³ç´¢å¼èç¹/***Createsindexnodewithgivenvalues.*/Index(Node<K,V>node,Index<K,V>down,Index<K,V>right){ this.node=node;this.down=down;this.right=right;}}HeadIndex éæå é¨ç±»ï¼å³å½å级å«ç´¢å¼ç头èç¹
/***å½å级å«ç´¢å¼ç头èç¹*/staticfinalclassHeadIndex<K,V>extendsIndex<K,V>{ finalintlevel;//æå¤ç´¢å¼çº§å«/***nodeï¼å½åç´¢å¼æåçæ°æ®èç¹*downï¼å½åç´¢å¼èç¹çæ£ä¸æ¹ç´¢å¼èç¹*rightï¼å½åç´¢å¼èç¹çå³ç´¢å¼èç¹*levelï¼å½åç´¢å¼å¤´èç¹æå¤çç´¢å¼çº§å«*/HeadIndex(Node<K,V>node,Index<K,V>down,Index<K,V>right,intlevel){ super(node,down,right);this.level=level;}}æ¥è¯¢æ ¹æ®æå®çkeyæ¥è¯¢èç¹ï¼æºç å¦ä¸ï¼
publicVget(Objectkey){ //è°ç¨doGetæ¹æ³returndoGet(key);}/***çæ£å®ç°æ¥è¯¢æ¹æ³*/privateVdoGet(Objectkey){ if(key==null)thrownewNullPointerException();Comparator<?superK>cmp=comparator;outer:for(;;){ for(Node<K,V>b=findPredecessor(key,cmp),n=b.next;;){ Objectv;intc;if(n==null)breakouter;Node<K,V>f=n.next;if(n!=b.next)//inconsistentreadbreak;if((v=n.value)==null){ //nisdeletedn.helpDelete(b,f);break;}if(b.value==null||v==n)//bisdeletedbreak;if((c=cpr(cmp,key,n.key))==0){ @SuppressWarnings("unchecked")Vvv=(V)v;returnvv;}if(c<0)breakouter;b=n;n=f;}}returnnull;}å¨ä¸è¿°ä»£ç ä¸ï¼outerå¤çforèªæä¸ï¼é¦å æ¥çfindPredecessorï¼æ¥è¯¢æå®keyèç¹çå驱èç¹ã该æ¹æ³å¨ä¸é¢ç好å¤å°æ¹ä¼è°ç¨ï¼ä¾å¦æå ¥å ç´ ï¼å é¤å ç´ ä»¥åå é¤å ç´ å¯¹åºçç´¢å¼æ¶é½ä¼è°ç¨ã
findPredecessoræ¹æ³æºç å¦ä¸ï¼
/***ä½ç¨1ï¼æ¾å°key对åºèç¹çå驱èç¹ï¼ä¸ä¸å®çççå驱èç¹ï¼ä¹å¯è½æ¯å驱ç»ç¹çå驱èç¹*ä½ç¨2ï¼å é¤æ æçç´¢å¼ï¼å³è¦å é¤èç¹æ¶ï¼å°èç¹çç´¢å¼ä¹å é¤æ*/privateNode<K,V>findPredecessor(Objectkey,Comparator<?superK>cmp){ if(key==null)thrownewNullPointerException();//don'tpostponeerrorsfor(;;){ //r为qèç¹çå³æéæåçèç¹ï¼r为å½åæ¯è¾èç¹,æ¯æ¬¡é½æ¯è¾rèç¹çkeyè·æ¥æ¾çkeyç大å°å ³ç³»for(Index<K,V>q=head,r=q.right,d;;){ if(r!=null){ Node<K,V>n=r.node;Kk=n.key;//该èç¹å·²ç»å é¤ï¼éè¦å é¤å ¶å¯¹åºçç´¢å¼if(n.value==null){ //该èç¹å·²ç»å é¤ï¼éè¦å é¤å ¶å¯¹åºçç´¢å¼if(!q.unlink(r))break;//restartr=q.right;//rereadrcontinue;}//å½åæ¥æ¾çkeyæ¯rèç¹çkey大ï¼æ以rãqèç¹é½åå³ç§»å¨if(cpr(cmp,key,k)>0){ q=r;r=r.right;continue;}}//å½qçä¸æ¹ç´¢å¼èç¹ä¸ºç©ºï¼å说æå·²ç»å°æ°æ®èç¹å±äºï¼éè¦éåºè¿è¡åç»æ¥æ¾å¤çif((d=q.down)==null)returnq.node;/***æ¤æ¶å½åæ¥æ¾çkeyå°äºrèç¹çkeyï¼éè¦å¾ä¸ä¸çº§ç´¢å¼æ¥æ¾*dèç¹èµå¼ä¸ºä¸ºqèç¹ä¸ºæ£ä¸æ¹èç¹ï¼å³ä¸ä¸çº§ç´¢å¼çæ£ä¸æ¹èç¹*/q=d;r=d.right;}}}findPredecessoræ¹æ³çæ¥æ¾è¿ç¨å¾ç¤ºå¦ä¸ï¼å设è¦æ¥æ¾èç¹6
ç±äºå½årèç¹çkeyæ¯æ¥è¯¢çkeyå°ï¼æ以ï¼rãqèç¹é½åå³ç§»å¨ï¼å³æ§è¡å¦ä¸ä»£ç ï¼
//å½åæ¥æ¾çkeyæ¯rèç¹çkey大ï¼æ以rãqèç¹é½åå³ç§»å¨if(cpr(cmp,key,k)>0){ q=r;r=r.right;continue;}æ¤æ¶rèç¹æåçæ°æ®èç¹ä¸ºï¼èç¹çkeyæ¯6èç¹çkey大ï¼æ¤æ¶éè¦æ§è¡å¦ä¸ä»£ç ï¼
/***æ¤æ¶å½åæ¥æ¾çkeyå°äºrèç¹çkeyï¼éè¦å¾ä¸ä¸çº§ç´¢å¼æ¥æ¾*dèç¹èµå¼ä¸ºä¸ºqèç¹ä¸ºæ£ä¸æ¹èç¹ï¼å³ä¸ä¸çº§ç´¢å¼çæ£ä¸æ¹èç¹*/q=d;r=d.right;æ¤æ¶rèç¹æåçæ°æ®èç¹ä¸º5ï¼5èç¹çkeyæ¯6èç¹çkeyå°ï¼qãrèç¹åå³ç§»å¨ï¼å¦ä¸å¾æ示
æ¤æ¶rèç¹æåçæ°æ®èç¹ä¸ºï¼èç¹çkeyæ¯6èç¹çkey大ï¼åçéè¦å¾ä¸çº§ç´¢å¼èµ°ï¼å¦ä¸å¾æ示ï¼
æ¤æ¶rèç¹æåçæ°æ®èç¹ä¸ºï¼èç¹çkeyæ¯6èç¹çkey大ï¼åçéè¦å¾ä¸çº§ç´¢å¼èµ°ï¼ä½æ¯æ¤æ¶ä¸ä¸çº§ç´¢å¼ä¸ºç©ºäºï¼å³(d = q.down) == nulläºï¼æ¤æ¶æ§è¡ç代ç å¦ä¸ï¼ è¿åqç´¢å¼æåçèç¹ï¼å³è¿åèç¹5.
//å½qçä¸æ¹ç´¢å¼èç¹ä¸ºç©ºï¼å说æå·²ç»å°æ°æ®èç¹å±äºï¼éè¦éåºè¿è¡åç»æ¥æ¾å¤çif((d=q.down)==null)returnq.node;以ä¸å°±æ¯æ¹æ³findPredecessorçæ¥æ¾æµç¨ï¼å±ä»¬æ¥ç继ç»çä¸é¢çdoGetæ¹æ³
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()0é¦å åå§åbãnãfä¸ä¸ªèç¹ï¼å¦ä¸å¾æ示
åç°æ¤æ¶nèç¹æåçèç¹å°±æ¯è¦æ¥è¯¢çèç¹ï¼äºæ¯æ§è¡å¦ä¸ä»£ç ï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()1ç´æ¥è¿ånèç¹çvalueå¼ãæ¥è¯¢æä½å®æã
æå ¥è·³è·è¡¨çæå ¥æä½å以ä¸åç§æ åµï¼
æ åµ1ï¼è·³è·è¡¨å åå¨keyä¸è´å ç´ ï¼åæ¿æ¢
æ åµ2ï¼æå ¥æ°å ç´ ï¼æ é¡»ç»æ°å ç´ çæç´¢å¼èç¹
æ åµ3ï¼æå ¥æ°å ç´ ï¼éè¦ç»æ°å ç´ çæç´¢å¼èç¹ï¼ä¸ç´¢å¼é«åº¦ < maxLevel
æ åµ4ï¼æå ¥æ°å ç´ ï¼éè¦ç»æ°å ç´ çæç´¢å¼èç¹ï¼ä¸ç´¢å¼é«åº¦ > maxLevel
æºç å¦ä¸ï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()2é¦å è¿æ¯è·æ¥è¯¢æä½ç±»ä¼¼ï¼è°ç¨findPredecessoræ¹æ³å æ¥æ¾å°å¾ æå ¥keyçå驱èç¹ï¼ä¸¾ä¸ªä¾åï¼ä¾å¦æ们æ³è¦æå ¥èç¹7ï¼å¦ä¸å¾æ示ï¼
æ¥çè·æ¥è¯¢æä½ä¸æ ·çæ¥éª¤å¦ä¸ï¼ç´æ¥çå¾ï¼
æ¤æ¶rèç¹æåæ°æ®èç¹1ï¼èç¹1çkeyå°äºå¾ æå ¥çèç¹7çkeyï¼äºæ¯èç¹qãråæ¶åå³ç§»å¨ã
æ¤æ¶rèç¹æåæ°æ®èç¹ï¼èç¹çkey大äºå¾ æå ¥èç¹7çkeyï¼äºæ¯å¾ä¸ä¸å±ç´¢å¼ç»§ç»æ¥æ¾ï¼æ§è¡ç代ç å¦ä¸ï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()3åé¢çæä½ç±»ä¼¼
æ¤æ¶rèç¹çkey大äºå¾ æå ¥çèç¹6çkeyï¼ä½æ¯qèç¹çdownæé已为空ï¼æ¤æ¶ç´æ¥è¿åqèç¹æåçèç¹5ã
æ¥çåå°doPutæ¹æ³ï¼å æ¥æ¥çouter循ç¯ï¼å¦ä¸ï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()4é¦å åå§åä¸ä¸ªèç¹bãnãfï¼nèç¹ä¸ºbèç¹çä¸ä¸ä¸ªèç¹ï¼èfèç¹ä¸ºnèç¹çä¸ä¸ä¸ªèç¹ï¼å¦ä¸å¾æ示
æ¥çæ¯è¾èç¹nä¸å¾ æå ¥çkeyç大å°ï¼æ¤æ¶nèç¹çkeyå°äºå¾ æå ¥èç¹çkeyï¼äºæ¯bãnãfä¸ä¸ªèç¹ååä¸ç§»å¨å¦ä¸å¾æ示
æ¤æ¶nèç¹çkey大äºå¾ æå ¥çkeyï¼æ¤æ¶æ§è¡å¦ä¸ä»£ç ï¼éè¿casæ¹å¼ä¿®æ¹bèç¹çä¸ä¸ä¸ªèç¹ä¸ºzèç¹ï¼æ¥çè·³åºouter循ç¯ã
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()5ç¶åæ们ç¥édoPutå©ä¸ç代ç æ éå°±æ¯å¤ææ¯å¦ç»æ°æå ¥çèç¹zå建索å¼ï¼å¦æéè¦å建对åºçç´¢å¼ã
é¦å éè¿int rnd = ThreadLocalRandom.nextSecondarySeed();计ç®åºä¸ä¸ªéæºæ°ï¼æ¥çè¿è¡å¦ä¸å¤æï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()6å¦ærnd & 0x) == 0å°±ç»æ°æå ¥çzèç¹å建索å¼ï¼æ们ç¥é0x = å³æé«ä½åæåä¸ä½ä¸º1ï¼å ¶ä½å ¨é¨æ¯0ï¼
æ¡ä»¶ï¼(rnd & 0x) == 0ä»ä¹æ¶åæç«ï¼
rndè¿ä¸ªéæºæ°æä½ä½åæé«ä½åæ¶æ¯0çæ¶åï¼æ¡ä»¶æç«ï¼æ¦çæ¯1/4
举个ä¾åï¼ä¾å¦rnd = = 3æ¡ä»¶å°±æç«ã
å¦ææ¡ä»¶æç«çè¯ï¼æ¥ç计ç®å°åºç»zèç¹å建å 级索å¼ï¼ä»£ç å¦ä¸ï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()7éè¿whileæ¡ä»¶((rnd >>>= 1) & 1) != 0满足å 次就å建å 级索å¼ãä¾å¦ï¼
rnd = 计ç®åºæ¥çlevel => 3
rnd = 计ç®åºæ¥çlevel => 8
ç¶åæ¥çæ¯è¾è®¡ç®åºæ¥çzèç¹çç´¢å¼è·ç°æçè·³è·è¡¨çç´¢å¼çº§å«å¤§å°ã
æ åµä¸ï¼zèç¹è®¡ç®åºæ¥çç´¢å¼levelæ¯è·³è·è¡¨çlevelå°
æ åµäºï¼zèç¹è®¡ç®å¤ççç´¢å¼levelæ¯è·³è·è¡¨çlevel大ãæ¤æ¶ä¼éæ©æç»çlevel为åæ¥çè°è¡¨çlevel + 1
æ åµä¸
ç»zèç¹å建索å¼çæ¥éª¤å¦ä¸å¾æ示ï¼æ¤æ¶zèç¹çç´¢å¼è¿æ²¡æå å ¥è·³è·è¡¨ç°æçç´¢å¼éåä¸
æ¥ç继ç»æ§è¡splice循ç¯ï¼ä»£ç å¦ä¸ï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()8åå§åqãrèç¹å¦ä¸å¾æ示
æ¤æ¶rèç¹çkeyæ¯æ°æå ¥zèç¹ï¼å³7èç¹å°ï¼äºæ¯ä¸¤ä¸ªèç¹qãté½åå³ç§»å¨å¦ä¸å¾æ示
æ¤æ¶rèç¹çkeyæ¯æ°æå ¥zèç¹ï¼å³7èç¹å¤§ï¼æ§è¡å¦ä¸ä»£ç ï¼
/***Specialvalueusedtoidentifybase-levelheader*/privatestaticfinalObjectBASE_HEADER=newObject()9æ¤æ¶rèç¹çkeyæ¯æ°æå ¥zèç¹ï¼å³7èç¹å°ï¼äºæ¯ä¸¤ä¸ªèç¹qãté½åå³ç§»å¨å¦ä¸å¾æ示
æ¤æ¶rèç¹çkeyæ¯æ°æå ¥zèç¹ï¼å³7èç¹å¤§,åçï¼ç´æ¥çå¾
æ åµäº
è·æ åµä¸ç±»ä¼¼ï¼è¿éå°±ä¸ä¸ä¸ç»å¾äº
å é¤å é¤æ¹æ³å®æçä»»å¡å¦ä¸ï¼
设置æå®å ç´ value为null
å°æå®nodeä»nodeé¾è¡¨ç§»é¤
å°æå®nodeçindexèç¹ ä» å¯¹åºç index é¾è¡¨ç§»é¤
/***æ°æ®èç¹*/staticfinalclassNode<K,V>{ finalKkey;//æ°æ®èç¹çkeyvolatileObjectvalue;//æ°æ®èç¹çvaluevolatileNode<K,V>next;//æåä¸ä¸ä¸ªæ°æ®èç¹/***Createsanewregularnode.*/Node(Kkey,Objectvalue,Node<K,V>next){ this.key=key;this.value=value;this.next=next;}}0åæ ·ï¼é¦å éè¿findPredecessoræ¹æ³æ¥æ¾å°è¦å é¤keyçå驱èç¹ï¼å°±ä¸ä¸ä¸ç»å¾äºï¼ç´æ¥çæ¾å°çå驱èç¹çå¾ï¼å¦ä¸ï¼
æ¥æ¯è¾nèç¹çkeyä¸å¾ å é¤çkeyç大å°ï¼æ¤æ¶nèç¹çkeyå°äºå¾ å é¤çkeyï¼å³7èç¹çkeyï¼äºæ¯å°bãnãfä¸ä¸ªèç¹é½åå³ç§»å¨ï¼å¦ä¸å¾ï¼
æ¤æ¶nèç¹çkeyè·å¾ å é¤çkeyä¸æ ·ï¼äºæ¯æ§è¡å¦ä¸ä»£ç ï¼
/***æ°æ®èç¹*/staticfinalclassNode<K,V>{ finalKkey;//æ°æ®èç¹çkeyvolatileObjectvalue;//æ°æ®èç¹çvaluevolatileNode<K,V>next;//æåä¸ä¸ä¸ªæ°æ®èç¹/***Createsanewregularnode.*/Node(Kkey,Objectvalue,Node<K,V>next){ this.key=key;this.value=value;this.next=next;}}1æååè°ç¨findPredecessoræ¸ æ¥æ æçç´¢å¼ï¼å³ä¸é¢å é¤çèç¹çç´¢å¼ã
/***æ°æ®èç¹*/staticfinalclassNode<K,V>{ finalKkey;//æ°æ®èç¹çkeyvolatileObjectvalue;//æ°æ®èç¹çvaluevolatileNode<K,V>next;//æåä¸ä¸ä¸ªæ°æ®èç¹/***Createsanewregularnode.*/Node(Kkey,Objectvalue,Node<K,V>next){ this.key=key;this.value=value;this.next=next;}}2éç¹é å¦ä¸ä»£ç åå é¤ç´¢å¼çï¼
/***æ°æ®èç¹*/staticfinalclassNode<K,V>{ finalKkey;//æ°æ®èç¹çkeyvolatileObjectvalue;//æ°æ®èç¹çvaluevolatileNode<K,V>next;//æåä¸ä¸ä¸ªæ°æ®èç¹/***Createsanewregularnode.*/Node(Kkey,Objectvalue,Node<K,V>next){ this.key=key;this.value=value;this.next=next;}}3æ们ç¥éå¨ä¸é¢å·²ç»å°å¾ å é¤ç7èç¹çvalue置为nulläºï¼ç´æ¥çå¾ï¼
æ¤æ¶rèç¹çkeyå°äºå¾ å é¤èç¹çkeyï¼äºæ¯rãqèç¹é½åå³ç§»å¨ã
æ¤æ¶r,nèç¹æåçæ°æ®èç¹çvalueå¼ä¸ºnulläºæ¯æ§è¡ä¸é¢çq.unlink(r)代ç ï¼å°qçå³æéæårçå³æéæåçèç¹ï¼å³å°±æ¯å é¤äºè¯¥levelä¸ç7èç¹çç´¢å¼èç¹ï¼å¦ä¸å¾æ示
æ¤æ¶rèç¹çkey大äºå¾ å é¤èç¹çkeyï¼äºæ¯å¾ä¸ä¸ç´¢å¼èµ°ï¼å¦ä¸å¾æ示
æ¤æ¶rèç¹çkeyå°äºå¾ å é¤èç¹çkeyï¼äºæ¯rãqèç¹é½åå³ç§»å¨ã
æ¤æ¶r,nèç¹æåçæ°æ®èç¹çvalueå¼ä¸ºnulläºæ¯æ§è¡ä¸é¢çq.unlink(r)代ç ï¼å°qçå³æéæårçå³æéæåçèç¹ï¼å³å°±æ¯å é¤äºè¯¥levelä¸ç7èç¹çç´¢å¼èç¹ï¼å¦ä¸å¾æ示
åç»æä½åçï¼æç»å°7èç¹çç´¢å¼ä¸ä¸å é¤å®ï¼æç»çå¾ä¸æ示