Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sascha Tayefeh http://www.tayefeh.de

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Most elementary Win-API "Hello, 'ere I am' code

This does nothing but opening a window for 5 seconds. No MFC, no C#, no resources, no whatsoever...

#include<windows.h> 
#include<tchar.h>

HWND NewWindow(
			   LPCTSTR str_Title,
			   int int_XPos, 
			   int int_YPos, 
			   int int_Width, 
			   int int_Height);

LRESULT CALLBACK OurWindowProcedure(
									HWND han_Wind,
									UINT uint_Message,
									WPARAM parameter1,
									LPARAM parameter2);

int WINAPI WinMain(
				   HINSTANCE hInstance,
				   HINSTANCE hPreviousInstance,
				   LPSTR lpcmdline,
				   int nCmdShow
				   )
	{
	HWND han_Window = NewWindow(_T("DirectX C++ Tutorial"),100,100,500,500);
	Sleep(5000);
	DestroyWindow(han_Window);
	return 0;
	}


HWND NewWindow(
			   LPCTSTR str_Title,
			   int int_XPos, 
			   int int_YPos, 
			   int int_Width, 
			   int int_Height)
	{

	WNDCLASSEX wnd_Structure;

	wnd_Structure.cbSize = sizeof(WNDCLASSEX);
	wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
	wnd_Structure.lpfnWndProc = OurWindowProcedure;
	wnd_Structure.cbClsExtra = 0;
	wnd_Structure.cbWndExtra = 0;
	wnd_Structure.hInstance = GetModuleHandle(NULL);
	wnd_Structure.hIcon = NULL;
	wnd_Structure.hCursor = NULL;
	wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
	wnd_Structure.lpszMenuName = NULL;
	wnd_Structure.lpszClassName = _T("WindowClassName");
	wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

	RegisterClassEx(&wnd_Structure);

	return CreateWindowEx(
		WS_EX_CONTROLPARENT, 
		_T("WindowClassName"), 
		str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
		int_XPos, 
		int_YPos, 
		int_Width, 
		int_Height, 
		NULL, 
		NULL, 
		GetModuleHandle(NULL),
		NULL);
	}

LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
	{
	return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
	}


Convert String-Type the C++-Way

A very useful snipped that converts strings to/from various types.

Here's the header:
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;

#ifndef ST_STRING
#define ST_STRING


enum CONVTYPE { STRTOINT, STRTOFLOAT, INTTOSTR, STRTODOUBLE }; 

/*
 *  Convtype converts an integer to a string and a string 
 *  string to an integer, a float, or a double. The third argument
 *  of this method determines what to do:
 *  STRTOINT, STRTOFLOAT, INTTOSTR, and STRTODOUBLE.
 *
 */
void convtype(void*, void*, const int& );  
  
#endif



The appropiate implementation:
void convtype(void* inp, void* out, const int& convtype)
{
  istringstream isbuf;
  ostringstream osbuf;

  switch(convtype)
    {
    case STRTOINT:
      isbuf.str(*(string*) inp);
      isbuf >> *((int*) out);
      break;
    case STRTOFLOAT:
      isbuf.str(*(string*) inp);
      isbuf >> *((float*) out);
      break;
    case STRTODOUBLE:
      isbuf.str(*(string*) inp);
      isbuf >> *((double*) out);
      break;
    case INTTOSTR:
      osbuf << *((int*) inp);
      *((string*)out)=osbuf.str();
      break;
    }
  return;
}

Vector Statistic Library Test

Using Intel-C++ compiler and Intel-Math Kernel Library, here is an example usage of random number generation.

 //
 // VSL test by Sascha Tayefeh 2006
 // http://www.tayefeh.de
 //
 // Vector Statistic Libraries Test 
 // You will have to use Intel icc-compiler along with its 
 // Math Kernel Library.
 // 
 // Compile with something like:
 //
 // icc  -I~/mkl_8.1/include 001.cc -L~/mkl_8.1/lib/em64t -lmkl_em64t vsl.cc -o vsl
 //
 //
 
