Javascript implements data structure: blockchain storage representation of strings

Source: Internet
Author: User

Similar to the chain storage structure of a linear table, you can also store string values in a chain mode. Due to the special nature of the string structure, each data element in the structure is a character, there is a "Node size" Problem When storing string values in a linked list, that is, each node can store one character, you can also store multiple characters. The following is a chain table with a node size of 4 (4 characters for each node): head --> (a) --> (B) --> (c) -->... --> (I) when the node size is greater than 1, because the string length is not necessarily an integer multiple of the node size, the last node in the linked list may not be fully occupied by the string value, in this case, "#" or other non-string characters are usually added. To facilitate string operations, when storing string values in a linked list, in addition to the header pointer, a tail pointer can be attached to indicate the last node in the linked list and the length of the current string is given, the defined string storage structure is a blockchain structure. Generally, you only need to scan strings sequentially from the beginning to the end, so you do not need to create a two-way linked list for string values. The tail pointer is set to facilitate connection operations. However, you must handle invalid characters at the end of the first string during connection. In the chain storage mode, node size selection is equally important as the format selection of the sequential storage mode, which directly affects the efficiency of string processing. If the string is long, we need to consider the storage density of the string value: storage density = the storage space occupied by the string value/The actually allocated storage space. The chain storage structure of the string value performs operations on some strings, such as connection operations are convenient, but in general it is not as flexible as the other two storage structures, it occupies a large amount of storage and the operation is complex. Implementation Code: Copy code 1 function Chunk (chunkSize) {2 this. chunkSize = chunkSize | 4; 3 this. ch = []; 4 for (var I = 0; I <this. chunkSize; I ++) {5 this. ch [I] = '#'; 6} 7 // type: Chunk 8 this. next = null; 9} 10 11 exports. LString = LString; 12 function LString (chunkSize) {13 // type Chunk 14 this. head = null; 15 // type: chunk 16 this. tail = null; 17 // The current length of the string is 18 this. length = 0; 19 this. chunkSize = ch UnkSize | 4; 20} 21 22 LString. prototype = {23 // convert string to LString type 24 strAssign: function (chars) {25 this. head = this. tail = new Chunk (this. chunkSize); 26 this. length = chars. length; 27 28 var current = this. head; 29 for (var I = 0, len = chars. length; I <len; I ++) {30 current. ch [I % this. chunkSize] = chars [I]; 31 if (I + 1 <len & (I + 1) % this. chunkSize = 0) {32 current. next = new Ch Unk (); 33 current = current. next; 34} 35} 36 37 this. tail = current; 38}, 39 // string comparison 40 // do TODO remove chunkSize contrast 41 strCompare: function (tLString) {42 var current = this. head; 43 var curT = tLString. head; 44 45 if (this. length! = TLString. length) return false; 46 47 while (current) {48 for (var I = 0; I <this. chunkSize; I ++) {49 if (current. ch [I]! = CurT. ch [I]) return false; 50} 51 52 current = current. next; 53 curT = curT. next; 54} 55 56 return true; 57}, 58 clearString: function () {59 this. head = this. tail = null; 60 this. length = 0; 61}, 62 concat: function (tLSting) {63 if (! TLSting. length) return; 64 65 var ret = new LString (this. chunkSize); 66 67 if (this. head = null) {68 copyString (ret, tLSting); 69} else {70 ret. head = ret. tail = new Chunk (this. chunkSize); 71 copyString (ret, this); 72 73 var index = ret. tail. ch. indexOf ('#'); 74 if (index =-1) {75 copyString (ret, tLSting); 76} else {77 copyString (ret, tLSting, ret. tail, tLSting. head, index); 78} 79} 80 81 return ret; 82}, 83 substring: function (pos, len) {84 pos = ~~ Pos | 0; 85 len = ~~ Len | this. length; 86 if (pos <0 | pos> this. length-1 | len <0 | len> this. length-pos) 87 throw new Error ('unexpected parameter '); 88 89 var sub = new LString (this. chunkSize); 90 var current = findPosChunk (this, pos); 91 var curS = sub. head = new Chunk (this. chunkSize); 92 var I = 0; 93 sub. length = len; 94 95 outerloop: while (current) {96 for (var j = 0, size = this. chunkSize; j <size; j ++) {97 if (I === len) {98 break outerloop; 99} else {100 curS. ch [j] = current. ch [(I + pos) % this. chunkSize]; 101 I ++; 102 if (I + pos) % this. chunkSize = 0) {103 current = current. next; 104} 105 if (I % this. chunkSize === 0 & (current. ch [I] | current. next) {106 curS. next = new Chunk (this. chunkSize); 107 curS = curS. next; 108} 109} 110} 111} 112 113 return sub; 114}, 115 toString: function () {116 var current = this. head; 117 118 if (current = null) return ''; 119 120 var str =''; 121 while (current) {122 for (var I = 0, len = this. chunkSize; I <len; I ++) {123 var ch = current. ch [I]; 124 if (ch = '#') {125 return str; 126} else {127 str + = current. ch [I]; 128} 129} 130 current = current. next; 131} 132 133 return str; 134} 135}; 136 137 function findPosChunk (lString, pos) {138 var current = lString. head; 139 while (current) {140 for (var I = 0, len = lString. chunkSize; I <len; I ++) {141 if (pos -- = 0) return current; 142} 143 current = current. next; 144} 145} 146 147 function copyString (destination, target, curD, currT, offset) {148 offset = offset | 0; 149 currT = currT | target. head; 150 curD = curD | destination. head; 151 var k = 0; 152 153 while (currT) {154 for (var I = 0, len = target. chunkSize; I <len; I ++, k ++) {155 var j = k % curD. chunkSize + offset; 156 curD. ch [j % curD. chunkSize] = currT. ch [I]; 157 158 if (j + 1) % curD. chunkSize = 0 & (currT. ch [I + 1] | currT. next) {159 curD. next = new Chunk (destination. chunkSize); 160 curD = curD. next; 161} 162} 163 164 currT = currT. next; 165} 166 167 destination. tail = curD; 168 destination. length + = target. length; 169} 170 171 var a = new LString (); 172 var B = new LString (); 173 var c = new LString (); 174 175. strAssign ('abcdefg'); 176 console. log (a + ''); 177 B. strAssign ('hijklmno'); 178 console. log (B + ''); 179 c. strAssign ('abcdefg'); 180 console. log (. strCompare (B); 181 console. log (. strCompare (c); 182 var t =. concat (B); 183 console. log (t + ''); 184 t = t. sub string (2, 5); 185 console. log (t + '');

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.