如果在 C++ 里面评选称得上是“魔法”的技术,Expression Template (ET,酷吧?) 当之无愧要成为其中一员。
如果要求两个数组的内积,该怎么做?
int a[4] = {1,100,0,-1}; int b[4] = {2,2,2,2}; 手写循环就不说了,STL 算法是这样:
inner_product(a, a + 4, b, 0);不错,但是有个问题。STL 算法本质上还是个循环,在大规模的运算中,循环带来的效率损失有点过高了,我们希望能有一个表达式,它的效率能跟下面这个一样高,但是又具有通用性,让我们只需要像 STL 算法那样写出来就可以运算。
a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]论及效率,上面这个表达式大概是无人能出其右,但是它实在太不通用了,如果能这样该多好:
inner_product<4>(a, b)怎样兼顾上面两种方法的效率和表达能力呢?ET 来帮忙了。
如果希望计算能尽量在编译期间完成,该怎么办?有了 TM (Template Metaprogramming) 的基础,我们知道毫无疑问要玩递归
template <int dim, class T>
struct inner_product
{
T operator()(T* a, T* b)
{
return inner_product<dim - 1, T>()(a + 1, b + 1) +
inner_product<1, T>()(a, b);
}
};
template <class T>
struct inner_product<1, T>
{
T operator()(T* a, T* b)
{ return (*a) * (*b); }
};
现在我们可以这么干了:
inner_product<4, int>()(a, b);
还不错吧?已经差不多了,如果加上一个 helper function, 目标就完全达到了:
template <int dim, class T>
T dot_product(T a[dim], T b[dim])
{
return inner_product<dim, T>()(a, b);
}
//...
dot_product<4>(a, b);
那么它跟普通的 STL 算法有什么区别呢?效率,当然是效率。在编译器看到
inner_product<dim - 1, T>()(a + 1, b + 1) + inner_product<1, T>()(a, b);
的时候,它会发现加号的后面一半可以直接写成 (*a) * (*b) ,而前面一半则需要实例化另外一个模板,也就是说:
inner_product<dim - 1, T>()(a + 1, b + 1) ==>
inner_product<dim - 2, T>()(a + 1 + 1, b + 1 + 1) + (*(a + 1)) * (*(b + 1))
这个过程毫无疑问会递归下去,直到所有的模板都能直接实例化为止,那个时候,表达式就变成了
(*(a + 3)) * (*(b + 3)) + (*(a + 2)) * (*(b + 2)) + (*(a + 1)) * (*(b + 1)) + (*a) * (*b)
而在运行期计算的,实际上就是这个。
分享到:
相关推荐
主要是为了给自己用,《C++ template》中的源代码以及自己写测试代码
一个通用的C ++矩阵库,使用表达式模板和元编程为各种类型的矩阵运算提供专门的代码。
PET是一个C ++库,可实现常规的正则路径表达式。 它提供了一组类和重载运算符,使程序员可以仅使用GCC编译器在用户定义的数据结构上定义规则的路径表达式
>> '/john/profile' Rize\UriTemplate支持RFC6570指定的所有Expression Types和Levels 。 <?phpuse Rize \ UriTemplate ;$ uri = new UriTemplate ();$ uri -> expand ( '/search/{term:1}/{term}/
Template.apex Template.apex是一个库,用于评估以visualforce表达式语法编写的模板。为什么选择Template.apex? 有时我们需要为可自定义的模板(如电子邮件模板)提供支持。 在这种情况下,Template.apex可以帮助您...
es6TemplateString 使用ejs和multiline 实现ES6 Template String的功能 ES6 Template String 支持的功能 //Basic usage with an expression placeholder var person = 'Hao Ju Zheng'; console.log(`Yo! My name is ...
You use this code like you would use other templating engines/expression languages: You add your objects to a Map and then merge the Map with a template. In the template, you reference the objects in ...
JSP 网页主要分为Elements 与Template Data 两部分。 Template Data:JSP Container 不处理的部分,例如:HTML 的内容,会直接送到Client 端执 行。 Elements:必须经由JSP Container 处理的部分,而大部分Elements ...
尽管替换后的文本不会对更改做出React,但常见的用例可能是在使用template绑定或with绑定绑定的容器内部,以便整个区域都可以换出。 值支持上下文变量( $root , $parent等)和表达式。 对于转换为无容器绑定的...
模板引擎允许替换和隐藏一些文本的模板引擎(使用布尔表达式评估器)如何使用它 ? 创建替换行为 {tag=xxxx} 使用的标签使用布尔表达式 {if="condition"}....var evaluator = new ExpressionEvaluator ( dict , false )
The library also provides a compile-time lambda expression facility enabling arbitrary currying and composition of class templates, a feature whose runtime counterpart is often cited as missing from ...
(2)Learn the solution expression of some problems by A Tree data structure. (3)Understand what kinds of problems can be solved by this algorithm. (4)Realize the source code for this algorithm. ...
Thymeleaf applications (TemplateEngine bean, template resolvers, etc.), and add the SpringSecurity dialect to our Template Engine so that we can use the sec:* attributes and special expression utility...
28.12Regular expression Iterators . 1070 28.13Modified ECMAScript regular expression grammar 1076 29 Atomic operations library 1079 29.1 General 1079 29.2 Header <atomic> synopsis 1079 CONTENTS vii c ...
Title: 10 Lessons About C++ You Need To Learn To Become A Master Programmer Author: Andrew Webb Length: 376 pages Edition: 1 Language: English ...Chapter 10: The Standard Template Library
If --offline is given, abort if figuring out a hash requires network access.yarn2nix --template [path/to/package.json] Generate a package template nix-expression for your `package.json`.特征将yarn....
Art Mustache(文艺的大胡子)是一个python模板引擎,它非常简单,它被设计为一个容易学习,... * @:single string rendered in template. 在@{...}中使用@:可以输出单行文本到模板上。 标签:ArtMustache
Use the brand new Component Search Expression framework, which enables you to more easily locate components from your template Extend the Component Search Expression framework with your own search ...
Use the brand new Component Search Expression framework, which enables you to more easily locate components from your template Extend the Component Search Expression framework with your own search ...
You’ll also learn how to take advantage of the powerful libraries available to C++ programmers: both the Standard Template Library (STL) and scientific libraries for arithmetic, linear algebra, ...