diff --git a/cpl/cplusplus.md b/cpl/cplusplus.md index d4b41bf..765693d 100644 --- a/cpl/cplusplus.md +++ b/cpl/cplusplus.md @@ -6,4 +6,103 @@ ## TMP(template metaprogramming) -C++ allows template metaprogramming (TMP), a technique in which so-called templates are used by a compiler to generate temporary source code that is merged with the rest of the source code and then compiled \ No newline at end of file +C++ allows template metaprogramming (TMP), a technique in which so-called templates are used by a compiler to generate temporary source code that is merged with the rest of the source code and then compiled + +最初模板编程是指控制代码生成来实现泛型,现在的模板更多指泛型约束、编译期计算、对类型做计算。 + +**代码生成** + +C语言使用宏来实现泛型,原理就是替换宏参数,属于编译期的字符串拼接 +C语言没有模板是因为其语言抽象能力不够,相比C++的抽象能力模板template来实现泛型,宏的缺点都解决掉了。 + +**对类型约束** + +函数重载是代码生成的一种方案,template就是先处理函数重载和隐式类型转换上花掉很多时间,报错也是一大推的根本原因。 + +标准库已经实现了很多的重载函数,这也是很多公司不使用标准库的原因吧 + +模板相对于宏的两个明显优势是 +- 自动类型推导 +- 隐式实例化 + +模板先推导参数,成功后就实例化,参数推导的报错提示很明显,实例化的报错提示就很复杂了,考虑标准库内部的实现与命名规则了。 + +类型约束就是在自动类型推导时减少匹配的工作,但约束语法时C++20才加入的.在此之前只能通过一种叫做SFINAE(Substitution failure is not an error)的技术来实现 +```c++ +struct A{}; +template +void print1(T x) { + std::cout << x << std::endl; +} +// before c++ 20 +template())> +void print2(T x) { + std::cout << x << std::endl; +} +// c++20 +template +requires (T x) { std::cout << x;} +void print2(T x) { + std::cout << x << std::endl; +} +``` +其他语言如C#和rust,对泛型的约束是通过where来表达的。 + +**编译期计算** + +- 编译器对常量表达式的优化,如C函数的strlen,在C++中strlen是constexpr的 +- 为减少运行时开销,会提前算好一些数据,典型的例子是计算三角函数表这种常量表 + +C++的编译期计算有明确的语义保证,并且内嵌于语言中,能和其他部分交互,通过模板元编程进行编译期计算。从历史上看,TMP是一个偶然事件,在标准化C++语言的过程中发现它的模板系统恰好是图灵完备的,即原则上能够计算任何可计算的东西。 + +```c++ +template +struct Factorial { + enum { value = N * Factorial::value }; +}; +template<> +struct Factorial<0> { + enum { value = 1 }; +}; +std::cout << Factorial<5>::value << std::endl; +``` +这是C++11之前的,之后引入了constexpr关键字,可以表示编译期常量这一概念了 +```c++ +template +struct Factorial { + constexpr static int value = N * Factorial::value; +}; +template<> +struct Factorial<0> { + constexpr static int value = 1; +}; +``` +编译期的计算仍然使用模板来,这样的代码可读性较低,主要原因有 +- 模板参数只能是编译期常量,没有编译期变量的概念 +- 只能递归而不能循环计算 + +不能满足上面的编程语言也有,如Haskell没有变量和循环,但是它有强大的模式匹配.这就是constexpr关键字的引入问题 +```c++ +constexpr std::size_t factorial(std::size_t N) { + std::size_t result = 1; + for (std::size_t i=1;i<=N;++i) { + result *= i; + } + return result; +} +``` +这也是C++20之后几乎所有的标准库函数都是constexpr写的,比如编译期排序 +```c++ +constexpr auto sort(auto &&range) { + std::sort(std::begin(range), std::end(range)); + return range; +} +int main() { + constexpr auto arr = sort(std::array{1,3,4,2,5}); + for (auto i : arr) std::cout << i << std::endl; +} +``` +允许一个运行期函数前面直接加上constexpr关键字修饰,表示函数既可以在运行期调用,也可以在编译期调用.如果只想在编译期执行函数,使用关键字consteval来修饰. + + + diff --git a/index/article.md b/index/article.md index 48c5fa4..e18aba6 100644 --- a/index/article.md +++ b/index/article.md @@ -3,6 +3,7 @@ - [留心的资源](../articles/resource.md) - [个人认识](../articles/personal.ideas.md) - [3D引擎](../cg/engines.md) +- [汉语拼音网](http://www.hanyupinyin.org/) ## dev-note - [git](../dev-note/git.md) @@ -19,6 +20,7 @@ - [网页开发内容](../web/index.md) - [webassembly](../web/webAssembly.md) - [http](../web/http.md) +- [css](../web/css.md) #### [nodejs](../nodejs/index.md) - [测试模块](../nodejs/test.md) diff --git a/index/computerScience.md b/index/computerScience.md index 8397204..5540c92 100644 --- a/index/computerScience.md +++ b/index/computerScience.md @@ -46,7 +46,7 @@ - [bezierjs ](https://pomax.github.io/bezierjs/) - [github](https://github.com/Pomax/bezierjs) -### 网格 +### 网格与CAD - [GMesh]() - [Triangulate Efficient Triangulation Algorithm Suitable for Terrain Modelling or An Algorithm for Interpolating Irregularly-Spaced Data with Applications in Terrain Modelling](http://paulbourke.net/papers/triangulate/) - [多边形网格算法](http://paulbourke.net/geometry/polygonmesh/) @@ -55,6 +55,8 @@ - [计算几何第四周:维诺图](https://zhuanlan.zhihu.com/p/33896575) - [Lattice学习笔记01:格的简介](https://zhuanlan.zhihu.com/p/161411204) - [How to make an infinite grid无限网格](http://asliceofrendering.com/scene%20helper/2020/01/05/InfiniteGrid/) +- [LNLib is a C++ NURBS Algorithms Library. These algorithms are primary referenced from The NURBS Book 2nd Edition. ](https://github.com/BIMCoderLiang/LNLib) + ### api - [OpenGL](../cg/opengl.md) diff --git a/index/online.md b/index/online.md index 5b4afb2..28d5f61 100644 --- a/index/online.md +++ b/index/online.md @@ -36,4 +36,10 @@ ### 文档 - [HandWiki is a wiki encyclopedia for collaborative editing of articles on computing, science, technology and general knowledge](https://handwiki.org/wiki/Start) ### 在线工具 -- [在线计算器及工具](https://www.rapidtables.org/zh-CN/) \ No newline at end of file +- [在线计算器及工具](https://www.rapidtables.org/zh-CN/) + +### 其他 + +- [MedPeer应用先进的新闻资讯抓取和分析技术,对用户关心的内容进行深度分类和整理,力求提供及时的生物医药行业资讯,依托MedPeer人工智能翻译系统对国外资讯、文献、报告和视频进行翻译,帮助国内用户轻松理解。编辑器免费,组件收费](https://medpeer.cn/) +- [Design Editor JS SDK Polotno for canvas | Polotno](https://polotno.com/) + - [github](https://github.com/polotno-project) \ No newline at end of file diff --git a/math/pbr3ed.html b/math/pbr3ed.html index 4c75ab6..2a01923 100644 --- a/math/pbr3ed.html +++ b/math/pbr3ed.html @@ -65,6 +65,32 @@
7.3 Stratified Sampling

