Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.
// {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
FILE* out) {
std::ifstream file(SHOP_STOCKS_FILE);
std::string line;
ShopStockStruct *shopStock= NULL;
AvailabilityStruct *availability;
int numShopStocks = 0;
char *itemID;
char *itemIDprev = (char *)"";
int shopID;
int amount;
std::vector<std::string> items;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
items = split(line, (char) '\t');
if (items.size() < 3)
continue;
itemID = strdup(items[0].c_str());
if (strcmp(itemIDprev, itemID) != 0) {
// item changed - post process previous item
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
}
shopID = atoi(items[1].c_str());
amount = atoi(items[2].c_str());
/*
* replaced - see below
auto shopStockFound = shopStocks.find(itemID);
if (shopStockFound == shopStocks.end()) {
// not found, so we need to create a new one
shopStock = new ShopStockStruct();
shopStock->itemID = itemID;
// and store it, we'll add from value a bit later
shopStocks[itemID] = *shopStock;
}
else {
// found group already, get a pointer to it for adding nGroupFrom
// value later
shopStock = &(shopStockFound->second);
}
// in any case (found or not), we've to add GroupFrom value
// to do: create struct, and store it in map
availability = new AvailabilityStruct();
availability->shopID = shopID;
availability->amount = amount;
availability->totalAmount = 0;
availability->totalAmount10k = 0;
(shopStock->availability)[shopID] = *availability;
*/
auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
shopStockFound.availability[shopID] = {
.shopID = shopID,
.amount = amount,
.totalAmount = 0,
.totalAmount10k = 0
};
shopStock = &shopStockFound;
numShopStocks++;
}
// process the last item if exists
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}
BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).
Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.
// {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
FILE* out) {
std::ifstream file(SHOP_STOCKS_FILE);
std::string line;
ShopStockStruct *shopStock= NULL;
AvailabilityStruct *availability;
int numShopStocks = 0;
char *itemID;
char *itemIDprev = (char *)"";
int shopID;
int amount;
std::vector<std::string> items;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
items = split(line, (char) '\t');
if (items.size() < 3)
continue;
itemID = strdup(items[0].c_str());
if (strcmp(itemIDprev, itemID) != 0) {
// item changed - post process previous item
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
}
shopID = atoi(items[1].c_str());
amount = atoi(items[2].c_str());
/*
* replaced - see below
auto shopStockFound = shopStocks.find(itemID);
if (shopStockFound == shopStocks.end()) {
// not found, so we need to create a new one
shopStock = new ShopStockStruct();
shopStock->itemID = itemID;
// and store it, we'll add from value a bit later
shopStocks[itemID] = *shopStock;
}
else {
// found group already, get a pointer to it for adding nGroupFrom
// value later
shopStock = &(shopStockFound->second);
}
// in any case (found or not), we've to add GroupFrom value
// to do: create struct, and store it in map
availability = new AvailabilityStruct();
availability->shopID = shopID;
availability->amount = amount;
availability->totalAmount = 0;
availability->totalAmount10k = 0;
(shopStock->availability)[shopID] = *availability;
*/
auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
shopStockFound.availability[shopID] = {
.shopID = shopID,
.amount = amount,
.totalAmount = 0,
.totalAmount10k = 0
};
shopStock = &shopStockFound;
numShopStocks++;
}
// process the last item if exists
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}
BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).
Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.
// {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
FILE* out) {
std::ifstream file(SHOP_STOCKS_FILE);
std::string line;
ShopStockStruct *shopStock= NULL;
AvailabilityStruct *availability;
int numShopStocks = 0;
char *itemID;
char *itemIDprev = (char *)"";
int shopID;
int amount;
std::vector<std::string> items;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
items = split(line, (char) '\t');
if (items.size() < 3)
continue;
itemID = strdup(items[0].c_str());
if (strcmp(itemIDprev, itemID) != 0) {
// item changed - post process previous item
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
}
shopID = atoi(items[1].c_str());
amount = atoi(items[2].c_str());
/*
* replaced - see below
auto shopStockFound = shopStocks.find(itemID);
if (shopStockFound == shopStocks.end()) {
// not found, so we need to create a new one
shopStock = new ShopStockStruct();
shopStock->itemID = itemID;
// and store it, we'll add from value a bit later
shopStocks[itemID] = *shopStock;
}
else {
// found group already, get a pointer to it for adding nGroupFrom
// value later
shopStock = &(shopStockFound->second);
}
// in any case (found or not), we've to add GroupFrom value
// to do: create struct, and store it in map
availability = new AvailabilityStruct();
availability->shopID = shopID;
availability->amount = amount;
availability->totalAmount = 0;
availability->totalAmount10k = 0;
(shopStock->availability)[shopID] = *availability;
*/
auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
shopStockFound.availability[shopID] = {
.shopID = shopID,
.amount = amount,
.totalAmount = 0,
.totalAmount10k = 0
};
shopStock = &shopStockFound;
numShopStocks++;
}
// process the last item if exists
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}
BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).
Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.
// {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
FILE* out) {
std::ifstream file(SHOP_STOCKS_FILE);
std::string line;
ShopStockStruct *shopStock= NULL;
AvailabilityStruct *availability;
int numShopStocks = 0;
char *itemID;
char *itemIDprev = (char *)"";
int shopID;
int amount;
std::vector<std::string> items;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
items = split(line, (char) '\t');
if (items.size() < 3)
continue;
itemID = strdup(items[0].c_str());
if (strcmp(itemIDprev, itemID) != 0) {
// item changed - post process previous item
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
}
shopID = atoi(items[1].c_str());
amount = atoi(items[2].c_str());
/*
* replaced - see below
auto shopStockFound = shopStocks.find(itemID);
if (shopStockFound == shopStocks.end()) {
// not found, so we need to create a new one
shopStock = new ShopStockStruct();
shopStock->itemID = itemID;
// and store it, we'll add from value a bit later
shopStocks[itemID] = *shopStock;
}
else {
// found group already, get a pointer to it for adding nGroupFrom
// value later
shopStock = &(shopStockFound->second);
}
// in any case (found or not), we've to add GroupFrom value
// to do: create struct, and store it in map
availability = new AvailabilityStruct();
availability->shopID = shopID;
availability->amount = amount;
availability->totalAmount = 0;
availability->totalAmount10k = 0;
(shopStock->availability)[shopID] = *availability;
*/
auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
shopStockFound.availability[shopID] = {
.shopID = shopID,
.amount = amount,
.totalAmount = 0,
.totalAmount10k = 0
};
shopStock = &shopStockFound;
numShopStocks++;
}
// process the last item if exists
if (shopStock != NULL)
PostProcessItem(*shopStock, shops, out);
printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}
BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).
Additionally, all shops within the same group also share their effective inventory without needing a special line in the sgm.csv
file.
Additionally, all shops within the same group also share their effective inventory without needing a special line in the sgm.csv
file.
- 67.2k
- 4
- 120
- 284
- 145.5k
- 22
- 190
- 479