Boost: How to add child tree into an existing tree - boost

I have the following property tree.
propset1
{
prop1 2
prop2 5
prop3 60
prop4 7
}
I am trying to add couple of child trees to this so that the resulting property tree would look like below.
propset1
{
prop1 2
prop2 5
prop3 60
prop4 5
childset1
{
child1prop1 4
child1prop1 6
child1prop1 9
}
childset2
{
child2prop1 2
child2prop1 6
child2prop1 7
}
}
I wrote the following code.
// Add General config
pt::ptree propset1;
propset1.put("propset1.prop1", 2);
propset1.put("propset1.prop2", 5);
propset1.put("propset1.prop3", 60);
propset1.put("propset1.prop4", 7);
// Add childset1 config
pt::ptree childset1;
childset1.add("child1prop1", 4);
childset1.add("child1prop2", 6);
childset1.add("child1prop3", 9);
propset1.add_child("childset1", childset1);
// Add childset2 config
pt::ptree childset2;
childset2.add("child2prop1", 2);
childset2.add("child2prop2", 6);
childset2.add("child2prop3", 7);
propset1.add_child("childset2", childset2);
However the above code is resulting into below property tree.
propset1
{
prop1 2
prop2 5
prop3 60
prop4 5
childset1
{
child1prop1 4
child1prop1 6
child1prop1 9
}
}
propset1
{
prop1 2
prop2 5
prop3 60
prop4 5
childset2
{
child2prop1 2
child2prop1 6
child2prop1 7
}
}
Can someone please suggest what is correct use of API?

You're something else differently that you're not showing:
Live On Coliru
#include <boost/property_tree/info_parser.hpp>
#include <sstream>
#include <iostream>
using boost::property_tree::ptree;
int main() {
ptree pt;
{
std::istringstream iss(R"(propset1
{
prop1 2
prop2 5
prop3 60
prop4 7
})");
read_info(iss, pt);
}
auto& propset1 = pt.get_child("propset1");
// Add childset1 config
ptree childset1;
childset1.add("child1prop1", 4);
childset1.add("child1prop2", 6);
childset1.add("child1prop3", 9);
propset1.add_child("childset1", childset1);
// Add childset2 config
ptree childset2;
childset2.add("child2prop1", 2);
childset2.add("child2prop2", 6);
childset2.add("child2prop3", 7);
propset1.add_child("childset2", childset2);
write_info(std::cout, pt);
}
Prints:
propset1
{
prop1 2
prop2 5
prop3 60
prop4 7
childset1
{
child1prop1 4
child1prop2 6
child1prop3 9
}
childset2
{
child2prop1 2
child2prop2 6
child2prop3 7
}
}

Related

Protobuf memory error when adding 6 array messages

