/* ** If the hash node is present, return its pointer, otherwise create a new ** node for the given reference and also return its pointer. ** On error, return NULL. */ public static Object_ lua_hashdefine(Hash t, Object_ @ref) { int h; NodeRef n; h = head(t, @ref); if (h < 0) { return(null); } n = present(t, @ref, h); if (n == null) { n = NodeRef.assign(new_Node()); //(Node) new_("Node"); if (n == null) { lua_error("not enough memory"); return(null); } n.get()[email protected](@ref); tag(n.get().val, Type.T_NIL); n.get().next = NodeRef.assign(list(t, h)); /* link node to head of list */ list(t, h, n.get()); } return(n.get().val); }
private static void freelist(NodeRef n) { while (n != null) { NodeRef next = NodeRef.assign(n.get().next); free(n.get()); n = next; } }
/* ** Mark a hash and check its elements */ public static void lua_hashmark(Hash h) { int i; markarray(h, (char)1); for (i = 0; i < nhash(h); i++) { NodeRef n; for (n = list(h, i); n != null; n = NodeRef.assign(n.get().next)) { lua_markobject(n.get().@ref); lua_markobject(n.get().val); } } }
private static NodeRef present(Hash t, Object_ @ref, int h) { NodeRef n = null, p = null; if (tag(@ref) == Type.T_NUMBER) { for (p = null, n = list(t, h); n != null; p = n, n = NodeRef.assign(n.get().next)) { if (ref_tag(n.get()) == Type.T_NUMBER && nvalue(@ref) == ref_nvalue(n.get())) { break; } } } else if (tag(@ref) == Type.T_STRING) { for (p = null, n = list(t, h); n != null; p = n, n = NodeRef.assign(n.get().next)) { // if (ref_tag(n.get()) == Type.T_STRING) // { // Console.WriteLine("=========================" + svalue(@ref).ToString() + ", " + ref_svalue(n.get())); // } if (ref_tag(n.get()) == Type.T_STRING && streq(svalue(@ref), ref_svalue(n.get()))) { // Console.WriteLine("========================="); break; } } } if (n == null) /* name not present */ { return(null); } #if false if (p != null) /* name present but not first */ { p.next = n.next; /* move-to-front self-organization */ n.next = list(t, h); list(t, h, n); } #endif return(n); }
//#define list(t,i) ((t)->list[i]) private static NodeRef list(Hash t, int i) { return(NodeRef.assign(t.list[i])); }
public static void lua_next() { Hash a; Object_ o = lua_getparam(1); Object_ r = lua_getparam(2); if (o == null || r == null) { lua_error("too few arguments to function `next'"); return; } if (lua_getparam(3) != null) { lua_error("too many arguments to function `next'"); return; } if (tag(o) != Type.T_ARRAY) { lua_error("first argument of function `next' is not a table"); return; } a = avalue(o); if (tag(r) == Type.T_NIL) { firstnode(a, 0); return; } else { int h = head(a, r); if (h >= 0) { NodeRef n = list(a, h); while (n != null) { if (n.get()[email protected](r)) { if (n.get().next == null) { firstnode(a, h + 1); return; } else if (tag(n.get().next.get().val) != Type.T_NIL) { lua_pushobject(n.get().next.get().@ref); lua_pushobject(n.get().next.get().val); return; } else { NodeRef next = NodeRef.assign(n.get().next.get().next); while (next != null && tag(next.get().val) == Type.T_NIL) { next = NodeRef.assign(next.get().next); } if (next == null) { firstnode(a, h + 1); return; } else { lua_pushobject(next.get().@ref); lua_pushobject(next.get().val); } return; } } n = NodeRef.assign(n.get().next); } if (n == null) { lua_error("error in function 'next': reference not found"); } } } }