Skip to content

Commit d57e6d1

Browse files
author
Vyacheslav
committed
refact: Reducing the nesting of IF-ELSE
1 parent c59d020 commit d57e6d1

File tree

2 files changed

+80
-98
lines changed

2 files changed

+80
-98
lines changed

Src/StackMemoryCollections/GeneratePrimitiveStack.cs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -296,26 +296,23 @@ public void ReducingCapacity(in nuint reducingCount)
296296
_stackMemoryC = newMemory;
297297
_start = ({typeof(T).Name}*)_stackMemoryC.Start;{incrementVersion}
298298
}}
299-
else
299+
else if(_stackMemoryS != null)
300300
{{
301-
if(_stackMemoryS != null)
301+
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
302302
{{
303-
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
304-
{{
305-
throw new Exception(""Failed to reduce available memory, stack moved further"");
306-
}}
307-
308-
_stackMemoryS->FreeMemory(reducingCount * {sizeOf});
303+
throw new Exception(""Failed to reduce available memory, stack moved further"");
309304
}}
310-
else if (_stackMemoryC != null)
311-
{{
312-
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
313-
{{
314-
throw new Exception(""Failed to reduce available memory, stack moved further"");
315-
}}
316305
317-
_stackMemoryC.FreeMemory(reducingCount * {sizeOf});
306+
_stackMemoryS->FreeMemory(reducingCount * {sizeOf});
307+
}}
308+
else if (_stackMemoryC != null)
309+
{{
310+
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
311+
{{
312+
throw new Exception(""Failed to reduce available memory, stack moved further"");
318313
}}
314+
315+
_stackMemoryC.FreeMemory(reducingCount * {sizeOf});
319316
}}
320317
321318
Capacity -= reducingCount;
@@ -353,26 +350,23 @@ public void ExpandCapacity(in nuint expandCount)
353350
_stackMemoryC = newMemory;
354351
_start = ({typeof(T).Name}*)_stackMemoryC.Start;{incrementVersion}
355352
}}
356-
else
353+
else if(_stackMemoryS != null)
357354
{{
358-
if(_stackMemoryS != null)
355+
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
359356
{{
360-
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
361-
{{
362-
throw new Exception(""Failed to expand available memory, stack moved further"");
363-
}}
364-
365-
_stackMemoryS->AllocateMemory(expandCount * {sizeOf});
357+
throw new Exception(""Failed to expand available memory, stack moved further"");
366358
}}
367-
else if (_stackMemoryC != null)
368-
{{
369-
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
370-
{{
371-
throw new Exception(""Failed to expand available memory, stack moved further"");
372-
}}
373359
374-
_stackMemoryC.AllocateMemory(expandCount * {sizeOf});
360+
_stackMemoryS->AllocateMemory(expandCount * {sizeOf});
361+
}}
362+
else if (_stackMemoryC != null)
363+
{{
364+
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
365+
{{
366+
throw new Exception(""Failed to expand available memory, stack moved further"");
375367
}}
368+
369+
_stackMemoryC.AllocateMemory(expandCount * {sizeOf});
376370
}}
377371
378372
Capacity += expandCount;
@@ -410,31 +404,28 @@ public bool TryExpandCapacity(in nuint expandCount)
410404
_stackMemoryC = newMemory;
411405
_start = ({typeof(T).Name}*)_stackMemoryC.Start;{incrementVersion}
412406
}}
413-
else
407+
else if(_stackMemoryS != null)
414408
{{
415-
if(_stackMemoryS != null)
409+
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
416410
{{
417-
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
418-
{{
419-
return false;
420-
}}
411+
return false;
412+
}}
421413
422-
if(!_stackMemoryS->TryAllocateMemory(expandCount * {sizeOf}, out _))
423-
{{
424-
return false;
425-
}}
414+
if(!_stackMemoryS->TryAllocateMemory(expandCount * {sizeOf}, out _))
415+
{{
416+
return false;
426417
}}
427-
else if (_stackMemoryC != null)
418+
}}
419+
else if (_stackMemoryC != null)
420+
{{
421+
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
428422
{{
429-
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
430-
{{
431-
return false;
432-
}}
423+
return false;
424+
}}
433425
434-
if(!_stackMemoryC.TryAllocateMemory(expandCount * {sizeOf}, out _))
435-
{{
436-
return false;
437-
}}
426+
if(!_stackMemoryC.TryAllocateMemory(expandCount * {sizeOf}, out _))
427+
{{
428+
return false;
438429
}}
439430
}}
440431