I am getting this error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 67112960 bytes) in C:\xampp\htdocs\... when i adding 6 array message in my proto file. I am using php protobuf protocol buffers for serialization.
syntax = "proto3";
message data {
.data_step_one test = 2;
}
message data_step_one {
.data_step_two test1 = 1;
}
message data_step_two {
.data_step_three test2 = 2;
}
message data_step_three {
.data_step_four test3 = 2;
}
message data_step_four {
.data_step_five test4 = 14;
}
message data_step_five {
.data_step_six test5 = 1;
}
message data_step_six {
string something = 1;
}
When i remove string (string something = 1;) from last six array
message data_step_six {
}
its worked without that error. Just i got that error when i add something in six message. I am new in this protobuf message, so don't understand where was the problem and how will i solve it.
Updated More:
This is my original data for post method (I followed this for create proto messages):
1 {
1: 1
2: 58480067980758310
4: 0
}
2 {
1 {
1: 112
2 {
1: id:a-7769307577821621420
2 {
14 {
1 {
1: mid:b-6430467773153509964
2 {
1: 1
2: mcacc.com
3: Emma Ava
10: mcacc.com
}
3 {
1: 1
2: scssd.com
}
7: 1627577996674
8: Test
9 {
2 {
1: 0
2: <div dir='auto'>This is test.</div>
}
7: 1
}
11: ^a
11: ^all
11: ^f_bt
11: ^f_cl
11: ^f_btns
11: ^pfg
18: 1627577996674
36 {
6: 0
}
37 {
4: 0
}
42: 0
52: s:23cbdb9f4cd2713a|#mid:b-6430467773153509964|0
}
3: 1
}
}
}
}
}
3 {
1: 4
2: 518600
5 {
1: 30
2: 0
}
7: 1
8 {
1: ^wa_ready
1: ^f
1: ^t_z
2: ^r
}
}
4 {
2: 1
3: 1627577996728
4: 1
5: 57
}
5: 2
This is proto message i created:
syntax = "proto3";
message rs_request {
.unknown_int_data unknown_int = 1;
.unknown_int_data1 unknown_int1 = 2;
.unknown_int_data2 unknown_int2 = 3;
.unknown_int_data3 unknown_int3 = 4;
bool unknown_int4 = 5;
}
message unknown_int_data {
bool unint = 1;
fixed32 unknown = 2;
bool unknown1 = 4;
}
message unknown_int_data1 {
.unknown_int_datas unknown2 = 1;
}
message unknown_int_datas {
int32 unknown3 = 1;
.unknown_int_datas1 unknown4 = 2;
}
message unknown_int_datas1 {
string threadaId = 1;
.unknown_int_datass unknowns = 2;
}
message unknown_int_datass {
.unknown_int_datas2 unknown5 = 14;
}
message unknown_int_datas2 {
.unknown_int_datas3 unknown6 = 1;
bool unknown7 = 3;
}
message unknown_int_datas3 {
string msgaId = 1;
.unknown_int_datas4 unknown9 = 2;
.unknown_int_datas5 unknown10 = 3;
fixed32 unknown11 = 7;
string subject = 8;
.unknown_int_datas6 unknown12 = 9;
string unknown13 = 11;
fixed32 unknown14 = 18;
.unknown_int_datas7 unknown15 = 36;
.unknown_int_datas8 unknown16 = 37;
int32 unknown17 = 42;
string unknown18 = 52;
}
message unknown_int_datas4 {
bool unknown19 = 1;
string from_domain = 2;
string from_name = 3;
string from_domain_again = 10;
}
message unknown_int_datas5 {
bool unknown20 = 1;
string to_domain = 2;
}
message unknown_int_datas6 {
.unknown_int_datas9 unknown21 = 2;
bool unknown22 = 7;
}
message unknown_int_datas9 {
bool unknown23 = 1;
string msg = 2;
}
message unknown_int_datas7 {
bool unknown24 = 6;
}
message unknown_int_datas8 {
bool unknown25 = 4;
}
message unknown_int_data2 {
bool unknowsthree = 1;
uint32 unknowsthree1 = 2;
.unknowsthree_data unknowsthree2 = 5;
bool unknowsthree3 = 7;
.unknowsthree_datas unknowsthree4 = 8;
}
message unknowsthree_data {
int32 unknowsthree5 = 1;
bool unknowsthree6 = 2;
}
message unknowsthree_datas {
string unknowsthree7 = 1;
string unknowsthree8 = 2;
}
message unknown_int_data3 {
bool unknown26 = 2;
fixed32 unknown27 = 3;
bool unknown28 = 4;
int32 unknown29 = 5;
}
and this is request model json for post (mcRequestModel.php):
"unknown_int1" => [
"unknown2" => [
"unknown4" => [
"unknowns" => [
"unknown5" => [
"unknown6" => [
"unknown9" => [
"from_domain" => "mcacc.com",
"from_name" => "Emma Ava",
"from_domain_again" => "mcacc.com"
],
"unknown10" => [
"to_domain" => "scssd.com"
],
"subject" => "Test",
"unknown12" => [
"unknown21" => [
"msg" => "<div dir='auto'>This is test.</div>"
]
]
]
]
]
]
]
]
Trying to get buffer compiler using php-protobuf:
$mcRequest = new mcRequestModel();
$class = new \rs_request();
$class->parseFromJsonStream($mcRequest);
var_dump($class);
When i remove all data from array unknown_int_datas3 i got buffer compiler arrays larger output but not getting compiler output when not remove data from unknown_int_datas3and just getting Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 67112960 bytes) in C:\xampp\htdocs\...
It's possible that the compiler is challenged by the levels of nesting in your messages.
I don't use PHP but I pasted your proto into Marc Gravell's protogen tool, selected PHP and it was able to compile.
The nesting in your messages is unusual and I was unable to understand why you would want to do this. Some context in your question to explain what you're trying to achieve may be useful.
With your current structure, in JSON, a data message would be:
{
test: {
test1: {
test2: {
test3: {
test4: {
test5: {
something: "...."
}
}
}
}
}
}
}
Google's style guide recommends PascalCase messages names (i.e. DataStepTwo) and snake_case field names (which you are doing test)
Your messages should probably use 1 consistently for the field numbers. Each of your messages only have 1 field so, unless you're intentionally avoiding collisions you may wish to avoid using 2 and 14.

Swift variable accessibility

