Skip to content

Commit

Permalink
使用 nph 格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
haoyu234 committed Mar 5, 2024
1 parent 380df6f commit 3081cdd
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 84 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## instru
`Instru` is an intrusive container library that implements queues and heaps using the Nim language.

1. InstruQueue
2. InstruHeap
11 changes: 5 additions & 6 deletions instru.nimble
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Package

version = "0.1.0"
author = "haoyu234"
description = "A new awesome nimble package"
license = "MIT"
srcDir = "src"

version = "0.1.0"
author = "haoyu234"
description = "A new awesome nimble package"
license = "MIT"
srcDir = "src"

# Dependencies

Expand Down
44 changes: 25 additions & 19 deletions src/instru/heap.nim
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import macros

type
LessThen = proc(a, b: var InstruHeapNode): bool

InstruHeap* = object
len: int
top: ptr InstruHeapNode
lessThan: proc (a, b: var InstruHeapNode): bool
lessThan: LessThen

InstruHeapNode* = object
left: ptr InstruHeapNode
right: ptr InstruHeapNode
parent: ptr InstruHeapNode

proc len*(h: InstruHeap): int {.inline.} = h.len
proc top*(h: InstruHeap): ptr InstruHeapNode {.inline.} = h.top
proc isEmpty*(h: InstruHeap): bool {.inline.} = isNil(h.top)
TraverseResult = object
parentAddr: ptr ptr InstruHeapNode
nodeAddr: ptr ptr InstruHeapNode

proc len*(h: InstruHeap): int {.inline.} =
h.len

proc top*(h: InstruHeap): ptr InstruHeapNode {.inline.} =
h.top

proc isEmpty*(h: InstruHeap): bool {.inline.} =
isNil(h.top)

proc isEmpty*(n: InstruHeapNode): bool {.inline.} =
isNil(n.parent) and isNil(
n.left) and isNil(n.right)
isNil(n.parent) and isNil(n.left) and isNil(n.right)

proc isQueued*(h: InstruHeap, n: var InstruHeapNode): bool {.inline.} =
h.top == n.addr or not n.isEmpty

proc initEmpty*(h: var InstruHeap,
lessThan: proc (a, b: var InstruHeapNode): bool) {.inline.} =

proc initEmpty*(h: var InstruHeap, lessThan: LessThen) {.inline.} =
h.len = 0
h.top = nil
h.lessThan = lessThan
Expand Down Expand Up @@ -61,8 +69,7 @@ proc swap(h: var InstruHeap, a, b: var InstruHeapNode) =
else:
b.parent.right = b.addr

proc traverse(h: var InstruHeap, n: int): (ptr ptr InstruHeapNode,
ptr ptr InstruHeapNode) =
proc traverse(h: var InstruHeap, n: int): TraverseResult =
var k: uint32 = 0
var path: uint32 = 0

