source: libcfa/src/collections/string_res.cfa@ 5f917740

Last change on this file since 5f917740 was 416b443, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

Implement full set of relational operators for strings

  • Property mode set to 100644
File size: 42.0 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// string_res -- variable-length, mutable run of text, with resource semantics
8//
9// Author : Michael L. Brooks
10// Created On : Fri Sep 03 11:00:00 2021
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Aug 14 18:06:01 2023
13// Update Count : 12
14//
15
16#include "string_res.hfa"
17#include "string_sharectx.hfa"
18#include "stdlib.hfa"
19#include <ctype.h>
20
21// Workaround for observed performance penalty from calling CFA's alloc.
22// Workaround is: EndVbyte = TEMP_ALLOC(char, CurrSize)
23// Should be: EndVbyte = alloc(CurrSize)
24#define TEMP_ALLOC(T, n) (( T* ) malloc( n * sizeof( T ) ))
25
26#include <assert.h>
27
28//######################### VbyteHeap "header" #########################
29
30#ifdef VbyteDebug
31HandleNode *HeaderPtr;
32#endif // VbyteDebug
33
34struct VbyteHeap {
35
36 int NoOfCompactions; // number of compactions of the byte area
37 int NoOfExtensions; // number of extensions in the size of the byte area
38 int NoOfReductions; // number of reductions in the size of the byte area
39
40 int InitSize; // initial number of bytes in the byte-string area
41 int CurrSize; // current number of bytes in the byte-string area
42 char *StartVbyte; // pointer to the `st byte of the start of the byte-string area
43 char *EndVbyte; // pointer to the next byte after the end of the currently used portion of byte-string area
44 void *ExtVbyte; // pointer to the next byte after the end of the byte-string area
45
46 HandleNode Header; // header node for handle list
47}; // VbyteHeap
48
49
50static void compaction( VbyteHeap & ); // compaction of the byte area
51static void garbage( VbyteHeap &, int ); // garbage collect the byte area
52static void extend( VbyteHeap &, int ); // extend the size of the byte area
53static void reduce( VbyteHeap &, int ); // reduce the size of the byte area
54
55static void ?{}( VbyteHeap &, size_t = 1000 );
56static void ^?{}( VbyteHeap & );
57
58static int ByteCmp( char *, int, int, char *, int, int ); // compare 2 blocks of bytes
59static char *VbyteAlloc( VbyteHeap &, int ); // allocate a block bytes in the heap
60static char *VbyteTryAdjustLast( VbyteHeap &, int );
61
62static void AddThisAfter( HandleNode &, HandleNode & );
63static void DeleteNode( HandleNode & );
64static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle
65
66
67// Allocate the storage for the variable sized area and intialize the heap variables.
68
69static void ?{}( VbyteHeap & this, size_t Size ) with(this) {
70#ifdef VbyteDebug
71 serr | "enter:VbyteHeap::VbyteHeap, this:" | &this | " Size:" | Size;
72#endif // VbyteDebug
73 NoOfCompactions = NoOfExtensions = NoOfReductions = 0;
74 InitSize = CurrSize = Size;
75 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
76 ExtVbyte = (void *)( StartVbyte + CurrSize );
77 Header.flink = Header.blink = &Header;
78 Header.ulink = & this;
79#ifdef VbyteDebug
80 HeaderPtr = &Header;
81 serr | "exit:VbyteHeap::VbyteHeap, this:" | &this;
82#endif // VbyteDebug
83} // VbyteHeap
84
85
86// Release the dynamically allocated storage for the byte area.
87
88static void ^?{}( VbyteHeap & this ) with(this) {
89 free( StartVbyte );
90} // ~VbyteHeap
91
92
93//######################### HandleNode #########################
94
95
96// Create a handle node. The handle is not linked into the handle list. This is the responsibilitiy of the handle
97// creator.
98
99static void ?{}( HandleNode & this ) with(this) {
100#ifdef VbyteDebug
101 serr | "enter:HandleNode::HandleNode, this:" | &this;
102#endif // VbyteDebug
103 s = 0;
104 lnth = 0;
105#ifdef VbyteDebug
106 serr | "exit:HandleNode::HandleNode, this:" | &this;
107#endif // VbyteDebug
108} // HandleNode
109
110// Create a handle node. The handle is linked into the handle list at the end. This means that this handle will NOT be
111// in order by string address, but this is not a problem because a string with length zero does nothing during garbage
112// collection.
113
114static void ?{}( HandleNode & this, VbyteHeap & vh ) with(this) {
115#ifdef VbyteDebug
116 serr | "enter:HandleNode::HandleNode, this:" | &this;
117#endif // VbyteDebug
118 s = 0;
119 lnth = 0;
120 ulink = &vh;
121 AddThisAfter( this, *vh.Header.blink );
122#ifdef VbyteDebug
123 serr | "exit:HandleNode::HandleNode, this:" | &this;
124#endif // VbyteDebug
125} // HandleNode
126
127
128// Delete a node from the handle list by unchaining it from the list. If the handle node was allocated dynamically, it
129// is the responsibility of the creator to destroy it.
130
131static void ^?{}( HandleNode & this ) with(this) {
132#ifdef VbyteDebug
133 serr | "enter:HandleNode::~HandleNode, this:" | & this;
134 {
135 serr | nlOff;
136 serr | " lnth:" | lnth | " s:" | (void *)s | ",\"";
137 for ( i; lnth ) {
138 serr | s[i];
139 } // for
140 serr | "\" flink:" | flink | " blink:" | blink | nl;
141 serr | nlOn;
142 }
143#endif // VbyteDebug
144 DeleteNode( this );
145} // ~HandleNode
146
147
148//######################### String Sharing Context #########################
149
150static string_sharectx * ambient_string_sharectx; // fickle top of stack
151static string_sharectx default_string_sharectx = {NEW_SHARING}; // stable bottom of stack
152
153void ?{}( string_sharectx & this, StringSharectx_Mode mode ) with( this ) {
154 (older){ ambient_string_sharectx };
155 if ( mode == NEW_SHARING ) {
156 (activeHeap){ new( (size_t) 1000 ) };
157 } else {
158 verify( mode == NO_SHARING );
159 (activeHeap){ 0p };
160 }
161 ambient_string_sharectx = & this;
162}
163
164void ^?{}( string_sharectx & this ) with( this ) {
165 if ( activeHeap ) delete( activeHeap );
166
167 // unlink this from older-list starting from ambient_string_sharectx
168 // usually, this==ambient_string_sharectx and the loop runs zero times
169 string_sharectx *& c = ambient_string_sharectx;
170 while ( c != &this ) &c = &c->older; // find this
171 c = this.older; // unlink
172}
173
174//######################### String Resource #########################
175
176
177VbyteHeap * DEBUG_string_heap() {
178 assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );
179 return ambient_string_sharectx->activeHeap;
180}
181
182size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ) {
183 return ((char*)heap->ExtVbyte) - heap->EndVbyte;
184}
185
186size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ) {
187 return heap->CurrSize;
188}
189
190const char * DEBUG_string_heap_start( VbyteHeap * heap ) {
191 return heap->StartVbyte;
192}
193
194// Returns the size of the string in bytes
195size_t size(const string_res &s) with(s) {
196 return Handle.lnth;
197}
198
199// Output operator
200ofstream & ?|?(ofstream &out, const string_res &s) {
201 // CFA string is NOT null terminated, so print exactly lnth characters in a minimum width of 0.
202 out | wd( 0, s.Handle.lnth, s.Handle.s ) | nonl;
203 return out;
204}
205
206void ?|?(ofstream &out, const string_res &s) {
207 (ofstream &)(out | s); ends( out );
208}
209
210// Input operator
211ifstream & ?|?(ifstream &in, string_res &s) {
212
213 // Reading into a temp before assigning to s is near zero overhead in typical cases because of sharing.
214 // If s is a substring of something larger, simple assignment takes care of that case correctly.
215 // But directly reading a variable amount of text into the middle of a larger context is not practical.
216 string_res temp;
217
218 // Read in chunks. Often, one chunk is enough. Keep the string that accumulates chunks last in the heap,
219 // so available room is rest of heap. When a chunk fills the heap, force growth then take the next chunk.
220 for (;;) {
221 // Append dummy content to temp, forcing expansion when applicable (occurs always on subsequent loops)
222 // length 2 ensures room for at least one real char, plus scanf/pipe-cstr's null terminator
223 temp += "--";
224 assert( temp.Handle.ulink->EndVbyte == temp.Handle.s + temp.Handle.lnth ); // last in heap
225
226 // reset, to overwrite the appended "--"
227 temp.Handle.lnth -= 2;
228 temp.Handle.ulink->EndVbyte -= 2;
229
230 // rest of heap, less 1 byte for null terminator, is available to read into
231 int lenReadable = (char*)temp.Handle.ulink->ExtVbyte - temp.Handle.ulink->EndVbyte - 1;
232 assert (lenReadable >= 1);
233
234 // get bytes
235 in | wdi( lenReadable + 1, lenReadable, temp.Handle.ulink->EndVbyte );
236 int lenWasRead = strlen(temp.Handle.ulink->EndVbyte);
237
238 // update metadata
239 temp.Handle.lnth += lenWasRead;
240 temp.Handle.ulink->EndVbyte += lenWasRead;
241
242 if (lenWasRead < lenReadable) break;
243 }
244
245 s = temp;
246 return in;
247}
248
249void ?|?( ifstream & in, string_res & this ) {
250 (ifstream &)(in | this); ends( in );
251}
252
253ifstream & ?|?( ifstream & is, _Istream_Rstr f ) {
254 // .---------------,
255 // | | | | |...|0|0| null terminator and guard if missing
256 // `---------------'
257 enum { gwd = 128 + 1, wd = gwd - 1 }; // guard and unguard width
258 char cstr[gwd]; // read in chunks
259 bool cont = false;
260
261 _Istream_Cstr cf = { cstr, (_Istream_str_base)f };
262 if ( ! cf.flags.rwd ) cf.wd = wd;
263
264 cstr[wd] = '\0'; // guard null terminate string
265 try {
266 is | cf;
267 } catch( cstring_length * ) {
268 cont = true;
269 } finally {
270 if ( ! cf.flags.ignore ) *(f.s) = cstr; // ok to initialize string
271 } // try
272 for ( ; cont; ) { // overflow read ?
273 cont = false;
274 try {
275 is | cf;
276 } catch( cstring_length * ) {
277 cont = true; // continue not allowed
278 } finally {
279 if ( ! cf.flags.ignore ) *(f.s) += cstr; // build string chunk at a time
280 } // try
281 } // for
282 return is;
283} // ?|?
284
285void ?|?( ifstream & in, _Istream_Rstr f ) {
286 (ifstream &)(in | f); ends( in );
287}
288
289
290// Empty constructor
291void ?{}(string_res &s) with(s) {
292 if( ambient_string_sharectx->activeHeap ) {
293 (Handle){ * ambient_string_sharectx->activeHeap };
294 (shareEditSet_owns_ulink){ false };
295 verify( Handle.s == 0p && Handle.lnth == 0 );
296 } else {
297 (Handle){ * new( (size_t) 10 ) }; // TODO: can I lazily avoid allocating for empty string
298 (shareEditSet_owns_ulink){ true };
299 Handle.s = Handle.ulink->StartVbyte;
300 verify( Handle.lnth == 0 );
301 }
302 s.shareEditSet_prev = &s;
303 s.shareEditSet_next = &s;
304}
305
306static void eagerCopyCtorHelper(string_res &s, const char* rhs, size_t rhslnth) with(s) {
307 if( ambient_string_sharectx->activeHeap ) {
308 (Handle){ * ambient_string_sharectx->activeHeap };
309 (shareEditSet_owns_ulink){ false };
310 } else {
311 (Handle){ * new( rhslnth ) };
312 (shareEditSet_owns_ulink){ true };
313 }
314 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
315 Handle.lnth = rhslnth;
316 memmove( Handle.s, rhs, rhslnth );
317 s.shareEditSet_prev = &s;
318 s.shareEditSet_next = &s;
319}
320
321// Constructor from a raw buffer and size
322void ?{}(string_res &s, const char* rhs, size_t rhslnth) with(s) {
323 eagerCopyCtorHelper(s, rhs, rhslnth);
324}
325
326// private ctor (not in header): use specified heap (ignore ambient) and copy chars in
327void ?{}( string_res &s, VbyteHeap & heap, const char* rhs, size_t rhslnth ) with(s) {
328 (Handle){ heap };
329 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);
330 Handle.lnth = rhslnth;
331 (s.shareEditSet_owns_ulink){ false };
332 memmove( Handle.s, rhs, rhslnth );
333 s.shareEditSet_prev = &s;
334 s.shareEditSet_next = &s;
335}
336
337// General copy constructor
338void ?{}(string_res &s, const string_res & s2, StrResInitMode mode, size_t start, size_t end ) {
339
340 verify( start <= end && end <= s2.Handle.lnth );
341
342 if (s2.Handle.ulink != ambient_string_sharectx->activeHeap && mode == COPY_VALUE) {
343 // crossing heaps (including private): copy eagerly
344 eagerCopyCtorHelper(s, s2.Handle.s + start, end - start);
345 verify(s.shareEditSet_prev == &s);
346 verify(s.shareEditSet_next == &s);
347 } else {
348 (s.Handle){};
349 s.Handle.s = s2.Handle.s + start;
350 s.Handle.lnth = end - start;
351 s.Handle.ulink = s2.Handle.ulink;
352
353 AddThisAfter(s.Handle, s2.Handle ); // insert this handle after rhs handle
354 // ^ bug? skip others at early point in string
355
356 if (mode == COPY_VALUE) {
357 verify(s2.Handle.ulink == ambient_string_sharectx->activeHeap);
358 // requested logical copy in same heap: defer copy until write
359
360 (s.shareEditSet_owns_ulink){ false };
361
362 // make s alone in its shareEditSet
363 s.shareEditSet_prev = &s;
364 s.shareEditSet_next = &s;
365 } else {
366 verify( mode == SHARE_EDITS );
367 // sharing edits with source forces same heap as source (ignore context)
368
369 (s.shareEditSet_owns_ulink){ s2.shareEditSet_owns_ulink };
370
371 // s2 is logically const but not implementation const
372 string_res & s2mod = (string_res &) s2;
373
374 // insert s after s2 on shareEditSet
375 s.shareEditSet_next = s2mod.shareEditSet_next;
376 s.shareEditSet_prev = &s2mod;
377 s.shareEditSet_next->shareEditSet_prev = &s;
378 s.shareEditSet_prev->shareEditSet_next = &s;
379 }
380 }
381}
382
383static void assignEditSet(string_res & this, string_res * shareEditSetStartPeer, string_res * shareEditSetEndPeer,
384 char * resultSesStart,
385 size_t resultSesLnth,
386 HandleNode * resultPadPosition, size_t bsize ) {
387
388 char * beforeBegin = shareEditSetStartPeer->Handle.s;
389 size_t beforeLen = this.Handle.s - beforeBegin;
390
391 char * afterBegin = this.Handle.s + this.Handle.lnth;
392 size_t afterLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - afterBegin;
393
394 size_t oldLnth = this.Handle.lnth;
395
396 this.Handle.s = resultSesStart + beforeLen;
397 this.Handle.lnth = bsize;
398 if (resultPadPosition)
399 MoveThisAfter( this.Handle, *resultPadPosition );
400
401 // adjust all substring string and handle locations, and check if any substring strings are outside the new base string
402 char *limit = resultSesStart + resultSesLnth;
403 for ( string_res * p = this.shareEditSet_next; p != &this; p = p->shareEditSet_next ) {
404 verify (p->Handle.s >= beforeBegin);
405 if ( p->Handle.s >= afterBegin ) {
406 verify ( p->Handle.s <= afterBegin + afterLen );
407 verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );
408 // p starts after the edit
409 // take start and end as end-anchored
410 size_t startOffsetFromEnd = afterBegin + afterLen - p->Handle.s;
411 p->Handle.s = limit - startOffsetFromEnd;
412 // p->Handle.lnth unaffected
413 } else if ( p->Handle.s <= beforeBegin + beforeLen ) {
414 // p starts before, or at the start of, the edit
415 if ( p->Handle.s + p->Handle.lnth <= beforeBegin + beforeLen ) {
416 // p ends before the edit
417 // take end as start-anchored too
418 // p->Handle.lnth unaffected
419 } else if ( p->Handle.s + p->Handle.lnth < afterBegin ) {
420 // p ends during the edit; p does not include the last character replaced
421 // clip end of p to end at start of edit
422 p->Handle.lnth = beforeLen - ( p->Handle.s - beforeBegin );
423 } else {
424 // p ends after the edit
425 verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen );
426 // take end as end-anchored
427 // stretch-shrink p according to the edit
428 p->Handle.lnth += this.Handle.lnth;
429 p->Handle.lnth -= oldLnth;
430 }
431 // take start as start-anchored
432 size_t startOffsetFromStart = p->Handle.s - beforeBegin;
433 p->Handle.s = resultSesStart + startOffsetFromStart;
434 } else {
435 verify ( p->Handle.s < afterBegin );
436 // p starts during the edit
437 verify( p->Handle.s + p->Handle.lnth >= beforeBegin + beforeLen );
438 if ( p->Handle.s + p->Handle.lnth < afterBegin ) {
439 // p ends during the edit; p does not include the last character replaced
440 // set p to empty string at start of edit
441 p->Handle.s = this.Handle.s;
442 p->Handle.lnth = 0;
443 } else {
444 // p includes the end of the edit
445 // clip start of p to start at end of edit
446 int charsToClip = afterBegin - p->Handle.s;
447 p->Handle.s = this.Handle.s + this.Handle.lnth;
448 p->Handle.lnth -= charsToClip;
449 }
450 }
451 if (resultPadPosition)
452 MoveThisAfter( p->Handle, *resultPadPosition ); // move substring handle to maintain sorted order by string position
453 }
454}
455
456// traverse the share-edit set (SES) to recover the range of a base string to which `this` belongs
457static void locateInShareEditSet( string_res &this, string_res *&shareEditSetStartPeer, string_res *&shareEditSetEndPeer ) {
458 shareEditSetStartPeer = & this;
459 shareEditSetEndPeer = & this;
460 for (string_res * editPeer = this.shareEditSet_next; editPeer != &this; editPeer = editPeer->shareEditSet_next) {
461 if ( editPeer->Handle.s < shareEditSetStartPeer->Handle.s ) {
462 shareEditSetStartPeer = editPeer;
463 }
464 if ( shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth < editPeer->Handle.s + editPeer->Handle.lnth) {
465 shareEditSetEndPeer = editPeer;
466 }
467 }
468}
469
470static string_res & assign_(string_res &this, const char* buffer, size_t bsize, const string_res & valSrc) {
471
472 string_res * shareEditSetStartPeer;
473 string_res * shareEditSetEndPeer;
474 locateInShareEditSet( this, shareEditSetStartPeer, shareEditSetEndPeer );
475
476 verify( shareEditSetEndPeer->Handle.s >= shareEditSetStartPeer->Handle.s );
477 size_t origEditSetLength = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - shareEditSetStartPeer->Handle.s;
478 verify( origEditSetLength >= this.Handle.lnth );
479
480 if ( this.shareEditSet_owns_ulink ) { // assigning to private context
481 // ok to overwrite old value within LHS
482 char * prefixStartOrig = shareEditSetStartPeer->Handle.s;
483 int prefixLen = this.Handle.s - prefixStartOrig;
484 char * suffixStartOrig = this.Handle.s + this.Handle.lnth;
485 int suffixLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - suffixStartOrig;
486
487 int delta = bsize - this.Handle.lnth;
488 if ( char * oldBytes = VbyteTryAdjustLast( *this.Handle.ulink, delta ) ) {
489 // growing: copy from old to new
490 char * dest = VbyteAlloc( *this.Handle.ulink, origEditSetLength + delta );
491 char *destCursor = dest; memcpy(destCursor, prefixStartOrig, prefixLen);
492 destCursor += prefixLen; memcpy(destCursor, buffer , bsize );
493 destCursor += bsize; memcpy(destCursor, suffixStartOrig, suffixLen);
494 assignEditSet(this, shareEditSetStartPeer, shareEditSetEndPeer,
495 dest,
496 origEditSetLength + delta,
497 0p, bsize);
498 free( oldBytes );
499 } else {
500 // room is already allocated in-place: bubble suffix and overwite middle
501 memmove( suffixStartOrig + delta, suffixStartOrig, suffixLen );
502 memcpy( this.Handle.s, buffer, bsize );
503
504 assignEditSet(this, shareEditSetStartPeer, shareEditSetEndPeer,
505 shareEditSetStartPeer->Handle.s,
506 origEditSetLength + delta,
507 0p, bsize);
508 }
509
510 } else if ( // assigning to shared context
511 this.Handle.lnth == origEditSetLength && // overwriting entire run of SES
512 & valSrc && // sourcing from a managed string
513 valSrc.Handle.ulink == this.Handle.ulink ) { // sourcing from same heap
514
515 // SES's result will only use characters from the source string => reuse source
516 assignEditSet(this, shareEditSetStartPeer, shareEditSetEndPeer,
517 valSrc.Handle.s,
518 valSrc.Handle.lnth,
519 &((string_res&)valSrc).Handle, bsize);
520
521 } else {
522 // overwriting a proper substring of some string: mash characters from old and new together (copy on write)
523 // OR we are importing characters: need to copy eagerly (can't refer to source)
524
525 // full string is from start of shareEditSetStartPeer thru end of shareEditSetEndPeer
526 // `this` occurs in the middle of it, to be replaced
527 // build up the new text in `pasting`
528
529 string_res pasting = {
530 * this.Handle.ulink, // maintain same heap, regardless of context
531 shareEditSetStartPeer->Handle.s, // start of SES
532 this.Handle.s - shareEditSetStartPeer->Handle.s }; // length of SES, before this
533 append( pasting,
534 buffer, // start of replacement for this
535 bsize ); // length of replacement for this
536 append( pasting,
537 this.Handle.s + this.Handle.lnth, // start of SES after this
538 shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth -
539 (this.Handle.s + this.Handle.lnth) ); // length of SES, after this
540
541 // The above string building can trigger compaction.
542 // The reference points (that are arguments of the string building) may move during that building.
543 // From this point on, they are stable.
544
545 assignEditSet(this, shareEditSetStartPeer, shareEditSetEndPeer,
546 pasting.Handle.s,
547 pasting.Handle.lnth,
548 &pasting.Handle, bsize);
549 }
550
551 return this;
552}
553
554string_res & assign(string_res &this, const char* buffer, size_t bsize) {
555 return assign_(this, buffer, bsize, *0p);
556}
557
558string_res & ?=?(string_res &s, char other) {
559 return assign(s, &other, 1);
560}
561
562// Copy assignment operator
563string_res & ?=?(string_res & this, const string_res & rhs) with( this ) {
564 return assign_(this, rhs.Handle.s, rhs.Handle.lnth, rhs);
565}
566
567string_res & ?=?(string_res & this, string_res & rhs) with( this ) {
568 const string_res & rhs2 = rhs;
569 return this = rhs2;
570}
571
572
573// Destructor
574void ^?{}(string_res &s) with(s) {
575 // much delegated to implied ^VbyteSM
576
577 // sever s from its share-edit peers, if any (four no-ops when already solo)
578 s.shareEditSet_prev->shareEditSet_next = s.shareEditSet_next;
579 s.shareEditSet_next->shareEditSet_prev = s.shareEditSet_prev;
580 // s.shareEditSet_next = &s;
581 // s.shareEditSet_prev = &s;
582
583 if (shareEditSet_owns_ulink && s.shareEditSet_next == &s) { // last one out
584 delete( s.Handle.ulink );
585 }
586}
587
588
589// Returns the character at the given index
590// With unicode support, this may be different from just the byte at the given
591// offset from the start of the string.
592char ?[?](const string_res &s, size_t index) with(s) {
593 //TODO: Check if index is valid (no exceptions yet)
594 return Handle.s[index];
595}
596
597void assignAt(const string_res &s, size_t index, char val) {
598 string_res editZone = { s, SHARE_EDITS, index, index+1 };
599 assign(editZone, &val, 1);
600}
601
602
603///////////////////////////////////////////////////////////////////
604// Concatenation
605
606void append(string_res &str1, const char * buffer, size_t bsize) {
607 size_t clnth = str1.Handle.lnth + bsize;
608 if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ?
609 // no-op
610 } else { // must copy some text
611 if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?
612 VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area
613 } else { // copy the two parts
614 char * str1newBuf = VbyteAlloc( *str1.Handle.ulink, clnth );
615 char * str1oldBuf = str1.Handle.s; // must read after VbyteAlloc call in case it gs's
616 str1.Handle.s = str1newBuf;
617 memcpy( str1.Handle.s, str1oldBuf, str1.Handle.lnth );
618 } // if
619 memcpy( str1.Handle.s + str1.Handle.lnth, buffer, bsize );
620 } // if
621 str1.Handle.lnth = clnth;
622}
623
624void ?+=?(string_res &str1, const string_res &str2) {
625 append( str1, str2.Handle.s, str2.Handle.lnth );
626}
627
628void ?+=?(string_res &s, char other) {
629 append( s, &other, 1 );
630}
631
632
633
634
635
636//////////////////////////////////////////////////////////
637// Comparisons
638
639int cmp(const string_res &s1, const string_res &s2) {
640 // return 0;
641 int ans1 = memcmp(s1.Handle.s, s2.Handle.s, min(s1.Handle.lnth, s2.Handle.lnth));
642 if (ans1 != 0) return ans1;
643 return s1.Handle.lnth - s2.Handle.lnth;
644}
645
646bool ?==?(const string_res &s1, const string_res &s2) { return cmp(s1, s2) == 0; }
647bool ?!=?(const string_res &s1, const string_res &s2) { return cmp(s1, s2) != 0; }
648bool ?>? (const string_res &s1, const string_res &s2) { return cmp(s1, s2) > 0; }
649bool ?>=?(const string_res &s1, const string_res &s2) { return cmp(s1, s2) >= 0; }
650bool ?<=?(const string_res &s1, const string_res &s2) { return cmp(s1, s2) <= 0; }
651bool ?<? (const string_res &s1, const string_res &s2) { return cmp(s1, s2) < 0; }
652
653int cmp (const string_res &s1, const char* s2) {
654 string_res s2x = s2;
655 return cmp(s1, s2x);
656}
657
658bool ?==?(const string_res &s1, const char* s2) { return cmp(s1, s2) == 0; }
659bool ?!=?(const string_res &s1, const char* s2) { return cmp(s1, s2) != 0; }
660bool ?>? (const string_res &s1, const char* s2) { return cmp(s1, s2) > 0; }
661bool ?>=?(const string_res &s1, const char* s2) { return cmp(s1, s2) >= 0; }
662bool ?<=?(const string_res &s1, const char* s2) { return cmp(s1, s2) <= 0; }
663bool ?<? (const string_res &s1, const char* s2) { return cmp(s1, s2) < 0; }
664
665int cmp (const char* s1, const string_res & s2) {
666 string_res s1x = s1;
667 return cmp(s1x, s2);
668}
669
670bool ?==?(const char* s1, const string_res &s2) { return cmp(s1, s2) == 0; }
671bool ?!=?(const char* s1, const string_res &s2) { return cmp(s1, s2) != 0; }
672bool ?>? (const char* s1, const string_res &s2) { return cmp(s1, s2) > 0; }
673bool ?>=?(const char* s1, const string_res &s2) { return cmp(s1, s2) >= 0; }
674bool ?<=?(const char* s1, const string_res &s2) { return cmp(s1, s2) <= 0; }
675bool ?<? (const char* s1, const string_res &s2) { return cmp(s1, s2) < 0; }
676
677
678
679//////////////////////////////////////////////////////////
680// Search
681
682bool contains(const string_res &s, char ch) {
683 for ( i; size(s) ) {
684 if (s[i] == ch) return true;
685 }
686 return false;
687}
688
689int find(const string_res &s, char search) {
690 return findFrom(s, 0, search);
691}
692
693int findFrom(const string_res &s, size_t fromPos, char search) {
694 // FIXME: This paricular overload (find of single char) is optimized to use memchr.
695 // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match.
696 char * searchFrom = s.Handle.s + fromPos;
697 size_t searchLnth = s.Handle.lnth - fromPos;
698 int searchVal = search;
699 char * foundAt = (char *) memchr(searchFrom, searchVal, searchLnth);
700 if (foundAt == 0p) return s.Handle.lnth;
701 else return foundAt - s.Handle.s;
702}
703
704int find(const string_res &s, const string_res &search) {
705 return findFrom(s, 0, search);
706}
707
708int findFrom(const string_res &s, size_t fromPos, const string_res &search) {
709 return findFrom(s, fromPos, search.Handle.s, search.Handle.lnth);
710}
711
712int find(const string_res &s, const char* search) {
713 return findFrom(s, 0, search);
714}
715int findFrom(const string_res &s, size_t fromPos, const char* search) {
716 return findFrom(s, fromPos, search, strlen(search));
717}
718
719int find(const string_res &s, const char* search, size_t searchsize) {
720 return findFrom(s, 0, search, searchsize);
721}
722
723int findFrom(const string_res &s, size_t fromPos, const char* search, size_t searchsize) {
724
725 /* Remaining implementations essentially ported from Sunjay's work */
726
727
728 // FIXME: This is a naive algorithm. We probably want to switch to someting
729 // like Boyer-Moore in the future.
730 // https://en.wikipedia.org/wiki/String_searching_algorithm
731
732 // Always find the empty string
733 if (searchsize == 0) {
734 return 0;
735 }
736
737 for ( i; fromPos ~ s.Handle.lnth ) {
738 size_t remaining = s.Handle.lnth - i;
739 // Never going to find the search string if the remaining string is
740 // smaller than search
741 if (remaining < searchsize) {
742 break;
743 }
744
745 bool matched = true;
746 for ( j; searchsize ) {
747 if (search[j] != s.Handle.s[i + j]) {
748 matched = false;
749 break;
750 }
751 }
752 if (matched) {
753 return i;
754 }
755 }
756
757 return s.Handle.lnth;
758}
759
760bool includes(const string_res &s, const string_res &search) {
761 return includes(s, search.Handle.s, search.Handle.lnth);
762}
763
764bool includes(const string_res &s, const char* search) {
765 return includes(s, search, strlen(search));
766}
767
768bool includes(const string_res &s, const char* search, size_t searchsize) {
769 return find(s, search, searchsize) < s.Handle.lnth;
770}
771
772bool startsWith(const string_res &s, const string_res &prefix) {
773 return startsWith(s, prefix.Handle.s, prefix.Handle.lnth);
774}
775
776bool startsWith(const string_res &s, const char* prefix) {
777 return startsWith(s, prefix, strlen(prefix));
778}
779
780bool startsWith(const string_res &s, const char* prefix, size_t prefixsize) {
781 if (s.Handle.lnth < prefixsize) {
782 return false;
783 }
784 return memcmp(s.Handle.s, prefix, prefixsize) == 0;
785}
786
787bool endsWith(const string_res &s, const string_res &suffix) {
788 return endsWith(s, suffix.Handle.s, suffix.Handle.lnth);
789}
790
791bool endsWith(const string_res &s, const char* suffix) {
792 return endsWith(s, suffix, strlen(suffix));
793}
794
795bool endsWith(const string_res &s, const char* suffix, size_t suffixsize) {
796 if (s.Handle.lnth < suffixsize) {
797 return false;
798 }
799 // Amount to offset the bytes pointer so that we are comparing the end of s
800 // to suffix. s.bytes + offset should be the first byte to compare against suffix
801 size_t offset = s.Handle.lnth - suffixsize;
802 return memcmp(s.Handle.s + offset, suffix, suffixsize) == 0;
803}
804
805 /* Back to Mike's work */
806
807
808///////////////////////////////////////////////////////////////////////////
809// charclass, include, exclude
810
811void ?{}( charclass_res & this, const string_res & chars) {
812 (this){ chars.Handle.s, chars.Handle.lnth };
813}
814
815void ?{}( charclass_res & this, const char * chars ) {
816 (this){ chars, strlen(chars) };
817}
818
819void ?{}( charclass_res & this, const char * chars, size_t charssize ) {
820 (this.chars){ chars, charssize };
821 // now sort it ?
822}
823
824void ^?{}( charclass_res & this ) {
825 ^(this.chars){};
826}
827
828static bool test( const charclass_res & mask, char c ) {
829 // instead, use sorted char list?
830 return contains( mask.chars, c );
831}
832
833int exclude(const string_res &s, const charclass_res &mask) {
834 for ( i; size(s) ) {
835 if ( test(mask, s[i]) ) return i;
836 }
837 return size(s);
838}
839
840int include(const string_res &s, const charclass_res &mask) {
841 for ( i; size(s) ) {
842 if ( ! test(mask, s[i]) ) return i;
843 }
844 return size(s);
845}
846
847//######################### VbyteHeap "implementation" #########################
848
849
850// Add a new HandleNode node n after the current HandleNode node.
851
852static void AddThisAfter( HandleNode & this, HandleNode & n ) with(this) {
853#ifdef VbyteDebug
854 serr | "enter:AddThisAfter, this:" | &this | " n:" | &n;
855#endif // VbyteDebug
856 // Performance note: we are on the critical path here. MB has ensured that the verifies don't contribute to runtime (are compiled away, like they're supposed to be).
857 verify( n.ulink != 0p );
858 verify( this.ulink == n.ulink );
859 flink = n.flink;
860 blink = &n;
861 n.flink->blink = &this;
862 n.flink = &this;
863#ifdef VbyteDebug
864 {
865 serr | "HandleList:";
866 serr | nlOff;
867 for ( HandleNode *ni = HeaderPtr->flink; ni != HeaderPtr; ni = ni->flink ) {
868 serr | "\tnode:" | ni | " lnth:" | ni->lnth | " s:" | (void *)ni->s | ",\"";
869 for ( i; ni->lnth ) {
870 serr | ni->s[i];
871 } // for
872 serr | "\" flink:" | ni->flink | " blink:" | ni->blink | nl;
873 } // for
874 serr | nlOn;
875 }
876 serr | "exit:AddThisAfter";
877#endif // VbyteDebug
878} // AddThisAfter
879
880
881// Delete the current HandleNode node.
882
883static void DeleteNode( HandleNode & this ) with(this) {
884#ifdef VbyteDebug
885 serr | "enter:DeleteNode, this:" | &this;
886#endif // VbyteDebug
887 flink->blink = blink;
888 blink->flink = flink;
889#ifdef VbyteDebug
890 serr | "exit:DeleteNode";
891#endif // VbyteDebug
892} // DeleteNode
893
894
895
896// Allocates specified storage for a string from byte-string area. If not enough space remains to perform the
897// allocation, the garbage collection routine is called.
898
899static char * VbyteAlloc( VbyteHeap & this, int size ) with(this) {
900#ifdef VbyteDebug
901 serr | "enter:VbyteAlloc, size:" | size;
902#endif // VbyteDebug
903 uintptr_t NoBytes;
904 char *r;
905
906 NoBytes = ( uintptr_t )EndVbyte + size;
907 if ( NoBytes > ( uintptr_t )ExtVbyte ) { // enough room for new byte-string ?
908 garbage( this, size ); // firer up the garbage collector
909 verify( (( uintptr_t )EndVbyte + size) <= ( uintptr_t )ExtVbyte && "garbage run did not free up required space" );
910 } // if
911 r = EndVbyte;
912 EndVbyte += size;
913#ifdef VbyteDebug
914 serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;
915#endif // VbyteDebug
916 return r;
917} // VbyteAlloc
918
919
920// Adjusts the last allocation in this heap by delta bytes, or resets this heap to be able to offer
921// new allocations of its original size + delta bytes. Positive delta means bigger;
922// negative means smaller. A null return indicates that the original heap location has room for
923// the requested growth. A non-null return indicates that copying to a new location is required
924// but has not been done; the returned value is the old heap storage location; `this` heap is
925// modified to reference the new location. In the copy-requred case, the caller should use
926// VbyteAlloc to claim the new space, while doing optimal copying from old to new, then free old.
927
928static char * VbyteTryAdjustLast( VbyteHeap & this, int delta ) with(this) {
929
930 if ( ( uintptr_t )EndVbyte + delta <= ( uintptr_t )ExtVbyte ) {
931 // room available
932 EndVbyte += delta;
933 return 0p;
934 }
935
936 char *oldBytes = StartVbyte;
937
938 NoOfExtensions += 1;
939 CurrSize *= 2;
940 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
941 ExtVbyte = StartVbyte + CurrSize;
942
943 return oldBytes;
944}
945
946
947// Move an existing HandleNode node h somewhere after the current HandleNode node so that it is in ascending order by
948// the address in the byte string area.
949
950static void MoveThisAfter( HandleNode & this, const HandleNode & h ) with(this) {
951#ifdef VbyteDebug
952 serr | "enter:MoveThisAfter, this:" | & this | " h:" | & h;
953#endif // VbyteDebug
954 verify( h.ulink != 0p );
955 verify( this.ulink == h.ulink );
956 if ( s < h.s ) { // check argument values
957 // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:"
958 // | ( h->s ) | " and keep handles in ascending order";
959 // exit(-1 );
960 verify( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order");
961 } // if
962
963 HandleNode *i;
964 for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h
965 if ( & this != i->blink ) {
966 DeleteNode( this );
967 AddThisAfter( this, *i->blink );
968 } // if
969#ifdef VbyteDebug
970 {
971 serr | "HandleList:";
972 serr | nlOff;
973 for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) {
974 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
975 for ( i; n->lnth ) {
976 serr | n->s[i];
977 } // for
978 serr | "\" flink:" | n->flink | " blink:" | n->blink | nl;
979 } // for
980 serr | nlOn;
981 }
982 serr | "exit:MoveThisAfter";
983#endif // VbyteDebug
984} // MoveThisAfter
985
986
987
988
989
990//######################### VbyteHeap #########################
991
992// Compare two byte strings in the byte-string area. The routine returns the following values:
993//
994// 1 => Src1-byte-string > Src2-byte-string
995// 0 => Src1-byte-string = Src2-byte-string
996// -1 => Src1-byte-string < Src2-byte-string
997
998int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) {
999#ifdef VbyteDebug
1000 serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth;
1001#endif // VbyteDebug
1002 int cmp;
1003
1004 CharZip: for ( int i = 0; ; i += 1 ) {
1005 if ( i == Src2Lnth - 1 ) {
1006 for ( ; ; i += 1 ) {
1007 if ( i == Src1Lnth - 1 ) {
1008 cmp = 0;
1009 break CharZip;
1010 } // exit
1011 if ( Src1[Src1Start + i] != ' ') {
1012 // SUSPECTED BUG: this could be be why Peter got the bug report about == " " (why is this case here at all?)
1013 cmp = 1;
1014 break CharZip;
1015 } // exit
1016 } // for
1017 } // exit
1018 if ( i == Src1Lnth - 1 ) {
1019 for ( ; ; i += 1 ) {
1020 if ( i == Src2Lnth - 1 ) {
1021 cmp = 0;
1022 break CharZip;
1023 } // exit
1024 if ( Src2[Src2Start + i] != ' ') {
1025 cmp = -1;
1026 break CharZip;
1027 } // exit
1028 } // for
1029 } // exit
1030 if ( Src2[Src2Start + i] != Src1[Src1Start+ i]) {
1031 cmp = Src1[Src1Start + i] > Src2[Src2Start + i] ? 1 : -1;
1032 break CharZip;
1033 } // exit
1034 } // for
1035#ifdef VbyteDebug
1036 serr | "exit:ByteCmp, cmp:" | cmp;
1037#endif // VbyteDebug
1038 return cmp;
1039} // ByteCmp
1040
1041
1042// The compaction moves all of the byte strings currently in use to the beginning of the byte-string area and modifies
1043// the handles to reflect the new positions of the byte strings. Compaction assumes that the handle list is in ascending
1044// order by pointers into the byte-string area. The strings associated with substrings do not have to be moved because
1045// the containing string has been moved. Hence, they only require that their string pointers be adjusted.
1046
1047void compaction(VbyteHeap & this) with(this) {
1048 HandleNode *h;
1049 char *obase, *nbase, *limit;
1050
1051 NoOfCompactions += 1;
1052 EndVbyte = StartVbyte;
1053 h = Header.flink; // ignore header node
1054 for () {
1055 memmove( EndVbyte, h->s, h->lnth );
1056 obase = h->s;
1057 h->s = EndVbyte;
1058 nbase = h->s;
1059 EndVbyte += h->lnth;
1060 limit = obase + h->lnth;
1061 h = h->flink;
1062
1063 // check if any substrings are allocated within a string
1064
1065 for () {
1066 if ( h == &Header ) break; // end of header list ?
1067 if ( h->s >= limit ) break; // outside of current string ?
1068 h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase );
1069 h = h->flink;
1070 } // for
1071 if ( h == &Header ) break; // end of header list ?
1072 } // for
1073} // compaction
1074
1075
1076static double heap_expansion_freespace_threshold = 0.1; // default inherited from prior work: expand heap when less than 10% "free" (i.e. garbage)
1077 // probably an unreasonable default, but need to assess early-round tests on changing it
1078
1079void TUNING_set_string_heap_liveness_threshold( double val ) {
1080 heap_expansion_freespace_threshold = 1.0 - val;
1081}
1082
1083
1084// Garbage determines the amount of free space left in the heap and then reduces, leave the same, or extends the size of
1085// the heap. The heap is then compacted in the existing heap or into the newly allocated heap.
1086
1087void garbage(VbyteHeap & this, int minreq ) with(this) {
1088#ifdef VbyteDebug
1089 serr | "enter:garbage";
1090 {
1091 serr | "HandleList:";
1092 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
1093 serr | nlOff;
1094 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
1095 for ( i; n->lnth ) {
1096 serr | n->s[i];
1097 } // for
1098 serr | nlOn;
1099 serr | "\" flink:" | n->flink | " blink:" | n->blink;
1100 } // for
1101 }
1102#endif // VbyteDebug
1103 int AmountUsed, AmountFree;
1104
1105 AmountUsed = 0;
1106 for ( HandleNode *i = Header.flink; i != &Header; i = i->flink ) { // calculate amount of byte area used
1107 AmountUsed += i->lnth;
1108 } // for
1109 AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed;
1110
1111 if ( ( double ) AmountFree < ( CurrSize * heap_expansion_freespace_threshold ) || AmountFree < minreq ) { // free space less than threshold or not enough to serve cur request
1112
1113 extend( this, max( CurrSize, minreq ) ); // extend the heap
1114
1115 // Peter says, "This needs work before it should be used."
1116 // } else if ( AmountFree > CurrSize / 2 ) { // free space greater than 3 times the initial allocation ?
1117 // reduce(( AmountFree / CurrSize - 3 ) * CurrSize ); // reduce the memory
1118
1119 // `extend` implies a `compaction` during the copy
1120
1121 } else {
1122 compaction(this); // in-place
1123 }// if
1124#ifdef VbyteDebug
1125 {
1126 serr | "HandleList:";
1127 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) {
1128 serr | nlOff;
1129 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\"";
1130 for ( i; n->lnth ) {
1131 serr | n->s[i];
1132 } // for
1133 serr | nlOn;
1134 serr | "\" flink:" | n->flink | " blink:" | n->blink;
1135 } // for
1136 }
1137 serr | "exit:garbage";
1138#endif // VbyteDebug
1139} // garbage
1140
1141#undef VbyteDebug
1142
1143
1144
1145// Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
1146// area is deleted.
1147
1148void extend( VbyteHeap & this, int size ) with (this) {
1149#ifdef VbyteDebug
1150 serr | "enter:extend, size:" | size;
1151#endif // VbyteDebug
1152 char *OldStartVbyte;
1153
1154 NoOfExtensions += 1;
1155 OldStartVbyte = StartVbyte; // save previous byte area
1156
1157 CurrSize += size > InitSize ? size : InitSize; // minimum extension, initial size
1158 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize);
1159 ExtVbyte = (void *)( StartVbyte + CurrSize );
1160 compaction(this); // copy from old heap to new & adjust pointers to new heap
1161 free( OldStartVbyte ); // release old heap
1162#ifdef VbyteDebug
1163 serr | "exit:extend, CurrSize:" | CurrSize;
1164#endif // VbyteDebug
1165} // extend
1166
1167//WIP
1168#if 0
1169
1170// Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
1171// area is deleted.
1172
1173void VbyteHeap::reduce( int size ) {
1174#ifdef VbyteDebug
1175 serr | "enter:reduce, size:" | size;
1176#endif // VbyteDebug
1177 char *OldStartVbyte;
1178
1179 NoOfReductions += 1;
1180 OldStartVbyte = StartVbyte; // save previous byte area
1181
1182 CurrSize -= size;
1183 StartVbyte = EndVbyte = new char[CurrSize];
1184 ExtVbyte = (void *)( StartVbyte + CurrSize );
1185 compaction(); // copy from old heap to new & adjust pointers to new heap
1186 delete OldStartVbyte; // release old heap
1187#ifdef VbyteDebug
1188 serr | "exit:reduce, CurrSize:" | CurrSize;
1189#endif // VbyteDebug
1190} // reduce
1191
1192
1193#endif
Note: See TracBrowser for help on using the repository browser.