Skip to content

Commit

Permalink
libdeng2: Added de::wrap template for wrapping inside a range
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 5, 2013
1 parent 065b56b commit 3104ccc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 9 additions & 0 deletions doomsday/libdeng2/include/de/math.h
Expand Up @@ -69,6 +69,15 @@ inline Type clamp(Type const &low, Type const &value, Type const &high) {
return min(max(value, low), high);
}

/// Wrap value within range [low, high).
template <typename Type>
Type wrap(Type value, Type const &low, Type const &high) {
Type const range = high - low;
while(value < low) value += range;
while(value >= high) value -= range;
return value;
}

inline dint32 floor(dfloat const &value) {
return dint32(std::floor(value));
}
Expand Down
5 changes: 2 additions & 3 deletions doomsday/libshell/src/lineeditwidget.cpp
Expand Up @@ -256,9 +256,8 @@ struct LineEditWidget::Instance
String const base = wordBehindCursor();

// Go to next suggestion.
completion.ordinal += (forwardCycle? 1 : -1);
if(completion.ordinal < 0) completion.ordinal += suggestions.size();
if(completion.ordinal >= suggestions.size()) completion.ordinal -= suggestions.size();
completion.ordinal = de::wrap(completion.ordinal + (forwardCycle? 1 : -1),
0, suggestions.size());
String comp = suggestions[completion.ordinal];
comp.remove(0, base.size());

Expand Down

0 comments on commit 3104ccc

Please sign in to comment.