C++ Question Building and sorting through data sets.

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,868
Reaction score
4
Points
0
Location
San Diego
still struggling with my RCS code and hoping that a more specific question will garner more responses.

I have a basic plan and algorithm but I'm having problems with the interface/infrastructure side of things.

What I need is a simple/efficient way to store sets of variables (thruster data) and then search/sort them based on the parameter given. IE list all thrusters that produce positive thrust along the x axis. I started using std::set because that's how Darrenc and the crew working on NAASP (my primary references) did it, but I'm in over my head.

As such I'm looking for suggestions/advice.
 
If the number of thrusters is fixed, my first idea would be an array of structs:

Code:
#define N_THRUSTERS 16

typedef struct _thruster {
  double x_thrust;
  double y_thrust;
  double z_thrust;
} thruster_t; 

thruster_t thrusters[16];

// Initialize
thrusters[0].x_thrust = 1;
thrusters[0].y_thrust = 0;
thrusters[0].z_thrust = 0;

thrusters[1].x_thrust = 0;
thrusters[1].y_thrust = 1;
thrusters[1].z_thrust = 0;
// etc. 

// Search
std::list<int> result;

for (int i=0; i<N_THRUSTERS; i++) {
  if ( thrusters[i].x_thrust > 0 )
    result.push_back(i);
}

// Do something with result
 
You can also use std::map for that, but it is a bit more complex to use than vectors or sets
 
If the number of thrusters is fixed, my first idea would be an array of structs:

It isn't fixed, which is part of the problem. :beathead:

I've considered using an arbitrarily large array (and then restricting myself to the current count) but have been advised against using such techniques in the past.

Essentially the vessel passes a list of thrusters, control surfaces etc... to the flight control class saying "this is what you have to work with" and the FCS class then looks through them to figure out what it can use to produce the desired torque force etc...

I already designed a struct to store my thruster data, and was going to use a set to store them but as far as I can tell there is no simple way to scroll through the set or access the individual members' data once I've done so which kind of defeats the purpose of using a set in the first place.

---------- Post added at 12:56 ---------- Previous post was at 12:53 ----------

You can also use std::map for that, but it is a bit more complex to use than vectors or sets

how/why would I use a vector or set in this situation, as I said above, I have not been able to find a simple or efficient way to iterate/search through individual set members which is why I started this thread in the first place.

---------- Post added at 13:05 ---------- Previous post was at 12:57 ----------

PS:
@Kamaz,

What is "result.push_back (i);"? It isn't in my list of standard operators.
 
Push_back is listed here. Push back basically just means insert at the end of the list.

I would personally use a vector for storing the thrusters; see the other thread here for example code with a vector.
 
On second thought, std::vector seems to be a better choice:

Code:
#include "stdafx.h"

// Move these to stdafx.h if using precompiled headers
#include <vector>
#include <iostream>

typedef struct _thruster {
	int id;
	double x_thrust;
	double y_thrust;
	double z_thrust;
} thruster_t; 


void print_thruster(thruster_t *t)
{
	std::cout << "id=" << t->id << " x=" << t->x_thrust << " y=" << t->y_thrust << " z=" << t->z_thrust << std::endl;
}

thruster_t *create_thruster(int id, double x_thrust, double y_thrust, double z_thrust) 
{
	thruster_t *t;
	t = new thruster_t;
	t->id = id;
	t->x_thrust = x_thrust;
	t->y_thrust = y_thrust;
	t->z_thrust = z_thrust;

	return t;
}

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<thruster_t *> thrusters;

	// Initialize
	thruster_t *t;

	t = create_thruster(1, 1, 0, 0);
	thrusters.push_back(t);
	t = create_thruster(2, 0, 1, 0);
	thrusters.push_back(t);
	t = create_thruster(3, 0, 0, 1);
	thrusters.push_back(t);
	t = create_thruster(4, 2, 0, 0);
	thrusters.push_back(t);

	std::cout << "All thrusters" << std::endl;
	for (int i=0; i<thrusters.size(); i++) {
		print_thruster(thrusters[i]);
	}

	// Search
	std::vector<thruster_t*> result;

	for (int i=0; i<thrusters.size(); i++) {
		if ( thrusters[i]->x_thrust > 0 )
			result.push_back(thrusters[i]);
	}

	std::cout << "X-axis thrusters" << std::endl;
	for (int i=0; i<result.size(); i++) {
		print_thruster(result[i]);
	}
	
	getchar();
	return 0;
}

Output:

All thrusters
id=1 x=1 y=0 z=0
id=2 x=0 y=1 z=0
id=3 x=0 y=0 z=1
id=4 x=2 y=0 z=0
X-axis thrusters
id=1 x=1 y=0 z=0
id=4 x=2 y=0 z=0

---------- Post added at 11:47 PM ---------- Previous post was at 11:39 PM ----------

I've considered using an arbitrarily large array (and then restricting myself to the current count) but have been advised against using such techniques in the past.

This technique it is not wrong, and it is quite widespread in the real world -- it is however considered bad style :)
 
Last edited:
Problem solved for the moment. See this post here for code + details.

Thank you all for your patience and assistance. :tiphat:
 
Back
Top