mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
add binary search for prop
This commit is contained in:
parent
760044609e
commit
2244e33943
@ -331,17 +331,48 @@ Arg* _obj_getProp(PikaObj* obj, char* name) {
|
||||
}
|
||||
}
|
||||
Hash method_hash = hash_time33(name);
|
||||
#define BINARY_SEARCH_ENABLE 1
|
||||
while (1) {
|
||||
if (prop == NULL) {
|
||||
break;
|
||||
}
|
||||
for (uint32_t i = 0; i < prop->methodGroupCount; i++) {
|
||||
#if BINARY_SEARCH_ENABLE
|
||||
/* binary search */
|
||||
int size = prop->methodGroupCount;
|
||||
if (size == 0) {
|
||||
goto next;
|
||||
}
|
||||
int left = 0;
|
||||
int right = size - 1;
|
||||
int mid = 0;
|
||||
int i = 0;
|
||||
while (1) {
|
||||
i++;
|
||||
if (left > right) {
|
||||
break;
|
||||
}
|
||||
mid = (right + left) >> 1;
|
||||
Arg* prop_this = (Arg*)(prop->methodGroup + mid);
|
||||
if (prop_this->name_hash == method_hash) {
|
||||
method = prop_this;
|
||||
goto exit;
|
||||
} else if (prop_this->name_hash < method_hash) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
for (int i = 0; i < (int)prop->methodGroupCount; i++) {
|
||||
Arg* prop_this = (Arg*)(prop->methodGroup + i);
|
||||
if (method_hash == prop_this->name_hash) {
|
||||
if (prop_this->name_hash == method_hash) {
|
||||
method = prop_this;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
goto next;
|
||||
#endif
|
||||
next:
|
||||
prop = (NativeProperty*)prop->super;
|
||||
}
|
||||
exit:
|
||||
|
@ -89,8 +89,8 @@ void PikaStdLib_SysObj_printMethod(PikaObj* self, Args* args);
|
||||
method_typedef(PikaStdLib_SysObj_print, "print", "*val,**ops");
|
||||
|
||||
class_def(PikaStdLib_SysObj){
|
||||
method_def(PikaStdLib_SysObj_float, hash_time33("float")),
|
||||
method_def(PikaStdLib_SysObj_int, hash_time33("int")),
|
||||
method_def(PikaStdLib_SysObj_float, hash_time33("float")),
|
||||
method_def(PikaStdLib_SysObj_print, hash_time33("print")),
|
||||
};
|
||||
class_inhert(PikaStdLib_SysObj, TinyObj);
|
||||
|
@ -200,12 +200,25 @@ impl ClassInfo {
|
||||
let class_def = format!("class_def({}){{\n", self.this_class_name);
|
||||
new_class_fn.push_str(&class_def);
|
||||
|
||||
for (_, method_info) in self.method_list.iter() {
|
||||
let method_info_list = self.method_list.values().collect::<Vec<&MethodInfo>>();
|
||||
|
||||
/* sort by name_hash */
|
||||
let mut method_info_list_sorted = method_info_list.clone();
|
||||
method_info_list_sorted.sort_by(|a, b| {
|
||||
let lhs = (a.name_hash) as u32;
|
||||
let rhs = (b.name_hash) as u32;
|
||||
return lhs.cmp(&rhs);
|
||||
});
|
||||
|
||||
for method_info in method_info_list_sorted.iter() {
|
||||
new_class_fn.push_str(&method_info.get_define());
|
||||
}
|
||||
|
||||
new_class_fn.push_str(&"};\n");
|
||||
let class_inhert = format!("class_inhert({}, {});\n\n", self.this_class_name, self.super_class_name);
|
||||
let class_inhert = format!(
|
||||
"class_inhert({}, {});\n\n",
|
||||
self.this_class_name, self.super_class_name
|
||||
);
|
||||
|
||||
new_class_fn.push_str(&class_inhert);
|
||||
|
||||
|
@ -11,10 +11,14 @@ pub struct MethodInfo {
|
||||
pub return_type: Option<PyType>,
|
||||
pub is_constructor: bool,
|
||||
pub decorator_list: Vec<Decorator>,
|
||||
pub name_hash: u32,
|
||||
pub sorted: bool,
|
||||
}
|
||||
|
||||
pub fn hash_time33(key: &String) -> u32{
|
||||
let res = key.chars().fold(5381 as u32, |hash, c| hash.wrapping_mul(33).wrapping_add(c as u32));
|
||||
pub fn hash_time33(key: &String) -> u32 {
|
||||
let res = key.chars().fold(5381 as u32, |hash, c| {
|
||||
hash.wrapping_mul(33).wrapping_add(c as u32)
|
||||
});
|
||||
return res & 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
@ -39,12 +43,14 @@ impl MethodInfo {
|
||||
None => None,
|
||||
};
|
||||
let method_info = MethodInfo {
|
||||
name: name,
|
||||
name: name.clone(),
|
||||
arg_list: ArgList::new(&arg_list),
|
||||
return_type: return_type,
|
||||
class_name: class_name.clone(),
|
||||
is_constructor: is_constructor,
|
||||
decorator_list: Vec::new(),
|
||||
name_hash: hash_time33(&name.clone()),
|
||||
sorted: false,
|
||||
};
|
||||
return Some(method_info);
|
||||
}
|
||||
@ -72,10 +78,6 @@ impl MethodInfo {
|
||||
}
|
||||
|
||||
pub fn get_define(&self) -> String {
|
||||
// let return_token = match &self.return_type {
|
||||
// Some(s) => format!("->{}", s.to_string()),
|
||||
// None => String::from(""),
|
||||
// };
|
||||
let mut class_define_method = String::from("method_def");
|
||||
if self.is_constructor {
|
||||
class_define_method = String::from("constructor_def");
|
||||
@ -90,10 +92,7 @@ impl MethodInfo {
|
||||
define.push_str(
|
||||
format!(
|
||||
" {}({}_{}, {}),\n",
|
||||
class_define_method,
|
||||
self.class_name,
|
||||
self.name,
|
||||
hash_time33(&self.name)
|
||||
class_define_method, self.class_name, self.name, self.name_hash
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user