Expand All @@ -85,7 +92,7 @@ proc traverse(h: var InstruHeap, n: int): (ptr ptr InstruHeapNode,
path = path shr 1
dec k

(p, c)
TraverseResult(parentAddr: p, nodeAddr: c)

proc shiftUp(h: var InstruHeap, n: var InstruHeapNode) {.inline.} =
let lessThan = h.lessThan
Expand All @@ -96,10 +103,9 @@ proc shiftUp(h: var InstruHeap, n: var InstruHeapNode) {.inline.} =
proc insert*(h: var InstruHeap, n: var InstruHeapNode) =
assert not h.isQueued(n)

let (p, c) = traverse(h, succ h.len)

n.parent = p[]
c[] = n.addr
let r = traverse(h, succ h.len)
n.parent = r.parentAddr[]
r.nodeAddr[] = n.addr

inc h.len

Expand All @@ -109,9 +115,9 @@ proc remove*(h: var InstruHeap, n: var InstruHeapNode) =
assert h.isQueued(n)

var c = block:
var (_, c) = traverse(h, h.len)
var result = c[]
c[] = nil
let r = traverse(h, h.len)
var result = r.nodeAddr[]
r.nodeAddr[] = nil
result

dec h.len
Expand Down
10 changes: 1 addition & 9 deletions src/instru/macros.nim
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import std/with
import std/macros

macro new*[T](a: typedesc[T], body: untyped): ptr T =
result = quote do:
var p = cast[ptr `a`](alloc0(sizeof(`a`)))
with p:
`body`
p

macro containerOf*(p: pointer, a: typedesc, b: untyped): untyped =
let pt = newNimNode(nnkPtrTy).add(a)

result = quote do:
result = quote:
let
offset = offsetof(`a`, `b`)
base = cast[uint](`p`) - cast[uint](offset)
Expand Down
27 changes: 8 additions & 19 deletions src/instru/queue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ type

InstruQueue* = distinct InstruQueueNode

proc next*(h: InstruQueueNode): ptr InstruQueueNode {.inline.} = h.next
proc previous*(h: InstruQueueNode): ptr InstruQueueNode {.inline.} = h.previous
proc head*(h: InstruQueue): ptr InstruQueueNode {.inline.} = InstruQueueNode(h).next
proc next*(h: InstruQueueNode): ptr InstruQueueNode {.inline.} =
h.next

proc previous*(h: InstruQueueNode): ptr InstruQueueNode {.inline.} =
h.previous

proc head*(h: InstruQueue): ptr InstruQueueNode {.inline.} =
InstruQueueNode(h).next

proc isEmpty*(h: var InstruQueueNode): bool {.inline.} =
h.next.isNil or h.addr == h.next
Expand All @@ -28,14 +33,6 @@ proc initEmpty*(h: var InstruQueueNode) {.inline.} =
h.next = h.addr
h.previous = h.addr

proc insertFront*(h, n: var InstruQueueNode) {.inline.} =
assert not n.isQueued

n.next = h.next
n.previous = h.addr
n.next.previous = n.addr
h.next = n.addr

proc insertFront*(h: var InstruQueue, n: var InstruQueueNode) {.inline.} =
assert not n.isQueued

Expand All @@ -44,14 +41,6 @@ proc insertFront*(h: var InstruQueue, n: var InstruQueueNode) {.inline.} =
n.next.previous = n.addr
InstruQueueNode(h).next = n.addr

proc insertBack*(h, n: var InstruQueueNode) {.inline.} =
assert not n.isQueued

n.next = h.addr
n.previous = h.previous
n.previous.next = n.addr
h.previous = n.addr

proc insertBack*(h: var InstruQueue, n: var InstruQueueNode) {.inline.} =
assert not n.isQueued

Expand Down
2 changes: 1 addition & 1 deletion tests/config.nims
Original file line number Diff line number Diff line change
@@ -1 +1 @@
switch("path", "$projectDir/../src")
switch("path", "$projectDir/../src")
9 changes: 9 additions & 0 deletions tests/new2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import std/with
import std/macros

macro new*[T](a: typedesc[T], body: untyped): ptr T =
result = quote:
var p = cast[ptr `a`](alloc0(sizeof(`a`)))
with p:
`body`
p
25 changes: 14 additions & 11 deletions tests/theap.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import unittest

import instru/heap
import instru/macros
import new2

import std/random

type
SchedJob = object
priority: int
executeJob: proc ()
instruHeap: InstruHeapNode
type SchedJob = object
priority: int
executeJob: proc()
instruHeap: InstruHeapNode

proc insertJob(h: var InstruHeap, j: ptr SchedJob) =
initEmpty(j.instruHeap)
Expand All @@ -20,14 +20,17 @@ proc insertJob(h: var InstruHeap, j: ptr SchedJob) =
template newJob(id: int, body: untyped): ptr SchedJob =
new(SchedJob):
priority = id
executeJob = proc () =
executeJob = proc() =
body

proc initHeap(h: var InstruHeap) =
initEmpty(h, proc (a, b: var InstruHeapNode): bool =
let aa = data(a, SchedJob, instruHeap)
let bb = data(b, SchedJob, instruHeap)
return aa.priority < bb.priority
initEmpty(
h,
proc(a, b: var InstruHeapNode): bool =
let aa = data(a, SchedJob, instruHeap)
let bb = data(b, SchedJob, instruHeap)
return aa.priority < bb.priority
,
)

test "insert":
Expand All @@ -49,7 +52,7 @@ test "isEmpty":

const n = 30

let round = proc () =
let round = proc() =
for i in countup(1, n):
let job = newJob(i):
discard "body"
Expand Down
39 changes: 20 additions & 19 deletions tests/tqueue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

import unittest

import std/sugar
import instru/queue
import instru/macros
import new2

import std/sugar

type
Job = object
executeJob: proc ()
instruQueue: InstruQueueNode
type Job = object
executeJob: proc()
instruQueue: InstruQueueNode

proc insertJob(q: var InstruQueue, j: ptr Job) =
initEmpty(j.instruQueue)
insertBack(q, j.instruQueue)

template newJob(body: untyped): ptr Job =
new(Job):
executeJob = proc () =
executeJob = proc() =
body

proc freeQueue(q: var InstruQueue) =
Expand All @@ -31,7 +32,7 @@ test "isEmpty":

check q.isEmpty

let job = newJob():
let job = newJob:
discard "body"

insertJob(q, job)
Expand All @@ -44,7 +45,7 @@ test "isEmpty":
check q.isEmpty

test "containerOf":
var job = newJob():
var job = newJob:
discard "body"

var addr1 = cast[pointer](job)
Expand All @@ -59,9 +60,9 @@ test "items":
var q = default(InstruQueue)
initEmpty(q)

for i in 1..num:
for i in 1 .. num:
insertJob(q):
newJob():
newJob:
n = n + 1

for i in q:
Expand All @@ -82,17 +83,17 @@ test "mergeInto":
initEmpty(q2)
initEmpty(q3)

for i in 1..5:
for i in 1 .. 5:
insertJob(q1):
newJob():
newJob:
n = n + 1

insertJob(q2):
newJob():
newJob:
n = n * 2

insertJob(q3):
newJob():
newJob:
n = n - 3

mergeInto(q2, q1)
Expand All @@ -119,9 +120,9 @@ test "moveInto":
initEmpty(q2)
initEmpty(q3)

for i in 1..5:
for i in 1 .. 5:
insertJob(q1):
newJob():
newJob:
n = n + 1

proc sum(q: InstruQueue): int =
Expand Down Expand Up @@ -154,10 +155,10 @@ test "popFront/popBack":

var s = 0

for i in 1..3:
for i in 1 .. 3:
insertJob(q):
capture i:
newJob():
newJob:
s = i

# popBack
Expand Down Expand Up @@ -192,7 +193,7 @@ test "isQueued":
var q = default(InstruQueue)
initEmpty(q)

let job = newJob():
let job = newJob:
discard "body"

assert not job.instruQueue.isQueued
Expand Down

0 comments on commit 3081cdd

Please sign in to comment.