Le 19/09/2024 à 15:40, Jürgen Spitzmüller a écrit :
Am Donnerstag, dem 19.09.2024 um 15:14 +0200 schrieb Jean-Marc
Lasgouttes:
But why go from a vector<> to a map<>?

Frankly: I can't remember.



What I have in mind as a simpler alternative is this (like the last patch, with vector instead of map).

Unfortunately, I have no idea how to test it properly.

JMarc

From 8a73dd337ee3479247cee0e90db2adcbae7033ac Mon Sep 17 00:00:00 2001
From: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date: Fri, 13 Sep 2024 17:10:58 +0200
Subject: [PATCH] Use a vector<bool> instead of map<col-type, bool>

Typically, to check whether an element is present and set to true, instead of
  foo.find(c) != foo.end() && foo.find(c)->second
one uses
  foo[c]

This avoids puzzlement of Coverity scan when seeing that find() result
is dereferenced without checking that it is not equal to end().
Moreover, this makes the code much much clearer. I still do not
understand it, though ;)
---
 src/insets/InsetTabular.cpp | 55 ++++++++++++++-----------------------
 1 file changed, 21 insertions(+), 34 deletions(-)

diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp
index a36808fd56..a5dcecd0c9 100644
--- a/src/insets/InsetTabular.cpp
+++ b/src/insets/InsetTabular.cpp
@@ -2613,7 +2613,7 @@ void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> const &
 	// is done in Tabular::TeXBottomHLine(...)
 
 	// get for each column the topline (if any)
-	map<col_type, bool> topline, topltrims, toprtrims;
+	vector<bool> topline(ncols()), topltrims(ncols()), toprtrims(ncols());
 	col_type nset = 0;
 	bool have_trims = false;
 	for (auto const & c : columns) {
@@ -2634,10 +2634,9 @@ void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> const &
 			topltrims[c] = topltrims[c-1];
 			toprtrims[c] = toprtrims[c-1];
 		}
-		if (topline.find(c) != topline.end() && topline.find(c)->second)
+		if (topline[c])
 			++nset;
-		if ((topltrims.find(c) != topltrims.end() && topltrims.find(c)->second)
-		     || (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second))
+		if (topltrims[c] || toprtrims[c])
 			have_trims = true;
 	}
 
@@ -2669,7 +2668,7 @@ void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> const &
 			if (cl < c)
 				continue;
 			c = cl;
-			if (topline.find(c)->second) {
+			if (topline[c]) {
 				col_type offset = 0;
 				for (col_type j = 0 ; j < c; ++j)
 					if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
@@ -2679,21 +2678,18 @@ void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> const &
 				while (isPartOfMultiColumn(row, c))
 					++c;
 				string trim;
-				if (topltrims.find(c) != topltrims.end()
-				     && topltrims.find(c)->second)
+				if (topltrims[c])
 					trim = "l";
 				col_type cstart = c;
-				for ( ; c < ncols() - 1 && topline.find(c + 1)->second ; ++c) {
+				for ( ; c < ncols() - 1 && topline[c + 1] ; ++c) {
 					if (isMultiColumn(cellIndex(row, c))
 					    && c < ncols() - 1 && isPartOfMultiColumn(row, c + 1))
 						continue;
-					if (c > cstart && topltrims.find(c) != topltrims.end()
-							&& topltrims.find(c)->second) {
+					if (c > cstart && topltrims[c]) {
 						if (!isPartOfMultiColumn(row, c))
 							--c;
 						break;
-					} else if (toprtrims.find(c) != toprtrims.end()
-						   && toprtrims.find(c)->second)
+					} else if (toprtrims[c])
 						break;
 				}
 
@@ -2701,8 +2697,7 @@ void Tabular::TeXTopHLine(otexstream & os, row_type row, list<col_type> const &
 					if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
 						++offset;
 				col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
-				if (toprtrims.find(c) != toprtrims.end()
-				    && toprtrims.find(c)->second)
+				if (toprtrims[c])
 					trim += "r";
 
 				os << cline;
@@ -2761,8 +2756,8 @@ void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> const
 			bottomltrims[c] = bottomltrims[c-1];
 			bottomrtrims[c] = bottomrtrims[c-1];
 		}
-			
-		nextrowset &= topline.find(c) != topline.end() && topline.find(c)->second;
+
+		nextrowset &= topline[c];
 	}
 
 	// combine this row's bottom lines and next row's toplines if necessary
@@ -2770,15 +2765,12 @@ void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> const
 	bool have_trims = false;
 	for (auto const & c : columns) {
 		if (!nextrowset)
-			bottomline[c] = bottomline.find(c)->second || topline.find(c)->second;
-		bottomltrims[c] = (bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
-				|| (topltrims.find(c) != topltrims.end() && topltrims.find(c)->second);
-		bottomrtrims[c] = (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second)
-				|| (toprtrims.find(c) != toprtrims.end() && toprtrims.find(c)->second);
+			bottomline[c] |= topline[c];
+		bottomltrims[c] |= topltrims[c];
+		bottomrtrims[c] |= toprtrims[c];
 		if (bottomline.find(c)->second)
 			++nset;
-		if ((bottomltrims.find(c) != bottomltrims.end() && bottomltrims.find(c)->second)
-		     || (bottomrtrims.find(c) != bottomrtrims.end() && bottomrtrims.find(c)->second))
+		if (bottomltrims[c] || bottomrtrims[c])
 			have_trims = true;
 	}
 
@@ -2803,7 +2795,7 @@ void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> const
 			if (cl < c)
 				continue;
 			c = cl;
-			if (bottomline.find(c)->second) {
+			if (bottomline[c]) {
 				col_type offset = 0;
 				for (col_type j = 0 ; j < c; ++j)
 					if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
@@ -2813,23 +2805,19 @@ void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> const
 				while (isPartOfMultiColumn(row, c))
 					++c;
 				string trim;
-				if (bottomltrims.find(c) != bottomltrims.end()
-				     && bottomltrims.find(c)->second)
+				if (bottomltrims[c])
 					trim = "l";
 				col_type cstart = c;
-				for ( ; c < ncols() - 1 && bottomline.find(c + 1)->second ; ++c) {
+				for ( ; c < ncols() - 1 && bottomline[c + 1] ; ++c) {
 					if (isMultiColumn(cellIndex(row, c))
 					    && c < ncols() - 1
 					    && isPartOfMultiColumn(row, c + 1))
 						continue;
-					if (c > cstart
-					    && bottomltrims.find(c) != bottomltrims.end()
-					    && bottomltrims.find(c)->second) {
+					if (c > cstart && bottomltrims[c]) {
 						if (!isPartOfMultiColumn(row, c))
 							--c;
 						break;
-					} else if (bottomrtrims.find(c) != bottomrtrims.end()
-						   && bottomrtrims.find(c)->second)
+					} else if (bottomrtrims[c])
 						break;
 				}
 
@@ -2837,8 +2825,7 @@ void Tabular::TeXBottomHLine(otexstream & os, row_type row, list<col_type> const
 					if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
 						++offset;
 				col_type lastcol = (*it1 == *it2) ? c + 1 + offset : columns.size() - c + offset;
-				if (bottomrtrims.find(c) != bottomrtrims.end()
-				    && bottomrtrims.find(c)->second)
+				if (bottomrtrims[c])
 					trim += "r";
 
 				os << cline;
-- 
2.43.0

-- 
lyx-devel mailing list
lyx-devel@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-devel

Reply via email to