all!
I request a JSON from TMDB and save it as a file on the local harddisk. Then I read in the file and decode it with nlohmann::json. The next step is to iterate the json data and extract a few parts of the info. While it is no problem getting types „string“, „boolean“ etc. I’m struggling with an „array“ type. (Later on the type „object“ might show same problems…) Goal is to transform the json data into some „ini“ style type like
[tt1234567]
title = abcdefghij
runtime = 123
...
I iterate through the root of the decoded json by:
using json = nlohmann::json;
{
auto jsonData = json::parse( jsonText );
// std::cout << jsonData.dump( 1 ) << "\n";
for ( const auto &jsonItem : jsonData.items() )
{
jsonKey = jsonItem.key();
jsonValue = "";
if ( jsonItem.value().is_null() ) { jsonValue = "(null)"; }
else if ( jsonItem.value().is_boolean() ) { if ( jsonItem.value() ) { jsonValue = "(boolean) yes"; } else { jsonValue = "(boolean) no"; } }
else if ( jsonItem.value().is_string() ) { jsonValue = "(string) '" + string_left( jsonItem.value(), 25 ) + "'"; }
[ . . . ]
std::cout << jsonKey << ": " << jsonValue << "\n";
Screen output is like:
adult: (boolean) no
belongs_to_collection: (null)
budget: (unsigned) 45000000
credits: (object)
genres: (array) [ . . . ]
[ et al ]
My problem is that I don’t know the correct syntax to handle the „array“ type and, in fact, I’m not quite sure if it is really an array despite the fact it is enclosed in []. The code block
else if ( jsonItem.key() == "genres" ) // array
{
std::cout << " jsonItem: " << jsonItem << "\n"; // {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]}
jsonKey = jsonItem.key();
std::cout << " jsonKey: " << jsonKey << "\n"; // genres
// jsonValue = jsonItem.value(); // <-- returns array, but jsonValue expects string
// std::cout << " jsonValue: " << jsonValue << "\n";
auto jsonValueArray = jsonItem.value().array();
std::cout << " jsonValueArray: " << jsonValueArray << " (" << sizeof( jsonValueArray ) << ")\n"; // [] (16)
auto jsonValueFlat = jsonItem.value().flatten();
std::cout << " jsonValueFlat: " << jsonValueFlat << "\n"; // {"/0/id":12,"/0/name":"Abenteuer","/1/id":28,"/1/name":"Action"}
std::cout << " " << jsonKey << " elements: " << jsonValueArray.size() << "\n"; // 0
i = 0;
// for ( const auto &jsonValue : jsonValueArray )
// for ( i = jsonValueArray.begin(); i < jsonValueArray.end(); i++ )
for ( i = 0; i < jsonValueArray.size(); i++ )
{
std::cout << jsonValue << "\n";
iniKey = "Genre" + std::to_string( i );
iniValue = "";
iniValue = jsonValue;
iniText.append( iniKey );
iniText.append( " = " );
iniText.append( iniValue );
iniText.append( "\n" );
// i++;
}
}
produces
jsonItem: {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]}
jsonKey: genres
jsonValueArray: [] (16)
jsonValueFlat: {"/0/id":12,"/0/name":"Abenteuer","/1/id":28,"/1/name":"Action"}
genres elements: 0
So I see a jsonItem with „genres: [xxx]“ content, thus it is identified as an array. The „sizeof“ returns 16 and I interpret it as 4 pointer with 4 bytes each (or 2 with 8 bytes?). On the other hand the array() function seems to return an empty array [] with 0 elements. And now I’m stuck…
What I want to achieve: Extracting the „genres“ list from the json and concatenate the elements with „;“ like
genres = Abenteuer;Action
in the above exampe.
Michael
Ok, I figured it out. The central point was the misunderstanding of .array() from the JSON object. After fiddling around a bit the following branch
else if ( jsonItem.key() == "genres" ) // array
{
std::cout << " jsonItem: " << jsonItem << "\n"; // {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]}
jsonKey = jsonItem.key();
std::cout << " jsonKey: " << jsonKey << "\n"; // genres
auto jsonValueArray = jsonItem.value(); // [{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}] (16)
std::cout << " jsonValueArray: " << jsonValueArray << " (" << sizeof( jsonValueArray ) << ")\n"; // [] (16)
i = 0;
for ( const auto &jsonValue : jsonValueArray )
{
iniKey = "Genres";
iniValue = jsonValue["name"];
std::cout << " " << jsonValue << " --> key:'" << iniKey << "', value:'" << iniValue << "'\n";
if ( i == 0 )
{
iniText.append( iniKey );
iniText.append( " = " );
}
else
{
iniText.append( ";" );
}
iniText.append( iniValue );
i++;
}
iniText.append( "\n" );
std::cout << " genres added: " << std::to_string( i ) << "\n";
}
now produces
genres: (array) [ . . . ]
jsonItem: {"genres":[{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}]}
jsonKey: genres
jsonValueArray: [{"id":12,"name":"Abenteuer"},{"id":28,"name":"Action"}] (16)
{"id":12,"name":"Abenteuer"} --> key:'Genres', value:'Abenteuer'
{"id":28,"name":"Action"} --> key:'Genres', value:'Action'
genres added: 2
resp.
[tt0446013]
adult = no
Genres = Abenteuer;Action
ID_TMDB = 1534
ID_IMDB = tt0446013
Title_Original = Pathfinder
Overview = An sich stammt der junge
Release_Date = 2007-01-11
Runtime = 99
Tagline = Zwei Welten , ein Krieger
Title = Pathfinder - Fährte des
and that's the output I wanted to have.
Related
continued from C++ API filter by id
code builds fine but im not sure why there's error in getting a response at runtime:
Error:
Bad url: /zxy/5/5/12.png?apikey=[apikey]&borders=False
Code:
this is the Request method
void WwaGlobalTileService::HandleRequest(const NetworkRequest& request)
{
NetworkResponse response = request.GenerateResponse();
VLOG(3) << "url: " << request.Url().path().toStdString();
QUrl url = request.Url();
TilePathParams params( url );
if( request.ContainsField("If-None-Match") )
{
params.setEtag( request.FieldValue("If-None-Match" ) );
}
if (m_ready)
{
if ( params.isTileUrl() &&
(params.hasQueryItem("categories") ||
params.hasQueryItem("significance")))
{
HandleTileRequest( response , params );
}
........
else {
response.SetStatusCode(NetworkResponse::BADREQUEST);
LOG(ERROR) << "Bad url: " << request.Url().toString().toStdString();
}
}
else
{
LOG_EVERY_N(ERROR, 100 ) << "Tiler service is not ready";
response.SetStatusCode(NetworkResponse::SERVUNAVAIL);
}
if (response.StatusCode() != NetworkResponse::NOTSET)
emit Response(response);
}
this is the Response method from the previous question
void Service::HandleTileRequest(NetworkResponse& response,
TilePathParams& params)
{
WwaRenderProperty property = GetRenderProperty( params );
uint etag_lookup = GetQueryHash( params );
QSet<QString> categories;
QString cat_str;
bool categories_valid = GetQueryCategories( params , categories , cat_str );
QSet<QString> significance;
QString sig_str;
bool significance_valid = GetQuerySignificance(params, significance, sig_str);
if( HandleStoredEtag( response , params , etag_lookup) )
{
VLOG(2) << " = Handle Tile Request = " << " Found etag: " << params.getEtag();
return;
}
if( !categories_valid && etag_lookup != 0 && !significance_valid)
{
LOG(ERROR) << "Category Request parameter not valid: "
<< "v: " << categories_valid << " s: " << qPrintable(cat_str) << " e: " << etag_lookup
<< " v: " << significance_valid;
HandleInvalidRequest( response );
return;
}
if( HandleStoredEtag( response , params , etag_lookup ) )
{
VLOG(2) << " = Handle Category Tile Request = " << " Found etag: " << params.getEtag();
return;
}
QRectF rect = m_tile_box->GetNativeBox( params.getX() , params.getY() , params.getZ() );
QMatrix4x4 mat = m_tile_box->GetMatrix( params.getX() , params.getY() , params.getZ() );
// Get ID
int id = 0;
if( params.hasQueryItem( "id" ) )
{
QString id_str = params.queryItemValue("id");
bool success;
id = id_str.toInt( &success );
}
auto wwa_list = m_handler.GetIdsInRect( rect );
auto filterList = [&wwa_list](std::function<bool(QSharedPointer<WwaItem> &)> pred) {
decltype(wwa_list) final_list;
std::copy_if(wwa_list.begin(), wwa_list.end(), std::back_inserter(final_list), pred );
return final_list;
};
for( const auto& item : wwa_list )
{
if (!id == 0 )
{
wwa_list = filterList([id](const QSharedPointer<WwaItem>& wwa_id) { return wwa_id.data()->Number() == id; });
wwa_list.push_back( item );
break;
}
if (!categories.isEmpty())
{
wwa_list = filterList([&categories](const QSharedPointer<WwaItem>& wwa_cat) { return categories.contains(wwa_cat.data()->Category()); });
wwa_list.push_back( item );
}
if (!significance.isEmpty())
{
wwa_list = filterList([&significance](const QSharedPointer<WwaItem>& wwa_sig) { return significance.contains(wwa_sig.data()->Significance()); });
wwa_list.push_back( item );
}
}
auto wwa_priority_list = m_handler.FilterByPriority( wwa_list );
QImage img;
int result = WwaRenderer::GetInstance().RenderTileRect(
property , mat , img , wwa_priority_list);
if( !result )
{
VLOG(2) << "Handle Empty Image";
HandleEmptyImage( response , params);
} else {
QtConcurrent::run(this, &Service::Compress, img, response, params , etag_lookup );
}
if( VLOG_IS_ON( 2 ) )
{
VLOG(2) << " = Handle Tile Request = "
<< " x: " << params.getX()
<< " y: " << params.getY()
<< " z: " << params.getZ()
<< " w: " << wwa_list.size()
<< " wpl: " << wwa_priority_list.size();
}
}
RE: "my goal is to filter the request so that it returns only responses filtered by id when a parameter id=[123456] to the request call URL.
but I'm just getting bad URL error
What am I missing?"
I'm relatively new programming c++. I'm implementing a tree like index for a db using unorderd_map on the implementation of the tree data structure to store the children nodes. As im working with tree like structures the construction an search methods are recursive, also i store the pointers of the nodes, so i suspect i may have a sort of not well handled memory issue. I'm getting a segmentation fault. Next is my code and the output of it.
#include <memory>
#include <sstream>
#include <unordered_map>
#include <iostream>
#include <string>
#include <sqlite3.h>
#include "aux_functions.cpp"
#include <math.h>
using namespace std;
class TreeLikeIndex
{
public:
TreeLikeIndex(string attribute, string indices, int indices_count, short int is_leaf, unordered_map<string, TreeLikeIndex*> children);
TreeLikeIndex(string indices, int indices_count);
TreeLikeIndex();
string search(unordered_map<string, string> *);
private:
string indices;
int indices_count;
short int is_leaf;
string attribute;
unordered_map<string, TreeLikeIndex*> children;
};
string TreeLikeIndex::search(unordered_map<string, string> * _tuple)
{
if((*_tuple).empty() || this->is_leaf ) return this->indices;
string att_val = (*_tuple)[this->attribute];
(*_tuple).erase(this->attribute);
TreeLikeIndex * child_with_that_value = this->children[att_val];
return (*child_with_that_value).search(_tuple);
}
class DecisionTreeLikeIndexer
{
public:
DecisionTreeLikeIndexer(string, string, string);
int rebuild_index();
TreeLikeIndex * get_index();
private:
TreeLikeIndex * build_index(unordered_set<string> attributes_list, int depth, string comma_separated_ids, int ids_list_count);
TreeLikeIndex * index;
string source_db_address;
string dest_folder_address;
time_t time_of_last_build;
unordered_set<string> columns_names;
string source_table_name;
unordered_set<string> temp_tables_names;
string id_column_name;
sqlite3 * source_db_connection;
int table_count;
};
int DecisionTreeLikeIndexer::rebuild_index()
{
this->index = this->build_index(this->columns_names, 0, "", 0);
this->time_of_last_build = time(NULL);
return 0;
}
TreeLikeIndex * DecisionTreeLikeIndexer::get_index()
{
return this->index;
}
DecisionTreeLikeIndexer::DecisionTreeLikeIndexer(string source_db_address, string table_name, string dest_folder_address)
{
this->source_db_address = source_db_address;
this->dest_folder_address = dest_folder_address;
this->columns_names = Aux::get_column_names(source_db_address, table_name);
this->source_table_name = table_name;
this->id_column_name = "rowid";
this->source_db_connection = Aux::get_db_connection(this->source_db_address);
// Getting count of this table
sqlite3_stmt* statement;
string query = "SELECT count(*) FROM " + this->source_table_name + ";";
if(sqlite3_prepare(this->source_db_connection, query.c_str(), -1, &statement, 0) == SQLITE_OK)
{
int res = sqlite3_step(statement);
const unsigned char * count_char = sqlite3_column_text(statement,0);
if(res == SQLITE_ROW)
{
stringstream _temp;
_temp << count_char;
_temp >> this->table_count;
}
sqlite3_finalize(statement);
}
else
{
cout << "Error initializating Indexer (Getting initial table count): " << sqlite3_errmsg(this->source_db_connection) << endl;
}
}
TreeLikeIndex * DecisionTreeLikeIndexer::build_index(unordered_set<string> attributes_list, int depth, string comma_separated_ids, int ids_list_count)
{
if( attributes_list.size() <=1 || (depth > 0 && ids_list_count <= 1))
{
Aux::tabs(depth);
cout << "Leaf at depth: " << depth << " Ids are: " << comma_separated_ids << " Ids count: " << ids_list_count << endl;
static TreeLikeIndex * node = new TreeLikeIndex((string)comma_separated_ids, (int)ids_list_count);
return node;
}
string source_table = this->source_table_name;
int count = this->table_count;
if(depth > 0)
{
while(1)
{
source_table = *Aux::get_random_list_of_strings(1).begin();
if(this->temp_tables_names.insert(source_table).second) break;
}
const string create_temp_table_stmnt = "CREATE TEMP TABLE " + source_table + " AS SELECT * FROM " + this->source_table_name + " WHERE " + this->id_column_name + " IN(" + comma_separated_ids + ")";
sqlite3_exec(this->source_db_connection, create_temp_table_stmnt.c_str(),Aux::sqlt_callback,0,NULL);
count = ids_list_count;
Aux::tabs(depth);
cout << "Not root node" << endl;
}
Aux::tabs(depth);
cout << "Source table is: " << source_table << " Table count is: " << count << endl;
Aux::tabs(depth);
cout << "Attributes list is: "; for_each(attributes_list.begin(), attributes_list.end(),[](string v){cout << v << " ";});
cout << endl;
const double E = log2(count) ;
Aux::tabs(depth);
cout << "Entropy of node: " << E << endl;
string best_attribute;
double best_gain;
unordered_set<string> best_attribute_values;
for(string attr: attributes_list)
{
Aux::tabs(depth+1);
cout << "Analysing attribute: " << attr << endl;
const string get_at_count_values_query = "SELECT " + attr + ", count(" + attr + ") FROM " + source_table + " GROUP BY " + attr + ";";
sqlite3_stmt * stmnt;
double weighted_entropy = 0;
unordered_set<string> this_att_values;
if(sqlite3_prepare(this->source_db_connection, get_at_count_values_query.c_str(), -1, &stmnt, 0) == SQLITE_OK)
{
for(;;)
{
int res = sqlite3_step(stmnt);
if(res == SQLITE_DONE || res==SQLITE_ERROR)
{
double gAti = E - weighted_entropy;
Aux::tabs(depth+1);
cout << "Finish computing WE for att: " << attr << " Gain is: " << gAti << endl;
if(gAti > best_gain)
{
Aux::tabs(depth+1);
cout << "Found attribute with better gain." << endl;
best_gain = gAti;
best_attribute = attr;
best_attribute_values.clear();
Aux::tabs(depth+1);
for(string v:this_att_values)
{
best_attribute_values.insert(v);
}
cout << endl;
this_att_values.clear();
}
sqlite3_finalize(stmnt);
//delete &res;
break;
}
if(res == SQLITE_ROW)
{
string val = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmnt,0)));
int vSize = sqlite3_column_int(stmnt,1);
Aux::tabs(depth+2);
this_att_values.insert(val);
double ratio = double(vSize) / double(count);
weighted_entropy += double(ratio) * double(log2(vSize));
Aux::tabs(depth+2);
cout << "Processing value: " << val << " With vSize: " << vSize << " Current WE is: " << weighted_entropy << endl;
}
}
}
}
Aux::tabs(depth);
cout << "Finish processing attributes list. Best attribute is: " << best_attribute << " Best gain is: " << best_gain << endl;
Aux::tabs(depth);
cout << "Best attribute values are: "; for_each(best_attribute_values.begin(), best_attribute_values.end(), [](string v){cout << v << ",";}); cout << endl;
unordered_map<string, TreeLikeIndex *> children;
for(string val: best_attribute_values)
{
const string get_ids_of_bestatt_val = "SELECT rowid FROM " + source_table + " WHERE " + best_attribute + " = " + val + ";";
int ids_count = 0;
sqlite3_stmt * stmnt;
string ids = "";
bool first = 1;
int next_depth = depth + 1;
unordered_set<string> next_attributes_set;
for(string attr: attributes_list) if(attr != best_attribute) next_attributes_set.insert(attr);
if(sqlite3_prepare(this->source_db_connection, get_ids_of_bestatt_val.c_str(), -1, &stmnt,0) == SQLITE_OK)
{
for(;;)
{
int res = sqlite3_step(stmnt);
if(res == SQLITE_ROW)
{
string id = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmnt,0)));
if(!first) ids += "," + id;
else ids += id;
ids_count++;
}
if(res == SQLITE_DONE || res == SQLITE_ERROR)
{
Aux::tabs(depth+1);
cout << "Adding branch for val: " << val << endl;
Aux::tabs(depth+1);
cout << " Next attributes are: "; for_each(next_attributes_set.begin(), next_attributes_set.end(), [](string v){cout << v << ",";});
cout << " Depth is: " << next_depth << " Ids are: " << ids << " Ids count: " << ids_count << endl;
sqlite3_finalize(stmnt);
static TreeLikeIndex * temp_child = this->build_index(next_attributes_set, next_depth, ids, ids_count);
pair<string, TreeLikeIndex*> child (val, temp_child);
children.insert(child);
}
}
}
}
Aux::tabs(depth);
cout << "Finish processing node, will return." << endl;
static TreeLikeIndex * no_leaf_node = new TreeLikeIndex(best_attribute, "all", count, 0, children);
return no_leaf_node;
}
}
TreeLikeIndex::TreeLikeIndex(std::string attribute, std::string indices, int indices_count, short int is_leaf, unordered_map<std::string, TreeLikeIndex*> children)
{
this->attribute = attribute;
this->indices = indices;
this->is_leaf = is_leaf;
this->children = children;
this->children.clear();
for(pair<string, TreeLikeIndex*> p: children) this->children.insert(p);
this->indices_count = indices_count;
}
TreeLikeIndex::TreeLikeIndex(string indices, int indices_count)
{
this->indices = indices;
this->indices_count = indices_count;
this->is_leaf = 1;
}
TreeLikeIndex::TreeLikeIndex()
{
this->indices = "";
this->indices_count = 0;
this->is_leaf = 1;
}
int main()
{
string source_db_address = "my_table";
string table_name = "b";
string dest_folder_address = ".";
DecisionTreeLikeIndexer indexer(source_db_address, table_name, dest_folder_address);
indexer.rebuild_index();
}
And the output is:
Source table is: b Table count is: 9
Attributes list is: cant_n_dec cant_n_des cant_n_control
Entropy of node: 3.16993
Analysing attribute: cant_n_dec
Processing value: 1 With vSize: 1 Current WE is: 0
Processing value: 2 With vSize: 4 Current WE is: 0.888889
Processing value: 3 With vSize: 2 Current WE is: 1.11111
Processing value: 4 With vSize: 1 Current WE is: 1.11111
Processing value: 5 With vSize: 1 Current WE is: 1.11111
Finish computing WE for att: cant_n_dec Gain is: 2.05881
Found attribute with better gain.
Analysing attribute: cant_n_des
Processing value: 1 With vSize: 2 Current WE is: 0.222222
Processing value: 2 With vSize: 4 Current WE is: 1.11111
Processing value: 3 With vSize: 2 Current WE is: 1.33333
Processing value: 5 With vSize: 1 Current WE is: 1.33333
Finish computing WE for att: cant_n_des Gain is: 1.83659
Analysing attribute: cant_n_control
Processing value: 1 With vSize: 2 Current WE is: 0.222222
Processing value: 2 With vSize: 3 Current WE is: 0.750543
Processing value: 3 With vSize: 3 Current WE is: 1.27886
Processing value: 5 With vSize: 1 Current WE is: 1.27886
Finish computing WE for att: cant_n_control Gain is: 1.89106
Finish processing attributes list. Best attribute is: cant_n_dec Best gain is: 2.05881
Best attribute values are: 1,2,3,4,5,
Adding branch for val: 1
Next attributes are: cant_n_control,cant_n_des, Depth is: 1 Ids are: 3 Ids count: 1
Leaf at depth: 1 Ids are: 3 Ids count: 1
Segmentation fault
I'm not shure but....
I think the problem can be in the following cycle
for(;;)
{
int res = sqlite3_step(stmnt);
if(res == SQLITE_ROW)
{
string id = std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmnt,0)));
if(!first) ids += "," + id;
else ids += id;
ids_count++;
}
if(res == SQLITE_DONE || res == SQLITE_ERROR)
{
Aux::tabs(depth+1);
cout << "Adding branch for val: " << val << endl;
Aux::tabs(depth+1);
cout << " Next attributes are: "; for_each(next_attributes_set.begin(), next_attributes_set.end(), [](string v){cout << v << ",";});
cout << " Depth is: " << next_depth << " Ids are: " << ids << " Ids count: " << ids_count << endl;
sqlite3_finalize(stmnt);
static TreeLikeIndex * temp_child = this->build_index(next_attributes_set, next_depth, ids, ids_count);
pair<string, TreeLikeIndex*> child (val, temp_child);
children.insert(child);
}
}
I don't understand when terminate (no exit conditions in the for(;;), no return's and no break's in the block).
And I suspect that che segmentation fault is caused by the following instruction
int res = sqlite3_step(stmnt);
when, after the SQLITE_DONE or SQLITE_ERROR case (whith a call to
sqlite3_finalize(stmnt);
), the cycle is iterated again, with a stmnt invalid.
The following can be a solution?
if ( sqlite3_prepare(this->source_db_connection, get_ids_of_bestatt_val.c_str(), -1, &stmnt,0) == SQLITE_OK)
{
while ( sqlite3_step(stmnt) == SQLITE_ROW )
{
ids += ( first ? "" : "," )
+ std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmnt,0)));
ids_count++;
}
Aux::tabs(depth+1);
cout << "Adding branch for val: " << val << endl;
Aux::tabs(depth+1);
cout << " Next attributes are: ";
for_each(next_attributes_set.begin(), next_attributes_set.end(), [](string v){cout << v << ",";});
cout << " Depth is: " << next_depth << " Ids are: " << ids << " Ids count: " << ids_count << endl;
sqlite3_finalize(stmnt);
static TreeLikeIndex * temp_child = this->build_index(next_attributes_set, next_depth, ids, ids_count);
pair<string, TreeLikeIndex*> child (val, temp_child);
children.insert(child);
}
I have a POD class and I want to make it movable for efficiency. I keep all the data in a std::array member object, and I make my public member variables references to parts of this std::array object. By doing this, now I am able to move the entire data by moving the std::array instance in the move constructor (I know that it is not literally a POD class anymore after writing constructors.).
Is this a good method of doing this? Does it actually move the data? See the code output below: After moving the std::array, I observe that both objects have the same values. It looks like it doesn't move, but it copies the data. What is the problem here?
#include <array>
class MyPodClass
{
private:
typedef double TYPE_x;
typedef double TYPE_y;
typedef double TYPE_z;
typedef int TYPE_p;
typedef int TYPE_r;
typedef int TYPE_s;
typedef char TYPE_k;
typedef char TYPE_l;
typedef char TYPE_m;
typedef float TYPE_a;
typedef float TYPE_b;
typedef float TYPE_c;
enum TypeSizes
{
STARTING_POSITION_x = 0,
STARTING_POSITION_y = STARTING_POSITION_x + sizeof(TYPE_x),
STARTING_POSITION_z = STARTING_POSITION_y + sizeof(TYPE_y),
STARTING_POSITION_p = STARTING_POSITION_z + sizeof(TYPE_z),
STARTING_POSITION_r = STARTING_POSITION_p + sizeof(TYPE_p),
STARTING_POSITION_s = STARTING_POSITION_r + sizeof(TYPE_r),
STARTING_POSITION_k = STARTING_POSITION_s + sizeof(TYPE_s),
STARTING_POSITION_l = STARTING_POSITION_k + sizeof(TYPE_k),
STARTING_POSITION_m = STARTING_POSITION_l + sizeof(TYPE_l),
STARTING_POSITION_a = STARTING_POSITION_m + sizeof(TYPE_m),
STARTING_POSITION_b = STARTING_POSITION_a + sizeof(TYPE_a),
STARTING_POSITION_c = STARTING_POSITION_b + sizeof(TYPE_b),
END_POSITION = STARTING_POSITION_c + sizeof(TYPE_c),
};
std::array<unsigned char, END_POSITION> MovableBulkData;
public:
MyPodClass()
: //x(*static_cast<TYPE_x*>(&MovableBulkData[STARTING_POSITION_x])), // ERROR: Invalid type conversion. Why?
x(*(TYPE_x*)(&MovableBulkData[STARTING_POSITION_x])),
y(*(TYPE_y*)(&MovableBulkData[STARTING_POSITION_y])),
z(*(TYPE_z*)(&MovableBulkData[STARTING_POSITION_z])),
p(*(TYPE_p*)(&MovableBulkData[STARTING_POSITION_p])),
r(*(TYPE_r*)(&MovableBulkData[STARTING_POSITION_r])),
s(*(TYPE_s*)(&MovableBulkData[STARTING_POSITION_s])),
k(*(TYPE_k*)(&MovableBulkData[STARTING_POSITION_k])),
l(*(TYPE_l*)(&MovableBulkData[STARTING_POSITION_l])),
m(*(TYPE_m*)(&MovableBulkData[STARTING_POSITION_m])),
a(*(TYPE_a*)(&MovableBulkData[STARTING_POSITION_a])),
b(*(TYPE_b*)(&MovableBulkData[STARTING_POSITION_b])),
c(*(TYPE_c*)(&MovableBulkData[STARTING_POSITION_c]))
{
}
MyPodClass(MyPodClass && RValue)
: MovableBulkData(std::move(RValue.MovableBulkData)),
x(*(TYPE_x*)(&MovableBulkData[STARTING_POSITION_x])),
y(*(TYPE_y*)(&MovableBulkData[STARTING_POSITION_y])),
z(*(TYPE_z*)(&MovableBulkData[STARTING_POSITION_z])),
p(*(TYPE_p*)(&MovableBulkData[STARTING_POSITION_p])),
r(*(TYPE_r*)(&MovableBulkData[STARTING_POSITION_r])),
s(*(TYPE_s*)(&MovableBulkData[STARTING_POSITION_s])),
k(*(TYPE_k*)(&MovableBulkData[STARTING_POSITION_k])),
l(*(TYPE_l*)(&MovableBulkData[STARTING_POSITION_l])),
m(*(TYPE_m*)(&MovableBulkData[STARTING_POSITION_m])),
a(*(TYPE_a*)(&MovableBulkData[STARTING_POSITION_a])),
b(*(TYPE_b*)(&MovableBulkData[STARTING_POSITION_b])),
c(*(TYPE_c*)(&MovableBulkData[STARTING_POSITION_c]))
{
}
const MyPodClass & operator=(MyPodClass && RValue)
{
MovableBulkData = std::move(RValue.MovableBulkData);
return *this;
}
TYPE_x & x;
TYPE_y & y;
TYPE_z & z;
TYPE_p & p;
TYPE_r & r;
TYPE_s & s;
TYPE_k & k;
TYPE_l & l;
TYPE_m & m;
TYPE_a & a;
TYPE_b & b;
TYPE_c & c;
};
int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
MyPodClass PodObject1, PodObject2;
PodObject1.y = 3.4;
PodObject1.s = 4;
PodObject1.m = 'm';
PodObject1.a = 2.3f;
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << "Moving PodObject1 to PodObject2..." << std::endl << std::endl;
PodObject2 = std::move(PodObject1);
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << "Modifying PodObject1 and PodObject2..." << std::endl << std::endl;
PodObject1.s = 5;
PodObject2.m = 'n';
std::cout << "PodObject1.y = " << PodObject1.y << std::endl;
std::cout << "PodObject1.s = " << PodObject1.s << std::endl;
std::cout << "PodObject1.m = " << PodObject1.m << std::endl;
std::cout << "PodObject1.a = " << PodObject1.a << std::endl << std::endl;
std::cout << "PodObject2.y = " << PodObject2.y << std::endl;
std::cout << "PodObject2.s = " << PodObject2.s << std::endl;
std::cout << "PodObject2.m = " << PodObject2.m << std::endl;
std::cout << "PodObject2.a = " << PodObject2.a << std::endl << std::endl;
std::cout << std::endl;
_wsystem(L"timeout /t 60 /nobreak");
return 0;
}
Output:
PodObject1.y = 3.4
PodObject1.s = 4
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = -9.25596e+61
PodObject2.s = -858993460
PodObject2.m = ╠
PodObject2.a = -1.07374e+08
Moving PodObject1 to PodObject2...
PodObject1.y = 3.4
PodObject1.s = 4
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = 3.4
PodObject2.s = 4
PodObject2.m = m
PodObject2.a = 2.3
Modifying PodObject1 and PodObject2...
PodObject1.y = 3.4
PodObject1.s = 5
PodObject1.m = m
PodObject1.a = 2.3
PodObject2.y = 3.4
PodObject2.s = 4
PodObject2.m = n
PodObject2.a = 2.3
This is a misuse of move semantics. Since your class contains a number of simple data members like int and float, there is really nothing to move. You'd be better off with memcpy(), which is probably close to what your compiler gives you for free if you just write the class the normal, naive way, with no std::array and no pointer gymnastics.
Move semantics would have been useful here if your class contained e.g. a std::string, because std::string uses dynamically allocated memory which can be "moved" (read: adopted) into the target of a move.
The above of course means that you could "fix" your problem by dynamically allocating the array, which would allow you to move it. But in the end this would be a baroque way to achieve the effect of using a trivial POD class with no gymnastics and storing it in a std::unique_ptr, which of course enables move semantics.
I have the following code:
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file( "C:/Users/James/Documents/Visual Studio 2013/Projects/Games/Jimmy/Game/Assets/Levels/Scene.dae" );
std::cout << "Load result: " << result.description( ) << ", mesh name: " << doc.child( "mesh" ).attribute( "name" ).value( ) << std::endl;
pugi::xml_node tools = doc.child( "Profile" ).child( "Tools" );
for( pugi::xml_node tool = tools.child( "Tool" ); tool; tool = tool.next_sibling( "Tool" ) )
{
std::cout << "Tool " << tool.attribute( "Filename" ).value( );
std::cout << ": AllowRemote " << tool.attribute( "AllowRemote" ).as_bool( );
std::cout << ", Timeout " << tool.attribute( "Timeout" ).as_int( );
std::cout << ", Description '" << tool.child_value( "Description" ) << "'/n";
}
For some reason I am getting:
+ result {status=status_file_not_found (1) offset=0 encoding=encoding_auto (0) } pugi::xml_parse_result
Any ideas why it can't find my file?
pugi only opens .xml... I also had to change my path to a relative one.
I used the library streed2006.cpp from source. The code has memory leak in deletion of edges. I cleared the number of edges from hashtable using the following code:
//throwing away the edges from hashtable
for(int t=0;t<HASH_TABLE_SIZE;t++)
{
Edges[t].Remove();
Edges[t].start_node == -1
}
valgrind output:
3,920 bytes in 245 blocks are definitely lost in loss record 9 of 12
==6301== at 0x4029F34: operator new(unsigned int) (in /usr/lib/valgrind /vgpreload_memcheck-x86-linux.so)
==6301== by 0x804A683: Edge::SplitEdge(Suffix&) (suffix_tree.cpp:555)
==6301== by 0x804B02F: AddPrefix(Suffix&, int) (suffix_tree.cpp:753)
Please guide me how to delete the edges.
was able to remove the memory leak. Following is the solution:
void AddPrefix( Suffix &active, int last_char_index )
{
int parent_node;
int last_parent_node = -1;
for ( ; ; ) {
Edge edge;
parent_node = active.origin_node;
if ( active.Explicit() ) {
edge = Edge::Find( active.origin_node, T[ last_char_index ] );
if ( edge.start_node != -1 )
break;
} else { //implicit node, a little more complicated
edge = Edge::Find( active.origin_node, T[ active.first_char_index ] );
int span = active.last_char_index - active.first_char_index;
if ( T[ edge.first_char_index + span + 1 ] == T[ last_char_index ] )
break;
parent_node = edge.SplitEdge( active );
}
Edge *new_edge = new Edge( last_char_index, T.N, parent_node );
new_edge->Insert();
//cout << "Created edge to new leaf: " << *new_edge << "\n";
AddSuffixLink( last_parent_node, parent_node );
if ( active.origin_node == 0 ) {
//cout << "Can't follow suffix link, I'm at the root\n";
active.first_char_index++;
} else {
/*
cout << "Following suffix link from node "
<< active.origin_node
<< " to node "
<< Suffix_Nodes[ active.origin_node ].suffix_node
<< ".\n";
*/
active.origin_node = Suffix_Nodes[ active.origin_node ].suffix_node;
//cout << "New prefix : " << active << "\n";
}
active.Canonize();
delete(new_edge);
new_edge = NULL;
}
AddSuffixLink( last_parent_node, parent_node );
active.last_char_index++; //Now the endpoint is the next active point
active.Canonize();
};
and
int Edge::SplitEdge( Suffix &s )
{
//cout << "Splitting edge: " << *this << "\n";
Remove();
Edge *new_edge =
new Edge( first_char_index,
first_char_index + s.last_char_index - s.first_char_index,
s.origin_node );
new_edge->Insert();
Suffix_Nodes[ new_edge->end_node ].suffix_node = s.origin_node;
first_char_index += s.last_char_index - s.first_char_index + 1;
start_node = new_edge->end_node;
Insert();
//cout << "New edge: " << *new_edge << "\n";
//cout << "Old edge: " << *this << "\n";
delete(new_edge);
//return new_edge->end_node;
return(start_node);
}
void AddPrefix( Suffix &active, int last_char_index )
{
int parent_node;
int last_parent_node = -1;
for ( ; ; ) {
Edge edge;
parent_node = active.origin_node;
if ( active.Explicit() ) {
edge = Edge::Find( active.origin_node, T[ last_char_index ] );
if ( edge.start_node != -1 )
break;
} else { //implicit node, a little more complicated
edge = Edge::Find( active.origin_node, T[ active.first_char_index ] );
int span = active.last_char_index - active.first_char_index;
if ( T[ edge.first_char_index + span + 1 ] == T[ last_char_index ] )
break;
parent_node = edge.SplitEdge( active );
}
Edge *new_edge = new Edge( last_char_index, T.N, parent_node );
new_edge->Insert();
//cout << "Created edge to new leaf: " << *new_edge << "\n";
AddSuffixLink( last_parent_node, parent_node );
if ( active.origin_node == 0 ) {
//cout << "Can't follow suffix link, I'm at the root\n";
active.first_char_index++;
} else {
/*
cout << "Following suffix link from node "
<< active.origin_node
<< " to node "
<< Suffix_Nodes[ active.origin_node ].suffix_node
<< ".\n";
*/
active.origin_node = Suffix_Nodes[ active.origin_node ].suffix_node;
//cout << "New prefix : " << active << "\n";
}
active.Canonize();
//ADDED THIS DELETE HERE
delete(new_edge);
new_edge = NULL;
}
AddSuffixLink( last_parent_node, parent_node );
active.last_char_index++; //Now the endpoint is the next active point
active.Canonize();
};
and
int Edge::SplitEdge( Suffix &s )
{
//cout << "Splitting edge: " << *this << "\n";
Remove();
Edge *new_edge =
new Edge( first_char_index,
first_char_index + s.last_char_index - s.first_char_index,
s.origin_node );
new_edge->Insert();
Suffix_Nodes[ new_edge->end_node ].suffix_node = s.origin_node;
first_char_index += s.last_char_index - s.first_char_index + 1;
start_node = new_edge->end_node;
Insert();
//cout << "New edge: " << *new_edge << "\n";
//cout << "Old edge: " << *this << "\n";
//ADDED THIS DELETE HERE
delete(new_edge);
//return new_edge->end_node;
return(start_node);
}