examples: fix `v build-examples`
parent
5acc437367
commit
bfca55b87a
|
@ -2,7 +2,7 @@ import sync
|
||||||
import time
|
import time
|
||||||
|
|
||||||
// Simulate expensive computing using sleep function
|
// Simulate expensive computing using sleep function
|
||||||
fn expensive_computing(id, duration int, wg &sync.WaitGroup) {
|
fn expensive_computing(id, duration int, mut wg sync.WaitGroup) {
|
||||||
println('Executing expensive computing task (${id})...')
|
println('Executing expensive computing task (${id})...')
|
||||||
time.sleep_ms(duration)
|
time.sleep_ms(duration)
|
||||||
println('Finish task ${id} on ${duration} ms')
|
println('Finish task ${id} on ${duration} ms')
|
||||||
|
@ -10,11 +10,11 @@ fn expensive_computing(id, duration int, wg &sync.WaitGroup) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
wg := sync.new_waitgroup()
|
mut wg := sync.new_waitgroup()
|
||||||
wg.add(3)
|
wg.add(3)
|
||||||
go expensive_computing(1, 100, wg)
|
go expensive_computing(1, 100, mut wg)
|
||||||
go expensive_computing(2, 500, wg)
|
go expensive_computing(2, 500, mut wg)
|
||||||
go expensive_computing(3, 1000, wg)
|
go expensive_computing(3, 1000, mut wg)
|
||||||
// Join all tasks
|
// Join all tasks
|
||||||
wg.wait()
|
wg.wait()
|
||||||
println('All jobs finished!')
|
println('All jobs finished!')
|
||||||
|
|
|
@ -2,7 +2,7 @@ import net.http
|
||||||
import sync
|
import sync
|
||||||
import time
|
import time
|
||||||
|
|
||||||
fn vlang_time(wg &sync.WaitGroup) ?string {
|
fn vlang_time(mut wg sync.WaitGroup) ?string {
|
||||||
start := time.ticks()
|
start := time.ticks()
|
||||||
data := http.get('https://vlang.io/utc_now')?
|
data := http.get('https://vlang.io/utc_now')?
|
||||||
finish := time.ticks()
|
finish := time.ticks()
|
||||||
|
@ -12,7 +12,7 @@ fn vlang_time(wg &sync.WaitGroup) ?string {
|
||||||
return data.text
|
return data.text
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remote_ip(wg &sync.WaitGroup) ?string {
|
fn remote_ip(mut wg sync.WaitGroup) ?string {
|
||||||
start := time.ticks()
|
start := time.ticks()
|
||||||
data := http.get('https://api.ipify.org')?
|
data := http.get('https://api.ipify.org')?
|
||||||
finish := time.ticks()
|
finish := time.ticks()
|
||||||
|
@ -23,10 +23,10 @@ fn remote_ip(wg &sync.WaitGroup) ?string {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
wg := sync.new_waitgroup()
|
mut wg := sync.new_waitgroup()
|
||||||
wg.add(2)
|
wg.add(2)
|
||||||
// Run tasks async
|
// Run tasks async
|
||||||
go vlang_time(wg)
|
go vlang_time(mut wg)
|
||||||
go remote_ip(wg)
|
go remote_ip(mut wg)
|
||||||
wg.wait()
|
wg.wait()
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,33 +100,30 @@ fn energy(sys System) f64 {
|
||||||
|
|
||||||
fn arr_momentum() []Momentum {
|
fn arr_momentum() []Momentum {
|
||||||
return [
|
return [
|
||||||
Momentum {0.0, 0.0, 0.0, solar_mass},
|
Momentum {0.0, 0.0, 0.0, solar_mass},
|
||||||
Momentum {1.66007664274403694e-03 * days_per_year, 7.69901118419740425e-03 * days_per_year, -6.90460016972063023e-05 * days_per_year, 9.54791938424326609e-04 * solar_mass},
|
Momentum {1.66007664274403694e-03 * days_per_year, 7.69901118419740425e-03 * days_per_year, -6.90460016972063023e-05 * days_per_year, 9.54791938424326609e-04 * solar_mass},
|
||||||
Momentum {-2.76742510726862411e-03 * days_per_year, 4.99852801234917238e-03 * days_per_year, 2.30417297573763929e-05 * days_per_year, 2.85885980666130812e-04 * solar_mass},
|
Momentum {-2.76742510726862411e-03 * days_per_year, 4.99852801234917238e-03 * days_per_year, 2.30417297573763929e-05 * days_per_year, 2.85885980666130812e-04 * solar_mass},
|
||||||
Momentum {2.96460137564761618e-03 * days_per_year, 2.37847173959480950e-03 * days_per_year, -2.96589568540237556e-05 * days_per_year, 4.36624404335156298e-05 * solar_mass},
|
Momentum {2.96460137564761618e-03 * days_per_year, 2.37847173959480950e-03 * days_per_year, -2.96589568540237556e-05 * days_per_year, 4.36624404335156298e-05 * solar_mass},
|
||||||
Momentum {2.68067772490389322e-03 * days_per_year, 1.62824170038242295e-03 * days_per_year, -9.51592254519715870e-05 * days_per_year, 5.15138902046611451e-05 * solar_mass},
|
Momentum {2.68067772490389322e-03 * days_per_year, 1.62824170038242295e-03 * days_per_year, -9.51592254519715870e-05 * days_per_year, 5.15138902046611451e-05 * solar_mass},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn arr_position() []Position {
|
fn arr_position() []Position {
|
||||||
return [
|
return [
|
||||||
Position {0.0, 0.0, 0.0},
|
Position {0.0, 0.0, 0.0},
|
||||||
Position {4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01},
|
Position {4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01},
|
||||||
Position {8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01},
|
Position {8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01},
|
||||||
Position {1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01},
|
Position {1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01},
|
||||||
Position {1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01},
|
Position {1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
mut sys := &System {arr_momentum(), arr_position()}
|
||||||
sys := &System {arr_momentum(), arr_position()}
|
offsetmomentum(mut sys)
|
||||||
offsetmomentum(mut sys)
|
println('${energy(sys):.9f}') //-0.169075164
|
||||||
|
for _ in 0..50_000_000 {
|
||||||
println('${energy(sys):.9f}') //-0.169075164
|
advance(mut sys, 0.01)
|
||||||
for _ in 0..50000000 {
|
}
|
||||||
advance(mut sys, 0.01)
|
println('${energy(sys):.9f}') //-0.169059907
|
||||||
}
|
|
||||||
println('${energy(sys):.9f}') //-0.169059907
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,5 +46,6 @@ fn callback(req picohttpparser.Request, mut res picohttpparser.Response) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
println('Starting webserver on http://127.0.0.1:8088/ ...')
|
||||||
picoev.new(8088, &callback).serve()
|
picoev.new(8088, &callback).serve()
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,7 +128,7 @@ const (
|
||||||
black = C.sfons_rgba(0, 0, 0, 255)
|
black = C.sfons_rgba(0, 0, 0, 255)
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (state &AppState) render_font() {
|
fn (mut state AppState) render_font() {
|
||||||
lh := 30
|
lh := 30
|
||||||
mut dy := lh
|
mut dy := lh
|
||||||
if !state.inited {
|
if !state.inited {
|
||||||
|
|
|
@ -137,7 +137,7 @@ struct Game {
|
||||||
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
|
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
|
||||||
|
|
||||||
[if showfps]
|
[if showfps]
|
||||||
fn (game &Game) showfps() {
|
fn (mut game Game) showfps() {
|
||||||
game.frame++
|
game.frame++
|
||||||
last_frame_ms := f64(game.frame_sw.elapsed().microseconds())/1000.0
|
last_frame_ms := f64(game.frame_sw.elapsed().microseconds())/1000.0
|
||||||
ticks := f64(game.second_sw.elapsed().microseconds())/1000.0
|
ticks := f64(game.second_sw.elapsed().microseconds())/1000.0
|
||||||
|
@ -149,7 +149,7 @@ fn (game &Game) showfps() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn frame(game &Game) {
|
fn frame(mut game Game) {
|
||||||
game.frame_sw.restart()
|
game.frame_sw.restart()
|
||||||
game.gg.begin()
|
game.gg.begin()
|
||||||
game.draw_scene()
|
game.draw_scene()
|
||||||
|
@ -301,7 +301,7 @@ fn (mut g Game) get_tetro() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO mut
|
// TODO mut
|
||||||
fn (g &Game) drop_tetro() {
|
fn (mut g Game) drop_tetro() {
|
||||||
for i in 0..tetro_size{
|
for i in 0..tetro_size{
|
||||||
tetro := g.tetro[i]
|
tetro := g.tetro[i]
|
||||||
x := tetro.x + g.pos_x
|
x := tetro.x + g.pos_x
|
||||||
|
|
|
@ -165,13 +165,14 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
||||||
for {
|
for {
|
||||||
pret := req.parse_request(s, 100)
|
pret := req.parse_request(s, 100)
|
||||||
if pret <= 0 && s.len > 0 {
|
if pret <= 0 && s.len > 0 {
|
||||||
C.memmove(buf, s.str, s.len)
|
unsafe { C.memmove(buf, s.str, s.len) }
|
||||||
p.idx[fd] = s.len
|
p.idx[fd] = s.len
|
||||||
p.oidx[fd] = int(res.buf) - int(res.buf_start)
|
p.oidx[fd] = int(res.buf) - int(res.buf_start)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if req.method.str[0]==`p` || req.method.str[0]==`P` || req.method.str[0]==`d` || req.method.str[0]==`D` {
|
c0 := unsafe { req.method.str[0] }
|
||||||
mut j := 0
|
if c0 ==`p` || c0 == `P` || c0 == `d` || c0 == `D` {
|
||||||
|
mut j := 0
|
||||||
for {
|
for {
|
||||||
if j == req.num_headers {
|
if j == req.num_headers {
|
||||||
break
|
break
|
||||||
|
@ -238,7 +239,7 @@ pub fn new(port int, cb voidptr) &Picoev {
|
||||||
|
|
||||||
C.picoev_init(max_fds)
|
C.picoev_init(max_fds)
|
||||||
loop := C.picoev_create_loop(max_timeout)
|
loop := C.picoev_create_loop(max_timeout)
|
||||||
pv := &Picoev{
|
mut pv := &Picoev{
|
||||||
loop: loop
|
loop: loop
|
||||||
cb: cb
|
cb: cb
|
||||||
date: C.get_date()
|
date: C.get_date()
|
||||||
|
|
|
@ -9,7 +9,7 @@ pub mut:
|
||||||
buf byteptr
|
buf byteptr
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline] [unsafe_fn]
|
[inline]
|
||||||
fn (mut r Response) write_str(s string) {
|
fn (mut r Response) write_str(s string) {
|
||||||
unsafe {
|
unsafe {
|
||||||
C.memcpy(r.buf, s.str, s.len)
|
C.memcpy(r.buf, s.str, s.len)
|
||||||
|
|
Loading…
Reference in New Issue