-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
303 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
switch("experimental", "typeBoundOps") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# context_thread_local | ||
import ./mtasks, ./mlistdeques | ||
|
||
export mlistdeques # Exporting the type with destructor doesn't help | ||
# export tasks # solution 1. Exporting the inner type | ||
|
||
type MagicCompile = object | ||
dq: ListDeque[Task] | ||
|
||
# var x: MagicCompile # solution 2. Instantiating the type with destructors | ||
echo "Success" | ||
|
||
type | ||
TLContext* = object | ||
deque*: ListDeque[Task] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import mhandles | ||
|
||
type | ||
File* = ref object | ||
handle: Handle[FD] | ||
|
||
proc close*[T: File](f: T) = | ||
f.handle.close() | ||
|
||
proc newFile*(fd: FD): File = | ||
File(handle: initHandle(FD -1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
type | ||
FD* = distinct cint | ||
|
||
type | ||
AnyFD* = concept fd | ||
close(fd) | ||
|
||
proc close*(fd: FD) = | ||
discard | ||
|
||
type | ||
Handle*[T: AnyFD] = object | ||
fd: T | ||
|
||
proc close*[T: AnyFD](h: var Handle[T]) = | ||
close h.fd | ||
|
||
proc initHandle*[T: AnyFD](fd: T): Handle[T] = | ||
Handle[T](fd: fd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sets, hashes | ||
|
||
type | ||
Fruit* = ref object | ||
id*: int | ||
|
||
# Generic implementation. This doesn't work | ||
EntGroup*[T] = ref object | ||
freed*: HashSet[T] | ||
|
||
proc hash*(self: Fruit): Hash = hash(self.id) | ||
|
||
## | ||
## VVV The Generic implementation. This doesn't work VVV | ||
## | ||
|
||
proc initEntGroup*[T: Fruit](): EntGroup[T] = | ||
result = EntGroup[T]() | ||
result.freed = initHashSet[Fruit]() | ||
var apple = Fruit(id: 20) | ||
result.freed.incl(apple) | ||
|
||
proc get*[T: Fruit](fg: EntGroup[T]): T = | ||
if len(fg.freed) == 0: return | ||
# vvv It errors here | ||
# type mismatch: ([1] fg.freed: HashSet[grouptest.Fruit]) | ||
for it in fg.freed: | ||
return it | ||
|
||
## | ||
## VVV The Non-Generic implementation works VVV | ||
## | ||
type | ||
# Non-generic implementation. This works. | ||
FruitGroup* = ref object | ||
freed*: HashSet[Fruit] | ||
|
||
proc initFruitGroup*(): FruitGroup = | ||
result = FruitGroup() | ||
result.freed = initHashSet[Fruit]() | ||
var apple = Fruit(id: 20) | ||
result.freed.incl(apple) | ||
|
||
proc getNoGeneric*(fg: FruitGroup): Fruit = | ||
if len(fg.freed) == 0: return | ||
for it in fg.freed: | ||
return it | ||
|
||
proc `$`*(self: Fruit): string = | ||
# For echo | ||
if self == nil: return "Fruit()" | ||
return "Fruit(" & $(self.id) & ")" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# listdeques | ||
|
||
# needed, type bound ops aren't considered for undeclared procs | ||
type Placeholder = object | ||
proc allocate(_: Placeholder) = discard | ||
proc delete(_: Placeholder) = discard | ||
|
||
type | ||
StealableTask* = concept task, var mutTask, type T | ||
task is ptr | ||
task.prev is T | ||
task.next is T | ||
task.parent is T | ||
task.fn is proc (param: pointer) {.nimcall.} | ||
allocate(mutTask) | ||
delete(task) | ||
|
||
ListDeque*[T: StealableTask] = object | ||
head, tail: T | ||
|
||
func isEmpty*(dq: ListDeque): bool {.inline.} = | ||
discard | ||
|
||
func popFirst*[T](dq: var ListDeque[T]): T = | ||
discard | ||
|
||
proc `=destroy`*[T: StealableTask](dq: var ListDeque[T]) = | ||
mixin delete | ||
if dq.isEmpty(): | ||
return | ||
|
||
while (let task = dq.popFirst(); not task.isNil): | ||
delete(task) | ||
|
||
delete(dq.head) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# original example used queues | ||
import deques | ||
|
||
type | ||
QueueContainer*[T] = object | ||
q: ref Deque[T] | ||
|
||
proc init*[T](c: var QueueContainer[T]) = | ||
new(c.q) | ||
c.q[] = initDeque[T](64) | ||
|
||
proc addToQ*[T](c: var QueueContainer[T], item: T) = | ||
c.q[].addLast(item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import std/sets | ||
template foo*[T](a: T) = | ||
# proc foo*[T](a: T) = # works | ||
var s: HashSet[T] | ||
# echo contains(s, a) # works | ||
let x = a in s # BUG | ||
doAssert not x | ||
doAssert not (a in s) | ||
doAssert a notin s | ||
when isMainModule: foo(1) # works |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import sets | ||
|
||
proc initH*[V]: HashSet[V] = | ||
result = initHashSet[V]() | ||
|
||
proc foo*[V](h: var HashSet[V], c: seq[V]) = | ||
h = h + c.toHashSet() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import sets, sequtils | ||
|
||
proc dedupe*[T](arr: openArray[T]): seq[T] = | ||
arr.toHashSet.toSeq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# tasks.nim | ||
type | ||
Task* = ptr object | ||
parent*: Task | ||
prev*: Task | ||
next*: Task | ||
fn*: proc (param: pointer) {.nimcall.} | ||
|
||
# StealableTask API | ||
proc allocate*(task: var Task) = | ||
discard | ||
|
||
proc delete*(task: Task) = | ||
discard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
discard """ | ||
output: ''' | ||
Success | ||
''' | ||
""" | ||
|
||
# modified issue #12620, see placeholder procs in mlistdeques | ||
|
||
# runtime.nim | ||
import ./mcontext_thread_local | ||
|
||
var localCtx* : TLContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# issue #16755 | ||
|
||
import mfiles | ||
from mhandles import FD | ||
#import handles <- do this and it works | ||
|
||
let wr = newFile(FD -1) | ||
close wr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# issue #22984 | ||
# import sets # <<-- Uncomment this to make the error go away | ||
|
||
import mitems | ||
|
||
## The generic implementation | ||
var grp: EntGroup[Fruit] = initEntGroup[Fruit]() | ||
doAssert $get(grp) == "Fruit(20)" ## Errors here | ||
|
||
|
||
## This works though (Non-generic) | ||
var fruitGroup: FruitGroup = initFruitGroup() | ||
doAssert $getNoGeneric(fruitGroup) == "Fruit(20)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# issue #4773 | ||
|
||
import mqueuecontainer | ||
|
||
# works if this is uncommented (or if the `queuecontainer` exports `queues`): | ||
# import queues | ||
|
||
var c: QueueContainer[int] | ||
c.init() | ||
c.addToQ(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# issue #14729 | ||
|
||
import sets, hashes | ||
|
||
type | ||
Iterable[T] = concept x | ||
for value in items(x): | ||
type(value) is T | ||
|
||
Foo[T] = object | ||
t: T | ||
|
||
proc myToSet[T](keys: Iterable[T]): HashSet[T] = | ||
for x in items(keys): result.incl(x) | ||
|
||
proc hash[T](foo: Foo[T]): Hash = | ||
echo "specific hash" | ||
|
||
proc `==`[T](lhs, rhs: Foo[T]): bool = | ||
echo "specific equals" | ||
|
||
let | ||
f = Foo[string](t: "test") | ||
hs = [f, f].myToSet() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# issue #18150 | ||
|
||
import msetin | ||
foo(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# comment on issue #11167 | ||
|
||
import hashes | ||
|
||
import msetiter | ||
|
||
type | ||
Choice = object | ||
i: int | ||
|
||
proc hash(c: Choice): Hash = | ||
result = Hash(c.i) | ||
|
||
var h = initH[Choice]() | ||
let c = @[Choice(i: 1)] | ||
|
||
foo(h, c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# comment on issue #11167 | ||
|
||
import msetiter2 | ||
|
||
let x = dedupe([1, 2, 3]) | ||
doAssert x.len == 3 | ||
doAssert 1 in x | ||
doAssert 2 in x | ||
doAssert 3 in x |