@@ -340,90 +340,87 @@ public void ReducingCapacity(in nuint reducingCount)
340340 _stackMemoryC = newMemory;
341341 _start = ({ typeof ( T ) . Name } *)_stackMemoryC.Start;{ incrementVersion }
342342 }}
343- else
343+ else if (_stackMemoryS != null)
344344 {{
345- if (_stackMemoryS != null )
345+ if (new IntPtr( _stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )) )
346346 {{
347- if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
348- {{
349- throw new Exception(""Failed to reduce available memory, stack moved further"");
350- }}
347+ throw new Exception(""Failed to reduce available memory, stack moved further"");
348+ }}
351349
352- if(Size == 0)
350+ if(Size == 0)
351+ {{
352+ _head = 0;
353+ _tail = 0;
354+ }}
355+ else
356+ if(_head > _tail)
357+ {{
358+ _stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity)), (long)(reducingCount * { sizeOf } ));
359+ _head -= reducingCount;{ incrementVersion }
360+ }}
361+ else
362+ if (_head <= _tail)
363+ {{
364+ if(Size == 1)
353365 {{
354- _head = 0 ;
366+ *(_start) = *(_start + _tail) ;
355367 _tail = 0;
368+ _head = 0;
356369 }}
357370 else
358- if(_head > _tail)
359- {{
360- _stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity)), (long)(reducingCount * { sizeOf } ));
361- _head -= reducingCount;{ incrementVersion }
362- }}
363- else
364- if (_head <= _tail)
365371 {{
366- if(Size == 1)
372+ var freeCountToEnd = Capacity - (_tail + 1);
373+ if(freeCountToEnd == 0 || freeCountToEnd < reducingCount)
367374 {{
368- *(_start) = *(_start + _tail);
369- _tail = 0;
375+ _stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * { sizeOf } ));
370376 _head = 0;
371- }}
372- else
373- {{
374- var freeCountToEnd = Capacity - (_tail + 1);
375- if(freeCountToEnd == 0 || freeCountToEnd < reducingCount)
376- {{
377- _stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * { sizeOf } ));
378- _head = 0;
379- _tail = Size - 1;{ incrementVersion }
380- }}
377+ _tail = Size - 1;{ incrementVersion }
381378 }}
382379 }}
383-
384- _stackMemoryS->FreeMemory(reducingCount * { sizeOf } );
385380 }}
386- else if (_stackMemoryC != null)
381+
382+ _stackMemoryS->FreeMemory(reducingCount * { sizeOf } );
383+ }}
384+ else if (_stackMemoryC != null)
385+ {{
386+ if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
387387 {{
388- if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
389- {{
390- throw new Exception(""Failed to reduce available memory, stack moved further"");
391- }}
388+ throw new Exception(""Failed to reduce available memory, stack moved further"");
389+ }}
392390
393- if(Size == 0)
391+ if(Size == 0)
392+ {{
393+ _head = 0;
394+ _tail = 0;
395+ }}
396+ else
397+ if(_head > _tail)
398+ {{
399+ _stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity * { sizeOf } )), (long)(reducingCount * { sizeOf } ));
400+ _head -= reducingCount;{ incrementVersion }
401+ }}
402+ else
403+ if (_head <= _tail)
404+ {{
405+ if(Size == 1)
394406 {{
395- _head = 0 ;
407+ *(_start) = *(_start + _tail) ;
396408 _tail = 0;
409+ _head = 0;
397410 }}
398411 else
399- if(_head > _tail)
400- {{
401- _stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity * { sizeOf } )), (long)(reducingCount * { sizeOf } ));
402- _head -= reducingCount;{ incrementVersion }
403- }}
404- else
405- if (_head <= _tail)
406412 {{
407- if(Size == 1)
413+ var freeCountToEnd = Capacity - (_tail + 1);
414+ if(freeCountToEnd == 0 || freeCountToEnd < reducingCount)
408415 {{
409- *(_start) = *(_start + _tail);
410- _tail = 0;
416+ _stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * { sizeOf } ));
411417 _head = 0;
418+ _tail = Size - 1;{ incrementVersion }
412419 }}
413- else
414- {{
415- var freeCountToEnd = Capacity - (_tail + 1);
416- if(freeCountToEnd == 0 || freeCountToEnd < reducingCount)
417- {{
418- _stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * { sizeOf } ));
419- _head = 0;
420- _tail = Size - 1;{ incrementVersion }
421- }}
422- }}
423- }}
420+ }}
421+ }}
424422
425- _stackMemoryC.FreeMemory(reducingCount * { sizeOf } );
426- }}
423+ _stackMemoryC.FreeMemory(reducingCount * { sizeOf } );
427424 }}
428425
429426 Capacity -= reducingCount;
@@ -489,35 +486,32 @@ public void ExpandCapacity(in nuint expandCount)
489486 _stackMemoryC = newMemory;
490487 _start = ({ typeof ( T ) . Name } *)_stackMemoryC.Start;{ incrementVersion }
491488 }}
492- else
489+ else if (_stackMemoryS != null)
493490 {{
494- if (_stackMemoryS != null )
491+ if (new IntPtr( _stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )) )
495492 {{
496- if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
497- {{
498- throw new Exception(""Failed to expand available memory, stack moved further"");
499- }}
493+ throw new Exception(""Failed to expand available memory, stack moved further"");
494+ }}
500495
501- _stackMemoryS->AllocateMemory(expandCount * { sizeOf } );
502- if(Size != 0 && _head != 0 && _head > _tail)
503- {{
504- _stackMemoryS->ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
505- _head += expandCount;
506- }}
496+ _stackMemoryS->AllocateMemory(expandCount * { sizeOf } );
497+ if(Size != 0 && _head != 0 && _head > _tail)
498+ {{
499+ _stackMemoryS->ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
500+ _head += expandCount;
507501 }}
508- else if (_stackMemoryC != null)
502+ }}
503+ else if (_stackMemoryC != null)
504+ {{
505+ if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
509506 {{
510- if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
511- {{
512- throw new Exception(""Failed to expand available memory, stack moved further"");
513- }}
507+ throw new Exception(""Failed to expand available memory, stack moved further"");
508+ }}
514509
515- _stackMemoryC.AllocateMemory(expandCount * { sizeOf } );
516- if (Size != 0 && _head != 0 && _head > _tail)
517- {{
518- _stackMemoryC.ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
519- _head += expandCount;
520- }}
510+ _stackMemoryC.AllocateMemory(expandCount * { sizeOf } );
511+ if (Size != 0 && _head != 0 && _head > _tail)
512+ {{
513+ _stackMemoryC.ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
514+ _head += expandCount;
521515 }}
522516 }}
523517
@@ -584,43 +578,40 @@ public bool TryExpandCapacity(in nuint expandCount)
584578 _stackMemoryC = newMemory;
585579 _start = ({ typeof ( T ) . Name } *)_stackMemoryC.Start;{ incrementVersion }
586580 }}
587- else
581+ else if (_stackMemoryS != null)
588582 {{
589- if (_stackMemoryS != null )
583+ if (new IntPtr( _stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )) )
590584 {{
591- if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
592- {{
593- return false;
594- }}
585+ return false;
586+ }}
595587
596- if(!_stackMemoryS->TryAllocateMemory(expandCount * { sizeOf } , out _))
597- {{
598- return false;
599- }}
588+ if(!_stackMemoryS->TryAllocateMemory(expandCount * { sizeOf } , out _))
589+ {{
590+ return false;
591+ }}
600592
601- if(Size != 0 && _head != 0 && _head > _tail)
602- {{
603- _stackMemoryS->ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
604- _head += expandCount;
605- }}
593+ if(Size != 0 && _head != 0 && _head > _tail)
594+ {{
595+ _stackMemoryS->ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
596+ _head += expandCount;
606597 }}
607- else if (_stackMemoryC != null)
598+ }}
599+ else if (_stackMemoryC != null)
600+ {{
601+ if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
608602 {{
609- if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * { sizeOf } )))
610- {{
611- return false;
612- }}
603+ return false;
604+ }}
613605
614- if(!_stackMemoryC.TryAllocateMemory(expandCount * { sizeOf } , out _))
615- {{
616- return false;
617- }}
606+ if(!_stackMemoryC.TryAllocateMemory(expandCount * { sizeOf } , out _))
607+ {{
608+ return false;
609+ }}
618610
619- if (Size != 0 && _head != 0 && _head > _tail)
620- {{
621- _stackMemoryC.ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
622- _head += expandCount;
623- }}
611+ if (Size != 0 && _head != 0 && _head > _tail)
612+ {{
613+ _stackMemoryC.ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * { sizeOf } ));{ incrementVersion }
614+ _head += expandCount;
624615 }}
625616 }}
626617
0 commit comments