1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Dynamic interval</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Icl"> 8<link rel="up" href="../examples.html" title="Examples"> 9<link rel="prev" href="interval.html" title="Interval"> 10<link rel="next" href="static_interval.html" title="Static interval"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="interval.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="static_interval.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_icl.examples.dynamic_interval"></a><a class="link" href="dynamic_interval.html" title="Dynamic interval">Dynamic interval</a> 28</h3></div></div></div> 29<p> 30</p> 31<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 32<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 33<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">math</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span> 34<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">is_same</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 35 36<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">icl</span><span class="special">/</span><span class="identifier">interval_set</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 37<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">icl</span><span class="special">/</span><span class="identifier">split_interval_set</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 38<span class="comment">// Dynamically bounded intervals 'discrete_interval' and 'continuous_interval'</span> 39<span class="comment">// are indirectly included via interval containers as library defaults.</span> 40<span class="preprocessor">#include</span> <span class="string">"../toytime.hpp"</span> 41<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">icl</span><span class="special">/</span><span class="identifier">rational</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 42 43<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span> 44<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span> 45<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">icl</span><span class="special">;</span> 46 47<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 48<span class="special">{</span> 49 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">">>Interval Container Library: Sample interval.cpp <<\n"</span><span class="special">;</span> 50 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"----------------------------------------------------\n"</span><span class="special">;</span> 51 52 <span class="comment">// Dynamically bounded intervals are the library default for </span> 53 <span class="comment">// interval parameters in interval containers.</span> 54 <span class="identifier">BOOST_STATIC_ASSERT</span><span class="special">((</span> 55 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special"><</span> <span class="identifier">interval_set</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">interval_type</span> 56 <span class="special">,</span> <span class="identifier">discrete_interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">>::</span><span class="identifier">value</span> 57 <span class="special">));</span> 58 59 60 <span class="identifier">BOOST_STATIC_ASSERT</span><span class="special">((</span> 61 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special"><</span> <span class="identifier">interval_set</span><span class="special"><</span><span class="keyword">float</span><span class="special">>::</span><span class="identifier">interval_type</span> 62 <span class="special">,</span> <span class="identifier">continuous_interval</span><span class="special"><</span><span class="keyword">float</span><span class="special">></span> <span class="special">>::</span><span class="identifier">value</span> 63 <span class="special">));</span> 64 65 <span class="comment">// As we can see the library default chooses the appropriate</span> 66 <span class="comment">// class template instance discrete_interval<T> or continuous_interval<T></span> 67 <span class="comment">// dependent on the domain_type T. The library default for intervals</span> 68 <span class="comment">// is also available via the template 'interval':</span> 69 <span class="identifier">BOOST_STATIC_ASSERT</span><span class="special">((</span> 70 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special"><</span> <span class="identifier">interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">type</span> 71 <span class="special">,</span> <span class="identifier">discrete_interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">>::</span><span class="identifier">value</span> 72 <span class="special">));</span> 73 74 <span class="identifier">BOOST_STATIC_ASSERT</span><span class="special">((</span> 75 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special"><</span> <span class="identifier">interval</span><span class="special"><</span><span class="keyword">float</span><span class="special">>::</span><span class="identifier">type</span> 76 <span class="special">,</span> <span class="identifier">continuous_interval</span><span class="special"><</span><span class="keyword">float</span><span class="special">></span> <span class="special">>::</span><span class="identifier">value</span> 77 <span class="special">));</span> 78 79 <span class="comment">// template interval also provides static functions for the four border types</span> 80 81 <span class="identifier">interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">int_interval</span> <span class="special">=</span> <span class="identifier">interval</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">closed</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="number">7</span><span class="special">);</span> 82 <span class="identifier">interval</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">sqrt_interval</span> <span class="special">=</span> <span class="identifier">interval</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">1</span><span class="special">/</span><span class="identifier">sqrt</span><span class="special">(</span><span class="number">2.0</span><span class="special">),</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="number">2.0</span><span class="special">));</span> 83 <span class="identifier">interval</span><span class="special"><</span><span class="identifier">string</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">city_interval</span> <span class="special">=</span> <span class="identifier">interval</span><span class="special"><</span><span class="identifier">string</span><span class="special">>::</span><span class="identifier">left_open</span><span class="special">(</span><span class="string">"Barcelona"</span><span class="special">,</span> <span class="string">"Boston"</span><span class="special">);</span> 84 <span class="identifier">interval</span><span class="special"><</span><span class="identifier">Time</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">time_interval</span> <span class="special">=</span> <span class="identifier">interval</span><span class="special"><</span><span class="identifier">Time</span><span class="special">>::</span><span class="identifier">open</span><span class="special">(</span><span class="identifier">Time</span><span class="special">(</span><span class="identifier">monday</span><span class="special">,</span><span class="number">8</span><span class="special">,</span><span class="number">30</span><span class="special">),</span> <span class="identifier">Time</span><span class="special">(</span><span class="identifier">monday</span><span class="special">,</span><span class="number">17</span><span class="special">,</span><span class="number">20</span><span class="special">));</span> 85 86 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"----- Dynamically bounded intervals ----------------------------------------\n"</span><span class="special">;</span> 87 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">" discrete_interval<int> : "</span> <span class="special"><<</span> <span class="identifier">int_interval</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 88 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"continuous_interval<double>: "</span> <span class="special"><<</span> <span class="identifier">sqrt_interval</span> <span class="special"><<</span> <span class="string">" does "</span> 89 <span class="special"><<</span> <span class="identifier">string</span><span class="special">(</span><span class="identifier">contains</span><span class="special">(</span><span class="identifier">sqrt_interval</span><span class="special">,</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="number">2.0</span><span class="special">))?</span><span class="string">""</span><span class="special">:</span><span class="string">"NOT"</span><span class="special">)</span> 90 <span class="special"><<</span> <span class="string">" contain sqrt(2)"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 91 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"continuous_interval<string>: "</span> <span class="special"><<</span> <span class="identifier">city_interval</span> <span class="special"><<</span> <span class="string">" does "</span> 92 <span class="special"><<</span> <span class="identifier">string</span><span class="special">(</span><span class="identifier">contains</span><span class="special">(</span><span class="identifier">city_interval</span><span class="special">,</span><span class="string">"Barcelona"</span><span class="special">)?</span><span class="string">""</span><span class="special">:</span><span class="string">"NOT"</span><span class="special">)</span> 93 <span class="special"><<</span> <span class="string">" contain 'Barcelona'"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 94 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"continuous_interval<string>: "</span> <span class="special"><<</span> <span class="identifier">city_interval</span> <span class="special"><<</span> <span class="string">" does "</span> 95 <span class="special"><<</span> <span class="identifier">string</span><span class="special">(</span><span class="identifier">contains</span><span class="special">(</span><span class="identifier">city_interval</span><span class="special">,</span> <span class="string">"Berlin"</span><span class="special">)?</span><span class="string">""</span><span class="special">:</span><span class="string">"NOT"</span><span class="special">)</span> 96 <span class="special"><<</span> <span class="string">" contain 'Berlin'"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 97 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">" discrete_interval<Time> : "</span> <span class="special"><<</span> <span class="identifier">time_interval</span> <span class="special"><<</span> <span class="string">"\n\n"</span><span class="special">;</span> 98 99 <span class="comment">// Using dynamically bounded intervals allows to apply operations</span> 100 <span class="comment">// with intervals and also with elements on all interval containers </span> 101 <span class="comment">// including interval containers of continuous domain types:</span> 102 103 <span class="identifier">interval</span><span class="special"><</span><span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">>::</span><span class="identifier">type</span> <span class="identifier">unit_interval</span> 104 <span class="special">=</span> <span class="identifier">interval</span><span class="special"><</span><span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">>::</span><span class="identifier">right_open</span><span class="special">(</span><span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="number">0</span><span class="special">),</span> <span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="number">1</span><span class="special">));</span> 105 <span class="identifier">interval_set</span><span class="special"><</span><span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">></span> <span class="identifier">unit_set</span><span class="special">(</span><span class="identifier">unit_interval</span><span class="special">);</span> 106 <span class="identifier">interval_set</span><span class="special"><</span><span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">></span> <span class="identifier">ratio_set</span><span class="special">(</span><span class="identifier">unit_set</span><span class="special">);</span> 107 <span class="identifier">ratio_set</span> <span class="special">-=</span> <span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="number">1</span><span class="special">,</span><span class="number">3</span><span class="special">);</span> <span class="comment">// Subtract 1/3 from the set</span> 108 109 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"----- Manipulation of single values in continuous sets ---------------------\n"</span><span class="special">;</span> 110 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"1/3 subtracted from [0..1) : "</span> <span class="special"><<</span> <span class="identifier">ratio_set</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 111 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"The set does "</span> <span class="special"><<</span> <span class="identifier">string</span><span class="special">(</span><span class="identifier">contains</span><span class="special">(</span><span class="identifier">ratio_set</span><span class="special">,</span> <span class="identifier">rational</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="number">1</span><span class="special">,</span><span class="number">3</span><span class="special">))?</span><span class="string">""</span><span class="special">:</span><span class="string">"NOT"</span><span class="special">)</span> 112 <span class="special"><<</span> <span class="string">" contain '1/3'"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 113 <span class="identifier">ratio_set</span> <span class="special">^=</span> <span class="identifier">unit_set</span><span class="special">;</span> 114 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Flipping the holey set : "</span> <span class="special"><<</span> <span class="identifier">ratio_set</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 115 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"yields the subtracted : 1/3\n\n"</span><span class="special">;</span> 116 117 <span class="comment">// Of course we can use interval types that are different from the</span> 118 <span class="comment">// library default by explicit instantiation:</span> 119 <span class="identifier">split_interval_set</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special">,</span> <span class="identifier">closed_interval</span><span class="special"><</span><span class="identifier">Time</span><span class="special">></span> <span class="special">></span> <span class="identifier">intuitive_times</span><span class="special">;</span> 120 <span class="comment">// Interval set 'intuitive_times' uses statically bounded closed intervals</span> 121 <span class="identifier">intuitive_times</span> <span class="special">+=</span> <span class="identifier">closed_interval</span><span class="special"><</span><span class="identifier">Time</span><span class="special">>(</span><span class="identifier">Time</span><span class="special">(</span><span class="identifier">monday</span><span class="special">,</span> <span class="number">9</span><span class="special">,</span><span class="number">00</span><span class="special">),</span> <span class="identifier">Time</span><span class="special">(</span><span class="identifier">monday</span><span class="special">,</span> <span class="number">10</span><span class="special">,</span><span class="number">59</span><span class="special">));</span> 122 <span class="identifier">intuitive_times</span> <span class="special">+=</span> <span class="identifier">closed_interval</span><span class="special"><</span><span class="identifier">Time</span><span class="special">>(</span><span class="identifier">Time</span><span class="special">(</span><span class="identifier">monday</span><span class="special">,</span> <span class="number">10</span><span class="special">,</span><span class="number">00</span><span class="special">),</span> <span class="identifier">Time</span><span class="special">(</span><span class="identifier">monday</span><span class="special">,</span> <span class="number">11</span><span class="special">,</span><span class="number">59</span><span class="special">));</span> 123 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"----- Here we are NOT using the library default for intervals --------------\n"</span><span class="special">;</span> 124 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">intuitive_times</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 125 126 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 127<span class="special">}</span> 128 129<span class="comment">// Program output:</span> 130<span class="comment">//>>Interval Container Library: Sample interval.cpp <<</span> 131<span class="comment">//----------------------------------------------------</span> 132<span class="comment">//----- Dynamically bounded intervals ----------------------------------------</span> 133<span class="comment">// discrete_interval<int> : [3,7]</span> 134<span class="comment">//continuous_interval<double>: [0.707107,1.41421) does NOT contain sqrt(2)</span> 135<span class="comment">//continuous_interval<string>: (Barcelona,Boston] does NOT contain 'Barcelona'</span> 136<span class="comment">//continuous_interval<string>: (Barcelona,Boston] does contain 'Berlin'</span> 137<span class="comment">// discrete_interval<Time> : (mon:08:30,mon:17:20)</span> 138<span class="comment">//</span> 139<span class="comment">//----- Manipulation of single values in continuous sets ---------------------</span> 140<span class="comment">//1/3 subtracted from [0..1) : {[0/1,1/3)(1/3,1/1)}</span> 141<span class="comment">//The set does NOT contain '1/3'</span> 142<span class="comment">//Flipping the holey set : {[1/3,1/3]}</span> 143<span class="comment">//yields the subtracted : 1/3</span> 144<span class="comment">//</span> 145<span class="comment">//----- Here we are NOT using the library default for intervals --------------</span> 146<span class="comment">//{[mon:09:00,mon:09:59][mon:10:00,mon:10:59][mon:11:00,mon:11:59]}</span> 147</pre> 148<p> 149 </p> 150</div> 151<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 152<td align="left"></td> 153<td align="right"><div class="copyright-footer">Copyright © 2007-2010 Joachim 154 Faulhaber<br>Copyright © 1999-2006 Cortex Software 155 GmbH<p> 156 Distributed under the Boost Software License, Version 1.0. (See accompanying 157 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 158 </p> 159</div></td> 160</tr></table> 161<hr> 162<div class="spirit-nav"> 163<a accesskey="p" href="interval.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="static_interval.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 164</div> 165</body> 166</html> 167