How to update array field in C++ - c++

I'm iterating on a query results computing an array of float values. Now from C++ I want to add it to the originating record, or, if already present, update it.
From Javascript I do something similar to:
db.scraps.find({type: {$exists: 0}}).forEach(function (doc) {
var new_array = []
// fill the elements of new_array from doc fields
doc.new_field = new_array;
db.scraps.save(doc);
}
Seems that this cannot be done with the C++ driver (I'm still running 2.6) except using update. If true, I think I should have to save in an array the pair (OID, new_array) from my query and then iterating on it calling: conn.update("kb.scraps", QUERY("_id" << OID), BSON("new_field" << new_array))
Thanks for your help!

Related

How to sort an object list in Java?

I have a list object type and I want to sort it by date in ascending order.
First of all I get the resevations that are between these dates and saving it to a new List.
Now I need someway to sort it.
I tried Collections.sort(reservationsByDate) & Collections.sort(reservationsByDate, Collections.reverseSort() , but it didn't produce anything.
I'm kinda new to java so if theres something that im missing please help me implement this.
heres my code:
public List<Reservation> getAllReservationsSortedByDate(LocalDate from, LocalDate to) {
int fromDate = from.getDayOfMonth();
int toDate = to.getDayOfMonth();
ArrayList<Reservation> reservationsByDate = new ArrayList<>();
for (Reservation reservation : reservations) {
if (reservation.getFromDate().getDayOfMonth() >= fromDate && reservation.getToDate().getDayOfMonth() <= toDate) {
reservationsByDate.add(reservation);
}
}
//reservationsByDate needs to be sorted by dates in ascending order...
return reservationsByDate;
}
Thank you for your help.
You are iterating over "reservations". The definition of this field is not shown. If it is empty the result would always be an empty list.

qt ASSERT failure in QList<T>::at: "index out of range"

I am still relatively new to Qt and I have recently been working on a large project. When I attempt to run the project I get this error:
ASSERT failure in QList<T>::at: "index out of range", file c:\qt\qt5.3.0\5.3\msvc2013_64\include\qtcore\qlist.h, line 479
Just wondering if anyone knows what this means or how I might go about tracking down the source of the problem?
[edit] I believe that the addition of this code is causing the error
autAtom *aP = new autAtom(Principal);
autAtom *aQ = new autAtom(Principal);
autData *P = new autData (DataAtom, aP);
autData *Q = new autData (DataAtom, aQ);
autData *X = new autData (AnyData);
AUTPostulate *p;
autStatementList preList;
{
preList.clear();
//autData *d1 = new autData(NotHereData, X);
autStatement *pre1 = new autStatement(aP, believes, X);
autStatement *goal = new autStatement(aP, sees, X);
preList.append(pre1);
p = new AUTPostulate("BS", BS, goal, preList);
cout << "" << p->getString().toStdString() << endl;
AUTPostulates.append(p);
}
When this is taken out the tool runs fine.
I ran into a similar issue because I did a connect on itemChanged before populating the widget and then while populating my slot code was called. After I put in a guard that prevented signal handling during widget population, I found I could populate the widget fine and I could also handle the signal fine after. Hope this helps.
Index out of range means you're trying to access an index of a QList object, or maybe an object that is a subclass of a QList that does not exist. So if you have a QList with a length of 5 and you try to access index 5, it will be out of range.
Also, it looks like your code contains a lot of classes that are not standard to Qt or C++. At least I don't recognize them. It's difficult to say what's going on here without knowing about those classes.

Can I put a lua_table in another table to build a multi-dimensional array via C++?

for my game im trying to accomplish the following array structure by C++ because the data come from an external source and should be available in a lua_script.
The array structure should look like this: (The data are in a map, the map contains the name of the variable and a list of Pairs (Each pair is a key value pair considered to be one element in one subarray)...
The data prepared in the map are complete and the structure is definetly okay.
So basically I have
typedef std::map<std::string, std::list<std::pair> >;
/\index(e.g: sword) /\ /\
|| ||
|| Pair: Contains two strings (key/value pair)
||
List of Pairs for each array
items = {
["sword"] = {item_id = 1294, price = 500},
["axe"] = {item_id = 1678, price = 200},
["red gem"] = {item_id = 1679, price = 2000},
}
What I got so far now is:
for(ArrayMap::iterator it = this->npc->arrayMap.begin(); it != this->npc->arrayMap.end(); it++) {
std::string arrayName = (*it).first;
if((*it).second.size() > 0) {
lua_newtable(luaState);
for(ArrayEntryList::iterator itt = (*it).second.begin(); itt != (*it).second.end(); itt++) {
LuaScript::setField(luaState, (*itt).first.c_str(), (*itt).second.c_str());
}
lua_setglobal(luaState, arrayName.c_str());
}
}
But this will only generate the following structure:
(table)
[item_id] = (string) 2000
[name] = (string) sword
[price] = (string) 500
The problem is that the table can ofcourse only contain each index once.
Thatswhy I need something like "a table in a table", is that possible?
Is there a way to achieve this? Im glad for any hints.
So from what I understand, if you have two "sword", then you can not store second one? If that's the case, you are doing it wrong. The key of map should be unique and if you decide that you are going to use std::map to store your items then your external source should provide unique keys. I used std::string as key in my previous game. Example:
"WeakSword" -> { some more data }
"VeryWeakSword" -> { some more data }
or, with your data (assuming item_ids are unique) you can get something like following from external source:
1294 -> { some more data }
1678 -> { some more data }
I'm not sure how efficient is this but I wasn't programming a hardware-hungry 3D bleeding-edge game so it just did a fine job.
The data structure that you are using also depends on the how you are going to use it. For example, if you are always iterating through this structure why don't you store as follows:
class Item {public: ... private: std::string name; int id; int value;}
std::vector<Item> items // be careful tho, std::vector copies item before it pushes
Extract(or Parse?) the actual value you want from each entity in external source and store them in std::vector. Reaching the middle of std::vector is expensive, however, if your intention is not instant accessing but rather iterating over data, why use map? But, if your intention is actually reaching a specific key/value pair, you should alter your external data and use unique keys.
Finally, there is also another associative container that stores non-unique key/value pairs called std::multimap but I really doubt you really need it here.

Check for structure key in array element using cfscript

I am trying to loop over an array called meta.
I am having issues with checking if an element exists. In this array sometimes the length is present and sometimes it is not. I am trying to get something like this to work:
for (i=1;i LTE ArrayLen(meta);i=i+1) {
if (meta[i].length==undefined) {
maxLen = '1';
}
else
{
maxLen = meta[i].length;
}
}
I cannot seem to get the syntax right.
I think you want a structkeyexists.
if (structkeyexists(meta[i],"length") ....

View Array contents in Qt Creator debugger

I am using Qt on Ubuntu. When I debug I only see the very first value of the array in Locals and Watchers. How can I view all the array contents?
struct node
{
int *keys;
void **pointers;
int num_keys;
struct node *parent;
int is_leaf;
struct node *nextLevelNode;
};
It shows only the first key value in the debugging window.
In Expression evaluator,
Try (int[10])(*myArray) instead of (int[10])myArray
Or, *myArray#10 instead of myArray#10
It shows only the first key value,in the debugging window
I presume you're referring to the pointer keys, declared with int *keys;
The debugger doesn't know that this is an array: all it knows is that this is a pointer to an int. So it can't know how many values you want it to display.
What I've found, using the Qt Creator 2.1.0 debugger on Ubuntu, is that the following code allows me to see all 5 values:
int array1[5];
array1[0] = 2;
array1[1] = 4;
array1[2] = 6;
array1[3] = 8;
array1[4] = 10;
Whereas with this code, the debugger only shows the first value, exactly as you describe.
int* array2 = new int[5];
array2[0] = 20;
array2[1] = 21;
array2[2] = 22;
array2[3] = 23;
array2[4] = 24;
Aside: of course, the above code would be followed by this, to avoid leaking memory:
delete[] array2;
Later: This Qt Developer Network Forum Post says that you can tell the debugger to display a pointer as an array:
In Locals and Watchers, context menu of your pointer’s entry, select “Watch Expression”. This creates a new watched expression below.
There, double click on the entry in the “Names” column, and add “#10” to display 10 entries.
This sounds like it should get you going.
Just right-click on your variable, and choose Change Value Display Format and check Array of 100 items.
In Qt for mac what worked for me was:
Add an expression evaluator for the desired variable (right click the variable on the debugger window then "Add expression evaluator for "var name here""
The array variable appears initially as a single value. Just change "var" to "var[start...end] and the array values appear.
Two dimensional arrays sometimes cannot be displayed that way. There is a work-around. First, declare a two-dimensional array as a one-dimensional array like this:
int width = 3;
int height = 4;
int* array2D = new int [width*height];
int x,y;
for(x=width-1;x>-1;x--)
for(y=height-1;y>-1;y--)
array2D[x*height + y] = -1; // mark a breakpoint here!
// add to expression evaluator: (int[3][4]) *array2D
delete [] array2D;
Then add (int[3][4]) *array2D to the expression evaluator. Unfortunately you have to index the array your self, but you can write a special-purpose inline function or use another encapsulation method to make it slightly cleaner.