Src/StackMemoryCollections/GenerateStack.cs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -268,26 +268,23 @@ public void ReducingCapacity(in nuint reducingCount)
268268
_stackMemoryC = newMemory;
269269
_start = _stackMemoryC.Start;{incrementVersion}
270270
}}
271-
else
271+
else if(_stackMemoryS != null)
272272
{{
273-
if(_stackMemoryS != null)
273+
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
274274
{{
275-
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
276-
{{
277-
throw new Exception(""Failed to reduce available memory, stack moved further"");
278-
}}
279-
280-
_stackMemoryS->FreeMemory(reducingCount * {sizeOfStr});
275+
throw new Exception(""Failed to reduce available memory, stack moved further"");
281276
}}
282-
else if (_stackMemoryC != null)
283-
{{
284-
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
285-
{{
286-
throw new Exception(""Failed to reduce available memory, stack moved further"");
287-
}}
288277
289-
_stackMemoryC.FreeMemory(reducingCount * {sizeOfStr});
278+
_stackMemoryS->FreeMemory(reducingCount * {sizeOfStr});
279+
}}
280+
else if (_stackMemoryC != null)
281+
{{
282+
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
283+
{{
284+
throw new Exception(""Failed to reduce available memory, stack moved further"");
290285
}}
286+
287+
_stackMemoryC.FreeMemory(reducingCount * {sizeOfStr});
291288
}}
292289
293290
Capacity -= reducingCount;
@@ -325,26 +322,23 @@ public void ExpandCapacity(in nuint expandCount)
325322
_stackMemoryC = newMemory;
326323
_start = _stackMemoryC.Start;{incrementVersion}
327324
}}
328-
else
325+
else if(_stackMemoryS != null)
329326
{{
330-
if(_stackMemoryS != null)
327+
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
331328
{{
332-
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
333-
{{
334-
throw new Exception(""Failed to expand available memory, stack moved further"");
335-
}}
336-
337-
_stackMemoryS->AllocateMemory(expandCount * {sizeOfStr});
329+
throw new Exception(""Failed to expand available memory, stack moved further"");
338330
}}
339-
else if (_stackMemoryC != null)
340-
{{
341-
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
342-
{{
343-
throw new Exception(""Failed to expand available memory, stack moved further"");
344-
}}
345331
346-
_stackMemoryC.AllocateMemory(expandCount * {sizeOfStr});
332+
_stackMemoryS->AllocateMemory(expandCount * {sizeOfStr});
333+
}}
334+
else if (_stackMemoryC != null)
335+
{{
336+
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
337+
{{
338+
throw new Exception(""Failed to expand available memory, stack moved further"");
347339
}}
340+
341+
_stackMemoryC.AllocateMemory(expandCount * {sizeOfStr});
348342
}}
349343
350344
Capacity += expandCount;
@@ -382,31 +376,28 @@ public bool TryExpandCapacity(in nuint expandCount)
382376
_stackMemoryC = newMemory;
383377
_start = _stackMemoryC.Start;{incrementVersion}
384378
}}
385-
else
379+
else if(_stackMemoryS != null)
386380
{{
387-
if(_stackMemoryS != null)
381+
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
388382
{{
389-
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
390-
{{
391-
return false;
392-
}}
383+
return false;
384+
}}
393385
394-
if(!_stackMemoryS->TryAllocateMemory(expandCount * {sizeOfStr}, out _))
395-
{{
396-
return false;
397-
}}
386+
if(!_stackMemoryS->TryAllocateMemory(expandCount * {sizeOfStr}, out _))
387+
{{
388+
return false;
398389
}}
399-
else if (_stackMemoryC != null)
390+
}}
391+
else if (_stackMemoryC != null)
392+
{{
393+
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
400394
{{
401-
if (new IntPtr(_stackMemoryC.Current) != new IntPtr((byte*)_start + (Capacity * {sizeOfStr})))
402-
{{
403-
throw new Exception(""Failed to expand available memory, stack moved further"");
404-
}}
395+
throw new Exception(""Failed to expand available memory, stack moved further"");
396+
}}
405397
406-
if(!_stackMemoryC.TryAllocateMemory(expandCount * {sizeOfStr}, out _))
407-
{{
408-
return false;
409-
}}
398+
if(!_stackMemoryC.TryAllocateMemory(expandCount * {sizeOfStr}, out _))
399+
{{
400+
return false;
410401
}}
411402
}}
412403

0 commit comments

Comments
 (0)