+
+
7.4 The Halton Sampler
+

The first Sampler implementation that we will introduce subdivides pixel areas into rectangular regions and generates a single sample inside each region. These regions are commonly called strata, and this sampler is called the StratifiedSampler.

+
+ 分层抽样(Stratified Sampling) +
+

+ The bits of an integer quantity can be efficiently reversed with a series of logical bit operations. The first line of the ReverseBits32() function, which reverses the bits of a 32-bit integer, swaps the lower 16 bits with the upper 16 bits of the value. The next line simultaneously swaps the first 8 bits of the result with the second 8 bits and the third 8 bits with the fourth. This process continues until the last line, which swaps adjacent bits. To understand this code, it’s helpful to write out the binary values of the various hexadecimal constants. For example, 0xff00ff00 is 11111111000000001111111100000000 in binary; it’s easy to see that a bitwise OR with this value masks off the first and third 8-bit quantities. +

+                        inline uint32_t ReverseBits32(uint32_t n) {
+                            n = (n << 16) | (n >> 16);
+                            n = ((n & 0x00ff00ff) << 8) | ((n & 0xff00ff00) >> 8);
+                            n = ((n & 0x0f0f0f0f) << 4) | ((n & 0xf0f0f0f0) >> 4);
+                            n = ((n & 0x33333333) << 2) | ((n & 0xcccccccc) >> 2);
+                            n = ((n & 0x55555555) << 1) | ((n & 0xaaaaaaaa) >> 1);
+                            return n;
+                        }
+                        inline uint64_t ReverseBits64(uint64_t n) {
+                            uint64_t n0 = ReverseBits32((uint32_t)n);
+                            uint64_t n1 = ReverseBits32((uint32_t)(n >> 32));
+                            return (n0 << 32) | n1;
+                        }
+                    
+

+

+
diff --git a/web/css.md b/web/css.md index f66f0ff..a82af96 100644 --- a/web/css.md +++ b/web/css.md @@ -18,6 +18,11 @@ bootstrap-vue上看到的,让网页宽度自动适应手机屏幕 ### 定位布局 +**absolute** +如果没有指定top,right,bottom,left等参数,它的位置关系是relative to its closest positioned ancestor(if any) or to the initial containing block. + +这样可以实现dropdown的样式, + ### flex #### 轴 diff --git a/web/http.md b/web/http.md index 4654fdf..83383da 100644 --- a/web/http.md +++ b/web/http.md @@ -5,3 +5,12 @@ [HTTP-network-or-cache fetch](https://fetch.spec.whatwg.org/#http-network-or-cache-fetch) cache mode的状态决定了浏览器的操作行为,这是httpRequest的参数决定了是否进行操作的逻辑 + +## header +Content-Disposition是MIME协议扩展,指示如何显示附加的文件 + +Content-Disposition:'attachment=filename;'。 + +Response.AppendHeader("Content-Disposition","attachment;filename=FileName.txt"); +attachment会以附件方式下载 +阿里云的OSS有一个政策就是对这个进行了控制,导致显示不能直接在浏览器中打开