Click here to Skip to main content
15,901,122 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Reusing C++ code for use with .NET (WPF) Pin
Paul Michalik18-Oct-10 7:28
Paul Michalik18-Oct-10 7:28 
GeneralRe: Reusing C++ code for use with .NET (WPF) Pin
Iain Clarke, Warrior Programmer17-Oct-10 10:06
Iain Clarke, Warrior Programmer17-Oct-10 10:06 
QuestionStrange Windows Timer Behavior Pin
softwaremonkey16-Oct-10 5:47
softwaremonkey16-Oct-10 5:47 
AnswerRe: Strange Windows Timer Behavior Pin
Cedric Moonen16-Oct-10 6:05
Cedric Moonen16-Oct-10 6:05 
GeneralRe: Strange Windows Timer Behavior Pin
softwaremonkey16-Oct-10 6:52
softwaremonkey16-Oct-10 6:52 
GeneralRe: Strange Windows Timer Behavior Pin
SAMZC16-Oct-10 23:55
SAMZC16-Oct-10 23:55 
QuestionQuestion on how to traverse this array Pin
Keith Vitali16-Oct-10 5:08
Keith Vitali16-Oct-10 5:08 
AnswerRe: Question on how to traverse this array Pin
Graham Shanks16-Oct-10 7:57
Graham Shanks16-Oct-10 7:57 
Firstly recognise that the generic index is calculated as follows:

index = X0 + X1*D0 + X2*D1*D0 + ... + Xn*Dn-1*Dn-2*...*D0

Where Xi is the index in axis i and Di is the size of dimension i

To make this word we need an array of the same size as the dimension array to hold each of the indices along the axes. Given this, the following code will calulate the index:

int calculate_product(int dim, int *dimensions)
{
  int product = 1;
  for(int i = 0; i < dim; ++i)
    product *= dimensions[i];
  return product;
}

int calculate_index(int num_dims, int *dimensions, int *indices)
{
  int index = 0;
  for(int i = 0; i < num_dims; ++i)
    index += indices[i]*calculate_product(i, dimensions);
  return index;
}


If we want to traverse along axis i, the outer loop should iterate over axis i - 1, the next loop iterates over axis i - 2, ..., then iterate over axis 0, then iterate over axis num_dims - 1, ..., then axis i + 1, and finally over axis i. First we need a function to get the previous axis number:

int previous(int current, int num_dims)
{
  if(current > 0)
    return current - 1;
  return num_dims - 1;
}


Now to traverse the array, starting from a given axis, it is easiest to use recursion.

void traverse(int axis, int current, float *histogram, int num_dims, int *dimensions, int *indices)
{
  for(indices[current] = 0; indices[current] < dimensions[current]; ++indices[current])
  {
    if(axis == current)
    {
      int index = calculate_index(num_dims, dimensions, indices);
      // Do something with histogram[index]
    }
    else
    {
      traverse(axis, previous(current, num_dims), histogram, num_dims, dimensions, indices);
    }
  }
}

void traverse_axis(int axis, float *histogram, int num_dims, int *dimensions)
{
  int *indices = new int[num_dims];
  for(int dim = 0; dim < num_dims; ++dim)
    indices[0] = 0;

  traverse(axis, previous(axis, num_dims), histogram, num_dims, dimensions, indices);
  delete [] indices;
}

void traverse_histogram(float *histogram, int num_dims, int *dimensions)
{
  traverse_axis(num_dims - 1, histogram, num_dims, dimensions);
  traverse_axis(num_dims - 1, histogram, num_dims, dimensions);
  //...
  traverse_axis(1, histogram, num_dims, dimensions);
  traverse_axis(0, histogram, num_dims, dimensions);
}

Graham

Librarians rule, Ook!

GeneralRe: Question on how to traverse this array Pin
Keith Vitali16-Oct-10 8:57
Keith Vitali16-Oct-10 8:57 
GeneralRe: Question on how to traverse this array Pin
Keith Vitali16-Oct-10 10:39
Keith Vitali16-Oct-10 10:39 
GeneralRe: Question on how to traverse this array Pin
Graham Shanks16-Oct-10 10:43
Graham Shanks16-Oct-10 10:43 
GeneralRe: Question on how to traverse this array Pin
Keith Vitali16-Oct-10 11:29
Keith Vitali16-Oct-10 11:29 
AnswerRe: Question on how to traverse this array Pin
Aescleal16-Oct-10 22:31
Aescleal16-Oct-10 22:31 
Questionhow can I learn?? Pin
AmbiguousName16-Oct-10 1:32
AmbiguousName16-Oct-10 1:32 
AnswerRe: how can I learn?? Pin
Niklas L16-Oct-10 3:01
Niklas L16-Oct-10 3:01 
GeneralRe: how can I learn?? Pin
AmbiguousName16-Oct-10 10:37
AmbiguousName16-Oct-10 10:37 
QuestionRe: how can I learn?? Pin
David Crow16-Oct-10 4:37
David Crow16-Oct-10 4:37 
AnswerRe: how can I learn?? Pin
AmbiguousName16-Oct-10 8:26
AmbiguousName16-Oct-10 8:26 
AnswerRe: how can I learn?? Pin
Maximilien16-Oct-10 8:35
Maximilien16-Oct-10 8:35 
AnswerRe: how can I learn?? Pin
Aescleal16-Oct-10 22:21
Aescleal16-Oct-10 22:21 
QuestionCEdit::GetLine return blank value when LineLength is 1. Pin
Le@rner15-Oct-10 23:51
Le@rner15-Oct-10 23:51 
AnswerRe: CEdit::GetLine return blank value when LineLength is 1. Pin
Richard MacCutchan16-Oct-10 0:56
mveRichard MacCutchan16-Oct-10 0:56 
AnswerRe: CEdit::GetLine return blank value when LineLength is 1. Pin
David Crow16-Oct-10 4:40
David Crow16-Oct-10 4:40 
QuestionHow can create thread pool? Pin
Le@rner15-Oct-10 23:23
Le@rner15-Oct-10 23:23 
AnswerRe: How can create thread pool? Pin
Rajesh R Subramanian16-Oct-10 2:51
professionalRajesh R Subramanian16-Oct-10 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.