mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: replace generic <>
with []
- part 2 (#16536)
This commit is contained in:
@ -5,7 +5,7 @@ waitgroups, mutexes etc.., you just need to supply a callback function, that
|
||||
will be called once per each item in your input array.
|
||||
|
||||
After all the work is done in parallel by the worker threads in the pool,
|
||||
pool.work_on_items will return. You can then call pool.get_results<Result>()
|
||||
pool.work_on_items will return. You can then call pool.get_results[Result]()
|
||||
to retrieve a list of all the results, that the worker callbacks returned
|
||||
for each input item. Example:
|
||||
|
||||
@ -17,7 +17,7 @@ pub struct SResult {
|
||||
}
|
||||
|
||||
fn sprocess(mut pp pool.PoolProcessor, idx int, wid int) &SResult {
|
||||
item := pp.get_item<string>(idx)
|
||||
item := pp.get_item[string](idx)
|
||||
println('idx: ${idx}, wid: ${wid}, item: ' + item)
|
||||
return &SResult{item.reverse()}
|
||||
}
|
||||
@ -26,7 +26,7 @@ fn main() {
|
||||
mut pp := pool.new_pool_processor(callback: sprocess)
|
||||
pp.work_on_items(['1abc', '2abc', '3abc', '4abc', '5abc', '6abc', '7abc'])
|
||||
// optionally, you can iterate over the results too:
|
||||
for x in pp.get_results<SResult>() {
|
||||
for x in pp.get_results[SResult]() {
|
||||
println('result: ${x.s}')
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ pub fn (mut pool PoolProcessor) set_max_jobs(njobs int) {
|
||||
// by the number of available cores on the system.
|
||||
// work_on_items returns *after* all threads finish.
|
||||
// You can optionally call get_results after that.
|
||||
pub fn (mut pool PoolProcessor) work_on_items<T>(items []T) {
|
||||
pub fn (mut pool PoolProcessor) work_on_items[T](items []T) {
|
||||
pool.work_on_pointers(unsafe { items.pointers() })
|
||||
}
|
||||
|
||||
@ -117,18 +117,18 @@ fn process_in_thread(mut pool PoolProcessor, task_id int) {
|
||||
|
||||
// get_item - called by the worker callback.
|
||||
// Retrieves a type safe instance of the currently processed item
|
||||
pub fn (pool &PoolProcessor) get_item<T>(idx int) T {
|
||||
pub fn (pool &PoolProcessor) get_item[T](idx int) T {
|
||||
return *(&T(pool.items[idx]))
|
||||
}
|
||||
|
||||
// get_result - called by the main thread to get a specific result.
|
||||
// Retrieves a type safe instance of the produced result.
|
||||
pub fn (pool &PoolProcessor) get_result<T>(idx int) T {
|
||||
pub fn (pool &PoolProcessor) get_result[T](idx int) T {
|
||||
return *(&T(pool.results[idx]))
|
||||
}
|
||||
|
||||
// get_results - get a list of type safe results in the main thread.
|
||||
pub fn (pool &PoolProcessor) get_results<T>() []T {
|
||||
pub fn (pool &PoolProcessor) get_results[T]() []T {
|
||||
mut res := []T{cap: pool.results.len}
|
||||
for i in 0 .. pool.results.len {
|
||||
res << *(&T(pool.results[i]))
|
||||
@ -137,7 +137,7 @@ pub fn (pool &PoolProcessor) get_results<T>() []T {
|
||||
}
|
||||
|
||||
// get_results_ref - get a list of type safe results in the main thread.
|
||||
pub fn (pool &PoolProcessor) get_results_ref<T>() []&T {
|
||||
pub fn (pool &PoolProcessor) get_results_ref[T]() []&T {
|
||||
mut res := []&T{cap: pool.results.len}
|
||||
for i in 0 .. pool.results.len {
|
||||
res << &T(pool.results[i])
|
||||
|
@ -10,14 +10,14 @@ pub struct IResult {
|
||||
}
|
||||
|
||||
fn worker_s(mut p pool.PoolProcessor, idx int, worker_id int) &SResult {
|
||||
item := p.get_item<string>(idx)
|
||||
item := p.get_item[string](idx)
|
||||
println('worker_s worker_id: ${worker_id} | idx: ${idx} | item: ${item}')
|
||||
time.sleep(3 * time.millisecond)
|
||||
return &SResult{'${item} ${item}'}
|
||||
}
|
||||
|
||||
fn worker_i(mut p pool.PoolProcessor, idx int, worker_id int) &IResult {
|
||||
item := p.get_item<int>(idx)
|
||||
item := p.get_item[int](idx)
|
||||
println('worker_i worker_id: ${worker_id} | idx: ${idx} | item: ${item}')
|
||||
time.sleep(5 * time.millisecond)
|
||||
return &IResult{item * 1000}
|
||||
@ -30,12 +30,12 @@ fn test_work_on_strings() {
|
||||
)
|
||||
|
||||
pool_s.work_on_items(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
|
||||
for x in pool_s.get_results<SResult>() {
|
||||
for x in pool_s.get_results[SResult]() {
|
||||
println(x.s)
|
||||
assert x.s.len > 1
|
||||
}
|
||||
println('---------- pool_s.get_results_ref: --------------')
|
||||
for x in pool_s.get_results_ref<SResult>() {
|
||||
for x in pool_s.get_results_ref[SResult]() {
|
||||
println(x.s)
|
||||
assert x.s.len > 1
|
||||
}
|
||||
@ -50,12 +50,12 @@ fn test_work_on_ints() {
|
||||
)
|
||||
|
||||
pool_i.work_on_items([1, 2, 3, 4, 5, 6, 7, 8])
|
||||
for x in pool_i.get_results<IResult>() {
|
||||
for x in pool_i.get_results[IResult]() {
|
||||
println(x.i)
|
||||
assert x.i > 100
|
||||
}
|
||||
println('---------- pool_i.get_results_ref: --------------')
|
||||
for x in pool_i.get_results_ref<IResult>() {
|
||||
for x in pool_i.get_results_ref[IResult]() {
|
||||
println(x.i)
|
||||
assert x.i > 100
|
||||
}
|
||||
|
Reference in New Issue
Block a user