#include <iostream>
using namespace std;
#include "mkl_cblas.h"
#include "mkl_vsl.h"
#define LENGTH 30000
int main ()
{
	int n(LENGTH); // Number of random values to be generated
	float a(60.0), s(10.9); // For a gaussian distribution parameter a and s(igma)
	float *pt(NULL); // The pointer to the array that is supposed to contain the random numbers
	int status; // Return Status of the generator algorithm

	// Allocate sufficiant memory
	pt=new float[LENGTH];
	

	// Create the VSL Stream (search the MKL/VSL-Docs for Service Routines)
	VSLStreamStatePtr stream;
		vslNewStream( &stream, VSL_BRNG_R250, 1232);
	// The random number generator routine
	status =vsRngGaussian( VSL_METHOD_SGAUSSIAN_BOXMULLER, stream, n, pt, a,s); 


	// Show us what we have done ;-)
	for (int i=0; i<LENGTH; i++)
	{
		cout  << *(pt+i) << endl;
	}
	vslDeleteStream( &stream );
	delete [] pt;
	return 0;
}

Inherided Constructors and Destructors

A little demonstration that shows how C++ acts with inherited constructors/destructors

#include <iostream>
using namespace std;

class First 
{
private:
  int i;

protected:

public:
  void getI(int &);
  void putI(const int &);
  First();
  ~First();
};

class Inh : First
{
private:
  int u;
protected:

public:
  Inh();
  ~Inh();
};

Inh::Inh()
{
  cout << "Inh Constructor" << endl;
}

Inh::~Inh()
{
  cout << "Inh Destructor" << endl;
}

void First::getI(int & o)
{
  o = i;
}

void First::putI(const int & o)
{
  i = o;
}

First::First()
{
  i = 1;
  cout << "First Constructor" << endl;
}

First::~First()
{
  cout << "First Destructor" << endl;
}

int main()
{
  int i(2),o(0);
  Inh inh;
  return;
}

Valarray vs. array matrix-calculation performance

Demonstrates the usage of valarray and measures the performance of some arithmetic operations on matriced to be compared with the same operations performed with ordinary arrays.

#include <iostream>
#include <valarray>
#include <vector>
#include <ctime>
#include <iomanip>
using namespace std;

void rndm(float& a);
void zerocheck(float& a);
double duration(vector<clock_t>::iterator start, vector<clock_t>::iterator stop);

