alias ir = inclusiveRange;
auto inclusiveRange(T = int)(T f = T(0), T l = T(0), T s = T(1)) {
if(!l) {
l = f;
f = 0;
}
return InclusiveRange!T(f, l, s);
}
struct InclusiveRange(T) {
T front, last, step, term;
this(T front, T last, T step) {
this.front = front;
this.last = last;
this.step = step;
this.term = last;
if(this.diff % step)
{
const t = cast(int)(this.diff / step);
this.term = cast(T)(t * step + this.front);
}
}
auto save() { return this; }
auto opSlice() { return this; }
auto opDollar() { return this.length; }
auto opSlice(T a, T b) in (b<=this.length)
{
return InclusiveRange!T(
cast(T)(front + a * step),
cast(T)(front + (b - 1) * step), step);
}
bool opBinaryRight(string op:"in")(T lhs)
{
foreach(r; this)
{
if(isClose(lhs, r, 1e-6))
{
return true;
}
}
return false;
}
T sum()
{
const residueCheck = front ? 2 * front + diff
: diff;
return cast(T)(length * residueCheck / 2);
}
auto diff() { return term - front; }
auto length() { return cast(int)(diff / step + 1); }
bool empty() { return front > last; }
T back() { return last; }
void popBack() { if(!empty) last -= step; }
void popFront() { if(!empty) front += step; }
}
讲解在此
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现