TR1 – How to use array class?

 

The C++ Standard Template Library (STL) provides a framework for processing algorithms on different kind of containers. However, ordinary arrays don’t provide the interface of STL containers (although, they provide the iterator interface of STL containers). As replacement for ordinary arrays, the STL provides class std::vector. However, std::vector<> provides the semantics of dynamic arrays. It manages data to be able to change the number of elements. This results in some overhead in case only arrays with static size are needed.

New new C++ provides a new class called array which is static in usage but able use a standard C++ container. It also provides access to the underlying data. Thus it’s possible to use as raw array pointers. See the sample snippet below to too see how to use array class. A better documentation can be found at boost::array page.

// using array class
void FooTR1Array()
{
	std::tr1::array<int, 5> arr = {1,2,3,4,5};

	using namespace std;

	cout << "Contents of array " <<endl;
	copy( arr.begin(), arr.end(),
			ostream_iterator<int>(std::cout,"\t"));
	cout << "Size of array - " << arr.size() <<endl;
	cout << "Front of array - " << arr.front() <<endl;
	cout << "Front of back - " << arr.back() <<endl;
	cout << "Array max Size- " << arr.max_size() <<endl;
	cout << "Array [3] - " << arr[3] <<endl;
	cout << "First element using pointer - " << *arr.data() <<endl;

	// array swapping - both arrays should have same properties and size
	std::tr1::array<int, 5> arrNew = {111,222,333,444,555};

	cout << "Contents of array 2" <<endl;
	copy( arrNew.begin(), arrNew.end(),
		ostream_iterator<int>(std::cout,"\t"));

	// swap array
	arr.swap( arrNew );

	cout << endl<< "Contents of array 1 after swapping with array 2" <<endl;
	copy( arr.begin(), arr.end(),
		ostream_iterator<int>(std::cout,"\t"));

	cout << endl<< "Contents of array 2 after swapping with array 1" <<endl;
	copy( arrNew.begin(), arrNew.end(),
		ostream_iterator<int>(std::cout,"\t"));
}
 

Cg Tips – Swizzle Operator

 

Intended Audience – Newbies of Graphics Programming.

nVidia Cg has a swizzle operator (.) that allows the components of a vector to be rearranged to form a new vector. Cg has different packed data types called vectors like float2,float4, color, color3, color4 etc. Okay giving example will make things a bit more clear.

float3(a,b,c).zyx; – This will initialize a float3 vector as float( c, b, a ). the values given are just reversed.
float4(a,b,c,d).zzyx; – This will initialize a float4 vector as float( c, c, b, a ).
float4(a,b,c,d).w; – This will initialize vector with d

Which means that you can repeat or omit the elements and just create a new one with the given order and values. The characters x,y,z and w represent the first, second, third and fourth components of the original vector respectively(You can also use r,g,b and a for the same purpose). You wont suffer with any performance hit as it’s brilliantly implemented in the hardware. It can be also used to convert a scalar to vector.

color.xxxx  – Initalizes a float4(a,a,a,a);

OK let’s take a simple fragment program. The original image is as follows. Let’s make this a bit more pale with swizzle.

Let’s pass throgh the following cg program which uses the swizzle.

struct Output {
float4 color : COLOR;
};

Output texture_frag(float2 texCoord : TEXCOORD0,
uniform sampler2D decal : TEX0)
{
Output OUT;

OUT.color = tex2D(decal,texCoord).xyyz; // Use swizzle
return OUT;
}

OK Just have a try!

 

New home for my blog – codereflect.com

 

Hello my dear readers,

I’ve been maintaining this blog for last couple of years and primarily it deals with windows programming tips and general thoughts on software engineering. In fact this is a mixed blog. I am not really constant with blogging topics. ( I know it’s bad while we are entering to serious blogging). That’s why I named this blog Sharing my thoughts.

Now to have my own identity, I own this blog in a new name www.codereflect.com. I am first time dealing with DNS configuration stuffs etc. Anyway it’s nice. It may take few day to get this stable. I am frequently facing lot of outages for this websites.

Why I named this site as codereflect.com? The answer is no specific reason it’s’ one the name quickly came to my mind when I thought about buying a domain name. Whatever be the things we write or talk about soft wares, the ultimate thing is code. We all implementing new policies, standards, best practices to improve the project development, improve quality of code. It’s the ultimate reflection.

So here I’ve a new home. CodeReflect.com. The blog is still hosted in wordpress.com. nothing else so far changed except the web address. Hope soon I will go to my own webspace.

Also today is Independence day for Indians. Wish you all a happy Independence day. Proud to be an Indian!