Skip to content

Commit

Permalink
🚀 Enhancing CellSeq for Better Readability and Maintainability (#21797)
Browse files Browse the repository at this point in the history
Refactor and improve readability of CellSeq in system directory

* Use half-open range in the contains procedure for better readability and to avoid potential off-by-one errors
* Extract resizing logic from add procedure into a separate resize procedure for better code readability and separation of concerns
  • Loading branch information
jordan-gillard authored May 7, 2023
1 parent 4a94f36 commit 71f2e1a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/system/cellseqs_v1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ type
d: PCellArray

proc contains(s: CellSeq, c: PCell): bool {.inline.} =
for i in 0 .. s.len-1:
if s.d[i] == c: return true
for i in 0 ..< s.len:
if s.d[i] == c:
return true
return false

proc resize(s: var CellSeq) =
s.cap = s.cap * 3 div 2
let d = cast[PCellArray](alloc(s.cap * sizeof(PCell)))
copyMem(d, s.d, s.len * sizeof(PCell))
dealloc(s.d)
s.d = d

proc add(s: var CellSeq, c: PCell) {.inline.} =
if s.len >= s.cap:
s.cap = s.cap * 3 div 2
var d = cast[PCellArray](alloc(s.cap * sizeof(PCell)))
copyMem(d, s.d, s.len * sizeof(PCell))
dealloc(s.d)
s.d = d
# XXX: realloc?
resize(s)
s.d[s.len] = c
inc(s.len)

Expand Down

0 comments on commit 71f2e1a

Please sign in to comment.