This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.go
3299 lines (2807 loc) · 78.5 KB
/
stack.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package stackage
/*
Stack embeds slices of any ([]any) in pointer form
and extends methods allowing convenient interaction
with stack structures.
*/
type Stack struct {
*stack
}
/*
stack represents the underlying ordered slice type, which
is embedded (in pointer form) within instances of [Stack].
*/
type stack []any
/*
List initializes and returns a new instance of [Stack]
configured as a simple list. [Stack] instances of this
design can be delimited using the [Stack.SetDelimiter]
method.
*/
func List(capacity ...int) Stack {
return Stack{newStack(list, false, capacity...)}
}
/*
And initializes and returns a new instance of [Stack]
configured as a Boolean [And] stack.
*/
func And(capacity ...int) Stack {
return Stack{newStack(and, false, capacity...)}
}
/*
Or initializes and returns a new instance of [Stack]
configured as a Boolean [Or] stack.
*/
func Or(capacity ...int) Stack {
return Stack{newStack(or, false, capacity...)}
}
/*
Not initializes and returns a new instance of [Stack]
configured as a Boolean [Not] stack.
*/
func Not(capacity ...int) Stack {
return Stack{newStack(not, false, capacity...)}
}
/*
Basic initializes and returns a new instance of [Stack], set
for basic operation only.
Please note that instances of this design are not eligible
for string representation, value encaps, delimitation, and
other presentation-related string methods. As such, a zero
string (“) shall be returned should [Stack.String] be executed.
[PresentationPolicy] instances cannot be assigned to [Stack]
instances of this design.
*/
func Basic(capacity ...int) Stack {
return Stack{newStack(basic, false, capacity...)}
}
/*
newStack initializes a new instance of *stack, configured
with the kind (t) requested by the user. This function
should only be executed when creating new instances of
[Stack](or a [Stack] type alias), which embeds the *stack
type instance.
*/
func newStack(t stackType, fifo bool, c ...int) *stack {
var (
cfg *nodeConfig = new(nodeConfig)
st stack
)
cfg.log = newLogSystem(sLogDefault)
cfg.log.lvl = logLevels(sLogLevelDefault)
cfg.typ = t
cfg.ord = fifo
if len(c) > 0 {
if c[0] > 0 {
cfg.cap = c[0] + 1 // 1 for cfg slice offset
st = make(stack, 0, cfg.cap)
}
} else {
st = make(stack, 0)
}
st = append(st, cfg)
instance := &st
return instance
}
/*
IsEmpty returns a Boolean value indicative of a receiver length of zero
(0). This method wraps a call of [Stack.Len] == 0, and is only present
for compatibility and convenience reasons.
*/
func (r Stack) IsEmpty() bool {
if r.IsInit() {
return r.Len() == 0
}
return true
}
/*
IsZero returns a Boolean value indicative of whether the receiver is nil,
or uninitialized. Following use of [Stack.Free], this method will return
true.
*/
func (r Stack) IsZero() bool {
return r.stack.isZero()
}
/*
isZero is a private method called by [Stack.IsZero].
*/
func (r *stack) isZero() bool {
return r == nil
}
/*
SetLogLevel enables the specified [LogLevel] instance(s), thereby
instructing the logging subsystem to accept events for submission
and transcription to the underlying logger.
Users may also sum the desired bit values manually, and cast the
product as a LogLevel. For example, if STATE (4), DEBUG (8) and
TRACE (32) logging were desired, entering LogLevel(44) would be
the same as specifying LogLevel3, LogLevel4 and LogLevel6 in
variadic fashion.
*/
func (r Stack) SetLogLevel(l ...any) Stack {
if r.IsInit() {
if !r.getState(ronly) {
cfg, _ := r.config()
cfg.log.shift(l...)
}
}
return r
}
/*
LogLevels returns the string representation of a comma-delimited list
of all active [LogLevel] values within the receiver.
*/
func (r Stack) LogLevels() string {
cfg, _ := r.config()
return cfg.log.lvl.String()
}
/*
UnsetLogLevel disables the specified [LogLevel] instance(s), thereby
instructing the logging subsystem to discard events submitted for
transcription to the underlying logger.
*/
func (r Stack) UnsetLogLevel(l ...any) Stack {
if r.IsInit() {
if !r.getState(ronly) {
cfg, _ := r.config()
cfg.log.unshift(l...)
}
}
return r
}
/*
Addr returns the string representation of the pointer address for the
receiver. This may be useful for logging or debugging operations.
Note: this call uses [fmt.Sprintf].
*/
func (r Stack) Addr() string {
return ptrString(r.stack)
}
func ptrString(x any) (addr string) {
addr = `uninitialized`
if x != nil {
addr = sprintf("%p", x)
}
return
}
/*
Less returns a Boolean value indicative of whether the slice at index
i is deemed to be less than the slice at j in the context of ordering.
This method is intended to satisfy the func(int,int) bool signature
required by [sort.Interface].
See also [Stack.SetLessFunc] method for a means of specifying instances
of this function.
If no custom closure was assigned, the package-default closure is used,
which evaluates the string representation of values in order to conduct
alphabetical sorting. This means that both i and j must reference slice
values in one of the following states:
- Type of slice instance must have its own String method
- Value is a go primitive, such as a string or int
Equal values return false, as do zero string values.
*/
func (r Stack) Less(i, j int) (less bool) {
if r.IsInit() {
cfg, _ := r.stack.config()
if meth := cfg.lss; meth != nil {
less = meth(i, j)
} else {
less = r.stack.defaultLesser(i, j)
}
}
return
}
func (r stack) defaultLesser(idx1, idx2 int) bool {
slice1, _, _ := r.index(idx1)
slice2, _, _ := r.index(idx2)
var strs []string = make([]string, 2)
for idx, slice := range []any{
slice1,
slice2,
} {
if slice != nil {
var ok bool
if strs[idx], ok = slice.(string); !ok {
if meth := getStringer(slice); meth != nil {
strs[idx] = meth()
} else if isKnownPrimitive(slice) {
strs[idx] = primitiveStringer(slice)
}
}
if strs[idx] == `` {
return false
}
}
}
switch scmp(strs[0], strs[1]) {
case -1:
return true
}
return false
}
/*
SetLessFunc assigns the provided closure instance to the receiver instance,
thereby allowing effective use of the [Stack.Less] method.
If a nil value, or no values, are submitted, the package-default sorting
mechanism will take precedence.
*/
func (r Stack) SetLessFunc(function ...LessFunc) Stack {
if r.IsInit() {
if !r.getState(ronly) {
r.stack.setLessFunc(function...)
}
}
return r
}
func (r *stack) setLessFunc(function ...LessFunc) {
var funk LessFunc
if len(function) > 0 {
if function[0] == nil {
funk = r.defaultLesser
} else {
funk = function[0]
}
} else {
funk = r.defaultLesser
}
cfg, _ := r.config()
cfg.lss = funk
return
}
/*
Swap implements the func(int,int) signature required by the [sort.Interface]
signature.
*/
func (r Stack) Swap(i, j int) {
if r.IsInit() {
if !r.getState(ronly) {
r.stack.swap(i, j)
}
}
}
func (r *stack) swap(i, j int) {
if ok := i <= r.ulen(); !ok {
return
} else if ok = j <= r.ulen(); !ok {
return
}
i++
j++
r.lock()
defer r.unlock()
(*r)[i], (*r)[j] = (*r)[j], (*r)[i]
}
/*
SetAuxiliary assigns aux, as initialized and optionally populated as
needed by the user, to the receiver instance. The aux input value may
be nil.
If no variadic input is provided, the default [Auxiliary] allocation
shall occur.
Note that this method shall obliterate any instance that may already
be present, regardless of the state of the input value aux.
*/
func (r Stack) SetAuxiliary(aux ...Auxiliary) Stack {
if r.IsInit() {
if !r.getState(ronly) {
r.stack.setAuxiliary(aux...)
}
}
return r
}
/*
setAuxiliary is a private method called by [Stack.SetAuxiliary].
*/
func (r *stack) setAuxiliary(aux ...Auxiliary) {
cfg, _ := r.config()
var _aux Auxiliary
if len(aux) == 0 {
_aux = make(Auxiliary, 0)
} else {
if aux[0] == nil {
_aux = make(Auxiliary, 0)
} else {
_aux = aux[0]
}
}
cfg.aux = _aux
}
/*
Auxiliary returns the instance of [Auxiliary] from within the receiver.
*/
func (r Stack) Auxiliary() (aux Auxiliary) {
if r.IsInit() {
aux = r.stack.auxiliary()
}
return
}
/*
auxiliary is a private method called by [Stack.Auxiliary].
*/
func (r stack) auxiliary() (aux Auxiliary) {
sc, _ := r.config()
aux = sc.aux
return
}
/*
IsFIFO returns a Boolean value indicative of whether the underlying
receiver instance exhibits First-In-First-Out behavior as it pertains
to the appending and truncation order of the receiver instance.
A value of false implies Last-In-Last-Out behavior, which is the
default ordering scheme imposed upon instances of this type.
*/
func (r Stack) IsFIFO() (is bool) {
if r.IsInit() {
is = r.stack.isFIFO()
}
return
}
/*
isFIFO is a private method called by the [Stack.IsFIFO] method, et al.
*/
func (r stack) isFIFO() bool {
sc, _ := r.config()
result := sc.ord
return result
}
/*
SetFIFO shall assign the bool instance to the underlying receiver
configuration, declaring the nature of the append/truncate scheme
to be honored.
- A value of true shall impose First-In-First-Out behavior
- A value of false (the default) shall impose Last-In-First-Out behavior
This setting shall impose no influence on any methods other than
the [Stack.Pop] method. In other words, [Stack.Push], [Stack.Defrag],
[Stack.Remove], [Stack.Replace], et al., will all operate in the same
manner regardless.
Once set to the non-default value of true, this setting cannot be
changed nor toggled ever again for this instance and shall not be
subject to any override controls.
In short, once you go FIFO, you cannot go back.
*/
func (r Stack) SetFIFO(fifo bool) Stack {
if r.IsInit() {
if !r.getState(ronly) {
r.stack.setFIFO(fifo)
}
}
return r
}
func (r *stack) setFIFO(fifo bool) {
if sc, _ := r.config(); !sc.ord {
// can only change it once!
sc.ord = fifo
}
}
/*
Err returns the error residing within the receiver, or nil
if no error condition has been declared.
This method will be particularly useful for users who do
not care for fluent-style operations and would instead
prefer to operate in a more procedural fashion.
Note that a chained sequence of method calls of this type
shall potentially obscure error conditions along the way,
as each successive method may happily overwrite any error
instance already present.
*/
func (r Stack) Err() (err error) {
if r.IsInit() {
err = r.getErr()
}
return
}
/*
SetErr sets the underlying error value within the receiver
to the assigned input value err, whether nil or not.
This method may be most valuable to users who have chosen
to extend this type by aliasing, and wish to control the
handling of error conditions in another manner.
This may be used regardless of [Stack.IsReadOnly] status.
*/
func (r Stack) SetErr(err error) Stack {
if r.IsInit() {
r.stack.setErr(err)
}
return r
}
/*
setErr assigns an error instance, whether nil or not, to
the underlying receiver configuration.
*/
func (r *stack) setErr(err error) {
sc, _ := r.config()
sc.setErr(err)
}
/*
getErr returns the instance of error, whether nil or not, from
the underlying receiver configuration.
*/
func (r stack) getErr() (err error) {
sc, _ := r.config()
err = sc.getErr()
return
}
/*
kind returns the string representation of the kind value
set within the receiver's configuration value.
*/
func (r stack) kind() string {
sc, _ := r.config()
kind := sc.kind()
return kind
}
/*
Valid returns an error if the receiver lacks a configuration
value, or is unset as a whole. This method does not check to
see whether the receiver is in an error condition regarding
user content operations (see the [Stack.Err] method).
*/
func (r Stack) Valid() (err error) {
if r.stack == nil {
err = errorf("embedded instance is nil")
} else if !r.stack.valid() {
err = errorf("embedded instance is invalid")
}
return
}
/*
valid is a private method called by [Stack.Valid].
*/
func (r *stack) valid() (is bool) {
if r.isInit() {
// try to see if the user provided a
// validity function
stk := Stack{r}
if meth := stk.getValidityPolicy(); meth != nil {
if err := meth(r); err != nil {
return
}
}
is = true
}
return
}
/*
IsInit returns a Boolean value indicative of whether the
receiver has been initialized using any of the following
package-level functions:
- [And]
- [Or]
- [Not]
- [List]
- [Basic]
This method does not take into account the presence (or
absence) of any user-provided values (e.g.: a length of
zero (0)) can still return true.
*/
func (r Stack) IsInit() (is bool) {
if !r.IsZero() {
is = r.stack.isInit()
}
return
}
/*
isInit is a private method called by [Stack.IsInit].
*/
func (r *stack) isInit() bool {
//var err error
_, err := r.config()
return err == nil
}
/*
SetLogger assigns the specified logging facility to the receiver
instance.
Logging is available but is set to discard all events by default.
An active logging subsystem within the receiver supercedes the
default package logger.
The following types/values are permitted:
- string: `none`, `off`, `null`, `discard` will turn logging off
- string: `stdout` will set basic STDOUT logging
- string: `stderr` will set basic STDERR logging
- int: 0 will turn logging off
- int: 1 will set basic STDOUT logging
- int: 2 will set basic STDERR logging
- *[log.Logger]: user-defined *[log.Logger] instance will be set; it should not be nil
Case is not significant in the string matching process.
*/
func (r Stack) SetLogger(logger any) Stack {
if r.IsInit() {
if !r.getState(ronly) {
r.stack.setLogger(logger)
}
}
return r
}
/*
Transfer will iterate the receiver (r) and add all slices contained
therein to the destination instance (dest), which must be a previously
initialized [Stack] or [Stack]-alias instance, else false is returned.
If capacity constraints are in-force within the destination instance,
and the transfer request cannot proceed due to it being larger than
the sum number of available slices, false is returned.
If [Stack.IsInit] returns false, this method returns false, as there
is nothing to transfer.
The receiver instance (r) is not modified in any way as a result of
calling this method. If the receiver (source) should undergo a call to
its [Stack.Reset] or [Stack.Free] methods following a call to the this
method, only the source will be emptied, and all of the slices that have
since been transferred instance shall remain in the destination instance.
A return value of true indicates a successful transfer.
*/
func (r Stack) Transfer(dest any) (ok bool) {
if r.IsInit() {
if s, sok := stackTypeAliasConverter(dest); sok {
if !s.getState(ronly) {
ok = r.transfer(s.stack)
}
}
}
return
}
/*
transfer is a private method executed by the [Stack.Transfer]
method. It will return a *stack instance containing the same
slices as the receiver (r). Configuration is not copied, nor
is a destination *stack subject to initialization within this
method. Thus, the user must submit a *stack instance ready to
receive slices immediately.
Capacity enforcement is honored. If the source (r) contains
more slices than a non-zero destination capacity allows, the
operation is canceled outright and false is returned.
*/
func (r *stack) transfer(dest *stack) (ok bool) {
// if a capacity was set, make sure
// the destination can handle it...
if dest.cap() > 0 {
if r.ulen() > dest.cap()-r.ulen() {
// capacity is in-force, and
// there are too many slices
// to xfer.
// err := errorf("failed: capacity violation"))
return
}
}
// xfer slices, without any regard for
// nilness. Slice type is not subject
// to discrimination.
for i := 0; i < r.ulen(); i++ {
sl, _, _ := r.index(i) // cfg offset handled by index method
dest.push(sl)
}
// return result
ok = dest.ulen() >= r.ulen()
return
}
/*
Replace will overwrite slice idx using value x and returns a Boolean
value indicative of success.
If slice i does not exist (e.g.: idx > receiver len), then nothing is
altered and a false Boolean value is returned.
Use of the Replace method shall not result in fragmentation of the
receiver instance; this method does not honor any attempt to replace
any receiver slice value with nil.
*/
func (r Stack) Replace(x any, idx int) (ok bool) {
if r.IsInit() && x != nil {
if !r.getState(ronly) {
ok = r.stack.replace(x, idx)
}
}
return
}
func (r *stack) replace(x any, i int) (ok bool) {
if r != nil {
if ok = i+1 <= r.ulen(); ok {
(*r)[i+1] = x
}
}
return
}
/*
Insert will insert value x to become the left index. For example,
using zero (0) as left shall result in value x becoming the first
slice within the receiver.
This method returns a Boolean value indicative of success. A value
of true indicates the receiver length became longer by one (1).
This method does not currently respond to forward/negative index
support. An integer value less than or equal to zero (0) shall
become zero (0). An integer value that exceeds the length of the
receiver shall become index len-1. A value that falls within the
bounds of the receiver's current length is inserted as intended.
Use of the Insert method shall not result in fragmentation of the
receiver instance, as any nil x value shall be discarded and not
considered for insertion into the stack.
*/
func (r Stack) Insert(x any, left int) (ok bool) {
if r.IsInit() && x != nil {
if !r.getState(ronly) {
ok = r.stack.insert(x, left)
}
}
return
}
/*
insert is a private method called by [Stack.Insert].
*/
func (r *stack) insert(x any, left int) (ok bool) {
// note the len before we start
var u1 int = r.ulen()
// bail out if a capacity has been set and
// would be breached by this insertion.
if u1+1 > r.cap()-1 && r.cap() != 0 {
//err := errorf("failed: capacity violation")
return
}
r.lock()
defer r.unlock()
cfg, _ := r.config()
// If left is greater-than-or-equal
// to the user length, just push.
if u1-1 < left {
*r = append(*r, x)
// Verify something was added
ok = u1+1 == r.ulen()
return
}
var R stack = make(stack, 0)
left += 1
// If left is less-than-or-equal to
// zero (0), we'll use a new stack
// alloc (R) and move items into it
// in the appropriate order. The
// new element will be the first
// user slice.
if left <= 1 {
left = 1
R = append(R, cfg)
R = append(R, x)
R = append(R, (*r)[left:]...)
// If left falls within the user length,
// append all elements up to and NOT
// including the desired index, and also
// append everything after desired index.
// This leaves a slot into which we can
// drop the new element (x)
} else {
R = append((*r)[:left+1], (*r)[left:]...)
R[left] = x
}
// Verify something was added
*r = R
ok = u1+1 == r.ulen()
return
}
/*
Free frees the receiver instance entirely, including the underlying
configuration. An error is returned if the instance is read-only or
uninitialized.
See also [Stack.Reset].
*/
func (r *Stack) Free() (err error) {
if r.IsInit() {
if !r.getState(ronly) {
r.stack = nil
return
}
err = errorf("%T is read-only; cannot free", r)
}
return
}
/*
Reset will silently iterate and delete each slice found within
the receiver, leaving it unpopulated but still retaining its
active configuration. Nothing is returned. No action is taken
if the receiver is empty.
See also [Stack.Free].
*/
func (r Stack) Reset() {
if r.IsInit() {
if !r.getState(ronly) {
r.stack.reset()
}
}
}
/*
reset is a private method called by [Stack.Reset].
*/
func (r *stack) reset() {
var ct int = 0
for i := r.ulen(); i > 0; i-- {
ct++
r.remove(i - 1)
}
}
/*
Remove will remove and return the Nth slice from the index,
along with a success-indicative Boolean value. A value of
true indicates the receiver length became shorter by one (1).
Use of the Remove method shall not result in fragmentation of
the stack: gaps resulting from the removal of slice instances
shall immediately be "collapsed" using the subsequent slices
available. No action is taken if the receiver is empty or
read-only.
*/
func (r Stack) Remove(idx int) (slice any, ok bool) {
if r.IsInit() {
if !r.getState(ronly) {
slice, ok = r.stack.remove(idx)
}
}
return
}
/*
remove is a private method called by [Stack.Remove].
*/
func (r *stack) remove(idx int) (slice any, ok bool) {
var found bool
var index int
if slice, index, found = r.index(idx); found {
// note the len before we start
var u1 int = r.ulen()
var contents []any
var preserved int
// zero out everything except the config slice
cfg, _ := r.config()
var R stack = make(stack, 0)
R = append(R, cfg)
r.lock()
defer r.unlock()
// Gather what we want to keep.
for i := 1; i < r.len(); i++ {
if index != i {
preserved++
contents = append(contents, (*r)[i])
}
}
R = append(R, contents...)
*r = R
// make sure we succeeded both in non-nilness
// and in the expected integer length change.
ok = slice != nil && u1-1 == r.ulen()
}
return
}
/*
SetParen sets the string-encapsulation bit for parenthetical
expression within the receiver. The receiver shall undergo
parenthetical encapsulation ( (...) ) during the string
representation process. Individual string values shall not
be encapsulated in parenthesis, only the whole (current)
stack.
A Boolean input value explicitly sets the bit as intended.
Execution without a Boolean input value will *TOGGLE* the
current state of the encapsulation bit (i.e.: true->false
and false->true)
*/
func (r Stack) SetParen(state ...bool) Stack {
r.setState(parens, state...)
return r
}
/*
Deprecated: Use [Stack.SetParen].
*/
func (r Stack) Paren(state ...bool) Stack {
return r.SetParen(state...)
}
/*
IsNesting returns a Boolean value indicative of whether
at least one (1) slice member is either a [Stack] or [Stack]
type alias. If true, this indicates the relevant slice
descends into another hierarchical (nested) context.
*/
func (r Stack) IsNesting() (is bool) {
if r.IsInit() {
is = r.stack.isNesting()
}
return
}
/*
isNesting is a private method called by [Stack.IsNesting].
When called, this method returns a Boolean value indicative
of whether the receiver contains one (1) or more slice elements
that match either of the following conditions:
- Slice type is a [Stack] native type instance, OR ...
- Slice type is a [Stack] type-aliased instance
A return value of true is thrown at the first of either
occurrence. Length of matched candidates is not significant
during the matching process.
*/
func (r stack) isNesting() (is bool) {
// start iterating at index #1, thereby
// skipping the configuration slice.
for i := 1; i < r.len(); i++ {
// perform a type switch on the
// current index, thereby allowing
// evaluation of slice types.
switch tv := r[i].(type) {
// native Stack instance
case Stack:
is = true
// type alias stack instnaces, since
// we have no knowledge of them here,
// will be matched in default using
// the stackTypeAliasConverter func.
default:
// If convertible is true, we know the
// instance (tv) is a stack alias.
_, is = stackTypeAliasConverter(tv)
}
if is {
break
}
}
return
}
/*
IsParen returns a Boolean value indicative of whether the
receiver is parenthetical.
*/