From 77b08daca7df1ec04a886983c634a6e2ba545031 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 30 Sep 2022 22:48:04 +0100 Subject: [PATCH] Add DohSortedKeys function Returns a list of sorted keys in a DOH Hash. --- Doc/Manual/Extending.html | 8 ++++++++ Source/DOH/base.c | 12 ++++++++++++ Source/DOH/doh.h | 3 +++ Source/Swig/typesys.c | 32 ++++++-------------------------- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/Doc/Manual/Extending.html b/Doc/Manual/Extending.html index cbb78bef8..27e2fcc65 100644 --- a/Doc/Manual/Extending.html +++ b/Doc/Manual/Extending.html @@ -1244,6 +1244,14 @@ if an object was removed, 0 otherwise. Returns the list of hash table keys. +

+List *SortedKeys(Hash *h, int (*cmp) (const DOH *, const DOH *)) +

+ +
+Returns the list of sorted hash table keys. +
+

40.5.3 Lists

diff --git a/Source/DOH/base.c b/Source/DOH/base.c index 442ea3934..602c79087 100644 --- a/Source/DOH/base.c +++ b/Source/DOH/base.c @@ -374,6 +374,18 @@ DOH *DohKeys(DOH *obj) { return 0; } +/* ----------------------------------------------------------------------------- + * DohSortedKeys() + * ----------------------------------------------------------------------------- */ + +DOH *DohSortedKeys(DOH *obj, int (*cmp) (const DOH *, const DOH *)) { + DOHList *keys = DohKeys(obj); + if (keys) { + DohSortList(keys, cmp); + } + return keys; +} + /* ----------------------------------------------------------------------------- * DohGetInt() * ----------------------------------------------------------------------------- */ diff --git a/Source/DOH/doh.h b/Source/DOH/doh.h index 4ee0dfffa..13e9cc23d 100644 --- a/Source/DOH/doh.h +++ b/Source/DOH/doh.h @@ -52,6 +52,7 @@ #define DohSetattr DOH_NAMESPACE(Setattr) #define DohDelattr DOH_NAMESPACE(Delattr) #define DohKeys DOH_NAMESPACE(Keys) +#define DohSortedKeys DOH_NAMESPACE(SortedKeys) #define DohGetInt DOH_NAMESPACE(GetInt) #define DohGetDouble DOH_NAMESPACE(GetDouble) #define DohGetChar DOH_NAMESPACE(GetChar) @@ -201,6 +202,7 @@ extern int DohSetattr(DOH *obj, const DOHString_or_char *name, const DOHObj_or_c extern int DohDelattr(DOH *obj, const DOHString_or_char *name); extern int DohCheckattr(DOH *obj, const DOHString_or_char *name, const DOHString_or_char *value); extern DOH *DohKeys(DOH *obj); +extern DOH *DohSortedKeys(DOH *obj, int (*cmp) (const DOH *, const DOH *)); extern int DohGetInt(DOH *obj, const DOHString_or_char *name); extern void DohSetInt(DOH *obj, const DOHString_or_char *name, int); extern double DohGetDouble(DOH *obj, const DOHString_or_char *name); @@ -444,6 +446,7 @@ extern void DohMemoryDebug(void); #define FileErrorDisplay DohFileErrorDisplay #define NewVoid DohNewVoid #define Keys DohKeys +#define SortedKeys DohSortedKeys #define Strcmp DohStrcmp #define Strncmp DohStrncmp #define Strstr DohStrstr diff --git a/Source/Swig/typesys.c b/Source/Swig/typesys.c index cadceb3eb..02137d8bd 100644 --- a/Source/Swig/typesys.c +++ b/Source/Swig/typesys.c @@ -1963,26 +1963,6 @@ int SwigType_issubtype(const SwigType *t1, const SwigType *t2) { return r; } -/* ----------------------------------------------------------------------------- - * compare_strings() - * - * Helper function to sort a list of strings - * ----------------------------------------------------------------------------- */ -static int compare_strings(const DOH *a, const DOH *b) { - return strcmp((char *) Data(a), (char *) Data(b)); -} - -/* ----------------------------------------------------------------------------- - * sorted_list_from_hash() - * - * Returns a sorted list of the keys in the given hash - * ----------------------------------------------------------------------------- */ -static List *sorted_list_from_hash(Hash *h) { - List *l = Keys(h); - SortList(l, compare_strings); - return l; -} - /* ----------------------------------------------------------------------------- * SwigType_inherit_equiv() * @@ -2004,7 +1984,7 @@ void SwigType_inherit_equiv(File *out) { if (!subclass) subclass = NewHash(); - r_resolved_sorted_keys = sorted_list_from_hash(r_resolved); + r_resolved_sorted_keys = SortedKeys(r_resolved, Strcmp); rk = First(r_resolved_sorted_keys); while (rk.item) { List *sub_sorted_keys; @@ -2028,7 +2008,7 @@ void SwigType_inherit_equiv(File *out) { /* Printf(stdout,"rk.item = '%s'\n", rk.item); Printf(stdout,"rh = %p '%s'\n", rh,rh); */ - sub_sorted_keys = sorted_list_from_hash(sub); + sub_sorted_keys = SortedKeys(sub, Strcmp); bk = First(sub_sorted_keys); while (bk.item) { prefix = SwigType_prefix(rk.item); @@ -2165,7 +2145,7 @@ void SwigType_emit_type_table(File *f_forward, File *f_table) { Printf(f_forward, "\n/* -------- TYPES TABLE (BEGIN) -------- */\n\n"); - mangled_list = sorted_list_from_hash(r_mangled); + mangled_list = SortedKeys(r_mangled, Strcmp); for (ki = First(mangled_list); ki.item; ki = Next(ki)) { List *el; Iterator ei; @@ -2231,7 +2211,7 @@ void SwigType_emit_type_table(File *f_forward, File *f_table) { } /* now build nt */ - ntlist = sorted_list_from_hash(nthash); + ntlist = SortedKeys(nthash, Strcmp); ltiter = First(ntlist); nt = 0; while (ltiter.item) { @@ -2259,7 +2239,7 @@ void SwigType_emit_type_table(File *f_forward, File *f_table) { Printf(types, "\"%s\", \"%s\", 0, 0, (void*)%s, 0};\n", ki.item, nt, cd); el = SwigType_equivalent_mangle(ki.item, 0, 0); - SortList(el, compare_strings); + SortList(el, Strcmp); for (ei = First(el); ei.item; ei = Next(ei)) { String *ckey; String *conv; @@ -2289,7 +2269,7 @@ void SwigType_emit_type_table(File *f_forward, File *f_table) { Delete(nt); } /* print the tables in the proper order */ - SortList(table_list, compare_strings); + SortList(table_list, Strcmp); i = 0; for (ki = First(table_list); ki.item; ki = Next(ki)) { Printf(f_forward, "#define SWIGTYPE%s swig_types[%d]\n", ki.item, i++);