tag:blogger.com,1999:blog-31838000365379049532024-03-14T01:35:18.606-04:00TEK et al.A notebook that I use for "scratching out" bits of code or ideas for the occasional moments of inspiration...riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.comBlogger41125tag:blogger.com,1999:blog-3183800036537904953.post-14945774251374070612015-09-04T19:39:00.002-04:002015-09-04T19:39:57.940-04:00Replace Conditional with PolymorphismTrying to convince my team to give up on if/then/else & case...<br />
<br />
Let's play with some code!<br />
<br />
First the the non Object Oriented / procedural version:<br />
<br />
<pre style="background: rgb(255, 255, 255);"><span style="color: dimgrey;">/*</span>
<span style="color: dimgrey;"> * non_oop.cpp</span>
<span style="color: dimgrey;"> *</span>
<span style="color: dimgrey;"> * Created on: Sep 4, 2015</span>
<span style="color: dimgrey;"> * Author: </span><span style="color: #7144c4;">ferrazlealrm@ornl.gov</span><span style="color: dimgrey;"></span>
<span style="color: dimgrey;"> *</span>
<span style="color: dimgrey;"> * Non OOP example of URL:</span>
<span style="color: dimgrey;"> * scheme:[//domain[:port]][/]path</span>
<span style="color: dimgrey;"> */</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">iostream</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">string</span><span style="color: maroon;">></span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> domain <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">ornl.gov</span><span style="color: maroon;">"</span><span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> http_port <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">80</span><span style="color: maroon;">"</span><span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> https_port <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">443</span><span style="color: maroon;">"</span><span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> ftp_port <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">20</span><span style="color: maroon;">"</span><span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">const</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> <span style="color: #808030;">&</span>scheme<span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">const</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> <span style="color: #808030;">&</span>domain<span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">const</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> <span style="color: #808030;">&</span>port<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> scheme <span style="color: #808030;">+</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">://</span><span style="color: maroon;">"</span> <span style="color: #808030;">+</span> domain <span style="color: #808030;">+</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">:</span><span style="color: maroon;">"</span> <span style="color: #808030;">+</span> port<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">int</span> <span style="color: #400000;">main</span><span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">int</span> argc<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">char</span><span style="color: #808030;">*</span> argv<span style="color: #808030;">[</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>argc <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #008c00;">2</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cerr</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Use </span><span style="color: maroon;">"</span> <span style="color: #808030;">+</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span><span style="color: #808030;">(</span>argv<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span> <span style="color: #808030;">+</span> <span style="color: maroon;">"</span><span style="color: #0000e6;"> [http, https, ftp]</span><span style="color: maroon;">"</span><span style="color: #808030;"><</span><span style="color: #808030;"><</span><span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: #603000;">exit</span><span style="color: #808030;">(</span><span style="color: #808030;">-</span><span style="color: #008c00;">1</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> scheme <span style="color: #808030;">=</span> argv<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>scheme <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">http</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> build_url<span style="color: #808030;">(</span>scheme<span style="color: #808030;">,</span>domain<span style="color: #808030;">,</span>http_port<span style="color: #808030;">)</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span><span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">else</span> <span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>scheme <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">https</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> build_url<span style="color: #808030;">(</span>scheme<span style="color: #808030;">,</span>domain<span style="color: #808030;">,</span>https_port<span style="color: #808030;">)</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span><span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">else</span> <span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>scheme <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">ftp</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> build_url<span style="color: #808030;">(</span>scheme<span style="color: #808030;">,</span>domain<span style="color: #808030;">,</span>ftp_port<span style="color: #808030;">)</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span><span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">else</span> <span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cerr</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Scheme not valid. Use of these: http, https, ftp</span><span style="color: maroon;">"</span><span style="color: #808030;"><</span><span style="color: #808030;"><</span><span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre>
<br />
And the finally the Object Oriented version:<br />
<br />
<pre style="background: rgb(255, 255, 255);"><span style="color: dimgrey;">/*</span>
<span style="color: dimgrey;"> * oop.cpp</span>
<span style="color: dimgrey;"> *</span>
<span style="color: dimgrey;"> * Created on: Sep 4, 2015</span>
<span style="color: dimgrey;"> * Author: </span><span style="color: #7144c4;">ferrazlealrm@ornl.gov</span><span style="color: dimgrey;"></span>
<span style="color: dimgrey;"> *</span>
<span style="color: dimgrey;"> * OOP example of URL:</span>
<span style="color: dimgrey;"> * scheme:[//domain[:port]][/]path</span>
<span style="color: dimgrey;"> */</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">iostream</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">string</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">map</span><span style="color: maroon;">></span>
<span style="color: dimgrey;">// Abstract class: cannot be instantiated</span>
<span style="color: maroon; font-weight: bold;">class</span> URL <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">protected</span><span style="color: #e34adc;">:</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> domain <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">ornl.gov</span><span style="color: maroon;">"</span><span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> port<span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> scheme<span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">const</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> <span style="color: #808030;">&</span>scheme<span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">const</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> <span style="color: #808030;">&</span>port<span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">const</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> scheme <span style="color: #808030;">+</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">://</span><span style="color: maroon;">"</span> <span style="color: #808030;">+</span> domain <span style="color: #808030;">+</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">:</span><span style="color: maroon;">"</span> <span style="color: #808030;">+</span> port<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
<span style="color: dimgrey;">// Pure virtual function: i.e. must be overridden by a derived class</span>
<span style="color: maroon; font-weight: bold;">virtual</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">const</span> <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: #808030;">~</span>URL<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span><span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">class</span> Http<span style="color: purple;">:</span> <span style="color: maroon; font-weight: bold;">public</span> URL <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
Http<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>port <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">80</span><span style="color: maroon;">"</span><span style="color: purple;">;</span> scheme <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">http</span><span style="color: maroon;">"</span><span style="color: purple;">;</span><span style="color: purple;">}</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">const</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> URL<span style="color: purple;">::</span>build_url<span style="color: #808030;">(</span>scheme<span style="color: #808030;">,</span> port<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">class</span> Https<span style="color: purple;">:</span> <span style="color: maroon; font-weight: bold;">public</span> URL <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
Https<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>port <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">443</span><span style="color: maroon;">"</span><span style="color: purple;">;</span> scheme <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">https</span><span style="color: maroon;">"</span><span style="color: purple;">;</span><span style="color: purple;">}</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">const</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> URL<span style="color: purple;">::</span>build_url<span style="color: #808030;">(</span>scheme<span style="color: #808030;">,</span> port<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">class</span> Ftp<span style="color: purple;">:</span> <span style="color: maroon; font-weight: bold;">public</span> URL <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
Ftp<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>port <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">20</span><span style="color: maroon;">"</span><span style="color: purple;">;</span> scheme <span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">ftp</span><span style="color: maroon;">"</span><span style="color: purple;">;</span><span style="color: purple;">}</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">const</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> URL<span style="color: purple;">::</span>build_url<span style="color: #808030;">(</span>scheme<span style="color: #808030;">,</span> port<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * Main function</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">const</span> URL <span style="color: #808030;">&</span>url<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> url<span style="color: #808030;">.</span>build_url<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">int</span> <span style="color: #400000;">main</span><span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">int</span> argc<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">char</span><span style="color: #808030;">*</span> argv<span style="color: #808030;">[</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>argc <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #008c00;">2</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cerr</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Use </span><span style="color: maroon;">"</span> <span style="color: #808030;">+</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span><span style="color: #808030;">(</span>argv<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span> <span style="color: #808030;">+</span> <span style="color: maroon;">"</span><span style="color: #0000e6;"> [http, https, ftp]</span><span style="color: maroon;">"</span>
<span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: #603000;">exit</span><span style="color: #808030;">(</span><span style="color: #808030;">-</span><span style="color: #008c00;">1</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> scheme <span style="color: #808030;">=</span> argv<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span><span style="color: purple;">;</span>
<span style="color: dimgrey;">// UGLY</span><span style="background: rgb(128, 128, 0); color: white;">!!!</span>
<span style="color: dimgrey;">// In real world I would have here some sort of Dependency Injection (e.g. factory)</span>
<span style="color: dimgrey;">// This just shows that we can get an object given a string</span>
Http http<span style="color: purple;">;</span>
Https https<span style="color: purple;">;</span>
Ftp ftp<span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">map</span><span style="color: purple;"><</span><span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span><span style="color: #808030;">,</span> URL<span style="color: #808030;">*</span><span style="color: purple;">></span> choices<span style="color: purple;">;</span>
choices<span style="color: #808030;">.</span>insert<span style="color: #808030;">(</span><span style="color: #666616;">std</span><span style="color: purple;">::</span>make_pair<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #0000e6;">http</span><span style="color: maroon;">"</span><span style="color: #808030;">,</span> <span style="color: #808030;">&</span>http<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
choices<span style="color: #808030;">.</span>insert<span style="color: #808030;">(</span><span style="color: #666616;">std</span><span style="color: purple;">::</span>make_pair<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #0000e6;">https</span><span style="color: maroon;">"</span><span style="color: #808030;">,</span> <span style="color: #808030;">&</span>https<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
choices<span style="color: #808030;">.</span>insert<span style="color: #808030;">(</span><span style="color: #666616;">std</span><span style="color: purple;">::</span>make_pair<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #0000e6;">ftp</span><span style="color: maroon;">"</span><span style="color: #808030;">,</span> <span style="color: #808030;">&</span>ftp<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>scheme <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">http</span><span style="color: maroon;">"</span> <span style="color: maroon; font-weight: bold;">or</span> scheme <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">https</span><span style="color: maroon;">"</span> <span style="color: maroon; font-weight: bold;">or</span> scheme <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">ftp</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> build_url<span style="color: #808030;">(</span><span style="color: #808030;">*</span>choices<span style="color: #808030;">[</span>scheme<span style="color: #808030;">]</span><span style="color: #808030;">)</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span> <span style="color: maroon; font-weight: bold;">else</span> <span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">cerr</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Scheme not valid. Use of these: http, https, ftp</span><span style="color: maroon;">"</span>
<span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">endl</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre>
<br />
I know it looks complicated, but imagine you want to add a new functionality. Let for example add a path to the HTTP url:<br />
<br />
<pre style="background: rgb(255, 255, 255);"><span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * Let's imagine that I want to add a path to the URL: http://ornl.gov:80/<path></path></span>
<span style="color: #3f5fbf;"> * I don't need to modify the code! Open/closed principle.</span>
<span style="color: #3f5fbf;"> * Just extend it, i.e., add new code.</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">class</span> HttpWithPath<span style="color: purple;">:</span> <span style="color: maroon; font-weight: bold;">public</span> Http <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">protected</span><span style="color: #e34adc;">:</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> path<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
HttpWithPath<span style="color: #808030;">(</span><span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> path<span style="color: #808030;">)</span> <span style="color: purple;">:</span> Http<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> path<span style="color: #808030;">(</span>path<span style="color: #808030;">)</span> <span style="color: purple;">{</span><span style="color: purple;">}</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> build_url<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">const</span> <span style="color: purple;">{</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">string</span> default_url <span style="color: #808030;">=</span> URL<span style="color: purple;">::</span>build_url<span style="color: #808030;">(</span>scheme<span style="color: #808030;">,</span> port<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> default_url <span style="color: #808030;">+</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">/</span><span style="color: maroon;">"</span> <span style="color: #808030;">+</span> path<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
</pre>
<div>
<span style="color: purple;"><br /></span></div>
<br />
It does not violate the <b>open/closed principle</b>, which states:<br />
"<i>software entities should be open for extension, but closed for modification</i>".<br />
<br />
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-27655542000222726782015-05-22T13:25:00.001-04:002015-05-22T13:37:19.814-04:00Just for fun<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
Running out of processing power...</div>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
<b>64 x AMD Opteron(tm) Processor 6376</b></div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 64
On-line CPU(s) list: 0-63
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 4
NUMA node(s): 8
Vendor ID: AuthenticAMD
CPU family: 21
Model: 2
Stepping: 0
CPU MHz: 2300.114
BogoMIPS: 4599.35
Virtualization: AMD-V
L1d cache: 16K
L1i cache: 64K
L2 cache: 2048K
L3 cache: 6144K
(...)
</code></pre>
<br />
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">$ grep MemTotal /proc/meminfo
MemTotal: 529254920 kB
</code></pre>
<br />
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">$ df -h
(...)
1.1P 552T 477T 54% /(...)
(...)</code></pre>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-A-5WedWMh2g/VV9mMIfBb6I/AAAAAAAAlSA/4fBRziBML3I/s1600/Screenshot%2Bfrom%2B2015-05-22%2B13%253A23%253A33.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="268" src="http://1.bp.blogspot.com/-A-5WedWMh2g/VV9mMIfBb6I/AAAAAAAAlSA/4fBRziBML3I/s640/Screenshot%2Bfrom%2B2015-05-22%2B13%253A23%253A33.png" width="640" /></a></div>
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-48012146441984721912015-05-05T10:38:00.002-04:002015-05-05T10:40:45.802-04:00JQuery autocomplete with ranged fields<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
Few weeks ago I was looking for a way to get a form field <i>autocomplete</i> from a remote JSON file with a single HTTP request. The solution is <a href="http://ricleal.blogspot.com/2015/02/jquery-javascript-autocomplete-with.html" style="color: #0b0080; text-decoration: none;">here</a>.</div>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
Now I want to do the same but using ranged fields (i.e., sort of an array where commas separate items and hyphen define ranges). A valid field would be: <code style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); color: black; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; padding: 0px 3px;">1-8,10,13,15-20</code>.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-d0XJoNyzKOM/VUjV-ZAlzII/AAAAAAAAk4U/mnJk7bT70lw/s1600/Screenshot%2Bfrom%2B2015-05-05%2B10%3A28%3A12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="193" src="http://3.bp.blogspot.com/-d0XJoNyzKOM/VUjV-ZAlzII/AAAAAAAAk4U/mnJk7bT70lw/s640/Screenshot%2Bfrom%2B2015-05-05%2B10%3A28%3A12.png" width="640" /></a></div>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
<br /></div>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
Here the javascript / JQuery code:</div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-javascript" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;"> <span class="hljs-comment" style="color: #888888;">/* Function to populate the autocomplete from a remote json.
For ranged fields, .e.g,: 4588-4590,465-658 */</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">split</span><span class="hljs-params">(val)</span> </span>{
<span class="hljs-keyword" style="font-weight: bold;">return</span> val.split(<span class="hljs-regexp" style="color: #008800;">/[,-]\s*/</span>);
}
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">extractLast</span><span class="hljs-params">(term)</span> </span>{
<span class="hljs-keyword" style="font-weight: bold;">return</span> split(term).pop();
}
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">set_autocomplete_ranged</span><span class="hljs-params">(selector, jsonurl)</span> </span>{
$.ajax({
url: jsonurl,
type: <span class="hljs-string" style="color: #880000;">'get'</span>,
dataType: <span class="hljs-string" style="color: #880000;">'json'</span>,
async: <span class="hljs-literal" style="color: #008800;">true</span>,
success: <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-params">(data)</span> </span>{
$(selector).autocomplete({
minLength: <span class="hljs-number" style="color: #008800;">0</span>,
source: <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-params">(request, response)</span> </span>{
response($.ui.autocomplete.filter(
data, extractLast(request.term) ));
},
focus: <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-params">()</span> </span>{
<span class="hljs-keyword" style="font-weight: bold;">return</span> <span class="hljs-literal" style="color: #008800;">false</span>;
},
select: <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-params">(event, ui)</span> </span>{
<span class="hljs-keyword" style="font-weight: bold;">var</span> this_value = ui.item.value
<span class="hljs-keyword" style="font-weight: bold;">var</span> all_values = <span class="hljs-keyword" style="font-weight: bold;">this</span>.value
<span class="hljs-keyword" style="font-weight: bold;">this</span>.value = all_values + this_value
<span class="hljs-keyword" style="font-weight: bold;">return</span> <span class="hljs-literal" style="color: #008800;">false</span>;
}
}).bind(<span class="hljs-string" style="color: #880000;">'focus'</span>, <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-params">()</span> </span>{
<span class="hljs-keyword" style="font-weight: bold;">if</span> (!$(<span class="hljs-keyword" style="font-weight: bold;">this</span>).val().trim()) $(<span class="hljs-keyword" style="font-weight: bold;">this</span>).keydown();
});
}
});
}
</code></pre>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
An then just add the URL for the JSON and the <code style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); color: black; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; padding: 0px 3px;">id</code> for the field you want to use the ranged autocomplete:</div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-javascript" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;"> <span class="hljs-comment" style="color: #888888;">/*
Function called only once! It populates the autocomplete from a remote json.
The filtering is made locally as the source is a local variable :)
*/</span>
$(<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword" style="font-weight: bold;">var</span> jsonurl = <span class="hljs-string" style="color: #880000;">"http://mysite.com/myjson.json"</span>;
<span class="hljs-keyword" style="font-weight: bold;">var</span> selector = <span class="hljs-string" style="color: #880000;">'#id_ranged_field'</span>;
<span class="hljs-keyword" style="font-weight: bold;">if</span> ($(selector).is(<span class="hljs-string" style="color: #880000;">'*'</span>)) {
set_autocomplete_ranged(selector,jsonurl);
}
});</code></pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-48182852689161578672015-04-26T09:40:00.000-04:002015-04-27T09:43:50.802-04:00Writing Blogger posts in MarkdownJust write your markdown post here:<br />
<br />
<a href="http://jbt.github.io/markdown-editor/">http://jbt.github.io/markdown-editor/</a><br />
<br />
And copy paste from the right pane :) That's all!!<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-ket6WOLIuBg/VT48MzJGAJI/AAAAAAAAkis/-Az0X9eBwkc/s1600/Screenshot%2Bfrom%2B2015-04-27%2B09%3A39%3A25.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-ket6WOLIuBg/VT48MzJGAJI/AAAAAAAAkis/-Az0X9eBwkc/s1600/Screenshot%2Bfrom%2B2015-04-27%2B09%3A39%3A25.png" height="400" width="376" /></a></div>
<br />
I have tried a few, including the <a href="http://stackedit.io/">stackedit.io</a>, and this looks the best as <i>Blogger</i> doesn't mess up the format...<br />
<br />
Flavoured (i.e., github) Markdown syntax is available here:<br />
<br />
<a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet">https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet</a><br />
<br />
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-52698395663649751992015-04-14T10:28:00.000-04:002015-04-27T09:32:37.713-04:00Git: Going back in time<ol style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin: 1em 0px; padding: 0px 0px 0px 2em;">
<li>Testing stuff on a old commit</li>
</ol>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-bash" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;"><span class="hljs-comment" style="color: #888888;"># This will detach the HEAD, i. e., it leaves no branch checked out:</span>
git checkout <commit id="">
<span class="hljs-comment" style="color: #888888;"># Or:</span>
<span class="hljs-comment" style="color: #888888;"># This will create a new branch from an old commit. Ideal to play and make new commits.</span>
git checkout -b old-state <commit id="">
</commit></commit></code></pre>
<ol start="2" style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin: 1em 0px; padding: 0px 0px 0px 2em;">
<li>Getting rid of everything done since a specific commit</li>
</ol>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-bash" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;"><span class="hljs-comment" style="color: #888888;"># If no published commits exist, i. e., it all the commited changes are were not pushed yet!</span>
git reset --hard <commit id="">
</commit></code></pre>
<ol start="3" style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin: 1em 0px; padding: 0px 0px 0px 2em;">
<li>Undo published commits with new commits.</li>
</ol>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-bash" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;"><span class="hljs-comment" style="color: #888888;"># Delete a commit somewhere in time (the following commits are kept!):</span>
git revert HEAD~<span class="hljs-number" style="color: #008800;">2</span>
<span class="hljs-comment" style="color: #888888;"># Delete the last commit:</span>
git revert HEAD
<span class="hljs-comment" style="color: #888888;"># Delete the last two commits:</span>
git revert HEAD~<span class="hljs-number" style="color: #008800;">2</span>..HEAD</code></pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-64940036906818807342015-03-31T10:01:00.000-04:002015-04-27T09:27:14.050-04:00Git: Delete Branch Remote and Locally<div>
<ol style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin: 1em 0px; padding: 0px 0px 0px 2em;">
<li>See all branches:</li>
</ol>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">git branch -a
</code></pre>
<ol start="2" style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin: 1em 0px; padding: 0px 0px 0px 2em;">
<li>Delete the branch locally:</li>
</ol>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">git branch -D <branch name="">
</branch></code></pre>
<ol start="3" style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin: 1em 0px; padding: 0px 0px 0px 2em;">
<li>Delete the branch remotely:</li>
</ol>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">git push origin --delete <branch name="">
</branch></code></pre>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
DONE!</div>
</div>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-40728780719020430242015-03-31T09:04:00.001-04:002015-04-27T09:35:52.541-04:00Git Roll Back<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
<strong>Rolling back to a previous commit:</strong></div>
<h2 style="border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; color: #111111; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 2em; line-height: 1em; padding-bottom: 5px;">
<span style="font-weight: normal;">Only, </span>and only<span style="font-weight: normal;">, if you are working </span>by your self<span style="font-weight: normal;">!</span></h2>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
(This is dangerous in a collaborative environment: we are rewriting history!)</div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">git reset --hard
git push -f origin
</code></pre>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
Note, if by any reason you want to have access to the lost commits, use:</div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">git reflog</code></pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-62063078429172967402015-02-09T22:30:00.001-05:002015-02-10T13:58:17.501-05:00Getting your pocket content delivered to your Kindle touch<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
I wanted to have the articles I save in my <a href="http://getpocket.com/" style="background: 0px 0px; box-sizing: border-box; color: #a0aabf; cursor: pointer;">Pocket</a> emailed to my old kindle touch... I hate reading in LCDs so I used for a while the <a href="http://en2kindle.com/pocket/" style="background: 0px 0px; box-sizing: border-box; color: #a0aabf; cursor: pointer;">en2kindle</a> until the point it became paid. It's not expensive but after checking the <a href="https://github.com/tapanpandita/pocket" style="background: 0px 0px; box-sizing: border-box; color: #a0aabf; cursor: pointer;">python wrapper for the pocket api</a>, I figured out that implementing something similar shouldn't be too hard...</div>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
So I came across the <a href="https://github.com/sunetos/kindler" style="background: 0px 0px; box-sizing: border-box; color: #a0aabf; cursor: pointer;">kindler</a> on github and decided to give it a go.</div>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
First think was to have a look at the <a href="http://getpocket.com/developer/docs/authentication" style="background: 0px 0px; box-sizing: border-box; color: #a0aabf; cursor: pointer;">Pocket Authentication API Documentation</a> and get a <a href="http://getpocket.com/developer/apps/new" style="background: 0px 0px; box-sizing: border-box; color: #a0aabf; cursor: pointer;">consumer key</a> for the Kindler script.</div>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
I forked the <a href="https://github.com/ricleal/kindler" style="background: 0px 0px; box-sizing: border-box; color: #a0aabf; cursor: pointer;">repo</a> changed a few things in the code because GMail wasn't working.</div>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
I now leave it running in my mac or Linux Box 24h a day and get stuff delivered almost instantaneously.</div>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
If you want to give it a go, here a few notes:</div>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
I had to install before:</div>
<pre style="background: linear-gradient(rgb(255, 255, 255) 0px, rgb(255, 255, 255) 0.75rem, rgb(245, 247, 250) 0.75rem, rgb(245, 247, 250) 2.75rem, rgb(255, 255, 255) 2.75rem, rgb(255, 255, 255) 4rem) rgb(255, 255, 255); border-radius: 4px; border: 1px solid rgb(211, 218, 234); box-sizing: border-box; color: #333333; font-family: monospace, monospace; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; overflow: auto; padding: 0.66001rem 9.5px 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: monospace, monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;">pip <span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">install</span> pyyaml
pip <span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">install</span> setproctitle
pip <span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">install</span> requests
pip <span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">install</span> pyquery
pip <span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">install</span> humanize
pip <span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">install</span> gevent
</code></pre>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
Get it to work having an http server running:</div>
<pre style="background: linear-gradient(rgb(255, 255, 255) 0px, rgb(255, 255, 255) 0.75rem, rgb(245, 247, 250) 0.75rem, rgb(245, 247, 250) 2.75rem, rgb(255, 255, 255) 2.75rem, rgb(255, 255, 255) 4rem) rgb(255, 255, 255); border-radius: 4px; border: 1px solid rgb(211, 218, 234); box-sizing: border-box; color: #333333; font-family: monospace, monospace; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; overflow: auto; padding: 0.66001rem 9.5px 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: monospace, monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">python</span> -m SimpleHTTPServer
</code></pre>
<div style="-webkit-font-feature-settings: 'kern' 1, 'onum' 1, 'liga' 1; background-color: white; box-sizing: border-box; color: #373d49; font-family: Georgia, Cambria, serif; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; padding-top: 0.66001rem;">
My cfg.yml:</div>
<pre style="background: linear-gradient(rgb(255, 255, 255) 0px, rgb(255, 255, 255) 0.75rem, rgb(245, 247, 250) 0.75rem, rgb(245, 247, 250) 2.75rem, rgb(255, 255, 255) 2.75rem, rgb(255, 255, 255) 4rem) rgb(255, 255, 255); border-radius: 4px; border: 1px solid rgb(211, 218, 234); box-sizing: border-box; color: #333333; font-family: monospace, monospace; font-size: 14px; line-height: 28px; margin-bottom: 1.33999rem; overflow: auto; padding: 0.66001rem 9.5px 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: monospace, monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;">api:
consumer: <span class="hljs-comment" style="box-sizing: border-box; color: #586e75;">########</span>
redirect: http://localhost:<span class="hljs-number" style="box-sizing: border-box; color: #2aa198;">8000</span>/
users:
- pocket:
<span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">user</span>: <span class="hljs-comment" style="box-sizing: border-box; color: #586e75;">########</span>
<span class="hljs-literal" style="box-sizing: border-box;">pass</span>: <span class="hljs-comment" style="box-sizing: border-box; color: #586e75;">########</span>
kindle:
email: <span class="hljs-comment" style="box-sizing: border-box; color: #586e75;">########@kindle.com</span>
smtp:
<span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">host</span>: smtp.gmail.com
port: <span class="hljs-number" style="box-sizing: border-box; color: #2aa198;">587</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #859900;">user</span>: <span class="hljs-comment" style="box-sizing: border-box; color: #586e75;">########@gmail.com</span>
<span class="hljs-literal" style="box-sizing: border-box;">pass</span>: <<span class="hljs-constant" style="box-sizing: border-box; color: #b58900;">Application</span> <span class="hljs-literal" style="box-sizing: border-box;">password</span>! <span class="hljs-constant" style="box-sizing: border-box; color: #b58900;">Not</span> your <span class="hljs-constant" style="box-sizing: border-box; color: #b58900;">G</span>MAIL!!></code></pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-22397020693036648062015-02-09T12:30:00.000-05:002015-05-01T10:06:23.925-04:00JQuery Autocomplete with an AJAX call to a remote JSON<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
I'm posting this because it took me ages to figure it out...</div>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
I wanted an input field with autcomplete values provided by a remote JSON. My requisites were the following:</div>
<ul style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin: 1em 0px; padding: 0px 0px 0px 2em;">
<li>No dynamic JSON request:</li>
<li>Every time the user types a letter no JSON remote call!</li>
<li>autocomplete accepts a source : data field, where data can be an URL to a remote location. The problem is that when the user is typing it keeps sending requests to the URL appending ?term=. The idea is that the filtering is done on the server side.... Not what I want... I want the filter to be done on the client side as in case when data is a local variable.</li>
<li>Also, because every JSON generation takes more than 1 second, I wanted just one, and a single one request, to the remote location.</li>
</ul>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
So here the code that sorted it out:</div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-javascript" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;"><span class="hljs-comment" style="color: #888888;">/* Function to populate the autocomplete from a remote json.
Note the Ajax call which success populates the autocomplete selector
*/</span>
<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">set_autocomplete</span><span class="hljs-params">(selector, jsonurl)</span> </span>{
$.ajax({
url: jsonurl,
type: <span class="hljs-string" style="color: #880000;">'get'</span>,
dataType: <span class="hljs-string" style="color: #880000;">'json'</span>,
async: <span class="hljs-literal" style="color: #008800;">true</span>,
success: <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span><span class="hljs-params">(data)</span> </span>{
<span class="hljs-comment" style="color: #888888;">//console.log(data);</span>
$(selector).autocomplete({
source: data,
minLength: <span class="hljs-number" style="color: #008800;">0</span>,
});
}
});
}
<span class="hljs-comment" style="color: #888888;">/* This is only called once when the page is loaded :)
Here the URL to the json and the selector are defined.
*/</span>
$(<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword" style="font-weight: bold;">var</span> jsonurl = <span class="hljs-string" style="color: #880000;">"URL TO CALL"</span>;
<span class="hljs-keyword" style="font-weight: bold;">var</span> selector = <span class="hljs-string" style="color: #880000;">'YOUR SELECTOR, e.g.: input'</span>;
<span class="hljs-keyword" style="font-weight: bold;">if</span> ($(selector).is(<span class="hljs-string" style="color: #880000;">'*'</span>)) {
set_autocomplete(selector, jsonurl);
}
});
</code></pre>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
My JSON looks like this (note that what the user types matches the label but the input field will keep what I have in the value) :</div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-json" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;">[
{
"<span class="hljs-attribute">value</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46477"</span></span>,
"<span class="hljs-attribute">label</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46477 - erererer"</span>
</span>},
{
"<span class="hljs-attribute">value</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46478"</span></span>,
"<span class="hljs-attribute">label</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46478 - erererere"</span>
</span>},
{
"<span class="hljs-attribute">value</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46479"</span></span>,
"<span class="hljs-attribute">label</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46479 - ererererer"</span>
</span>},
{
"<span class="hljs-attribute">value</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46480"</span></span>,
"<span class="hljs-attribute">label</span>":<span class="hljs-value"><span class="hljs-string" style="color: #880000;">"46480 - trhteyretytryteyety"</span>
</span>}
]
</code></pre>
<div style="color: #444444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 16px; line-height: 24px; margin-bottom: 1em; margin-top: 1em;">
And the CSS (just to keep the drop down box with a fixed width and scroll bar):</div>
<pre style="background-color: #f8f8f8; border-radius: 3px; border: 1px solid rgb(204, 204, 204); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 24px; padding: 5px; white-space: pre-wrap; word-wrap: break-word;"><code class="language-css" style="background: transparent !important; border-radius: 3px; border: 0px !important; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.94em; line-height: 1.3em; padding: 0px 3px;"><span class="hljs-class">.ui-autocomplete</span> <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">height</span>:<span class="hljs-value" style="color: #880000;"> <span class="hljs-number" style="color: #008800;">200px</span></span></span>; <span class="hljs-rule"><span class="hljs-attribute">overflow-y</span>:<span class="hljs-value" style="color: #880000;"> scroll</span></span>; <span class="hljs-rule"><span class="hljs-attribute">overflow-x</span>:<span class="hljs-value" style="color: #880000;"> hidden</span></span>;<span class="hljs-rule">}</span></span></code></pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0Knoxville, TN, USA35.9606384 -83.92073920000001435.5493479 -84.566186200000018 36.3719289 -83.27529220000001tag:blogger.com,1999:blog-3183800036537904953.post-82108820621144083142014-07-08T05:11:00.001-04:002015-02-10T13:55:07.317-05:00Python is now the most popular introductory teaching language at top U.S. universities<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="margin-left: 1em; margin-right: 1em;">
<img src="http://cacm.acm.org/system/assets/0001/6470/Top39-700.3.png" height="408" width="640" /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<br /></div>
<br />
<br />
<div style="background: rgb(255, 255, 255); border: 0px; font-family: Georgia, serif; font-size: 14px; line-height: 20px; margin-bottom: 10px; margin-top: 10px; outline: 0px; padding: 0px 20px 0px 0px; vertical-align: baseline;">
<a href="http://www.python.org/" style="background: transparent; border: 0px; color: #077fba; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">Python</a> is currently the most popular language for teaching introductory computer science courses at top-ranked U.S. departments. Specifically, 8 of the top 10 CS departments (80%), and 27 of the top 39 (69%), teach Python in introductory CS0 or CS1 courses.</div>
<br />
<br />
<br />
Source : <a href="http://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-us-universities/fulltext#.U7u1aYxkxWU.blogger">Python is now the most popular introductory teaching language at top U.S. universities | blog@CACM | Communications of the ACM</a>riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com1tag:blogger.com,1999:blog-3183800036537904953.post-51713499772404439112014-06-20T10:11:00.004-04:002014-06-20T10:11:59.157-04:00Mantid for the ILL<br />
<div class="bodytext" style="background-color: white; color: #4f4f4f; font-size: 11px; margin-bottom: 5px; margin-top: 5px;">
<span style="font-family: inherit;"><b>June 18</b><b>th</b></span></div>
<blockquote style="background-color: white; color: #4f4f4f; font-size: 11px; margin-bottom: 0px; margin-top: 0px;">
<div class="bodytext" style="font-size: 8pt; margin-bottom: 5px; margin-top: 5px;">
<span style="font-family: inherit;">10:00 Mantid presentation (College 1 seminar)</span></div>
</blockquote>
<blockquote style="background-color: white; color: #4f4f4f; font-size: 11px; margin-bottom: 0px; margin-top: 0px;">
<div class="bodytext" style="font-size: 8pt; margin-bottom: 5px; margin-top: 5px;">
<span style="font-family: inherit;">Quick introduction to Mantid for the ILL. Scope, current status (E. Farhi, 15').</span></div>
<span style="font-family: inherit;">Introduction to the MantidPlot user interface (A. Markvardsen, 25').</span><br />
<b><span style="font-family: inherit;">Specific work done in Mantid to support ILL instruments (R. Leal, 35').</span></b><br />
<br />
</blockquote>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-DL-xKz5Wc74/U6RA8v16L2I/AAAAAAAAb2w/K8HHZ5kuwEg/s1600/Screenshot+from+2014-06-20+16:10:39.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-DL-xKz5Wc74/U6RA8v16L2I/AAAAAAAAb2w/K8HHZ5kuwEg/s1600/Screenshot+from+2014-06-20+16:10:39.png" height="177" width="320" /></a></div>
<br />
Full presentation:<br />
<a href="https://docs.google.com/presentation/d/1k7MYLnOyaUblTMzt2dn-oVe9Q-Y0Nz8k9v9WRiT47ZU/edit?usp=sharing" target="_blank">Mantid @ILL</a><br />
<div>
<br /></div>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-34998085102480720242014-05-13T03:02:00.002-04:002014-05-13T03:02:58.772-04:00Thoughts on scientific code...Two interesting articles I've just read on the way to work:<br />
<br />
<a href="http://techblog.bozho.net/?p=1423" target="_blank">The Low Quality of Scientific Code</a><br />
<br />
<a href="http://www.yosefk.com/blog/why-bad-scientific-code-beats-code-following-best-practices.html" target="_blank">Why bad scientific code beats code following "best practices"</a><br />
<br />
On (roughly) the same subject" this is also quite interesting:<br />
<br />
<a href="http://arstechnica.com/science/2014/05/scientific-computings-future-can-any-coding-language-top-a-1950s-behemoth/1/" target="_blank">Scientific computing’s future: Can any coding language top a 1950s behemoth?</a> - Scientific computing’s future: Can Haskell, Clojure, or Julia top Fortran?<br />
<br />
<br />
<br />
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-57926438549760211872014-05-06T04:40:00.001-04:002014-05-06T04:40:41.398-04:00Most Popular Programming Languages of 2014<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="margin-left: 1em; margin-right: 1em;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em; text-align: left;">
Python retains it's #1 dominance :)</div>
<div style="margin-left: 1em; margin-right: 1em; text-align: left;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<img height="300" src="http://static.squarespace.com/static/51361f2fe4b0f24e710af7ae/t/52dc3638e4b0d99728f927ae/1390163522743/codeeval2014.jpg?format=750w" width="400" /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<img alt="Screen Shot 2014-01-17 at 2.46.35 PM.png" height="299" src="http://static.squarespace.com/static/51361f2fe4b0f24e710af7ae/t/52d9b5e5e4b01e290ccc768b/1389999590935/Screen%20Shot%202014-01-17%20at%202.46.35%20PM.png?format=750w" width="400" /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em; text-align: left;">
Source: <a href="http://blog.codeeval.com/codeevalblog/2014#.U2ifGHV53AE" target="_blank">CodeEval</a>.</div>
<br />
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-41644297788917858232014-05-04T13:26:00.001-04:002014-05-06T04:43:03.593-04:00GitHub language trends 2014<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="margin-left: 1em; margin-right: 1em; text-align: left;">
JavaScript ahead??</div>
<div style="margin-left: 1em; margin-right: 1em; text-align: left;">
Probably thanks to the thousands of automatically generated lines of code...</div>
<div style="margin-left: 1em; margin-right: 1em; text-align: left;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<br /></div>
<div style="margin-left: 1em; margin-right: 1em;">
<img src="http://dberkholz-media.redmonk.com/dberkholz/files/2014/04/github_new_repos-custom.png" height="361" width="400" /></div>
<br />
<br />
Source : <a href="http://redmonk.com/dberkholz/2014/05/02/github-language-trends-and-the-fragmenting-landscape/" target="_blank">Redmonk</a>.riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0Grenoble, France45.188529 5.72452399999997445.098995 5.563162499999974 45.278063 5.8858854999999739tag:blogger.com,1999:blog-3183800036537904953.post-57252309112852168592013-12-04T04:48:00.001-05:002013-12-04T04:49:36.556-05:00C++ : Constructors involved when returning values from functions<br />
Trying to figure out how to return huge objects when they are created inside a function or changed inside a function (i.e. avoiding copy constructors).<br />
<br />
Code and output should be easy to understand.<br />
<br />
Code:<br />
<table border="0" style="font-family: sans-serif; width: 100%px;"><tbody>
<tr><td><br /></td><td valign="top" width="45%"><div style="overflow: auto;">
<pre style="background-color: white;"><pre><span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">iostream</span><span style="color: maroon;">></span>
<span style="color: maroon; font-weight: bold;">using</span> <span style="color: maroon; font-weight: bold;">namespace</span> <span style="color: #666616;">std</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">class</span> Dummy <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">int</span> x<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
Dummy<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">:</span>
x<span style="color: #808030;">(</span><span style="color: #008c00;">0</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> Default Constructor</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">virtual</span> <span style="color: #808030;">~</span>Dummy<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> desctructor</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
Dummy<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">int</span> i<span style="color: #808030;">)</span> <span style="color: purple;">:</span>
x<span style="color: #808030;">(</span>i<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> Parameter Constructor</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
Dummy<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">const</span> Dummy<span style="color: #808030;">&</span> other<span style="color: #808030;">)</span> <span style="color: purple;">:</span>
x<span style="color: #808030;">(</span>other<span style="color: #808030;">.</span>x<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> Copy Constructor</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: dimgrey;">// Two existing objects!</span>
Dummy<span style="color: #808030;">&</span> <span style="color: maroon; font-weight: bold;">operator</span><span style="color: #808030;">=</span><span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">const</span> Dummy<span style="color: #808030;">&</span> other<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
x <span style="color: #808030;">=</span> other<span style="color: #808030;">.</span>x<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> Copy Assignment Operator</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #808030;">*</span><span style="color: maroon; font-weight: bold;">this</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: dimgrey;">//C++11</span>
Dummy<span style="color: #808030;">(</span>Dummy<span style="color: #808030;">&</span><span style="color: #808030;">&</span> other<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
x <span style="color: #808030;">=</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span>move<span style="color: #808030;">(</span>other<span style="color: #808030;">.</span>x<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> C++11 Move Constructor</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: dimgrey;">//C++11</span>
Dummy<span style="color: #808030;">&</span> <span style="color: maroon; font-weight: bold;">operator</span><span style="color: #808030;">=</span><span style="color: #808030;">(</span>Dummy<span style="color: #808030;">&</span><span style="color: #808030;">&</span> other<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
x <span style="color: #808030;">=</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span>move<span style="color: #808030;">(</span>other<span style="color: #808030;">.</span>x<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> C++11 Move Operator</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #808030;">*</span><span style="color: maroon; font-weight: bold;">this</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">void</span> setX<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">int</span> x<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">this</span><span style="color: #808030;">-</span><span style="color: #808030;">></span>x <span style="color: #808030;">=</span> x<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">friend</span> <span style="color: #603000;">ostream</span><span style="color: #808030;">&</span> <span style="color: maroon; font-weight: bold;">operator</span><span style="color: #808030;"><</span><span style="color: #808030;"><</span><span style="color: #808030;">(</span><span style="color: #603000;">ostream</span> <span style="color: #808030;">&</span>out<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">const</span> Dummy <span style="color: #808030;">&</span>m<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: #603000;">ostream</span><span style="color: #808030;">&</span> <span style="color: maroon; font-weight: bold;">operator</span><span style="color: #808030;"><</span><span style="color: #808030;"><</span><span style="color: #808030;">(</span><span style="color: #603000;">ostream</span> <span style="color: #808030;">&</span>out<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">const</span> Dummy <span style="color: #808030;">&</span>m<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
out <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0f69ff;">\t</span><span style="color: #0000e6;"> -> Dummy.x=</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> m<span style="color: #808030;">.</span>x <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> out<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
Dummy fRetClassValue<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> Dummy<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
Dummy fRetClassValue<span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">int</span> x<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
Dummy d <span style="color: #808030;">=</span> Dummy<span style="color: #808030;">(</span>x<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> d<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: dimgrey;">// Cant't return references to local variables</span>
<span style="color: dimgrey;">//Dummy& fRetClassRef() {</span>
<span style="color: dimgrey;">// return Dummy();</span>
<span style="color: dimgrey;">//}</span>
<span style="color: dimgrey;">//</span>
<span style="color: dimgrey;">//Dummy& fRetClassRef(int x) {</span>
<span style="color: dimgrey;">// Dummy d = Dummy(x);</span>
<span style="color: dimgrey;">// return d;</span>
<span style="color: dimgrey;">//}</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * This will return a reference for the class passed by param</span>
<span style="color: #3f5fbf;"> * No copy constructors involved</span>
<span style="color: #3f5fbf;"> */</span>
Dummy<span style="color: #808030;">&</span> fRetClassRef<span style="color: #808030;">(</span>Dummy <span style="color: #808030;">&</span>d<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> d<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
Dummy<span style="color: #808030;">&</span> fRetClassRef<span style="color: #808030;">(</span>Dummy <span style="color: #808030;">&</span>d<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">int</span> x<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
d<span style="color: #808030;">.</span>setX<span style="color: #808030;">(</span>x<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> d<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * This will return a copy for the class passed by param</span>
<span style="color: #3f5fbf;"> * Copy constructors involved!</span>
<span style="color: #3f5fbf;"> */</span>
Dummy fRetClassValue<span style="color: #808030;">(</span>Dummy <span style="color: #808030;">&</span>d<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> d<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
Dummy fRetClassValue<span style="color: #808030;">(</span>Dummy <span style="color: #808030;">&</span>d<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">int</span> x<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
d<span style="color: #808030;">.</span>setX<span style="color: #808030;">(</span>x<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> d<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
Dummy<span style="color: #808030;">&</span> f3<span style="color: #808030;">(</span>Dummy <span style="color: #808030;">&</span>m<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
m<span style="color: #808030;">.</span>setX<span style="color: #808030;">(</span><span style="color: #008c00;">21</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> m<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">int</span> <span style="color: #400000;">main</span><span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">void</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">1.........................</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span>endl<span style="color: purple;">;</span>
Dummy d1 <span style="color: #808030;">=</span> fRetClassValue<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d1<span style="color: purple;">;</span>
Dummy d2 <span style="color: #808030;">=</span> fRetClassValue<span style="color: #808030;">(</span><span style="color: #008c00;">2</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d2<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">1_2......................... (Need const to work!)</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span>endl<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">const</span> Dummy <span style="color: #808030;">&</span>d1_2 <span style="color: #808030;">=</span> fRetClassValue<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d1_2<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">const</span> Dummy <span style="color: #808030;">&</span>d2_2 <span style="color: #808030;">=</span> fRetClassValue<span style="color: #808030;">(</span><span style="color: #008c00;">22</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d2_2<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">2.........................</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span>endl<span style="color: purple;">;</span>
Dummy d3 <span style="color: #808030;">=</span> fRetClassRef<span style="color: #808030;">(</span>d1<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d3<span style="color: purple;">;</span>
Dummy d4 <span style="color: #808030;">=</span> fRetClassRef<span style="color: #808030;">(</span>d1<span style="color: #808030;">,</span><span style="color: #008c00;">4</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d4<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">2_2.........................</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span>endl<span style="color: purple;">;</span>
Dummy <span style="color: #808030;">&</span>d3_2 <span style="color: #808030;">=</span> fRetClassRef<span style="color: #808030;">(</span>d1<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d3_2<span style="color: purple;">;</span>
Dummy <span style="color: #808030;">&</span>d4_2 <span style="color: #808030;">=</span> fRetClassRef<span style="color: #808030;">(</span>d1<span style="color: #808030;">,</span><span style="color: #008c00;">42</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d4_2<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">3.........................</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span>endl<span style="color: purple;">;</span>
Dummy <span style="color: #808030;">&</span>d5 <span style="color: #808030;">=</span> fRetClassRef<span style="color: #808030;">(</span>d1<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d5<span style="color: purple;">;</span>
Dummy <span style="color: #808030;">&</span>d6 <span style="color: #808030;">=</span> fRetClassRef<span style="color: #808030;">(</span>d1<span style="color: #808030;">,</span><span style="color: #008c00;">6</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d1</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d1<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d6</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d6<span style="color: purple;">;</span>
d6<span style="color: #808030;">.</span>setX<span style="color: #808030;">(</span><span style="color: #008c00;">123</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d1</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d1<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d6</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d6<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">4.........................</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span>endl<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">const</span> Dummy <span style="color: #808030;">&</span>d7 <span style="color: #808030;">=</span> fRetClassValue<span style="color: #808030;">(</span>d1<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d7<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">const</span> Dummy <span style="color: #808030;">&</span>d8 <span style="color: #808030;">=</span> fRetClassValue<span style="color: #808030;">(</span>d1<span style="color: #808030;">,</span><span style="color: #008c00;">7</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d1</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d1<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d8</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d8<span style="color: purple;">;</span>
d6<span style="color: #808030;">.</span>setX<span style="color: #808030;">(</span><span style="color: #008c00;">1234</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d1</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d1<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">d8</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> d8<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">END .........................</span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span>endl<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre>
</pre>
</div>
</td></tr>
</tbody></table>
<br />
Output:<br />
<br />
<pre><span style="color: #008c00;">1</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span>
-<span style="color: #e34adc;">></span> Default Constructor
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">0</span>
-<span style="color: #e34adc;">></span> Parameter Constructor
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">2</span>
1_2<span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span> <span style="color: purple;">(</span>Need const to work<span style="color: maroon; font-weight: bold;">!</span><span style="color: purple;">)</span>
-<span style="color: #e34adc;">></span> Default Constructor
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">0</span>
-<span style="color: #e34adc;">></span> Parameter Constructor
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">22</span>
<span style="color: #008c00;">2</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span>
-<span style="color: #e34adc;">></span> Copy Constructor
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">0</span>
-<span style="color: #e34adc;">></span> Copy Constructor
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">4</span>
2_2<span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span>
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">4</span>
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">42</span>
<span style="color: #008c00;">3</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span>
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">42</span>
d1 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">6</span>
d6 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">6</span>
d1 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">123</span>
d6 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">123</span>
<span style="color: #008c00;">4</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span>
-<span style="color: #e34adc;">></span> Copy Constructor
-<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">123</span>
-<span style="color: #e34adc;">></span> Copy Constructor
d1 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">7</span>
d8 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">7</span>
d1 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">1234</span>
d8 -<span style="color: #e34adc;">></span> <span style="color: #797997;">Dummy.x</span><span style="color: #808030;">=</span><span style="color: #008c00;">7</span>
END <span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span><span style="color: maroon; font-weight: bold;">.</span>
-<span style="color: #e34adc;">></span> desctructor
-<span style="color: #e34adc;">></span> desctructor
-<span style="color: #e34adc;">></span> desctructor
-<span style="color: #e34adc;">></span> desctructor
-<span style="color: #e34adc;">></span> desctructor
-<span style="color: #e34adc;">></span> desctructor
-<span style="color: #e34adc;">></span> desctructor
-<span style="color: #e34adc;">></span> desctructor</pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-75683887340126963672013-07-12T16:00:00.000-04:002013-12-04T04:56:47.527-05:00RVO: Return value optimization and NRVO: Named return value optimization<br />
Note the last lines:<br />
<br />
Here no copy constructors are involved:<br />
<pre class="de1" style="background-color: #fafafa; color: #000066; font-size: 13px; padding-bottom: 5px; padding-left: 10px; padding-top: 3px;">MyClass <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>m14 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f3<span class="br0" style="color: green; line-height: 13px !important;">(</span>m11<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span> <span class="co1" style="color: #666666; line-height: 13px !important;">// if function return ref this is OK! NO COPY!</span></pre>
<br />
<br />
But the same variable (which is not an lvalue anymore!) calls a copy constructor when assigned:<br />
<pre class="de1" style="background-color: #fafafa; color: #000066; font-size: 13px; padding-bottom: 5px; padding-left: 10px; padding-top: 3px;"> m14 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f3<span class="br0" style="color: green; line-height: 13px !important;">(</span>m14<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span></pre>
<br />
Took me a while to understand why the the heck the copy constructor were beeing called!<br />
<pre class="de1" style="background-color: #fafafa; color: #000066; font-size: 13px; padding-bottom: 5px; padding-left: 10px; padding-top: 3px;">
<span class="kw2" style="color: blue; line-height: 13px !important;">class</span> MyClass <span class="br0" style="color: green; line-height: 13px !important;">{</span>
<span class="kw4" style="color: blue; line-height: 13px !important;">int</span> x<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw2" style="color: blue; line-height: 13px !important;">public</span><span class="sy4" style="color: teal; line-height: 13px !important;">:</span>
MyClass<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="sy4" style="color: teal; line-height: 13px !important;">:</span>
x<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="nu0" style="color: #0000dd; line-height: 13px !important;">0</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"Default Constructor"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
MyClass<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="kw4" style="color: blue; line-height: 13px !important;">int</span> i<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="sy4" style="color: teal; line-height: 13px !important;">:</span>
x<span class="br0" style="color: green; line-height: 13px !important;">(</span>i<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"Parameter Constructor"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
MyClass<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="kw4" style="color: blue; line-height: 13px !important;">const</span> MyClass<span class="sy3" style="color: #000040; line-height: 13px !important;">&</span> other<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="sy4" style="color: teal; line-height: 13px !important;">:</span>
x<span class="br0" style="color: green; line-height: 13px !important;">(</span>other.<span class="me1" style="color: #007788; line-height: 13px !important;">x</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"Copy Constructor"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// Two existing objects!</span>
MyClass<span class="sy3" style="color: #000040; line-height: 13px !important;">&</span> operator<span class="sy1" style="color: navy; line-height: 13px !important;">=</span><span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="kw4" style="color: blue; line-height: 13px !important;">const</span> MyClass<span class="sy3" style="color: #000040; line-height: 13px !important;">&</span> other<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
x <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> other.<span class="me1" style="color: #007788; line-height: 13px !important;">x</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"Copy Assignment Operator"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> <span class="sy2" style="color: #000040; line-height: 13px !important;">*</span><span class="kw3" style="color: #0000dd; line-height: 13px !important;">this</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">//C++11</span>
MyClass<span class="br0" style="color: green; line-height: 13px !important;">(</span>MyClass<span class="sy3" style="color: #000040; line-height: 13px !important;">&&</span> other<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
x <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> std<span class="sy4" style="color: teal; line-height: 13px !important;">::</span><span class="me2" style="color: #007788; line-height: 13px !important;">move</span><span class="br0" style="color: green; line-height: 13px !important;">(</span>other.<span class="me1" style="color: #007788; line-height: 13px !important;">x</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"Move Constructor"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">//C++11</span>
MyClass<span class="sy3" style="color: #000040; line-height: 13px !important;">&</span> operator<span class="sy1" style="color: navy; line-height: 13px !important;">=</span><span class="br0" style="color: green; line-height: 13px !important;">(</span>MyClass<span class="sy3" style="color: #000040; line-height: 13px !important;">&&</span> other<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
x <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> std<span class="sy4" style="color: teal; line-height: 13px !important;">::</span><span class="me2" style="color: #007788; line-height: 13px !important;">move</span><span class="br0" style="color: green; line-height: 13px !important;">(</span>other.<span class="me1" style="color: #007788; line-height: 13px !important;">x</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"Move Operator"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> <span class="sy2" style="color: #000040; line-height: 13px !important;">*</span><span class="kw3" style="color: #0000dd; line-height: 13px !important;">this</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="kw4" style="color: blue; line-height: 13px !important;">void</span> setX<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="kw4" style="color: blue; line-height: 13px !important;">int</span> x<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="br0" style="color: green; line-height: 13px !important;">{</span>
this<span class="sy2" style="color: #000040; line-height: 13px !important;">-</span><span class="sy1" style="color: navy; line-height: 13px !important;">></span>x<span class="sy1" style="color: navy; line-height: 13px !important;">=</span>x<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="kw2" style="color: blue; line-height: 13px !important;">friend</span> ostream<span class="sy3" style="color: #000040; line-height: 13px !important;">&</span> operator<span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="br0" style="color: green; line-height: 13px !important;">(</span>ostream <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>out, <span class="kw4" style="color: blue; line-height: 13px !important;">const</span> MyClass <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>m<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
ostream<span class="sy3" style="color: #000040; line-height: 13px !important;">&</span> operator<span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="br0" style="color: green; line-height: 13px !important;">(</span>ostream <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>out, <span class="kw4" style="color: blue; line-height: 13px !important;">const</span> MyClass <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>m<span class="br0" style="color: green; line-height: 13px !important;">)</span>
<span class="br0" style="color: green; line-height: 13px !important;">{</span>
out <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"MyClass x="</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> m.<span class="me1" style="color: #007788; line-height: 13px !important;">x</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> out<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// Dummy func to show C++11 move constructor</span>
MyClass f<span class="br0" style="color: green; line-height: 13px !important;">(</span>MyClass m<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> m<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// create class and return</span>
MyClass f1<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
MyClass m<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="nu0" style="color: #0000dd; line-height: 13px !important;">10</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> m<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// return new class</span>
MyClass f2<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> MyClass<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="nu0" style="color: #0000dd; line-height: 13px !important;">11</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// Change the value of the class, return ref!</span>
MyClass<span class="sy3" style="color: #000040; line-height: 13px !important;">&</span> f3<span class="br0" style="color: green; line-height: 13px !important;">(</span>MyClass <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>m<span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
m.<span class="me1" style="color: #007788; line-height: 13px !important;">setX</span><span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="nu0" style="color: #0000dd; line-height: 13px !important;">21</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> m<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span>
<span class="kw4" style="color: blue; line-height: 13px !important;">int</span> main<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="kw4" style="color: blue; line-height: 13px !important;">void</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="br0" style="color: green; line-height: 13px !important;">{</span>
MyClass m1<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="nu0" style="color: #0000dd; line-height: 13px !important;">1</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m2 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> m1<span class="sy4" style="color: teal; line-height: 13px !important;">;</span> <span class="co1" style="color: #666666; line-height: 13px !important;">//copy</span>
MyClass m3<span class="br0" style="color: green; line-height: 13px !important;">(</span>m1<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span> <span class="co1" style="color: #666666; line-height: 13px !important;">//copy</span>
MyClass m4<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
m4 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> m1<span class="sy4" style="color: teal; line-height: 13px !important;">;</span> <span class="co1" style="color: #666666; line-height: 13px !important;">//Assignment</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"* Move Contructor C++11"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m5<span class="br0" style="color: green; line-height: 13px !important;">(</span>f<span class="br0" style="color: green; line-height: 13px !important;">(</span>MyClass<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"* Move Operator C++11"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m6<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
m6 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> MyClass<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"* Optimisation 1:"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m7 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f1<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> m7<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"* Optimisation 2:"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m8 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f2<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> m8<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"* Optimisation 3:"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw4" style="color: blue; line-height: 13px !important;">const</span> MyClass <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>m9 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f1<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> m9<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// Does not work! lvalue must be const!</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// f() returns a temporary object (i.e., rvalue) and only lvalues can be bound to references to non-const.</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// cout << "Optimisation 4:" << endl;</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// MyClass &m10 = f2();</span>
<span class="co1" style="color: #666666; line-height: 13px !important;">// cout << m10;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"* NO Optimisation 5:"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m11<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="nu0" style="color: #0000dd; line-height: 13px !important;">15</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">" :( Copy constructor:"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m12 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f3<span class="br0" style="color: green; line-height: 13px !important;">(</span>m11<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">" :( Copy Assignement Operator:"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
m12 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f3<span class="br0" style="color: green; line-height: 13px !important;">(</span>m12<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> m12<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> <span class="st0" style="color: red; line-height: 13px !important;">"* Optimisation 6:"</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> endl<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass m13<span class="br0" style="color: green; line-height: 13px !important;">(</span><span class="nu0" style="color: #0000dd; line-height: 13px !important;">15</span><span class="br0" style="color: green; line-height: 13px !important;">)</span> <span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
MyClass <span class="sy3" style="color: #000040; line-height: 13px !important;">&</span>m14 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f3<span class="br0" style="color: green; line-height: 13px !important;">(</span>m11<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span> <span class="co1" style="color: #666666; line-height: 13px !important;">// if function return ref this is OK! NO COPY!</span>
m14 <span class="sy1" style="color: navy; line-height: 13px !important;">=</span> f3<span class="br0" style="color: green; line-height: 13px !important;">(</span>m14<span class="br0" style="color: green; line-height: 13px !important;">)</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw3" style="color: #0000dd; line-height: 13px !important;">cout</span> <span class="sy1" style="color: navy; line-height: 13px !important;"><<</span> m14<span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="kw1" style="color: blue; line-height: 13px !important;">return</span> <span class="nu0" style="color: #0000dd; line-height: 13px !important;">0</span><span class="sy4" style="color: teal; line-height: 13px !important;">;</span>
<span class="br0" style="color: green; line-height: 13px !important;">}</span></pre>
<br />
<br />
<br />
<label style="background-color: #eaeaea; color: #333333; display: block; font-family: Helvetica; font-size: 13px; margin-bottom: 3px; margin-left: 0px; margin-top: 5px;">output:</label><br />
<pre class="box" style="background-color: #f0f0f0; border: 1px solid rgb(153, 153, 153); font-size: 13px; padding: 0.5em;"><span style="color: #000066;">Parameter Constructor
Copy Constructor
Copy Constructor
Default Constructor
Copy Assignment Operator
* Move Contructor C++11
Default Constructor
Move Constructor
* Move Operator C++11
Default Constructor
Default Constructor
Move Operator
* Optimisation 1:
Parameter Constructor
MyClass x=10
* Optimisation 2:
Parameter Constructor
MyClass x=11
* Optimisation 3:
Parameter Constructor
MyClass x=10
* NO Optimisation 5:
Parameter Constructor
:( Copy constructor:
Copy Constructor
:( Copy Assignement Operator:
Copy Assignment Operator
MyClass x=21
* Optimisation 6:
Parameter Constructor
</span><span style="color: red;">Copy Assignment Operator</span><span style="color: #000066;">
MyClass x=21</span></pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-57121750541749531392013-04-05T10:12:00.001-04:002013-12-04T04:49:17.118-05:00C++11 : Interface Test for dummies...<br />
<pre><span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">iostream</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">string</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">map</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">memory</span><span style="color: maroon;">></span>
<span style="color: maroon; font-weight: bold;">using</span> <span style="color: maroon; font-weight: bold;">namespace</span> <span style="color: #666616;">std</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * Server Interface</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">class</span> IServer <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">protected</span><span style="color: #e34adc;">:</span>
<span style="color: #603000;">string</span> protocol<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
<span style="color: maroon; font-weight: bold;">virtual</span> <span style="color: maroon; font-weight: bold;">void</span> reponse<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">virtual</span> <span style="color: maroon; font-weight: bold;">void</span> accept<span style="color: #808030;">(</span><span style="color: #603000;">string</span> message<span style="color: #808030;">)</span> <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * Server Factory</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">class</span> Factory
<span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
<span style="color: dimgrey;">//virtual IServer* create() = 0;</span>
<span style="color: maroon; font-weight: bold;">virtual</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span>shared_ptr<span style="color: purple;"><</span>IServer<span style="color: purple;">></span> create<span style="color: #808030;">(</span><span style="color: #603000;">string</span> s<span style="color: #808030;">)</span> <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">template</span> <span style="color: purple;"><</span><span style="color: maroon; font-weight: bold;">class</span> aServer<span style="color: purple;">></span>
<span style="color: maroon; font-weight: bold;">class</span> ServerFactory <span style="color: purple;">:</span> <span style="color: maroon; font-weight: bold;">public</span> Factory
<span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
<span style="color: dimgrey;">//IServer* create() { return new aServer(); }</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span>shared_ptr<span style="color: purple;"><</span>IServer<span style="color: purple;">></span> create<span style="color: #808030;">(</span><span style="color: #603000;">string</span> s<span style="color: #808030;">)</span> <span style="color: purple;">{</span> <span style="color: maroon; font-weight: bold;">return</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span>make_shared<span style="color: purple;"><</span>aServer<span style="color: purple;">></span><span style="color: #808030;">(</span>s<span style="color: #808030;">)</span><span style="color: purple;">;</span> <span style="color: purple;">}</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * A server</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">class</span> Server1 <span style="color: purple;">:</span> <span style="color: maroon; font-weight: bold;">public</span> IServer <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
Server1<span style="color: #808030;">(</span><span style="color: #603000;">string</span> protocol_<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: dimgrey;">// does not work as normal constructor variable</span>
protocol <span style="color: #808030;">=</span> protocol_<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Server1 constructor with protocol: </span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> protocol <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">void</span> reponse<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Server1 Responding with protocol: </span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> protocol <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">void</span> accept<span style="color: #808030;">(</span><span style="color: #603000;">string</span> message<span style="color: #808030;">)</span><span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Server1 accepted message with protocol: </span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> protocol <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Message: </span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> message <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> * The client</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">class</span> Client <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">private</span><span style="color: #e34adc;">:</span>
<span style="color: #603000;">string</span> id<span style="color: purple;">;</span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span>shared_ptr<span style="color: purple;"><</span>IServer<span style="color: purple;">></span> server<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
Client<span style="color: #808030;">(</span><span style="color: #603000;">string</span> clientId<span style="color: #808030;">)</span> <span style="color: purple;">:</span> id<span style="color: #808030;">(</span>clientId<span style="color: #808030;">)</span><span style="color: purple;">{</span>
<span style="color: #603000;">cout</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> <span style="color: maroon;">"</span><span style="color: #0000e6;">Client constructor with id: </span><span style="color: maroon;">"</span> <span style="color: #808030;"><</span><span style="color: #808030;"><</span> clientId <span style="color: #808030;"><</span><span style="color: #808030;"><</span> endl<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">void</span> setServer<span style="color: #808030;">(</span><span style="color: #666616;">std</span><span style="color: purple;">::</span>shared_ptr<span style="color: purple;"><</span>IServer<span style="color: purple;">></span> server_<span style="color: #808030;">)</span><span style="color: purple;">{</span>
server <span style="color: #808030;">=</span> server_<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">void</span> request<span style="color: #808030;">(</span><span style="color: #603000;">string</span> message<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
server<span style="color: #808030;">-</span><span style="color: #808030;">></span>accept<span style="color: #808030;">(</span>message<span style="color: #808030;">)</span><span style="color: purple;">;</span>
server<span style="color: #808030;">-</span><span style="color: #808030;">></span>reponse<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">int</span> <span style="color: #400000;">main</span> <span style="color: #808030;">(</span><span style="color: maroon; font-weight: bold;">void</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: dimgrey;">// register server 1 in the::make_shared<myclass>(1); factory</myclass></span>
<span style="color: #666616;">std</span><span style="color: purple;">::</span><span style="color: #603000;">map</span><span style="color: purple;"><</span><span style="color: #603000;">string</span><span style="color: #808030;">,</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span>shared_ptr<span style="color: purple;"><</span>Factory<span style="color: purple;">></span> <span style="color: purple;">></span> factoryMap<span style="color: purple;">;</span>
factoryMap<span style="color: #808030;">[</span><span style="color: maroon;">"</span><span style="color: #0000e6;">Server1</span><span style="color: maroon;">"</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #666616;">std</span><span style="color: purple;">::</span>make_shared<span style="color: purple;"><</span> ServerFactory<span style="color: purple;"><</span>Server1<span style="color: purple;">></span> <span style="color: purple;">></span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: dimgrey;">//Server1 s("DummyProtocol");</span>
<span style="color: maroon; font-weight: bold;">auto</span> s <span style="color: #808030;">=</span> factoryMap<span style="color: #808030;">[</span><span style="color: maroon;">"</span><span style="color: #0000e6;">Server1</span><span style="color: maroon;">"</span><span style="color: #808030;">]</span><span style="color: #808030;">-</span><span style="color: #808030;">></span>create<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #0000e6;">DummyProtocol</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
Client c<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #0000e6;">Dummy Client</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
c<span style="color: #808030;">.</span>setServer<span style="color: #808030;">(</span>s<span style="color: #808030;">)</span><span style="color: purple;">;</span>
c<span style="color: #808030;">.</span>request<span style="color: #808030;">(</span><span style="color: maroon;">"</span><span style="color: #0000e6;">Dummy Message!</span><span style="color: maroon;">"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-74805147457113157972013-03-31T10:32:00.000-04:002013-04-03T05:18:02.008-04:00C++ : The copy vs move constructor vs the assignment operatorThe the code and the output below, should be obvious to distinguish when one or the other are invoked. I have also added the new C++11 rvalue references to create move constructors and move assignment operators.
<br />
<br />
<pre style="background: rgba(238,238,238,0.92); color: black;">#<span style="color: blue;">include</span> <span style="color: #009933;"><iostream></span>
<span style="color: blue;">using</span> <span style="font-weight: 700;">namespace</span> std;
<span style="font-weight: 700;">class</span> MyClass {
int x;
public:
<span style="color: #ff8000;">MyClass</span>() :
<span style="color: #ff8000;">x</span>(0) {
cout << "Default Constructor" << endl;
}
<span style="color: #ff8000;">MyClass</span>(int i) :
x(i) {
cout << "Parameter Constructor" << endl;
}
<span style="color: #ff8000;">MyClass</span>(const MyClass& other) :
x(other.x) {
cout << "Copy Constructor" << endl;
}
<span style="color: #0066ff; font-style: italic;">// Two existing objects!</span>
MyClass& <span style="color: blue;">operator</span>=(<span style="font-weight: 700;">const</span> MyClass& other) {
x = other.x;
cout << "Assignment Operator" << endl;
return *this;
}
<span style="color: #0066ff; font-style: italic;">//C++11</span>
<span style="color: #ff8000;">MyClass</span>(MyClass&& other) {
x = std::move(other.x);
cout << "Move Constructor" << endl;
}
<span style="color: #0066ff; font-style: italic;">//C++11</span>
MyClass& <span style="color: blue;">operator</span>=(MyClass&& other) {
x = std::move(other.x);
cout << "Move Operator" << endl;
return *this;
}
};
<span style="color: #0066ff; font-style: italic;">// Dummy func to show C++11 move constructor</span>
MyClass <span style="color: #ff8000;">f</span>(MyClass m) {
return m;
}
<span style="font-weight: 700;">int</span> <span style="color: #ff8000;">main</span>(void) {
MyClass m1(1);
MyClass m2 = m1; //copy
MyClass m3(m1); //copy
MyClass m4;
m4 = m1; //Assignment
cout << "* Move Contructor C++11" << endl;
MyClass m5(<span style="color: #3333ff; font-weight: 700;">f</span>(<span style="color: #3333ff; font-weight: 700;">MyClass</span>()));
cout << "* Move Operator C++11" << endl;
MyClass m6;
m6 = MyClass();
return 0;
}
</pre>
And the output.<br />
<br />
<pre style="background: rgba(238,238,238,0.92); color: black;">Parameter Constructor
Copy Constructor
Copy Constructor
Default Constructor
Assignment Operator
* Move Contructor C++11
Default Constructor
Move Constructor
* Move Operator C++11
Default Constructor
Default Constructor
Move Operator
</pre>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0Grenoble, France45.188529 5.72452399999997445.0989945 5.563162499999974 45.2780635 5.8858854999999739tag:blogger.com,1999:blog-3183800036537904953.post-19964861212126920412013-03-24T12:57:00.000-04:002013-04-03T05:18:29.728-04:00Object-Oriented DesignQuoting Uncle Bob Martin's :<br />
<br />
<h2>
You know the software is rotting when it starts to exhibit any of the following odors:</h2>
<div>
<b>Rigidity</b> – The system is hard to change because every change forces many other changes to other parts of the system.</div>
<div>
<div>
<b>Fragility</b> – Changes cause the system to break in places that have no conceptual relationship to the part that was changed.</div>
<div>
<b>Immobility</b> – It is hard to disentangle the system into components that can be reused in the other systems.</div>
<div>
<b>Viscosity</b> – Doing things right is harder than doing things wrong. Needless Complexity – The design contains infrastructure that adds no direct benefit.</div>
<div>
<b>Needless Repetition</b> – The design contains repeating structures that could be unified under a single abstraction.</div>
<div>
<b>Opacity</b> – It is hard to read and understand. It does not express its intent well</div>
</div>
<div>
<br /></div>
<div>
<h2>
Simple Design</h2>
<h3>
Extreme Programming's principle of Simple Design, in order of importance:</h3>
<div>
<ol>
<li>Runs all the tests</li>
<li>Contains no duplication</li>
<li>Expresses the intent of the programmer</li>
<li>Minimizes the number of classes and methods</li>
</ol>
</div>
<h3>
Truly agile teams don't allow the software to rot:</h3>
<div>
<ul>
<li>Keeping the design clean and simple at all times is the only way to go fast. It makes the design flexible and easy to change.</li>
<li>Making a mess will always slow you down the next time that you need to read or change the same code.</li>
</ul>
</div>
</div>
<h2>
Architecture</h2>
<div>
<blockquote class="tr_bq">
"Architecture is about the important stuff. Whatever that is."</blockquote>
<blockquote class="tr_bq">
"Architecture is the decisions that you wish you could get right early in a project."</blockquote>
<h3>
Why do people feel the need to get some things right early in the project?</h3>
<div>
The answer, of course, is because they perceive those things as hard to change. So you might end up defining architecture as “<i><u>things that people perceive as hard to change</u></i>”.</div>
<div>
<ul>
<li>Big Design Up Front (BDUF), i.e. spinning system designs based on untested hypotheses for many months, is harmful. </li>
<ul>
<li>BDUF inhibits adapting to change.</li>
</ul>
<li>Little/Enough Design Up Front (LDUF/EDUF) is good. </li>
<ul>
<li>With a big project spending a week or even a month thinking about the important things is nothing wrong.</li>
</ul>
<li>Agile was a response to BDUF, but not DUF. </li>
<ul>
<li>Far from “design nothing,” the XP strategy is “design always.”</li>
</ul>
<li>Whatever designs have been done up front, they should always be open for change.</li>
<ul>
<li>Let the tests drive the system architecture.</li>
</ul>
</ul>
<h2>
Principles of Modular Design</h2>
</div>
</div>
<div>
<h3>
Cohesion – higher is better</h3>
<div>
<ul>
<li>Cohesion is a measure of how strongly-related or focused the responsibilities of a single module are.</li>
</ul>
</div>
<h3>
Coupling – lower is better</h3>
<div>
<ul>
<li>Coupling or dependency is the degree to which each program module relies on each one of the other modules.</li>
</ul>
<h2>
SOLID Principles</h2>
<div>
<ul>
<li><b>SRP</b>: Single Responsibility Principle</li>
<li><b>OCP</b>: Open Closed Principle</li>
<li><b>LSP</b>: Liskov Substitution Principle</li>
<li><b>ISP</b>: Interface Segregation Principle</li>
<li><b>DIP</b>: Dependency Inversion Principle</li>
</ul>
</div>
</div>
</div>
<div>
(to be continued...)</div>
<div>
<br /></div>
<div>
Adapted from: <a href="http://www.cs.helsinki.fi/u/luontola/tdd-2009/kalvot/09-Object-Oriented-Design.pdf">http://www.cs.helsinki.fi/u/luontola/tdd-2009/kalvot/09-Object-Oriented-Design.pdf</a></div>
<div>
<br /></div>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-1684065832940231192013-03-17T09:34:00.000-04:002013-04-03T05:18:46.308-04:00Most Popular Programming Languages of 2013<div class="visually_embed" data-category="Computers" rel="infographic">
<div style="text-align: center;">
<img alt="Most Popular Programming Languages of 2013" class="visually_embed_infographic" rel="http://thumbnails.visually.netdna-cdn.com/most-popular-programming-languages-of-2013_5113fc6a37abb.jpg" src="http://thumbnails.visually.netdna-cdn.com/most-popular-programming-languages-of-2013_5113fc6a37abb_w587.jpg" />
</div>
<div class="visually_embed_bar">
<div style="text-align: center;">
<span class="visually_embed_cycle"><a href="http://visual.ly/most-popular-programming-languages-2013/?utm_source=visually_embed">Most Popular Programming Languages of 2013 infographic</a> </span>
</div>
</div>
<a href="http://visual.ly/most-popular-programming-languages-2013?utm_source=visually_embed" id="visually_embed_view_more" target="_blank"></a>
<link href="http://visual.ly/embeder/style.css" rel="stylesheet" type="text/css"></link>
<script src="http://visual.ly/embeder/embed.js" type="text/javascript"></script>
</div>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-5382233352500780472013-02-24T09:51:00.000-05:002013-04-03T05:19:08.356-04:00Python computation speed....Once again...Just come across this <a href="http://www.youtube.com/watch?v=H_LHeGP_PtI" target="_blank">video</a> on youtube.<br />
I was amazed by the difference in computation time between C++ and Python. I tried to do the same in my PC and compared with what I can obtain using the numpy library. Below the result.<br />
<br />
<pre style="background: rgba(238,238,238,0.92); color: black;"><span style="color: #0066ff; font-style: italic;">#! /usr/bin/python2.7</span>
<span style="color: blue;">from</span> datetime <span style="color: blue;">import</span> datetime
flips <span style="color: blue;">=</span> <span style="color: #0066ff;">4000000</span>
<span style="font-weight: 700;">def</span> <span style="color: #ff8000;">regularPython</span>():
<span style="color: blue;">import</span> random
heads <span style="color: blue;">=</span> <span style="color: #0066ff;">0</span>
<span style="color: blue;">for</span> i <span style="color: blue;">in</span> <span style="color: #3333ff; font-weight: 700;">range</span>(flips):
<span style="color: blue;">if</span> random.randint(<span style="color: #0066ff;">0</span>,<span style="color: #0066ff;">1</span>) <span style="color: blue;">==</span> <span style="color: #0066ff;">0</span>:
heads<span style="color: blue;">+=</span><span style="color: #0066ff;">1</span>
tails <span style="color: blue;">=</span> flips<span style="color: blue;">-</span>heads
<span style="color: blue;">print</span> <span style="color: #009933;">'heads='</span>,heads
<span style="color: blue;">print</span> <span style="color: #009933;">'tails='</span>,tails
<span style="font-weight: 700;">def</span> <span style="color: #ff8000;">numpyVersion1</span>():
<span style="color: blue;">import</span> numpy <span style="color: blue;">as</span> np
heads <span style="color: blue;">=</span> <span style="color: #0066ff;">0</span>
<span style="color: blue;">for</span> i <span style="color: blue;">in</span> <span style="color: #3333ff; font-weight: 700;">range</span>(flips):
<span style="color: blue;">if</span> np.random.random_integers(<span style="color: #0066ff;">0</span>,<span style="color: #0066ff;">1</span>) <span style="color: blue;">==</span> <span style="color: #0066ff;">0</span>:
heads<span style="color: blue;">+=</span><span style="color: #0066ff;">1</span>
tails <span style="color: blue;">=</span> flips<span style="color: blue;">-</span>heads
<span style="color: blue;">print</span> <span style="color: #009933;">'heads='</span>,heads
<span style="color: blue;">print</span> <span style="color: #009933;">'tails='</span>,tails
<span style="font-weight: 700;">def</span> <span style="color: #ff8000;">numpyVersion2</span>():
<span style="color: blue;">import</span> numpy <span style="color: blue;">as</span> np
randomArray <span style="color: blue;">=</span> np.random.random_integers(<span style="color: #0066ff;">0</span>,<span style="color: #0066ff;">1</span>,flips)
heads <span style="color: blue;">=</span> <span style="color: #3333ff; font-weight: 700;">len</span>(randomArray[randomArray<span style="color: blue;">==</span><span style="color: #0066ff;">0</span>])
tails <span style="color: blue;">=</span> flips<span style="color: blue;">-</span>heads
<span style="color: blue;">print</span> <span style="color: #009933;">'heads='</span>,heads
<span style="color: blue;">print</span> <span style="color: #009933;">'tails='</span>,tails
<span style="color: blue;">if</span> <span style="color: #3333ff; font-weight: 700;">__name__</span><span style="color: blue;">==</span><span style="color: #009933;">"__main__"</span>:
t_start <span style="color: blue;">=</span> datetime.now()
regularPython()
t_end <span style="color: blue;">=</span> datetime.now()
<span style="color: blue;">print</span> <span style="color: #009933;">"Total time: "</span>, (t_end<span style="color: blue;">-</span>t_start)
t_start <span style="color: blue;">=</span> datetime.now()
numpyVersion1()
t_end <span style="color: blue;">=</span> datetime.now()
<span style="color: blue;">print</span> <span style="color: #009933;">"Total time: "</span>, (t_end<span style="color: blue;">-</span>t_start)
numpyVersion2()
t_end <span style="color: blue;">=</span> datetime.now()
<span style="color: blue;">print</span> <span style="color: #009933;">"Total time: "</span>, (t_end<span style="color: blue;">-</span>t_start)
</pre>
<br />
Here the output:<br />
<br />
<pre><span style="font-family: Courier New, Courier, monospace;">heads= 2001161
tails= 1998839
Total time: <b>0:00:06.139672</b>
heads= 1999621
tails= 2000379
Total time: <b>0:00:01.597668</b>
heads= 1999881
tails= 2000119
Total time: <b>0:00:01.707101</b></span><b>
</b></pre>
<br />
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-26522744832699429292012-12-18T08:49:00.001-05:002013-01-26T10:26:56.946-05:00Python vs C++ PerformanceJust for curiosity, I wanted to see how fast is C++ compared with regular Python and Python using the numpy library. Below the results:<br />
<br />
A bit of Python code with regular python, same loop using numpy, and numpy vectorial (like in MatLab):<br />
<br />
<pre style="background: #f6f8ff; color: #000020;"><span style="color: #595979;">'''</span>
<span style="color: #595979;">Created on Nov 19, 2012</span>
<span style="color: #595979;"></span>
<span style="color: #595979;">@author: leal</span>
<span style="color: #595979;">'''</span>
<span style="color: #200080; font-weight: bold;">import</span> numpy as np
<span style="color: #200080; font-weight: bold;">from</span> datetime <span style="color: #200080; font-weight: bold;">import</span> datetime
</pre>
<pre style="background: #f6f8ff; color: #000020;"><pre>nSpectra <span style="color: #308080;">=</span> <span style="color: #008c00;">12</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">32</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">256</span>
nBinsTotal <span style="color: #308080;">=</span> nSpectra <span style="color: #308080;">*</span> <span style="color: #008c00;">512</span><span style="color: #308080;">;</span>
</pre>
<span style="color: #200080; font-weight: bold;">def</span> testSpectraIN5<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #308080;">:</span>
total <span style="color: #308080;">=</span> <span style="color: #308080;">[</span><span style="color: #308080;">]</span>
<span style="color: #200080; font-weight: bold;">for</span> i <span style="color: #200080; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #308080;">(</span>nBinsTotal<span style="color: #308080;">)</span><span style="color: #308080;">:</span>
res <span style="color: #308080;">=</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">+</span> i
total<span style="color: #308080;">.</span>append<span style="color: #308080;">(</span>res<span style="color: #308080;">)</span>
<span style="color: #200080; font-weight: bold;">def</span> testSpectraIN5np<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #308080;">:</span>
total <span style="color: #308080;">=</span> np<span style="color: #308080;">.</span>empty<span style="color: #308080;">(</span>nBinsTotal<span style="color: #308080;">,</span> dtype<span style="color: #308080;">=</span><span style="color: #e34adc;">float</span><span style="color: #308080;">)</span>
<span style="color: #200080; font-weight: bold;">for</span> i <span style="color: #200080; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #308080;">(</span>nBinsTotal<span style="color: #308080;">)</span><span style="color: #308080;">:</span>
res <span style="color: #308080;">=</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">+</span> i
total<span style="color: #308080;">[</span>i<span style="color: #308080;">]</span><span style="color: #308080;">=</span>res
<span style="color: #200080; font-weight: bold;">def</span> testSpectraIN5npOpt<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #308080;">:</span>
<span style="color: #595979;"># res = [0..nBinsTotal-1]</span>
res <span style="color: #308080;">=</span> np<span style="color: #308080;">.</span>arange<span style="color: #308080;">(</span>nBinsTotal<span style="color: #308080;">,</span> dtype<span style="color: #308080;">=</span><span style="color: #e34adc;">float</span><span style="color: #308080;">)</span>
total <span style="color: #308080;">=</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">+</span> res
<span style="color: #200080; font-weight: bold;">if</span> <span style="color: #e34adc;">__name__</span> <span style="color: #308080;">=</span><span style="color: #308080;">=</span> <span style="color: #1060b6;">'__main__'</span><span style="color: #308080;">:</span>
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"Main has started!"</span>
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"* testSpectraIN5"</span>
t_start <span style="color: #308080;">=</span> datetime<span style="color: #308080;">.</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
testSpectraIN5<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
t_end <span style="color: #308080;">=</span> datetime<span style="color: #308080;">.</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
t_total <span style="color: #308080;">=</span> t_end <span style="color: #308080;">-</span> t_start
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"Total time: "</span><span style="color: #308080;">,</span> t_total<span style="color: #308080;">,</span> <span style="color: #1060b6;">" seconds"</span>
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"* testSpectraIN5np"</span>
t_start <span style="color: #308080;">=</span> datetime<span style="color: #308080;">.</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
testSpectraIN5np<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
t_end <span style="color: #308080;">=</span> datetime<span style="color: #308080;">.</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
t_total <span style="color: #308080;">=</span> t_end <span style="color: #308080;">-</span> t_start
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"Total time: "</span><span style="color: #308080;">,</span> t_total<span style="color: #308080;">,</span> <span style="color: #1060b6;">" seconds"</span>
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"* testSpectraIN5npOpt"</span>
t_start <span style="color: #308080;">=</span> datetime<span style="color: #308080;">.</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
testSpectraIN5npOpt<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
t_end <span style="color: #308080;">=</span> datetime<span style="color: #308080;">.</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span>
t_total <span style="color: #308080;">=</span> t_end <span style="color: #308080;">-</span> t_start
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"Total time: "</span><span style="color: #308080;">,</span> t_total<span style="color: #308080;">,</span> <span style="color: #1060b6;">" seconds"</span>
<span style="color: #200080; font-weight: bold;">print</span> <span style="color: #1060b6;">"Main has finished!"</span>
</pre>
<br />
<div>
Python v 2.7.3 results:<br />
<br /></div>
<pre style="background: #f6f8ff; color: #000020;">Main has started<span style="color: #308080;">!</span>
<span style="color: #308080;">*</span> testSpectraIN5
Total time<span style="color: #308080;">:</span> <span style="color: #8745a0;">0:00:12</span><span style="color: #008c00;">.781883</span> seconds
<span style="color: #308080;">*</span> testSpectraIN5np
Total time<span style="color: #308080;">:</span> <span style="color: #8745a0;">0:00:09</span><span style="color: #008c00;">.553932</span> seconds
<span style="color: #308080;">*</span> testSpectraIN5npOpt
Total time<span style="color: #308080;">:</span> <span style="color: #8745a0;">0:00:00</span><span style="color: #008c00;">.823714</span> seconds
Main has finished<span style="color: #308080;">!</span>
</pre>
<br />
For C++, I just did two cycles, one without initialising the vector with its size, and other one with it. Code is below:<br />
<br />
<pre style="background: #f6f8ff; color: #000020;"><span style="color: #595979;">// constructing vectors</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">iostream</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">vector</span><span style="color: maroon;">></span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">valarray</span><span style="color: maroon;">></span>
<span style="color: #595979;">// C++11</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: maroon;"><</span><span style="color: #40015a;">chrono</span><span style="color: maroon;">></span>
<span style="color: #200080; font-weight: bold;">typedef</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>chrono<span style="color: #406080;">::</span>high_resolution_clock Clock<span style="color: #406080;">;</span>
<span style="color: #200080; font-weight: bold;">typedef</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>chrono<span style="color: #406080;">::</span>milliseconds milliseconds<span style="color: #406080;">;</span>
<span style="color: #200080; font-weight: bold;">int</span> <span style="color: #400000;">main</span><span style="color: #308080;">(</span><span style="color: #308080;">)</span> <span style="color: #406080;">{</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">cout</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: maroon;">"</span><span style="color: #1060b6;">Main has started!</span><span style="color: maroon;">"</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>endl<span style="color: #406080;">;</span>
Clock<span style="color: #406080;">::</span>time_point t0 <span style="color: #308080;">=</span> Clock<span style="color: #406080;">::</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #200080; font-weight: bold;">int</span> nSpectra <span style="color: #308080;">=</span> <span style="color: #008c00;">12</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">32</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">256</span><span style="color: #406080;">;</span>
<span style="color: #200080; font-weight: bold;">int</span> nBinsTotal <span style="color: #308080;">=</span> nSpectra <span style="color: #308080;">*</span> <span style="color: #008c00;">512</span><span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">vector</span><span style="color: #406080;"><</span><span style="color: #200080; font-weight: bold;">double</span><span style="color: #406080;">></span> total<span style="color: #406080;">;</span> <span style="color: #595979;">//(nBinsTotal);</span>
<span style="color: #200080; font-weight: bold;">for</span> <span style="color: #308080;">(</span><span style="color: #200080; font-weight: bold;">int</span> i <span style="color: #308080;">=</span> <span style="color: #008c00;">0</span><span style="color: #406080;">;</span> i <span style="color: #308080;"><</span> nBinsTotal<span style="color: #406080;">;</span> i<span style="color: #308080;">+</span><span style="color: #308080;">+</span><span style="color: #308080;">)</span> <span style="color: #406080;">{</span>
<span style="color: #200080; font-weight: bold;">double</span> res <span style="color: #308080;">=</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">+</span> i<span style="color: #406080;">;</span>
total<span style="color: #308080;">.</span>push_back<span style="color: #308080;">(</span>res<span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
Clock<span style="color: #406080;">::</span>time_point t1 <span style="color: #308080;">=</span> Clock<span style="color: #406080;">::</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
milliseconds ms <span style="color: #308080;">=</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>chrono<span style="color: #406080;">::</span>duration_cast<span style="color: #406080;"><</span>milliseconds<span style="color: #406080;">></span><span style="color: #308080;">(</span>t1 <span style="color: #308080;">-</span> t0<span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">cout</span><span style="color: #308080;">.</span>precision<span style="color: #308080;">(</span><span style="color: #008c00;">2</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">cout</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: maroon;">"</span><span style="color: #1060b6;">Total time: </span><span style="color: maroon;">"</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>fixed <span style="color: #308080;"><</span><span style="color: #308080;"><</span> ms<span style="color: #308080;">.</span><span style="color: #003060;">count</span><span style="color: #308080;">(</span><span style="color: #308080;">)</span> <span style="color: #308080;">*</span> <span style="color: green;">1e-3</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: maroon;">"</span><span style="color: #1060b6;"> seconds</span><span style="color: maroon;">"</span><span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>endl<span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">cout</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: maroon;">"</span><span style="color: #1060b6;">Vector with known size:</span><span style="color: maroon;">"</span><span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>endl<span style="color: #406080;">;</span>
t0 <span style="color: #308080;">=</span> Clock<span style="color: #406080;">::</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">vector</span><span style="color: #406080;"><</span><span style="color: #200080; font-weight: bold;">double</span><span style="color: #406080;">></span> total2<span style="color: #308080;">(</span>nBinsTotal<span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #200080; font-weight: bold;">for</span> <span style="color: #308080;">(</span><span style="color: #200080; font-weight: bold;">int</span> i <span style="color: #308080;">=</span> <span style="color: #008c00;">0</span><span style="color: #406080;">;</span> i <span style="color: #308080;"><</span> nBinsTotal<span style="color: #406080;">;</span> i<span style="color: #308080;">+</span><span style="color: #308080;">+</span><span style="color: #308080;">)</span> <span style="color: #406080;">{</span>
<span style="color: #200080; font-weight: bold;">double</span> res <span style="color: #308080;">=</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">+</span> i<span style="color: #406080;">;</span>
total2<span style="color: #308080;">[</span>i<span style="color: #308080;">]</span> <span style="color: #308080;">=</span> res<span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
t1 <span style="color: #308080;">=</span> Clock<span style="color: #406080;">::</span>now<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
ms <span style="color: #308080;">=</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>chrono<span style="color: #406080;">::</span>duration_cast<span style="color: #406080;"><</span>milliseconds<span style="color: #406080;">></span><span style="color: #308080;">(</span>t1 <span style="color: #308080;">-</span> t0<span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">cout</span><span style="color: #308080;">.</span>precision<span style="color: #308080;">(</span><span style="color: #008c00;">2</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">cout</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: maroon;">"</span><span style="color: #1060b6;">Total time: </span><span style="color: maroon;">"</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>fixed <span style="color: #308080;"><</span><span style="color: #308080;"><</span> ms<span style="color: #308080;">.</span><span style="color: #003060;">count</span><span style="color: #308080;">(</span><span style="color: #308080;">)</span> <span style="color: #308080;">*</span> <span style="color: green;">1e-3</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: maroon;">"</span><span style="color: #1060b6;"> seconds</span><span style="color: maroon;">"</span><span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>endl<span style="color: #406080;">;</span>
<span style="color: #0066ee;">std</span><span style="color: #406080;">::</span><span style="color: #003060;">cout</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: maroon;">"</span><span style="color: #1060b6;">Main has finished!</span><span style="color: maroon;">"</span> <span style="color: #308080;"><</span><span style="color: #308080;"><</span> <span style="color: #0066ee;">std</span><span style="color: #406080;">::</span>endl<span style="color: #406080;">;</span>
<span style="color: #200080; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
</pre>
<br />
The result is:<br />
<br />
<pre style="background: #f6f8ff; color: #000020;">Main has started<span style="color: #308080;">!</span>
Total time<span style="color: #308080;">:</span> <span style="color: #008c00;">1.70</span> seconds
Vector with known size<span style="color: #308080;">:</span>
Total time<span style="color: #308080;">:</span> <span style="color: #008c00;">0.74</span> seconds
Main has finished<span style="color: #308080;">!</span>
</pre>
<br />
It's interesting to see how the programming approach changes the results!<br />
Surprisingly, Python, using a vectorial numpy approach, can be as fast as C++. It's not a surprise however, that an initlised array in C++ can be twice faster!<br />
<br />
<h3>
EDIT:</h3>
What about java???<br />
So I tried first with ArrayList and Vector but they were so slow (more than 30 seconds), that I didn't even dare to add them below... I came up with this code, but doesn't really compare to what I did in C++.<br />
<br />
<pre style="background: #f6f8ff; color: #000020;"><span style="color: #200080; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #308080;">.</span><span style="color: #004a43;">util</span><span style="color: #308080;">.</span><span style="color: #004a43;">Vector</span><span style="color: #406080;">;</span>
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">class</span> VectorTest <span style="color: #406080;">{</span>
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">static</span> <span style="color: #7779bb;">void</span> main<span style="color: #308080;">(</span><span style="color: #6679aa; font-weight: bold;">String</span><span style="color: #308080;">[</span><span style="color: #308080;">]</span> args<span style="color: #308080;">)</span> <span style="color: #406080;">{</span>
<span style="color: #7779bb;">int</span> nSpectra <span style="color: #308080;">=</span> <span style="color: #008c00;">12</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">32</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">256</span><span style="color: #406080;">;</span>
<span style="color: #7779bb;">int</span> nBinsTotal <span style="color: #308080;">=</span> nSpectra <span style="color: #308080;">*</span> <span style="color: #008c00;">512</span><span style="color: #406080;">;</span>
<span style="color: #7779bb;">double</span><span style="color: #308080;">[</span><span style="color: #308080;">]</span> total2 <span style="color: #308080;">=</span> <span style="color: #200080; font-weight: bold;">new</span> <span style="color: #7779bb;">double</span><span style="color: #308080;">[</span>nBinsTotal<span style="color: #308080;">]</span><span style="color: #406080;">;</span>
<span style="color: #7779bb;">long </span>startTime <span style="color: #308080;">=</span> <span style="color: #6679aa; font-weight: bold;">System</span><span style="color: #308080;">.</span>nanoTime<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span></pre>
<pre style="background: #f6f8ff; color: #000020;"> <span style="color: #595979;">// <E> Element type of Vector e.g. String, Integer, Object ...</span>
<span style="color: #200080; font-weight: bold;">for</span> <span style="color: #308080;">(</span><span style="color: #7779bb;">int</span> i <span style="color: #308080;">=</span> <span style="color: #008c00;">0</span><span style="color: #406080;">;</span> i <span style="color: #308080;"><</span> nBinsTotal<span style="color: #406080;">;</span> i<span style="color: #308080;">+</span><span style="color: #308080;">+</span><span style="color: #308080;">)</span> <span style="color: #406080;">{</span>
<span style="color: #7779bb;">double</span> res <span style="color: #308080;">=</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">*</span> <span style="color: #008c00;">500</span> <span style="color: #308080;">+</span> i<span style="color: #406080;">;</span>
total2<span style="color: #308080;">[</span>i<span style="color: #308080;">]</span> <span style="color: #308080;">=</span> res<span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
<span style="color: #7779bb;">long </span>endTime <span style="color: #308080;">=</span> <span style="color: #6679aa; font-weight: bold;">System</span><span style="color: #308080;">.</span>nanoTime<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span></pre>
<pre style="background: #f6f8ff; color: #000020;"> <span style="color: #7779bb;">long </span>duration <span style="color: #308080;">=</span> endTime <span style="color: #308080;">-</span> startTime<span style="color: #406080;">;</span></pre>
<pre style="background: #f6f8ff; color: #000020;"> <span style="color: #6679aa; font-weight: bold;">System</span><span style="color: #308080;">.</span>out<span style="color: #308080;">.</span>println<span style="color: #308080;">(</span><span style="color: #1060b6;">"Total time: "</span> <span style="color: #308080;">+</span> duration <span style="color: #308080;">*</span> <span style="color: green;">1e-9</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span>
<span style="color: #406080;">}</span>
<span style="color: #406080;">}</span>
</pre>
<br />
<pre style="background: #f6f8ff; color: #000020;">Start calculation with arrays and initilisation<span style="color: #308080;">.</span>
Total time<span style="color: #308080;">:</span> <span style="color: #008c00;">0.073217508</span>
</pre>
<br />
Using regular arrays, Java can be as fast as C++ Vectors with predefined length.<br />
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0Grenoble, France45.188529 5.72452399999997445.098995 5.563162499999974 45.278063 5.8858854999999739tag:blogger.com,1999:blog-3183800036537904953.post-35582735207619006942012-12-14T12:27:00.003-05:002012-12-16T04:40:09.385-05:00The Future of MobileSo I'm exploring possible software solutions, in the form of a pilot project, to treat and analyse neutron data.<br />
Although, for now, no decisions have been taken, I do feel that we have to provide (only?) a web interface, in the form of a rich HTML5 internet application.<br />
<br />
There's not many (if any) scientific internet applications: Well, something with 3D and 2D plotting facilities and a scripting console for math operations (a restricted/safe ipython console being the ideal). Looking forward for suggestions :)<br />
<br />
Coming back to the main subject: tablets and smartphones market will explode in the coming years:
<br />
<br />
<img alt="Connected Devices Growth" src="http://static1.businessinsider.com/image/4f6b732f69bedd4a59000000-650/connected-devices-growth.jpg" /><br />
<br />
There's a great presentation about this topic available on the Business Insider web site:<br />
<a href="http://www.businessinsider.com/the-future-of-mobile-deck-2012-3?op=1" style="background-color: transparent;">http://www.businessinsider.com/the-future-of-mobile-deck-2012-3?op=1</a>
<br />
<br />
It's interesting to see in this presentation that app developers still prefer iOS to Android! Even worse they might be loosing interest in Android!
<br />
And I though Java was easier and more intuitive than ObjectC !?!?!?!
<br />
Android version fragmentation appears to be the source of the problems..<br />
<br />
For the capitalists out there, the cross-platform revenue is 3 times higher with iOS than with Android: `"iOS is where the money is"!<br />
<br />
I do believe that we will all be using a browser and HTML5 in future. Well, let's see!<br />
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0Grenoble, France45.188529 5.72452445.1437655 5.64556 45.233292500000005 5.803488tag:blogger.com,1999:blog-3183800036537904953.post-15525272250915222012-12-08T07:20:00.000-05:002012-12-08T07:20:13.981-05:00New articleMy last publication while working at the <a href="http://www.esrf.fr/" target="_blank">ESRF </a>is finally out:<br />
<br />
<table class="tocs_entry_table" style="color: black; text-align: start;"><tbody>
<tr><td align="center" class="tocs_entry_td" style="padding-right: 12px;" width="110"><a href="http://journals.iucr.org/s/issues/2013/01/00/xh5035/xh5035bdy.html" style="color: #660099; text-decoration: initial;"><img alt="thumbnail of article figure" border="0" class="tocs_thumbnail" src="http://journals.iucr.org/s/issues/2013/01/00/xh5035/xh5035fig1_thumbnail.gif" style="-webkit-box-shadow: rgb(128, 128, 128) 1px 4px 9px; box-shadow: rgb(128, 128, 128) 1px 4px 9px;" /></a></td><td class="tocs_entry_td" style="padding-right: 12px;"><div class="tocs_entry_div">
<a href="" name="xh5035"></a><a href="http://journals.iucr.org/s/issues/2013/01/00/xh5035/index.html" style="color: #660099; text-decoration: initial;" target="_parent"><img alt="[HTML version]" border="0" src="http://journals.iucr.org/s/graphics/htmlborder.gif" /></a><a href="http://journals.iucr.org/s/issues/2013/01/00/xh5035/xh5035.pdf" style="color: #660099; text-decoration: initial;"><img alt="[PDF version]" border="0" src="http://journals.iucr.org/s/graphics/pdfborder.gif" /></a><a href="http://journals.iucr.org/s/issues/2013/01/00/xh5035/xh5035sup1.pdf" style="color: #660099; text-decoration: initial;"><img alt="[Supplementary Material]" border="0" src="http://journals.iucr.org/s/graphics/supplementarymaterialsborder.gif" /></a> <a href="http://scripts.iucr.org/cgi-bin/paper?xh5035&buy=1" style="color: #660099; text-decoration: initial;"><img alt="[Buy article online]" border="0" src="http://journals.iucr.org/logos/buy.gif" /></a><br />
<i>J. Synchrotron Rad.</i> (2013). <b>20</b> [ <span style="font-size: x-small;"><a href="http://dx.doi.org/10.1107/S0909049512049114" style="color: #660099; text-decoration: initial;">doi:10.1107/S0909049512049114</a></span> ]<br />
<h3>
A survey of global radiation damage to 15 different protein crystal types at room temperature: a new decay model</h3>
<h3>
<a href="http://scripts.iucr.org/cgi-bin/citedin?search_on=name&author_name=Ferraz%20Leal%2C%20R%2EM%2E" style="color: #660099; text-decoration: initial;">R. M. Ferraz Leal</a>, <a href="http://scripts.iucr.org/cgi-bin/citedin?search_on=name&author_name=Burenkov%2C%20G%2E" style="color: #660099; text-decoration: initial;">G. Burenkov</a>, <a href="http://scripts.iucr.org/cgi-bin/citedin?search_on=name&author_name=Russi%2C%20S%2E" style="color: #660099; text-decoration: initial;">S. Russi</a> and <a href="http://scripts.iucr.org/cgi-bin/citedin?search_on=name&author_name=Popov%2C%20A%2EN%2E" style="color: #660099; text-decoration: initial;">A. N. Popov</a></h3>
<b>Synopsis: </b>A systematic study of the sensitivity to radiation damage of crystals held at room temperature for a large set of model macromolecular structures is presented.<br />
Online 6 December 2012<br />
<br /></div>
</td></tr>
</tbody></table>
<br />
<br />
<div style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px;">
<b>Abstract:</b> The radiation damage rates to crystals of 15 model macromolecular structures were studied using an automated radiation sensitivity characterization procedure. The diffracted intensity variation with dose is described by a two-parameter model. This model includes a strong resolution-independent decay specific to room-temperature measurements along with a linear increase in overall Debye-Waller factors. An equivalent representation of sensitivity <span class="it"><i>via</i></span> a single parameter, normalized half-dose, is introduced. This parameter varies by an order of magnitude between the different structures studied. The data show a correlation of crystal radiation sensitivity with crystal solvent content but no dose-rate dependency was detected in the range 0.05-300 kGy s<span class="sup"><sup>-1</sup></span>. The results of the crystal characterization are suitable for either optimal planning of room-temperature data collection or <span class="it"><i>in situ</i></span> crystallization plate screening experiments.</div>
<div style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px;">
<b>Keywords: <a href="http://scripts.iucr.org/cgi-bin/full_search?words=radiation%20damage&Action=Search" style="color: #000066; text-decoration: initial;">radiation damage</a>; <a href="http://scripts.iucr.org/cgi-bin/full_search?words=room%20temperature&Action=Search" style="color: #000066; text-decoration: initial;">room temperature</a>; <a href="http://scripts.iucr.org/cgi-bin/full_search?words=X-ray%20diffraction&Action=Search" style="color: #000066; text-decoration: initial;">X-ray diffraction</a>; <a href="http://scripts.iucr.org/cgi-bin/full_search?words=data%20collection&Action=Search" style="color: #000066; text-decoration: initial;">data collection</a>.</b></div>
<form action="http://scripts.iucr.org/cgi-bin/biblio" method="post" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px;">
<table bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0" style="width: 100%px;"><tbody>
<tr></tr>
</tbody></table>
</form>
<br />
<br />riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0tag:blogger.com,1999:blog-3183800036537904953.post-53194672439692511782012-12-04T14:55:00.000-05:002012-12-08T07:21:27.399-05:00QuoteI usually don't like to share other people phrases, but this one is to good to keep just for me:<br />
<br />
<blockquote class="tr_bq">
<blockquote class="tr_bq">
<i>C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, you blow away your whole leg!</i></blockquote>
<blockquote class="tr_bq">
— Bjarne Stroustrup</blockquote>
</blockquote>
riclealhttp://www.blogger.com/profile/05198753753628311378noreply@blogger.com0