int main()
{
  int i(0);
  int p(0);
  float k(5.3235),l(9.212);
  int size(4000000);

  vector<clock_t> vstart;
  vector<clock_t> vstop;
  vector<clock_t>::iterator istartclock;
  vector<clock_t>::iterator istopclock;
  vector<float> vfloat(size);
  vector<float>::iterator ifloat;
  valarray<float> valfloat(size);

  cout << "Filling float array with " << size << " random elements" << endl;
  cout << "(each element needs " << sizeof(float) << " bytes of memory, so " \
       << "we need to make use of " << size*sizeof(float)/1000000<< "MBytes.)"<< endl;

  // ======================= ORDINARY OPERATIONS =======================
  // fill array

  vstart.push_back(clock());
  //  vfloat.resize(size);
  ifloat=vfloat.begin();
  for_each(vfloat.begin(),vfloat.end(),rndm);
  vstop.push_back(clock());

  cout << "****** Phase 1: Math. operations on ordinary array" << endl;  

  // Add k to each element
  cout << "*** Adding " << k << " to each element " ;
  cout << "(10th element was: " << *(ifloat+10) ;
  vstart.push_back(clock());
  for (i=0; i<vfloat.size(); i++) { *(ifloat+i)+=k; }
  vstop.push_back(clock());
  cout << " now is: " << *(ifloat+10) << ")" << endl;


  // Multiply each element with k
  cout << "*** Multiplying each element with " << k ;
  cout << " (10th element was: " << *(ifloat+10) ;
  vstart.push_back(clock());
  for (i=0; i<vfloat.size(); i++) { *(ifloat+i)*=k; }
  vstop.push_back(clock());
  cout << " now is: " << *(ifloat+10) << ")"<< endl;

  // Divide each element by k
  cout << "*** Dividing each element by " << l ;
  cout << " (10th element was: " << *(ifloat+10) ;
  vstart.push_back(clock());
  for (i=0; i<vfloat.size(); i++) { *(ifloat+i)/=l; }
  vstop.push_back(clock());
  cout << " now is: " << *(ifloat+10) << ")" << endl;

  // Cos of each
  cout << "*** Calculate cos for each element "  ;
  cout << " (10th element was: " << *(ifloat+10) ;
  vstart.push_back(clock());
  for (i=0; i<vfloat.size(); i++) { *(ifloat+i)= cosf(*(ifloat+i)); }
  vstop.push_back(clock());
  cout << " now is: " << *(ifloat+10) << ")" << endl;

  for_each(vfloat.begin(),vfloat.end(),zerocheck);

  // Expand each element by itself
  cout << "*** Calculate element ^ element "  ;
  cout << " (10th element was: " << *(ifloat+10) ;
  vstart.push_back(clock());
  for (i=0; i<vfloat.size(); i++) { *(ifloat+i)= pow(*(ifloat+i),*(ifloat+i)); }
  vstop.push_back(clock());
  cout << " now is: " << *(ifloat+10) << ")" << endl;

  // log
  cout << "*** Calculate log of each element "  ;
  cout << " (10th element was: " << *(ifloat+10) ;
  vstart.push_back(clock());
  for (i=0; i<vfloat.size(); i++) { *(ifloat+i)= log(*(ifloat+i)); }
  vstop.push_back(clock());
  cout << " now is: " << *(ifloat+10) << ")" << endl;


  // Summary
  istartclock=vstart.begin(); istopclock=vstop.begin();
  cout << "****** Clock ticks for operations: " << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++) << " (random fill)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++) << " (add.)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++) << " (mult.)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++) << " (div.)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++) << " (cos)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++) << " (pow)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock,istopclock) << " (log)" << endl;
  cout << right << fixed << setw(13) << *(vstop.rbegin())-*(vstart.begin()) << " (total) " << endl;


  p=vstart.size();
  // ======================= VALARRAY OPERATIONS =======================
  // fill valarray

  cout << "****** Phase 2: Math. operations on valarray" << endl;  
  vstart.push_back(clock());
  for (i=0;i<size;i++)
    {
      rndm(valfloat[i]);
    }
  vstop.push_back(clock());


  // Add k to each element
  cout << "*** Adding " << k << " to each element " ;
  cout << "(10th element was: " << valfloat[10] ;
  vstart.push_back(clock());
  valfloat+=k;
  vstop.push_back(clock());
  cout << " now is: " << valfloat[10] << ")" << endl;

  // Multiply
  cout << "*** Multiplying each element with " << k ;
  cout << "(10th element was: " << valfloat[10] ;
  vstart.push_back(clock());
  valfloat*=k;
  vstop.push_back(clock());
  cout << " now is: " << valfloat[10] << ")" << endl;

  // Divide
  cout << "*** Dividing each element by " << l ;
  cout << "(10th element was: " << valfloat[10] ;
  vstart.push_back(clock());
  valfloat/=l;
  vstop.push_back(clock());
  cout << " now is: " << valfloat[10] << ")" << endl;

  // Cosinus
  cout << "*** Calculate cos for each element "  ;
  cout << "(10th element was: " << valfloat[10] ;
  vstart.push_back(clock());
  valfloat=cos(valfloat);
  vstop.push_back(clock());
  cout << " now is: " << valfloat[10] << ")" << endl;


  for (i=0;i<size;i++)
    {
      zerocheck(valfloat[i]);
    }

  // Expand each element by itself
  cout << "*** Calculate element ^ element "  ;
  cout << "(10th element was: " << valfloat[10] ;
  vstart.push_back(clock());
  valfloat=pow(valfloat,valfloat);
  vstop.push_back(clock());
  cout << " now is: " << valfloat[10] << ")" << endl;

  // log
  cout << "*** Calculate log of each element "  ;
  cout << "(10th element was: " << valfloat[10] ;
  vstart.push_back(clock());
  valfloat=log(valfloat);
  vstop.push_back(clock());
  cout << " now is: " << valfloat[10] << ")" << endl;

  // Summary

  cout << "****** Clock ticks for operations: " << endl;
  istartclock=vstart.begin(); istopclock=vstop.begin();
  
  istartclock+=p; istopclock+=p;
  
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++)  << " (random fill)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++)  << " (add.)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++)  << " (mult.)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++)  << " (div.)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++)  << " (cos)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock++,istopclock++)  << " (pow)" << endl;
  cout << right << fixed << setw(13) << duration(istartclock,istopclock)      << " (log)" << endl;
  cout << right << fixed << setw(13) << *(vstop.rbegin())-*(vstart.begin()+p) << " (total) " << endl;



}

void rndm(float& a)
{
  a=rand()%1000000;
  a/=(1000);
}

void zerocheck(float& a)
{
  if (a<0) { a*=-1; } 
}

double duration(
	      vector<clock_t>::iterator start, 
	      vector<clock_t>::iterator stop
	      )
{
  return difftime(*stop,*start) ;
}

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS