Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.

With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
   MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
   the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
  MAX and MIN combination. In theory, this makes the code slightly
  smaller, in case that (low) or (high) or both are computed at
  runtime, so that compilers cannot optimize them. (The third
  comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
  results. Unlike MIN & MAX which will force either (low) or (high) to
  win. No assertion of ((low)<=(high)) is done in this macro, for now.

This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
This commit is contained in:
Explorer09
2016-01-15 20:26:01 +08:00
parent 195f5edbc8
commit 6dae8108f8
14 changed files with 60 additions and 22 deletions

19
Meter.c
View File

@ -114,6 +114,9 @@ typedef struct GraphData_ {
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#ifndef CLAMP
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
#endif
MeterClass Meter_class = {
.super = {
@ -297,8 +300,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
int items = Meter_getItems(this);
for (int i = 0; i < items; i++) {
double value = this->values[i];
value = MAX(value, 0);
value = MIN(value, this->total);
value = CLAMP(value, 0.0, this->total);
if (value > 0) {
blockSizes[i] = ceil((value/this->total) * w);
} else {
@ -306,7 +308,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
}
int nextOffset = offset + blockSizes[i];
// (Control against invalid values)
nextOffset = MIN(MAX(nextOffset, 0), w);
nextOffset = CLAMP(nextOffset, 0, w);
for (int j = offset; j < nextOffset; j++)
if (bar[j] == ' ') {
if (CRT_colorScheme == COLORSCHEME_MONOCHROME) {
@ -324,8 +326,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
attrset(CRT_colors[Meter_attributes(this)[i]]);
mvaddnstr(y, x + offset, bar + offset, blockSizes[i]);
offset += blockSizes[i];
offset = MAX(offset, 0);
offset = MIN(offset, w);
offset = CLAMP(offset, 0, w);
}
if (offset < w) {
attrset(CRT_colors[BAR_SHADOW]);
@ -406,13 +407,13 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) {
const double dot = (1.0 / (GraphMeterMode_pixPerRow * 4));
int v1 = MIN(GraphMeterMode_pixPerRow * 4, MAX(1, data->values[i] / dot));
int v2 = MIN(GraphMeterMode_pixPerRow * 4, MAX(1, data->values[i+1] / dot));
int v1 = CLAMP(data->values[i] / dot, 1, GraphMeterMode_pixPerRow * 4);
int v2 = CLAMP(data->values[i+1] / dot, 1, GraphMeterMode_pixPerRow * 4);
int colorIdx = GRAPH_1;
for (int line = 0; line < 4; line++) {
int line1 = MIN(GraphMeterMode_pixPerRow, MAX(0, v1 - (GraphMeterMode_pixPerRow * (3 - line))));
int line2 = MIN(GraphMeterMode_pixPerRow, MAX(0, v2 - (GraphMeterMode_pixPerRow * (3 - line))));
int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (3 - line)), 0, GraphMeterMode_pixPerRow);
int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (3 - line)), 0, GraphMeterMode_pixPerRow);
attrset(CRT_colors[colorIdx]);
mvaddstr(y+line, x+k, GraphMeterMode_dots[line1 * (GraphMeterMode_pixPerRow + 1) + line2]);