From e5a3e6a845e105566dd6def263cd2d7326155d42 Mon Sep 17 00:00:00 2001 From: James R Date: Sat, 5 Dec 2020 01:14:52 -0800 Subject: [PATCH] Fix removing a tag unsetting the bit array even if more elements with that tag exist --- src/lua_taglib.c | 2 +- src/taglist.c | 28 +++++++++++++++++++++++----- src/taglist.h | 1 + 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/lua_taglib.c b/src/lua_taglib.c index 73f033312..07646af87 100644 --- a/src/lua_taglib.c +++ b/src/lua_taglib.c @@ -145,7 +145,7 @@ static int lib_numTaggroupElements(lua_State *L) else { const taggroup_t ** garray = lua_touserdata(L, up_garray); - lua_pushnumber(L, garray[tag] ? garray[tag]->count : 0); + lua_pushnumber(L, Taggroup_Count(garray[tag])); } return 1; } diff --git a/src/taglist.c b/src/taglist.c index cfd9cbb9c..a759f4d02 100644 --- a/src/taglist.c +++ b/src/taglist.c @@ -110,6 +110,12 @@ size_t Taggroup_Find (const taggroup_t *group, const size_t id) return -1; } +/// group->count, but also checks for NULL +size_t Taggroup_Count (const taggroup_t *group) +{ + return group ? group->count : 0; +} + /// Iterate thru elements in a global taggroup. INT32 Taggroup_Iterate ( taggroup_t *garray[], @@ -153,9 +159,10 @@ void Taggroup_Add (taggroup_t *garray[], const mtag_t tag, size_t id) return; if (! in_bit_array(tags_available, tag)) + { num_tags++; - - set_bit_array(tags_available, tag); + set_bit_array(tags_available, tag); + } // Create group if empty. if (!group) @@ -182,6 +189,16 @@ void Taggroup_Add (taggroup_t *garray[], const mtag_t tag, size_t id) group->elements[i] = id; } +static size_t total_elements_with_tag (const mtag_t tag) +{ + return + ( + Taggroup_Count(tags_sectors[tag]) + + Taggroup_Count(tags_lines[tag]) + + Taggroup_Count(tags_mapthings[tag]) + ); +} + /// Remove an element from a global taggroup. void Taggroup_Remove (taggroup_t *garray[], const mtag_t tag, size_t id) { @@ -197,10 +214,11 @@ void Taggroup_Remove (taggroup_t *garray[], const mtag_t tag, size_t id) if ((rempos = Taggroup_Find(group, id)) == (size_t)-1) return; - if (in_bit_array(tags_available, tag)) + if (group->count == 1 && total_elements_with_tag(tag) == 1) + { num_tags--; - - unset_bit_array(tags_available, tag); + unset_bit_array(tags_available, tag); + } // Strip away taggroup if no elements left. if (!(oldcount = group->count--)) diff --git a/src/taglist.h b/src/taglist.h index e5db08806..a0529ab6b 100644 --- a/src/taglist.h +++ b/src/taglist.h @@ -54,6 +54,7 @@ extern taggroup_t* tags_mapthings[]; void Taggroup_Add (taggroup_t *garray[], const mtag_t tag, size_t id); void Taggroup_Remove (taggroup_t *garray[], const mtag_t tag, size_t id); size_t Taggroup_Find (const taggroup_t *group, const size_t id); +size_t Taggroup_Count (const taggroup_t *group); INT32 Taggroup_Iterate ( taggroup_t *garray[],