How to get back the dangling objects - serialization

I am working in a project with large data that needs to partition the FlatBuffers into many smaller pieces. I am encountering some cases like this.
Schema:
table A {
number: int;
}
table B {
a: [A];
}
root_type B
Code:
flatbuffers::FlatBufferBuilder builder;
int num0 = 3;
int num1 = 1;
int num2 = 5;
int num3 = 7;
auto a0 = CreateA(builder, num0);
auto a1 = CreateA(builder, num1);
auto a2 = CreateA(builder, num2);
auto a3 = CreateA(builder, num3);
std::vector<flatbuffers::Offset<A>> A_vector;
A_vector.push_back(a0);
A_vector.push_back(a1);
auto B = builder.CreateVector(A_vector);
auto orc = CreateB(builder, B);
builder.Finish(orc);
If I can not push_back a2 and a3 into A_vector for some reasons, will a2 and a3 also be serialized in this case? How can I get a2 and a3 back after serialization via builder.GetBufferPointer()?
Thanks for your help.

If you serialize an object in a FlatBuffer and don't store its offset anywhere, there is no way to access it. You need to serialize it into a different FlatBufferBuilder instance.

Related

In LiliyPond, how do you alter a music expression?

I have a situation where I have a recurring piece of music with only slight variations. I want to lay down a base piece as a variable. Then reuse it multiple times, each time altering it by replacing a few notes or measures. Here's a simplified example
base = { c4 c c c }
% pseudo function \replace MUSIC FROM TO ORIG
% FROM and To are pairs of numbers COUNT DURATION
from = #'(1 2)
to = #'(3 4)
var1 = \replace { d4 } \from \to \base
% this means replace
% from "after 1 half note into the music"
% to "after 3 quarter notes into the music"
% that is replace the third beat with { d4 }
% this will be equivalent to
var1 = { c4 c d c }
How do I do this?
A LilyPond solution to this is \tag. I haven't found a built-in functionality for altering a music expression on the fly. However, for the variations usecase, tags serve this purpose. The above example would be this
base = { c4 c \tag #'base c \tag #'var1 d c }
var1 = \keepWithTag #'var1 \base

Assign 3 typed numbers to a variable in Processing

I am making a paint program in Processign and want to allow the user to type 3 numbers to change int r;.
I want to know if I could do something to take three typed numbers and assign them to a single variable like int r. Such as typing 2, 5, 5, and storing them as int r =255;
You could use an array.
An array is a variable that holds multiple values.
int[] r = {1, 2, 3};
int x = r[0];
Here is the Processing array reference.
You could create your own class.
Better yet, you could create a class that keeps track of your 3 values:
class MyNumbers{
int r;
int g;
int b;
public MyNumbers(int r, int g, int b){
this.r = r;
this.g = g;
this.b = b;
}
}
Then you just create an instance of that class and pass in your values:
MyNumbers rgb = new MyNumbers(1, 2, 3);
int r = rgb.r;
Here is the Processing class reference.
You could use the color type.
If all you want to store are rgb values, consider using the existing color type in Processing:
color c = color(1, 2, 3);
int r = red(c);
Here is the Processing color reference.
You also might want to look into the ArrayList or HashMap classes.

How to convert bit column to int

I want to convert a bit column to a integer column, do I need a case and convert function at the same time for this?
False = 0
True = 1
You do not need a conversion, because bit is already an integer data type:
An integer data type that can take a value of 1, 0, or NULL.
You can use bits in integer expressions without conversion. Here is a short demo:
create table demo (b bit, v int);
insert into demo (b, v) values (1,5), (0,4), (1, -2), (0, -5);
SELECT b, v, b+v AS b_plus_v FROM demo
Running this produces the following output:
B V B_PLUS_V
- - --------
1 5 6
0 4 4
1 -2 -1
0 -5 -5
EDIT : (based on this comment: "I'm using Code first EF")
Entity Framework requires that a bit column mapped to a bool field. One way to work around this requirement is introducing a computed property to your entity class to hide the "Booleanness" of the underlying column, like this:
partial class MyEntity {
// This code assumes that a bool property MyBoolProperty exists,
// and that it is mapped to the table using EF
public int MyIntProperty {
get {
return MyBoolProperty ? 1 : 0;
}
set {
MyBoolProperty = value != 0;
}
}
}

Multiply 2 very big numbers in IOS

I have to multiply 2 large integer numbers, every one is 80+ digits.
What is the general approach for such kind of tasks?
You will have to use a large integer library. There are some open source ones listed on Wikipedia's Arbitrary Precision arithmetic page here
We forget how awesome it is that CPUs can multiply numbers that fit into a single register. Once you try to multiply two numbers that are bigger than a register you realize what a pain in the ass it is to actually multiply numbers.
I had to write a large number class awhile back. Here is the code for my multiply function. KxVector is just an array of 32 bit values with a count, and pretty self explanatory, and not included here. I removed all the other math functions for brevity. All the math operations are easy to implement except multiply and divide.
#define BIGNUM_NEGATIVE 0x80000000
class BigNum
{
public:
void mult( const BigNum& b );
KxVector<u32> mData;
s32 mFlags;
};
void BigNum::mult( const BigNum& b )
{
// special handling for multiply by zero
if ( b.isZero() )
{
mData.clear();
mFlags = 0;
return;
}
// apply sign
mFlags ^= b.mFlags & BIGNUM_NEGATIVE;
// multiply two numbers using a naive multiplication algorithm.
// this would be faster with karatsuba or FFT based multiplication
const BigNum* ppa;
const BigNum* ppb;
if ( mData.size() >= b.mData.size() )
{
ppa = this;
ppb = &b;
} else {
ppa = &b;
ppb = this;
}
assert( ppa->mData.size() >= ppb->mData.size() );
u32 aSize = ppa->mData.size();
u32 bSize = ppb->mData.size();
BigNum tmp;
for ( u32 i = 0; i < aSize + bSize; i++ )
tmp.mData.insert( 0 );
const u32* pb = ppb->mData.data();
u32 carry = 0;
for ( u32 i = 0; i < bSize; i++ )
{
u64 mult = *(pb++);
if ( mult )
{
carry = 0;
const u32* pa = ppa->mData.data();
u32* pd = tmp.mData.data() + i;
for ( u32 j = 0; j < aSize; j++ )
{
u64 prod = ( mult * *(pa++)) + *pd + carry;
*(pd++) = u32(prod);
carry = u32( prod >> 32 );
}
*pd = u32(carry);
}
}
// remove leading zeroes
while ( tmp.mData.size() && !tmp.mData.last() ) tmp.mData.pop();
mData.swap( tmp.mData );
}
It depends on what you want to do with the numbers. Do you want to use more arithmetic operators or do you simply want to multiply two numbers and then output them to a file? If it's the latter it's fairly simple to put the digits in an int or char array and then implement a multiplication function which works just like you learned to do multiplication by hands.
This is the simplest solution if you want to do this in C, but of course it's not very memory efficient. I suggest looking for Biginteger libraries for C++, e.g. if you want to do more, or just implement it by yourself to suit your needs.

how to check for empty pointers

say I have a struct,
struct room{
char name[21];
int num1;
int num2;
struct room *doors[4];
};
so number of rooms are given ,
struct room rm[Number_of_room];
and each room:
scanf(name | num1 | num2)
strcpy(rm[i].name, name)
rm[i].num1 = num1
rm[i].num2 = num2
all that works fine.
Until, I wish to check rather the door is pointed at something already or not,
UP=0, RIGHT=1, DOWN=2, LEFT=3
rm[i].doors[0] = &rm[j] // this is how I point the door to an other room, works fine too.
but when I check for strlen or null, suppose I only assign one door from room1 to room2, room1 somehow has more than one non-empty doors.
Is there a way to check rather the doors are empty or not?
Thanks
When you initialize a new object of the struct room type, set its doors members to NULL. Afterwards compare to NULL and find if they're already set
struct room object;
object.doors[0] = NULL;
object.doors[1] = NULL;
object.doors[2] = NULL;
object.doors[3] = NULL;
if (object.doors[2] != NULL) {
/* already assigned something */
/* maybe reset? */
object.doors[2] = NULL;
};