高级语言参考
最后更新于
results: [..]Enemy;
results.reserve(128);values: [..]int;
values.append(10);
values.append(20);
values.append(30);
values.unordered_remove_by_value(20); // 与最后一个交换,速度快
values.ordered_remove_by_value(10); // 移动元素,保持顺序values.unordered_remove_by_index(0);
values.ordered_remove_by_index(0);values.clear(); // O(1):只是把 count 设为 0sum :: proc(arr: []int) -> int {
total := 0;
for v: arr {
total += v;
}
return total;
}
nums: [..]int;
nums.append(1);
nums.append(2);
nums.append(3);
total := sum(nums); // 隐式 [..]int -> []intfor v: nums {
log_info("v=%", {v});
}for i: 0..<inventory.capacity {
item := inventory.get_item(i);
if item == null continue;
// ...
}for i: 0..<inventory.capacity #reverse {
item := inventory.get_item(i);
if item == null continue;
// ...
}scores: Hashtable(string, s64);
scores.add("alice", 10);
scores.add_or_overwrite("bob", 25);
score, ok := scores.find("alice");
if ok {
log_info("alice score: %", {score});
}
for value, key: scores {
log_info("% = %", {key, value});
}Button_Handler :: class {
clicks: int;
}
UI_Button :: class {
on_click_userdata: Object;
on_click: proc(userdata: Object);
}
setup :: proc(btn: UI_Button, handler: Button_Handler) {
btn.on_click_userdata = handler;
btn.on_click = proc(userdata: Object) {
h := userdata.(Button_Handler);
h.clicks += 1;
};
}