I am doing this for a class, so I am not looking for someone to code this for me, just for a little guidance. Using Swift3 in Xcode 8. I fixed the previous errors and I think I'm making progress but...
class Cat {
var catName, catBreed, catColor: String
var catAge, catWeight: Int
init(name:String, age:Int, weight:Int, breed:String, color:String)
{
catName = name
catAge = age
catWeight = weight
catBreed = breed
catColor = color
}
func calculateAge (catAge: Int) -> Int
{
var humanYears = 0
if catAge == 1 {
humanYears = 15
} else if catAge == 2 {
humanYears = 24
} else if catAge > 2 {
humanYears = (24 + (catAge * 4))
}
print ("Your cat is \(humanYears) human years old!")
return humanYears
}
func createCats(name: String, age: Int, weight: Int, breed: String, color: String) -> String
{
let humanYears = calculateAge(catAge: age)
let catInfo : String = "\(name) is a \(color) \(breed) who weights \(weight) named \(name), and he is \(humanYears) human years old."
print (catInfo)
return catInfo
}
}
Cat.createCats()
I get an error at Cat.createCats() stating use of instance member 'createCats' on type 'Cat'; did you mean to use a value of type 'Cat' instead?
I have tried putting Cat in the (), I have tried naming all of my variables in there...
As I see in your code , you are re-declaring the variable "humanYears".
One is immediate after Class and second is in the function "calculateAge" . Edit your function "calculate" as below :
func calculateAge (catAge: Int) -> Int {
if catAge == 1 {
humanYears = 15
} else if catAge == 2 {
humanYears = 24
} else if catAge > 2 {
humanYears = (24 + (catAge * 4))
}
return humanYears
}
I figured out what I was doing wrong, thanks to you guys and another post on Stack Overflow that I searched up. Just in case it helps anyone here, I will post my final code below. My biggest issue was that I was trying to call the function before I created an instance of the class. This is not the finished version of the solution, but I was excited that I got it working so I wanted to post it.
Thanks again for all of your help,
class Cat
{
func calculateAge (catAge: Int) -> String
{
var humanYears = 0
if catAge == 1 {
humanYears = 15
} else if catAge == 2 {
humanYears = 24
} else if catAge > 2 {
humanYears = (24 + (catAge * 4))
}
return "Your cat is \(humanYears) human years old!"
}
func createCats(name: String = "Fluffy", age: Int = 1, weight: Int = 3, breed: String = "tabby", color: String = "brown") -> String
{
var catInfo : String
let humanYears = calculateAge(catAge: age)
catInfo = "\(name) is a \(color) \(breed) who weights \(weight) pounds named \(name), and he is \(humanYears) human years old."
print (catInfo)
return catInfo
}
}
let firstCat = Cat()
firstCat.createCats()
let secondCat = Cat()
secondCat.createCats(name: "bob")

how do i create a hierarchy list from a list of class<T> using LINQ

i have the below data , employee column consider as parent and each employee have invoice as child and each invoice have invoicedetails as subchild.
i have binded this data into a list of class. now i want create hierarchy list from this list using LINQ
parent---> child--->subchild
Employee-Invoice--->InvoiceDetails
List Data:
Employee invoice InvoiceDetails jan feb Mar Apr
E1 I1 ID1 1 2 10 5
E1 I1 ID2 1 3 11 6
E1 I1 ID3 1 4 12 7
E1 I2 ID1 1 5 13 8
E1 I2 ID2 1 6 14 9
E1 I2 ID3 1 7 15 10
E2 I1 ID1 1 8 16 11
E2 I1 ID2 1 10 17 12
Output :
E1
--I1
--ID1 1 2 10 5
--ID2 1 3 11 6
--ID3 1 4 12 7
-I2
--ID1 1 5 13 8
--ID2 1 6 14 9
--ID3 1 7 15 10
E2
--I1
--ID1 1 8 16 11
--ID2 1 10 17 12
Please let me know if you need more clarification.
var dataTree = employeeInvoiceDetails.GroupBy(__item => __item.Employee, __item => __item, (__key, __items) => new { Employee = __key, Invoices = __items.GroupBy(___item => ___item.invoice) });
foreach (var employe in dataTree)
{
Console.WriteLine("--{0}", employe.Employee);
foreach (var invoice in employe.Invoices)
{
Console.WriteLine("\t--{0}", invoice.Key);
foreach (var employeeInvoiceDetail in invoice)
Console.WriteLine("\t\t--{0} {1} {2} {3} {4}", employeeInvoiceDetail.InvoiceDetails, employeeInvoiceDetail.jan, employeeInvoiceDetail.feb, employeeInvoiceDetail.Mar, employeeInvoiceDetail.Apr);
}
}
I've assumed that your classes look like this:
public class Employee
{
public string Id;
public List<Invoice> Invoices;
}
public class Invoice
{
public string Id;
public List<InvoiceDetails> InvoiceDetails;
}
public class InvoiceDetails
{
public string Id;
}
And I've assumed that your source data is like this:
var source = new []
{
new { Employee = "E1", Invoice = "I1", InvoiceDetails = "ID1" },
new { Employee = "E1", Invoice = "I1", InvoiceDetails = "ID2" },
new { Employee = "E1", Invoice = "I1", InvoiceDetails = "ID3" },
new { Employee = "E1", Invoice = "I2", InvoiceDetails = "ID1" },
new { Employee = "E1", Invoice = "I2", InvoiceDetails = "ID2" },
new { Employee = "E1", Invoice = "I2", InvoiceDetails = "ID3" },
new { Employee = "E2", Invoice = "I1", InvoiceDetails = "ID1" },
new { Employee = "E2", Invoice = "I1", InvoiceDetails = "ID2" },
};
Then this is the query you need:
var employees =
(
from x in source
group x by x.Employee into gxs
select new Employee()
{
Id = gxs.Key,
Invoices =
(
from y in gxs
group y by y.Invoice into gys
select new Invoice()
{
Id = gys.Key,
InvoiceDetails =
(
from z in gys
select new InvoiceDetails()
{
Id = z.InvoiceDetails,
}
).ToList()
}
).ToList()
}
).ToList();
That gives me this result:

