<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Freya.no &#187; template</title>
	<atom:link href="http://wp.freya.no/tag/template/feed/" rel="self" type="application/rss+xml" />
	<link>http://wp.freya.no</link>
	<description>Knowledge is power</description>
	<lastBuildDate>Mon, 06 Feb 2012 19:56:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>STL container sort predicate with templates</title>
		<link>http://wp.freya.no/2010/10/stl-container-sort-predicate-with-templates/</link>
		<comments>http://wp.freya.no/2010/10/stl-container-sort-predicate-with-templates/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 14:16:18 +0000</pubDate>
		<dc:creator>kent</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[predicate]]></category>
		<category><![CDATA[std]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://wp.freya.no/?p=873</guid>
		<description><![CDATA[This is a reminder to myself. To sort a collection of non-POD types (not Plain Old Data) with STL we need to tell it how to sort these objects. To the std::sort method we simply supply a predicate! It&#8217;s just a method taking 2 arguments of that type and apply our logic to tell if [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>This is a reminder to myself.</p>
<p>To sort a collection of non-POD types (not Plain Old Data) with STL we need to tell it how to sort these objects.</p>
<p>To the std::sort method we simply supply a predicate! It&#8217;s just a method taking 2 arguments of that type and apply our logic to tell if the first argument is lesser than the last argument. With templates it becomes a little more complex, but still it&#8217;s very simple when you know how it works.</p>
<p>Here is some sample code:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;     // Provides cout
#include &lt;vector&gt;       // Provides our basic container
#include &lt;algorithm&gt;    // Provides std::sort

using namespace std;

// Structures I'll sort with 1 template function
//
//
struct A
{
    A(size_t i, size_t t) : id(i), time(t), other_data(false){}
    size_t  id;
    size_t  time;
    bool    other_data;
};

struct B
{
    B(size_t i, size_t t) : id(i), time(t), other_data(&quot;string data&quot;){}
    size_t  id;
    size_t  time;
    string  other_data;
};

struct C
{
    C(size_t i, size_t t) : id(i), time(t), other_data(&quot;character array data&quot;){}
    size_t  id;
    size_t  time;
    char*   other_data;
};

// Our predicate
//
template&lt;typename CLASS&gt;
bool timeSortPredicate( const CLASS &amp;a, const CLASS &amp;b )
{
    return a.time &lt; b.time;
}

template&lt;typename CLASS&gt;
bool idSortPredicate( const CLASS &amp;a, const CLASS &amp;b )
{
    return a.id &lt; b.id;
}

// Our collection printer
//
template&lt;typename CLASS&gt;
void print( const CLASS &amp;c )
{
    typedef typename CLASS::const_iterator cit;

    for ( cit it = c.begin(); it != c.end(); ++it )
    {
        cout &lt;&lt; it-&gt;id &lt;&lt; &quot; &quot; &lt;&lt; it-&gt;time &lt;&lt; endl;
    }
}

int main()
{
    // Construct the objects ... tedious I know
    A a1(1,15), a2(2,20),   a3(3,14);
    B b1(3,4),  b2(2,10),   b3(1,12);
    C c1(11,0), c2(12,0),   c3(13,0);

    // Construct collections
    vector&lt;A&gt; alist;
    alist.push_back(a1);
    alist.push_back(a2);
    alist.push_back(a3);

    vector&lt;B&gt; blist;
    blist.push_back(b1);
    blist.push_back(b2);
    blist.push_back(b3);

    vector&lt;C&gt; clist;
    clist.push_back(c1);
    clist.push_back(c2);
    clist.push_back(c3);

    // Print stuff
    cout &lt;&lt; &quot;Collection A&quot; &lt;&lt; endl;
    print(alist);

    cout &lt;&lt; &quot;Collection B&quot; &lt;&lt; endl;
    print(blist);

    cout &lt;&lt; &quot;Collection C&quot; &lt;&lt; endl;
    print(clist);

    cout &lt;&lt; &quot;Sorting collections by time&quot; &lt;&lt; endl;

    sort(alist.begin(), alist.end(), timeSortPredicate&lt;A&gt; );
    sort(blist.begin(), blist.end(), timeSortPredicate&lt;B&gt; );
    sort(clist.begin(), clist.end(), timeSortPredicate&lt;C&gt; );

    // Print stuff
    cout &lt;&lt; &quot;Collection A&quot; &lt;&lt; endl;
    print(alist);

    cout &lt;&lt; &quot;Collection B&quot; &lt;&lt; endl;
    print(blist);

    cout &lt;&lt; &quot;Collection C&quot; &lt;&lt; endl;
    print(clist);

    cout &lt;&lt; &quot;Sorting collections by id&quot; &lt;&lt; endl;

    sort(alist.begin(), alist.end(), idSortPredicate&lt;A&gt; );
    sort(blist.begin(), blist.end(), idSortPredicate&lt;B&gt; );
    sort(clist.begin(), clist.end(), idSortPredicate&lt;C&gt; );

    // Print stuff
    cout &lt;&lt; &quot;Collection A&quot; &lt;&lt; endl;
    print(alist);

    cout &lt;&lt; &quot;Collection B&quot; &lt;&lt; endl;
    print(blist);

    cout &lt;&lt; &quot;Collection C&quot; &lt;&lt; endl;
    print(clist);

    return 0;
}
</pre>
<p>Compile with:</p>
<pre class="brush: plain; title: ; notranslate">g++ sortpredicate.cpp -o sortpredicate</pre>
<p>The output should be:</p>
<pre class="brush: plain; title: ; notranslate">Collection A
1 15
2 20
3 14
Collection B
3 4
2 10
1 12
Collection C
11 0
12 0
13 0
Sorting collections by time
Collection A
3 14
1 15
2 20
Collection B
3 4
2 10
1 12
Collection C
11 0
12 0
13 0
Sorting collections by id
Collection A
1 15
2 20
3 14
Collection B
1 12
2 10
3 4
Collection C
11 0
12 0
13 0
</pre>
 <img src="http://wp.freya.no/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=873" width="1" height="1" style="display: none;" /><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwp.freya.no%2F2010%2F10%2Fstl-container-sort-predicate-with-templates%2F&amp;title=STL%20container%20sort%20predicate%20with%20templates" id="wpa2a_6"><img src="http://wp.freya.no/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://wp.freya.no/2010/10/stl-container-sort-predicate-with-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class templates in C++</title>
		<link>http://wp.freya.no/c/class-templates-in-c/</link>
		<comments>http://wp.freya.no/c/class-templates-in-c/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 21:58:18 +0000</pubDate>
		<dc:creator>Tatyana</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[point3d]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://wp.freya.no/</guid>
		<description><![CDATA[Templates in C++ are the way functions and classes operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one [wiki]. Note that there is a difference between class template and a template class: class template is a template used to generate template [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Templates in  C++ are the way functions and classes operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one [<a href="http://en.wikipedia.org/wiki/Template_%28programming%29">wiki</a>].</p>
<p>Note that there is a difference between class template and a template class: class template is a template used to generate template classes. You cannot declare an object of a class template. Template class is an instance of a class template<a href="http://scv.bu.edu/computation/bluegene/IBMdocs/compiler/xlc-8.0/html/language/ref/class_templates.htm">.</a></p>
<p>Here we are going to look at 3 main clas templates that are very useful in game and 3D design: Point, Vector and Matrix class templates.</p>
<h3>Point and Point3D:</h3>
<p>Here is how a simple Point template will look like in C++. This template is constructed to support multiple dimensions like Point2D, 3D and so on:</p>
<pre class="brush: cpp; title: ; notranslate">
template &lt;class T, int n&gt; class Point {
protected:
	T mat[n];
	 void cp(const Point&lt;T,n&gt; &amp;p)
    {
		memcpy(mat, p.mat, n*sizeof(T) );
	 }

    void cp(const T p[n])
    {
		memcpy(mat, p, n*sizeof(T) );
	}

    void cp(const T &amp;val)
    {
		for (int i=0; i&lt;n; i++)
			mat[i] = val;
    }

public:
	// new constructor
	Point(const T pt[n])
	{
		cp(pt);
	}

	// copy constructor
	Point(const Point&lt;T,n&gt; &amp;cp)
	{
		this-&gt;cp(cp);
	}

	// default constructor
	Point()
	{
		for (int i=0; i&lt;n; i++)
			mat[i] = 0;
	}
	virtual ~Point(void) {};
};
</pre>
<p>Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. Here vi can add multiple operator overloading functions to our Point class template, for example addition:</p>
<pre class="brush: cpp; title: ; notranslate">
        //Point + some value
	Point&lt;T,n&gt; &amp;operator+=(const T &amp;val)
    {
		for (int i=0; i&lt;n; i++)
			mat[i] += val;

		return (*this);
    }

    //sum of two Points
    Point&lt;T,n&gt; &amp;operator+=(Point&lt;T,n&gt; &amp;pt)
    {
		for (int i=0; i&lt;n; i++)
			mat[i] += pt[i];

		return (*this);
    }
</pre>
<p>A Point3D class template will look like this:</p>
<pre class="brush: cpp; title: ; notranslate">
template &lt;class T&gt; class Point3D : public Point&lt;T,3&gt; {
public:
	Point3D() : Point&lt;T,3&gt;() {};

	Point3D(const Point3D&lt;T&gt; &amp;p) : Point&lt;T,3&gt;(p) {
		for (int i=0; i&lt;3; i++)
		mat[i] = p.mat[i];
	}
	Point3D( const T px, const T py, const T pz)
	{
		mat[0]=px; mat[1]=py; mat[2]=pz;
	}
};
</pre>
<p>An example of how we can use it is a motion programming:</p>
<pre class="brush: cpp; title: ; notranslate">
	Point3D&lt;float&gt; EulerAngle(float sensitivity, double x, double y, double z) {
		return Point3D&lt;float&gt; (
				sensitivity*cos(z),
				sensitivity*sin(z),
				0
		);
	}
</pre>
 <img src="http://wp.freya.no/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=598" width="1" height="1" style="display: none;" /><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwp.freya.no%2Fc%2Fclass-templates-in-c%2F&amp;title=Class%20templates%20in%20C%2B%2B" id="wpa2a_14"><img src="http://wp.freya.no/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://wp.freya.no/c/class-templates-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

