Design the presentation of own class in the variable viewer - c++

I'm using Visual Studio and writing in C++. I have written a table (named Vector) which columns can be of any data type (any size the user wishes). In this example I filled one row with three columns. First is of type Byte (1 Byte in size), seconds is also of type Byte and the third of type Int64 (8 Bytes). I saved the numbers 1, 2 and 3.
I also created a vector from the c++ standard (std::vector) and filled it with the same numbers.
As you can see, the seconds picture with the std::vector variable myStdVector shows the int-numbers sorted with [0], 1, 2. I like this class view.
And I would like my own class (Vector) to have the same design respectively presentation in the variable viewer.
Probably it is also possible to show the data type in the third column of the variable viewer which could be different in my Vector-class.
Does Visual Studio offer a possibility to structure the member variables or show some data in a special way?
own vector table class view
std vector class view

Related

How to map a data list generated from some user input to another array or list

This problem is a precursor as well as the design problem of a program I'm working upon.
We have 3 types of cards - Red (R), Green (G) and Blue (B). And they have to be placed on trays where there are multiple slots where each slot can hold exactly one card (i.e, number of cards in a filled tray at any given time = number of slots in the tray). The player/user is given a choice of choosing from the following options:
3-slot tray, where any card type (R/G/B) can occupy any slot. for example, RRG, RGB, BRR etc.
4-slot tray, where the first slot can be occupied by only one of the card types G or B and the remaining 3 can be occupied by any of the the card types. for example, BRRG, GRGB, BBRR etc.
5-slot tray, where the first slot can be occupied by only one of the card types G or B and the remaining 4 can be occupied by any of the the card types. for example, GRRBG, GRRGB, GBRRG etc.
Now, with the program, I would like to:
validate the condition(s) for 4-slot tray and 5-slot tray
generate an array (a list in python) to store the permutation entered by the player/user. In all, the 3-slot tray has 27 (3*3*3), the 4-slot tray has 54 (2*3*3*3) and the 5-slot tray has 162 (2*3*3*3*3) possible permutations and combinations.
map each of these unique permutation to another array of numbers (let us call it the number list). For example, I want to map the permutation BGRG to [1,1,2,3] (which is based on the logic i[n]=1 for n=0,1 and i[n]=n for n>1); RGG to [1,1,2] and so on.
The requirement, put concisely, is:
accept the user input
validate the input according to the conditions
generate the array number list
I can always create a hard-coded multi-dimensional array/list, but I want to write a function which is flexible to any change in the logic (as mentioned in the last point above) and generate the lis. Is there an optimized, pythonic (I'm aware that python is smart with data structures and strings and can do things neatly with fewer lines of code) way to do this? Isn't a 3D list the best way to achieve this?

How can I send complex data to my visualizer in TotalView?

I routinely have to debug legacy Fortran code that is utilizing large arrays of complex data, and the best option available is TotalView. I have created my own visualizer to view data (as per TotalView's instructions here) that works well. It is more flexible than the default one and has the ability to ingest and display complex data, but TotalView will not send complex arrays through its visualization pipe.
Is there any way to make TotalView be able to display complex data without having to recompile the code with additional debugging arrays just to take the absolute value?
E.g. for code like the following short example, I could make another array in Fortran, but I'd really like to just stop and examine the variable my_arr:
program main
implicit none
integer N, M, i, j
parameter (N=100, M=30)
complex my_arr(N, M)
real pi
pi = ACOS(-1.0)
do j = 1, M
do i = 1, N
my_arr(i,j) = cmplx(i*cos(j/pi), i*sin(j/pi))
end do
end do
return
end
For small arrays I can get away with something like this as an expression:
my_arr%Real_Part**2 + my_arr%Imaginary_Part**2
but that won't work for anything very large, TotalView complains about the memory.
I'm using TotalView 8.13.
You can do this if your array is contiguous in memory and you can adjust your visualizer to input the complex data as a real array with an extra dimension containing the real and imaginary parts.
In your example above, if you 'dive' into the variable my_arr, it will show up as type
COMPLEX(4)(100,30)
This is actually the same as the TotalView built-in $complex_8. You can recast the type and dimensions by simply retyping the following into the "Type:" field:
$real_4(2,100,30)
Then the real and imaginary parts will reside in the first (fastest-iterating) dimension and TotalView will allow you to pass the 3D float array to the visualizer. Note: by default TotalView restricts itself to visualizing 2D arrays, so you'll need to change that to 3D (or however many your visualizer can handle) under "Preferences->Launch Strings" in the "Enable Visualizer Launch" box under "Maximum array rank."
Allocatable arrays:
Dynamically sized arrays can be handled in the same way, but require a couple extra steps.
Usually the address of the reference to the array is not the address of the actual array in memory, so you will have to manually adjust the address of the dive window.
In the dive window on the right side there is an option button just above the scroll bar to indicate what columns are shown in the window - turn on "Address" and write down the hex address of the first element in the array. After you recast by changing the type string, type that hex address into the "Address" field at the top, then your data will show up correctly.
The type string will contain something along the lines of COMPLEX(4),allocatable::(:,:), whereas the "Actual Type" string will show you the dimensions. When you do the recast, make sure to remove the ,allocatable:: and change the (:,:) to the actual dimensions (e.g. (100,30)).

Index-based access on Matrix-like structure in C++

I have a mapping Nx2 between two set of encodings (not relevant: Unicode and GB18030) under this format:
Warning: huge XML, don't open if having slow connection:
http://source.icu-project.org/repos/icu/data/trunk/charset/data/xml/gb-18030-2000.xml
Snapshot:
<a u="00B7" b="A1 A4"/>
<a u="00B8" b="81 30 86 30"/>
<a u="00B9" b="81 30 86 31"/>
<a u="00BA" b="81 30 86 32"/>
I would like to save the b-values (right column) in a data structure and to access them directly (no searching) with indexes based on a-values (left column).
Example:
I can store those elements in a data structure like this:
unsigned short *my_page[256] = {my_00,my_01, ....., my_ff}
, where the elements are defined like:
static unsigned short my_00[256] etc.
.
So basically a matrix of matrix => 256x256 = 65536 available elements.
In the case of other encodings with less elements and different values (ex. Chinese Big5, Japanese Shift, Korean KSC etc), I can access the elements using a bijective function like this:
element = my_page[(unicode[i]>>8)&0x00FF][unicode[i]&0x00FF];, where unicode[i] is filled with the a-like elements from the mapping (as mentioned above). How do I generate and fill the my_page structure is analogous. For the working encodings, I have like around 7000 characters to store (and they are stored in a unique place in my_page).
The problem comes with the GB18030 encoding, trying to store 30861 elements in my_page (65536 elements). I am trying to use the same bijective function for filling (and then accessing, analogously) the my_page structure, but it fails since the access mode does not return unique results.
For example: For the unicode values, there are more than 1 element accessed via
my_page[(unicode[i]>>8)&0x00FF][unicode[i]&0x00FF] since the indexes can be the same for i and for i+1 for example. Do you know another way of accessing/filling the elements in the my_page structure based only on pre-computed indexes like I was trying to do?
I assume I have to use something like a pseudo-hash function that returns me a range of values VRange and based on a set of rules I can extract from the range VRange the integer indexes of my_page[256][256].
If you have any advice, please let me know :)
Thank you !
For GB18030, refer to this document: http://icu-project.org/docs/papers/gb18030.html
As explained in this article:
“The number of valid byte sequences -- of Unicode code points covered and of mappings defined between them -- makes it impractical to directly use a normal, purely mapping-table-based codepage converter. With about 1.1 million mappings, a simple mapping table would be several megabytes in size.”
So most probably is not good to implement a conversion based on a pure mapping table.
For large parts, there is a direct mapping between GB18030 and Unicode. Most of the four-bytes characters can be translated algorithmically. The author of the article suggests to handle them such ranges with a special code, and the other ones with a classic mapping table. These characters are the ones given in the XML mapping table: http://source.icu-project.org/repos/icu/data/trunk/charset/data/xml/gb-18030-2000.xml
Therefore, the index-based access on Matrix-like structure in C++ can be a problem opened for whom wants to research on such bijective functions.

removing the elements in the array with certain properties and sort

There was one two dimensonal array used in my office application
for eg int arr[10][4];
say suppose the first index represents fruits and seconds index represent the properties of fruit like price,colour`etc.
so i can store 10 fruits with 4 properties for each fruit.
say suppose my task is to remove the entries in array whose fruit colour is red.
To achieve this i just looped through the array and made the entries of apple to zero.
for eg if apple was at 4th index, then arr[4][0]=0;
like wise i made the matched criterion to zero
and to remove the entry of apple in array
with looping i moved the entries with 0 at arr[x][0] to down of the array.(bubble sort)
but the above method is not seems to be good programming practice,
let me know your thoughts
Note: I cannot change the data structure because it is used at many parts of my application

How to display a dynamically allocated array in the Visual Studio debugger?

If you have a statically allocated array, the Visual Studio debugger can easily display all of the array elements. However, if you have an array allocated dynamically and pointed to by a pointer, it will only display the first element of the array when you click the + to expand it. Is there an easy way to tell the debugger, show me this data as an array of type Foo and size X?
Yes, simple.
say you have
char *a = new char[10];
writing in the debugger:
a,10
would show you the content as if it were an array.
There are two methods to view data in an array m4x4:
float m4x4[16]={
1.f,0.f,0.f,0.f,
0.f,2.f,0.f,0.f,
0.f,0.f,3.f,0.f,
0.f,0.f,0.f,4.f
};
One way is with a Watch window (Debug/Windows/Watch). Add watch =
m4x4,16
This displays data in a list:
Another way is with a Memory window (Debug/Windows/Memory). Specify a memory start address =
m4x4
This displays data in a table, which is better for two and three dimensional matrices:
Right-click on the Memory window to determine how the binary data is visualized. Choices are limited to integers, floats and some text encodings.
In a watch window, add a comma after the name of the array, and the amount of items you want to be displayed.
a revisit:
let's assume you have a below pointer:
double ** a; // assume 5*10
then you can write below in Visual Studio debug watch:
(double(*)[10]) a[0],5
which will cast it into an array like below, and you can view all contents in one go.
double[5][10] a;
For,
int **a; //row x col
add this to watch
(int(**)[col])a,row
Yet another way to do this is specified here in MSDN.
In short, you can display a character array as several types of string. If you've got an array declared as:
char *a = new char[10];
You could print it as a unicode string in the watch window with the following:
a,su
See the tables on the MSDN page for all of the different conversions possible since there are quite a few. Many different string variants, variants to print individual items in the array, etc.
You can find a list of many things you can do with variables in the watch window in this gem in the docs:
https://msdn.microsoft.com/en-us/library/75w45ekt.aspx
For a variable a, there are the things already mentioned in other answers like
a,10
a,su
but there's a whole lot of other specifiers for format and size, like:
a,en (shows an enum value by name instead of the number)
a,mb (to show 1 line of 'memory' view right there in the watch window)
For MFC arrays (CArray, CStringArray, ...)
following the next link in its Tip #4
http://www.codeproject.com/Articles/469416/10-More-Visual-Studio-Debugging-Tips-for-Native-De
For example for "CArray pArray", add in the Watch windows
pArray.m_pData,5
to see the first 5 elements .
If pArray is a two dimensional CArray you can look at any of the elements of the second dimension using the next syntax:
pArray.m_pData[x].m_pData,y
I haven't found a way to use this with a multidimensional array. But you can at least (if you know the index of your desired entry) add a watch to a specific value. Simply use the index-operator.
For an Array named current, which has an Array named Attribs inside, which has an Array named Attrib inside, it should look like this if you like to have to position 26:
((*((*current).Attribs)).Attrib)[26]
You can also use an offset
((*((*current).Attribs)).Attrib)+25
will show ne "next" 25 elements.
(I'm using VS2008, this shows only 25 elements maximum).