Using bitwise & (AND) in ViewHolder.icon.setImageBItmap

I was watching Google's I/O video on ViewHolder, but I'm lost on line 17. It looks like:
if (position & 1) {
holder.icon.setImageBitmap(mIcon1);
} else {
holder.icon.setImageBitmap(mIcon2);
}
What is the importance of if (position & 1)
1 public View getView(int position, View convertView, ViewGroup parent) {
2 ViewHolder holder;
3
4 if (convertView == null) {
5 convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
6
7 holder = new ViewHolder();
8 holder.text = (TextView) convertView.findViewById(R.id.text);
9 holder.icon = (ImageView) convertView.findViewById(R.id.icon);
10
11 convertView.setTag(holder);
12 } else {
13 holder = (ViewHolder) convertView.getTag();
14 }
15
16 holder.text.setText(DATA[position]);
17 holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
18
19 return convertView;
20 }
Slide 13
https://dl.google.com/io/2009/pres/Th_0230_TurboChargeYourUI-HowtomakeyourAndroidUIfastandefficient.pdf
https://www.youtube.com/watch?v=N6YdwzAvwOA&t=10m14s

How to get maximum Id in List and then return list

I have a structure like this:
No ID Name Status
1 1 A 1
2 1 B 1
3 1 c 1
4 1 D 1
5 2 E 3
6 2 F 3
7 2 G 3
I want to run a linq when I get a list results get maximum row where for each status and row details as well.Like:
No ID Name Status
4 1 D 1
7 2 G 3
Means latest entry for the status.
Is there a way around, as I have tried all Max, Orderby descending but I get single result but I need a List as a result.
You have to extract the groups of a same id (GroupBy), and then export the max No for each group, with a SelectMany :
public void Exec()
{
var items = new List<Item>{
new Item{ No = 1, Id = 1, Name = "A", Status = 1} ,
new Item{ No = 2, Id = 1, Name = "B", Status = 1} ,
new Item{ No = 3, Id = 1, Name = "C", Status = 1} ,
new Item{ No = 4, Id = 1, Name = "D", Status = 1} ,
new Item{ No = 5, Id = 2, Name = "E", Status = 1} ,
new Item{ No = 6, Id = 2, Name = "F", Status = 1} ,
new Item{ No = 7, Id = 2, Name = "G", Status = 1} ,
};
var result = items
.GroupBy(groupedItems => groupedItems.Id)
.SelectMany(i => items
.Where(innerItem => innerItem.Id == i.Key && innerItem.No == i.Max(ii => ii.No))
.Select(innerItem => innerItem)
);
foreach (var item in result)
Console.WriteLine("Max item of Id {0} : No = {1}, Name = {2}, Status = {3}", item.Id, item.No, item.Name, item.Status);
}
private class Item
{
public Int32 No { get; set; }
public Int32 Id { get; set; }
public String Name { get; set; }
public Int32 Status { get; set; }
}
output :
Max item of Id 1 : No = 4, Name = D, Status = 1
Max item of Id 2 : No = 7, Name = G, Status = 1
Alternative:
items.GroupBy(groupedItems => groupedItems.Id)
.Select(g => g.OrderByDescending(x => x.No).First())

Resources