Listas com nome mutáveis no lugar (sem cópia); vazamento no "cor da imagem"
"lista [nome]" copiava a lista inteira a cada leitura e adicione / insira /
apague / substitua reconstruíam a lista a cada operação: um Z-buffer ou
qualquer lista grande ficava O(n) por acesso. Agora a lista com nome é um
objeto mutável, como no Scratch ("mude a lista para" continua copiando).
O JSON do core ganha tb_json_array_set / insert / remove / clear.
20 mil leituras + escritas numa lista de 10 mil: 13 ms (test-stdlib).
"cor da imagem" (Image e PPM) vazava os números de cada pixel lido.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bfa17eecea
commit
63c8fb0c1c
6 files changed
+127
-39
No files matched your search
@@ -6,6 +6,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
#include <tb_string.h>
|
||||
@@ -253,6 +254,63 @@ int main(void) {
|
||||
tb_block_destroy(li, rt); tb_block_destroy(st, rt); tb_block_destroy(lo, rt);
|
||||
}
|
||||
|
||||
/* listas com nome: objetos mutaveis (como no Scratch), sem copia */
|
||||
{
|
||||
run("List.Clear", "nl", NULL, NULL);
|
||||
run("List.Push", "a", "nl", NULL);
|
||||
run("List.Push", "c", "nl", NULL);
|
||||
run("List.InsertAt", "b", "2", "nl");
|
||||
run("List.InsertAt", "d", "último", "nl");
|
||||
CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[\"a\",\"b\",\"c\",\"d\"]"), "lista com nome: adicione e insira");
|
||||
run("List.SetAt", "1", "nl", "A");
|
||||
run("List.DeleteAt", "último", "nl", NULL);
|
||||
CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[\"A\",\"b\",\"c\"]"), "lista com nome: substitua e apague");
|
||||
run("List.Assign", "copia", "[1,2]", NULL);
|
||||
run("List.Push", "3", "copia", NULL);
|
||||
CHECK(is_list(run("List.Var", "copia", NULL, NULL), "[1,2,3]"), "mude a lista para: guarda uma cópia");
|
||||
run("List.Clear", "nl", NULL, NULL);
|
||||
CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[]"), "apague todos os itens");
|
||||
|
||||
/* 10 mil itens: ler e trocar um item nao pode custar a lista inteira */
|
||||
run("List.Clear", "big", NULL, NULL);
|
||||
TinyBlocks_Block* push = tb_block_create(rt, tb_rt_find_type(rt, "List.Push"));
|
||||
TinyBlocks_Value nm = tb_value_text("big");
|
||||
tb_block_set_default(push, 1, nm);
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
TinyBlocks_Value v = tb_value_integer(i);
|
||||
tb_block_set_default(push, 0, v);
|
||||
push->dirty = true;
|
||||
tb_block_eval(push, rt);
|
||||
}
|
||||
TinyBlocks_Block* var = tb_block_create(rt, tb_rt_find_type(rt, "List.Var"));
|
||||
TinyBlocks_Block* item = tb_block_create(rt, tb_rt_find_type(rt, "List.Item"));
|
||||
TinyBlocks_Block* set = tb_block_create(rt, tb_rt_find_type(rt, "List.SetAt"));
|
||||
tb_block_set_default(var, 0, nm);
|
||||
tb_block_connect(item, 0, var, 0);
|
||||
tb_block_set_default(set, 1, nm);
|
||||
clock_t t0 = clock();
|
||||
for (int i = 1; i <= 20000; i++) {
|
||||
TinyBlocks_Value k = tb_value_integer(i % 10000 + 1), v = tb_value_integer(-i);
|
||||
tb_block_set_default(item, 1, k);
|
||||
item->dirty = var->dirty = true;
|
||||
tb_block_eval(item, rt);
|
||||
tb_block_set_default(set, 0, k);
|
||||
tb_block_set_default(set, 2, v);
|
||||
set->dirty = true;
|
||||
tb_block_eval(set, rt);
|
||||
}
|
||||
double secs = (double)(clock() - t0) / CLOCKS_PER_SEC;
|
||||
TinyBlocks_Value k1 = tb_value_integer(10000);
|
||||
tb_block_set_default(item, 1, k1);
|
||||
item->dirty = var->dirty = true;
|
||||
tb_block_eval(item, rt);
|
||||
printf(" 20 mil leituras + escritas numa lista de 10 mil: %.3f s\n", secs);
|
||||
CHECK(is_int(&item->outputs[0], -19999), "lista grande: a escrita vale");
|
||||
CHECK(secs < 1.0, "lista grande: ler e trocar um item não copia a lista");
|
||||
tb_value_free(&nm);
|
||||
tb_block_destroy(set, rt); tb_block_destroy(item, rt); tb_block_destroy(var, rt); tb_block_destroy(push, rt);
|
||||
}
|
||||
|
||||
if (last) tb_block_destroy(last, rt);
|
||||
tinyblocks_deinit(&rt);
|
||||
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||
|
||||
Reference in new issue
Block a user