So I have close to 300 variables (just right now), and I have numerically ordered their IDs in Resource.h so it's:
#define IDC_BOX1 1
#define IDC_BOX2 2
#define IDC_BOX3 3
#define IDC_BOX4 4
etc. My question involves the DoDataExchange that I'm performing for each different dialog that I have that contains all of these variables. I REALLY don't want to go through doing the following for each variable:
DDX_CText(pDX, IDC_BOX1, m_nBox1);
DDX_CText(pDX, IDC_BOX2, m_nBox2);
DDX_CText(pDX, IDC_BOX3, m_nBox3);
DDX_CText(pDX, IDC_BOX4, m_nBox4);
because that's just ridiculous.
How can I do something along the same lines as this:
for(int i = 0; i < **totalVariables**; i++)
DDX_CText(pDX, **nameByIdInResourceFile(i)**, **indexOfVariableNameInArray**;
I'm sure this is possible, I just don't know what the function might be that pulls the IDC_... variable names by their ID number. Any thoughts?
Since you've gone to the trouble of creating your resource IDs consecutively and in order, it's easy to go through all of them in the loop:
for(int i = 0; i <= (IDC_BOX300-IDC_BOX1); i++)
DDX_CText(pDX, IDC_BOX1+i, ...
Naturally this will fail if someone comes along and adds IDC_BOX301 and doesn't put it in the sequence properly, so be careful!
The simplest solution for the variable names is to replace the individual variables with an array.
for(int i = 0; i <= (IDC_BOX300-IDC_BOX1); i++)
DDX_CText(pDX, IDC_BOX1+i, m_nBoxes[i]);
#Mark Ransom's answer is great. I do exactly what he suggests, but I have one other thing I do as well. During my app's startup (guarded by an #ifdef DEBUG), I have some code that verifies that all of my IDs are in consecutive numerical order. That way, I can be sure that someone (likely me in the future) doesn't come along and add an out-of-numerical-sequence ID.
Related
I have a loop that executes a large number of times (say 25000). Inside the loop there is a code to add a row to a DataGridView.
dvg->Rows->Add();
Complete code below:
for(int i = 0 ; i < m_Items->Length ; i++)
{
dgv->Rows->Add();
dgv->Rows[i]->Cells[this->IDColumn->Index]->Value = safe_cast<System::Object^>(0);
dgv->Rows[i]->Cells[this->ModColumn->Index]->Value = <<"Data from database">>
dgv->Rows[i]->Cells[this->RunColumn->Index]->Value = <<"Data from database">>
}
This statement is taking a couple of seconds to execute 25000 times. So I am trying to optimize this code for time.
I know the number of rows prior to entering the loop. So I thought moving this call out of the loop and call a function that will add 25000 rows at once. I searched online and this is what I tried so far.
I tried using DataGridView::AddRange function by passing a DataGridViewRow object as an argument. But the sample code found online is confusing. Since I am new to C# code, I don't know how to proceed.
I declared
DataGridViewRow Test;
Now, how to write the code that tells, the columns in DataGridView need to be same as the columns in dvg (If that is even required)?
Do I have to fill the data to test before calling DataGridView::AddRange?
Can somebody help, please?
I am not hugely experienced in C++ coding, but I learn pretty well as I go. But, I have not been able to properly query how to do this, may be using wrong terms or insufficiently expressing my desire. Here's the situation.
I have a lot of variables (3x12) that I have set up under a structure:
struct Tracking
{
String Title;
BoolArray n24hr;
bool State;
unsigned char Days, Weeks;
uint16_t Minutes, TotalMinutes, Daily, Weekly, Monthly, n7d[7], n4w[4];
} Components[3];
I also have code that performs basically the same thing 3 times but on different "levels", e.g. daily, weekly, monthly. It keeps tracks of status over those time periods, filling arrays, finding totals, and duty cycles, etc. It fills the minutes into days, and when that reaches a week, it puts the totals into a week format, and repeats until it reaches monthly levels. So basically, I have it doing something like:
in my main loop:
//calls status formula
StatusFormula();
in a separate file:
//status formula defined
void StatusFormula()
{
// for each element of Components:
//determine current status
//for daily
//add it to the correct spot in the array
//perform calculations on it
//when it reaches a week:
//add it to the correct spot in the next array
//perform calculations on it
//when it reaches a month:
//add it to the correct spot in the next array
//perform calculations on it
}
These calculations are all basically the same, the only differences are the structure member names & the constants for the calculations (i.e., MinsADay, DaysAWk, etc.).
I can get it to work this way, it just means a lot more lines and if I want to change something, I have to repeat it 3 times. What I want is something like this:
in my main loop:
//calls status formula
StatusFormula("Daily"); //sends the status formula information to decide which level (daily, weekly, monthly), it supposed to work on
if (Components[i].Minutes == MinsADay)
{
StatusFormula("Weekly"); //sends the status formula information to decide which level (daily, weekly, monthly), it supposed to work on
if (Components[i].Daily == DaysAWk)
{
StatusFormula("Monthly");
}
}
in a separate file:
//status formula defined
void StatusFormula()
{
//determine which level & variables to use (I would probably use case for this), then
//add it to the correct spot in the correct array
//perform calculations on it
}
I tried passing the level using a string, but it didn't work:
in my main loop:
StatusFormula(i, "Daily"); //sending data to formula, where i is value 0 to 2 for the Components array & defined earlier in the for loop.
in a separate file:
//formula defined as:
void StatusFormula(uint8_t counter, string level)
{Components[counter].level -= //etc... performing calculations as desired.
//so I thought this should evaluate to "Components[i].Daily -=" (& i would be a value 0 to 2) & treat it like the structure, but it doesn't work that way apparently.
I tried passing the structure & variable itself, but that didn't work either:
in my main loop:
StatusFormula(i, Components[i].Daily); //sending data to formula
in a separate file:
//formula defined as:
void StatusFormula(uint8_t counter, Tracking& level)
{level -= //etc... //(level should be Components[i].Daily -=" (& i would be a value 0 to 2)) this didn't work either.
I couldn't find any google searches to help me, and I trialed-and-errored a bunch of ways, but I couldn't figure out how to do that in C++, let alone on the Arduino platform. In Excel VBA, I would just have the variable passed as a string to the formula, which would substitute the word and then treat it like the variable that it is, but I couldn't make that happen in C++. Also to note, I am going to try and define this a separate file/tab so that my massive code file is easier to read/edit, in case that makes a difference. I would paste my code directly, but it is long and super confusing.
I guess what I am asking is how would I pass the structure and/or structure member to the formula in a way that would say the equivalent of:
case 1: //"Daily"
//use Components[i].Daily & Components[i].Minutes & MinsaDay
break;
case 2: //"Weekly"
//use Components[i].Weekly & Components[i].Days & DaysaWk
break;
//etc.
I feel like there should be a way & that I am just missing a small, vital piece. Several people in the comments suggested enums, and after researching, it might possibly be what I want, but I am having trouble visualizing it at the moment and need to do more research and examples. Any suggestions or examples on how to send the appropriate structure & members to the formula to be modified in it?
I'm trying to profile (with Callgrind) a specific part of my code by removing noise and computation that I don't care about.
Here is an example of what I want to do:
for (int i=0; i<maxSample; ++i) {
//Prepare data to be processed...
//Method to be profiled with these data
//Post operation on the data
}
My use-case is a regression test, I want to make sure that the method in question is still fast enough (something like less than 10% extra instructions since the last implementation).
This is why I'd like to have the cleaner output form Callgrind.
(I need a for loop in order to have a significant amount of data processed in order to have a good estimation of the behavior of the method I want to profile)
My first try was to change the code to:
for (int i=0; i<maxSample; ++i) {
//Prepare data to be processed...
CALLGRIND_START_INSTRUMENTATION;
//Method to be profiled with these data
CALLGRIND_STOP_INSTRUMENTATION;
//Post operation on the data
}
CALLGRIND_DUMP_STATS;
Adding the Callgrind macros to control the instrumentation. I also added the --instr-atstart=no options to be sure that I profile only the part of the code I want...
Unfortunately with this configuration when I start to launch my executable with callgrind, it never ends... It is not a question of slowness, because a full instrumentation run last less than one minute.
I also tried
for (int i=0; i<maxSample; ++i) {
//Prepare data to be processed...
CALLGRIND_TOGGLE_COLLECT;
//Method to be profiled with these data
CALLGRIND_TOGGLE_COLLECT;
//Post operation on the data
}
CALLGRIND_DUMP_STATS;
(or the --toggle-collect="myMethod" option)
But Callgrind returned me a log without any call (KCachegrind is white as snow :( and says zero instructions...)
Did I use the macros/options correctly? Any idea of what I need to change in order to get the expected result?
I finally managed to solve this issue... This was a config issue:
I kept the code
for (int i=0; i<maxSample; ++i) {
//Prepare data to be processed...
CALLGRIND_TOGGLE_COLLECT;
//Method to be profiled with these data
CALLGRIND_TOGGLE_COLLECT;
//Post operation on the data
}
CALLGRIND_DUMP_STATS;
But ran the callgrind with --collect-atstart=no (and without the --instr-atstart=no!!!) and it worked perfectly, in a reasonable time (~1min).
The issue with START/STOP instrumentation was that callgrind dumps a file (callgrind.out.#number) at each iteration (each STOP) thus it was really really slow... (after 5min I had only 5000 runs for a 300 000 iterations benchmark... unsuitable for a regression test).
The toggle-collect option is very picky in how you specify the method to use as trigger. You actually need to specify its argument list as well, and even the whitespace needs to match! Use the method name exactly as it appears in the callgrind output. For instance, I am using this invokation:
$ valgrind
--tool=callgrind
--collect-atstart=no
"--toggle-collect=ctrl_simulate(float, int)"
./swaag
Please observe:
The double quotes around the option.
The argument list including parentheses.
The whitespace after the comma character.
I need to write a program that indents a block of code just like visual studio.
I need the logic behind this.
Thanks
I dont need the program ..I need the logic
Put the scintilla into your program, and you're done.
The basic logic is to find the blocks. For example if you have:
for(int i = 0; i < 10; i++){
print i
for(int j = 0; j < 10; j++){
print j
}
}
by seeing {, you will find out that a block will start. You can use stacks to keep track of the blocks. for example if you see {, push it to the stack. the number of element in the stack indicates the size of your indentation because if you push 3 { into the stack, it means you are in the third nested block so you have to use 3 tabs to indent. Now if you see any }, just pop the last { from the stack. It means that your block is done.
This will work for blocks using { and }. You can use the same idea for other situations as well. For example, if you find a for syntax and no { following it, it means it is a single line for loop.
Do you mean the "Increase indent" function triggered by pressing "TAB" when having selected a code block?
This can be built using simple String options: Split the code at the newline char (e.g. Envirnoment.NewLine in C#) and then iterate over the lines and add some tabs or white spaces in front of them.
I've read about the solutuon written here on a post a year ago
wx.TextCtrl.LoadFile()
Now I have a windows application that will generate color frequency statistics that are saved in 3D arrays. Here is a part of my code as you will see on the code below the printing of the statistics is dependent on a slider which specifies the threshold.
void Project1Frm::WxButton2Click(wxCommandEvent& event)
{
char stat[32] ="";
int ***report = pGLCanvas->GetPixel();
float max = pGLCanvas->GetMaxval();
float dist = WxSlider5->GetValue();
WxRichTextCtrl1->Clear();
WxRichTextCtrl1->SetMaxLength(100);
if(dist>0)
{
WxRichTextCtrl1->AppendText(wxT("Statistics\nR\tG\tB\t\n"));
for(int m=0; m<256; m++){
for(int n=0; n<256; n++){
for(int o=0; o<256; o++){
if((report[m][n][o]/max)>=(dist/100.0))
{
sprintf(stat,"%d\t%d\t%d\t%3.6f%%\n",m,n,o,report[m][n][o]/max*100.0);
WxRichTextCtrl1->AppendText(wxT(stat));
}
}
}
}
}
else if(dist==0) WxRichTextCtrl1->LoadFile("histodata.txt");
}
The solution I've tried so far is that when I am to print all the statistics I'll get it from a text file rather than going through the 3D array... I would like to ask if the Python implementation of the segmenting can be ported to C++ or are there better ways to deal with this problem. Thank you.
EDIT:
Another reason why I used a text file instead is that I observed that whenever I do sprintf only [with the line WxRichTextCtrl1->AppendText(wxT(stat)); was commented out] the computer starts to slow down.
-Ric
Disclaimer: My answer is more of an alternative than a solution.
I don't believe that there's any situation in which a user of this application is going to find it useful to have a scrolled text window containing ~16 million lines of numbers. It would be impossible to scroll to one specific location in the list that the user might need to see easily. This is all assuming that every single number you output here has some significance to the user of course (you are showing them on the screen for a reason). Providing the user with controls to look up specific, fixed (reasonable) ranges of those numbers would be a better solution, not only in regards to a better user experience, but also in helping to resolve your issue here.
On the other hand, if you still insist on one single window containing all 64 million numbers, you seem to have a very rigid data structure here, which means you can (and should) take advantage of using a virtual grid control (wxGrid), which is intended to work smoothly even with incredibly large data sets like this. The user will likely find this control easier to read and find the section of data they are looking for.