看看样条插值区间查找函数写的多细腻

优秀的程序员不仅要有深厚理论基础,更要有缜密的思维, 一个简单的函数, 有很多人都写不好,为什么,

不是做不到,不是想不到,  往往是由于懒而不愿意深入思考.  有句话叫, 天下大事, 必做于细.

int Spline::findTimeInterval(Number time, int startIndex)

{

int length = this->_times.size();

if (time < this->_times[0] || time > this->_times[length - 1])

{

throw  DeveloperError("time is out of range.");

}

if (startIndex < 0 || startIndex > length-1)

{

throw DeveloperError("length is out of range.");

}

// Take advantage of temporal coherence by checking current, next and previous intervals

// for containment of time.

if (time >= this->_times[startIndex])

{

if (startIndex + 1 < length && time < this->_times[startIndex + 1])

{

return startIndex;

}

else if (startIndex + 2 < length && time < this->_times[startIndex + 2])

{

return startIndex + 1;

}

}

else if (startIndex - 1 >= 0 && time >= this->_times[startIndex - 1])

{

return startIndex - 1;

}

// The above failed so do a linear search. For the use cases so far, the

// length of the list is less than 10. In the future, if there is a bottle neck,

// it might be here.

int i;

if (time > this->_times[startIndex])

{

for (i = startIndex; i < length - 1; ++i) {

if (time >= this->_times[i] && time < this->_times[i + 1]) {

break;

}

}

} else {

for (i = startIndex - 1; i >= 0; --i) {

if (time >= this->_times[i] && time < this->_times[i + 1]) {

break;

}

}

}

if (i == length - 1) {

i = length - 2;

}

return i;

}

时间: 2024-08-25 11:55:50

看看样条插值区间查找函数写的多细腻的相关文章

区间成员函数优先于与之对应的单元素成员函数

 例子:使v1的内容和v2的后半部分相同的最简单操作是什么?看下面四个答案: ①v1.assign(v2.begin()+v2.size()/2,v2.end()); ②v1.clear(); copy(v2.begin()+v2.size()/2,v2.end(),back_inserter(v1)); ③v1.insert(v1.end(),v2.begin()+v2.size()/2,v2.end()); ④vector<int>v1,v2; - v1.clear(); for(ve

命题作文:Dimension Tree区间查找与IP数据包分类

这个题目有点大,而且我要严格控制字数,不能像<命题作文:在一棵IPv4地址树中彻底理解IP路由表的各种查找过程>那样扯得那么开了.事实上,这篇作文是上 一篇作文中关于区间查找小节的扩展. 1.IP数据包分类 根据IP数据包协议头的若干字段,也叫匹配域,将数据包划分到某个类别,这就是IP数据包分类的核心. 事实上,IP路由查找的过程就是IP数据包分类的一个特例,一个极其简单的特例,此时的匹配域就是目标IP地址,而类别就是路由项或者说更简单一点,下一 跳.此时考虑一下源地址Policy routi

STL区间成员函数及区间算法总结

STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间成员函数不适用的情况下也应该使用区间算法,至少,相比手写循环而言,它更加简单,有效,并且不容易出错: 区间成员函数 区间构造 标准容器都支持区间构造函数: container::container(InputIterator begin, // 区间的起点 InputIterator end); /

leetcode | Implement strStr() | 实现字符串查找函数

Implement strStr() : https://leetcode.com/problems/implement-strstr/ Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 如:haystack = "bcbcda"; needle = "bcd" 则 return 2 解析:字符串查找函数,

c语言:用fgetc函数从键盘逐个输入字符,用fputc函数写到磁盘文件

用fgetc函数从键盘逐个输入字符,用fputc函数写到磁盘文件. 解:程序: #include<stdio.h> #include<stdlib.h>//exit使程序终止 int main() { FILE* fp; char ch, filename[10]; printf("请输入所用的文件名:"); scanf("%s", &filename); if ((fp = fopen(filename, "w"

Balanced Lineup(线段树之区间查找最大最小值)

传送门 线段树的区间查找最大最小值模板. 裸的线段树 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls

c#中的字符串查找函数

indexOf()方法,查找某字符串在一个字符串内的位置,没有则返回-1string aa="abcdef";int a=aa.indexOf("bc");//a会等于1int b=aa.indexOf("a");//b会等于0int c=aa.indexOf("g");c会等于-1所以你只要判断返回出来的int值是不是小于0就知道这个字符串里有没有包含指定的另一个字符串 c#中的字符串查找函数,布布扣,bubuko.com

折半查找函数

折半查找 二分查找又称折半查找 优点:比较次数少,查找速度快,平均性能好: 缺点:要求待查表为有序表,且插入删除困难. 因此,折半查找方法适用于不经常变动而查找频繁的有序列表.首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功:否则利用中间位置记录将表分成前.后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表.重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功. #i

[C++]string类的查找函数

string类的查找函数: int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置 int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置 int find(const string &s, int pos =