Creating and iterating through a hash table #42
-
I'm having trouble getting hash table iteration to work. Based on the documentation, here's what I am trying:
The issue is that the for loop is never entered, and it seems the iterator is always null. When debugging I am able to see that container has an xtype of WR_EX_HASH_TABLE and a size of 2. Am I going about this the right way? (I am using C++ 17 by the way) For more context, I would ultimately like to create a hash table, add values, then pass it as an argument to a wrench function, then iterate through it in the C++ code to check for changes and update a corresponding C++ hash accordingly. Here's a full example:
Again, the problem here is that the for loop is never entered (and as such the C++ values map is not updated), however the wrench code does run correctly and interacts with the hash table as expected. Here is the output:
Any help is greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Okay I'll look into this as soon as I can, but I am off for the week.
…On Tue, Jul 2, 2024 at 8:08 PM Ander ***@***.***> wrote:
I'm having trouble getting hash table iteration to work. Based on the
documentation, here's what I am trying:
WRValue container;
wr_makeContainer( &container );
WRValue integer;
wr_makeInt( &integer, 99 );
wr_addValueToContainer( &container, "integer", &integer );
WRValue integer2;
wr_makeInt( &integer2, 100 );
wr_addValueToContainer( &container, "integer2", &integer2 );
for (WRIteratorEntry const& member : container)
{
cout << member.value->asInt() << "\n";
}
wr_destroyContainer(&container);
The issue is that the for loop is never entered, and it seems the iterator
is always null. When debugging I am able to see that container has an xtype
of WR_EX_HASH_TABLE and a size of 2. Am I going about this the right way?
(I am using C++ 17 by the way)
For more context, I would ultimately like to create a hash table, add
values, then pass it as an argument to a wrench function, then iterate
through it in the C++ code to check for changes and update a corresponding
C++ hash accordingly. Here's a full example:
#include <iostream>
#include <map>
#include <string.h>
#include "wrench.h"
using namespace std;
void print(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
char buf[128];
for (int i = 0; i < argn; ++i)
{
cout << (argv[i].asString(buf, 128));
cout << "\n";
}
}
int main() {
WRState* wr_state = wr_newState(20);
wr_registerFunction(wr_state, "print", print);
std::map<string, int> values;
values["temperature"] = 80;
values["humidity"] = 11;
string code = "function wrenchfunction(values){"
"print(\"humidity:\");print( values[\"humidity\"] );"
"print(\"temperature:\");print( values[\"temperature\"] );"
"values[\"humidity\"] += 100;"
"print(\"humidity:\");print( values[\"humidity\"] );"
"print(\"temperature:\");print( values[\"temperature\"] );"
"return values;}";
//Transform Values
WRValue wrench_values;
wr_makeContainer(&wrench_values);
for (const auto& [id, value] : values) {
WRValue* wr_value = new WRValue();
wr_makeInt(wr_value, value);
wr_addValueToContainer(&wrench_values, id.c_str(), wr_value);
}
//Compile and Run
unsigned char* outBytes;
int outLen;
const char* wrenchCode = code.c_str();
wr_compile(wrenchCode, strlen(wrenchCode), &outBytes, &outLen);
WRContext* context = wr_run(wr_state, outBytes, outLen);
wr_callFunction(context, "wrenchfunction", &wrench_values, 1);
free(outBytes);
//Update Values and Free Memory
for (WRIteratorEntry const& member : wrench_values)
{
char key[156];
values[(member.key->asString(key, 156))] = member.value->asInt();
delete member.value;
}
cout << "\nUpdated Values:\n";
for (const auto& [key, value] : values) {
cout << key << ": " << value << "\n";
}
wr_destroyContainer(&wrench_values);
}
Again, the problem here is that the for loop is never entered (and as such
the C++ values map is not updated), however the wrench code does run
correctly and interacts with the hash table as expected.
—
Reply to this email directly, view it on GitHub
<#42>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALIKA6F2BZMALBQKJNAFYTZKM6HDAVCNFSM6AAAAABKIPWGUCVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZWHA4TCNJRGE>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Okay preliminary check it seems I am generating the end/begin objects incorrectly for hash tables. This is only a c++ iterator problem because internally wrench doesn't use them. I'll figure out a good solution to this and should have a fix in a few days This issue was removed? is it still relevant? |
Beta Was this translation helpful? Give feedback.
-
This has been fixed in 6.0.4 I changed the way memory is allocated and also made sure the iterating will work. |
Beta Was this translation helpful? Give feedback.
This has been fixed in 6.0.4 I changed the way memory is allocated and also made sure the iterating will work.