Algorithm for extracting common table definitions? - algorithm

I'm using code generation to generate interfaces that correspond to table definition in a certain database.
Since the database is very messy I get around 500 interfaces (for 500 tables) each with it's own definition.
Some of the interfaces can inherit from each other, and for some a common interface can be extracted to minimize code definition.
For example:
interface One
{
int FirstField { get; set; }
bool SecondField { get; set; }
DateTime ThirdField { get; set; }
}
interface Two
{
int FirstField { get; set; }
DateTime ThirdField { get; set; }
double FourthField { get; set; }
}
I would like to perform some kind of minimization on the code to have minimum amount of it generated (through multiple inheritance and common code extraction).
From above example I would need to get something like:
interface OneTwoCommon
{
int FirstField { get; set; }
DateTime ThirdField { get; set; }
}
interface One : OneTwoCommon
{
bool SecondField { get; set; }
}
interface Two : OneTwoCommon
{
double FourthField { get; set; }
}
Which branch of algorithms deals with these problems?
Where do I start looking up those algorithms?
I don't even know what to write in Google to get relevant results.

I figured out a simple algorithm.
define Surface of interface as number of other interfaces that implement it, times the number of properties of that interface
the goal is to construct a new interface, with subset of all used properties, with maximum possible surface
Initialization:
first we order descedning all properites by number of interfaces that contain it, into LProperties (so most used properties will be on top)
select first property (P1) from LProperties, create new interface ITemp with only that property
pop P1 from LProperties
calculate Surface of ITemp (number of existing interfaces that can implement it times number of properties)
Iteration:
select first property from LProperties (now P2), put it in ITemp (note: P2 is not P1 since we popped P1)
new Surface
new Surface > old Surface, remove P2 from LProperties, save it to ITemp, remember Surface
*, remove P2 from ITemp
Iteration:
next property from LProperties (now P3), put it in ITemp (note: P2 is still in LProperties, but we already processed it)
if new Surface > old Surface remove P3 from LProperties, save it to ITemp, else remove P3 from ITemp

Related

How to check record in c# 9 in NET 5 is immutable at runtime

Record is a new feature in c#9, Net 5
It's said
If you want the whole object to be immutable and behave like a value, then you should consider declaring it as a record
Creating a record in c#9 , NET 5:
public record Rectangle
{
public int Width { get; init; }
public int Height { get; init; }
}
Then instantiating it:
var rectangle = new Rectangle (20,30);
Trying to change the value:
rectange.Width=50; //compiler error
Compiler raise the error:
error CS8852: Init-only property or indexer 'Rectangle.Width' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.
That is right and insure that the record is immutable.
Using a method like to test IsImmutable type give false, because in record there is no generated readonly properties.
How to check the record in c# 9, Net 5 is immutable at runtime or even it has init property?
A record is indeed mutable at runtime. This is intentional, is it means most serializer frameworks work without updating.
It is however possible to check if a property is initonly by checking:
public static bool IsInitOnly(PropertyInfo propertyInfo)
{
return propertyInfo?.SetMethod.ReturnParameter
.GetRequiredCustomModifiers()
.Any(x => x.FullName == _isExternalInitName)
?? false;
}
private static string _isExternalInitName =
typeof(System.Runtime.CompilerServices.IsExternalInit).FullName;
I don't think that it's possible to check for immutability at runtime.
Here's some of the generated code for your record. You can see that both properties have a public setter.
public class Rectangle : IEquatable<Rectangle>
{
[CompilerGenerated]
private readonly int <Width>k__BackingField;
[CompilerGenerated]
private readonly int <Height>k__BackingField;
protected virtual Type EqualityContract
{
[CompilerGenerated]
get
{
return typeof(Rectangle);
}
}
public int Width
{
[CompilerGenerated]
get
{
return <Width>k__BackingField;
}
[CompilerGenerated]
set
{
<Width>k__BackingField = value;
}
}
public int Height
{
[CompilerGenerated]
get
{
return <Height>k__BackingField;
}
[CompilerGenerated]
set
{
<Height>k__BackingField = value;
}
}
The following code will compile and run without errors.
var rect = new Rectangle { Height = 1, Width = 2 };
typeof(Rectangle).GetProperty("Height").SetValue(rect, 5);
Console.Write(rect.Height);
//Prints 5
At runtime the init accessor is just a regular setter. It's only at compile time that a check is made to only allow init accessor to be called during object initialization.
So I don't see any way to check at runtime that Rectangle is immutable.

How to measure the height of the navigation bar

I am working on a xamarin.forms application where I need to measure the height of the navigation bar. How do I measure it? I tried to follow this but I don't understand where to insert this code:
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
You are on the right track. The problem is that your app is cross platform and the navigation bar is a Android-only feature. Because you probably need to access the navigation bar height from within the shared code, you will need to use Dependency injection. This is thoroughly described here.
In short you will have to write a namespace like this:
public interface INavigationBarInfo
{
int Height { get; };
}
And implement it for Android within the Android project:
public class NavigationBarInfo : INavigationBarInfo
{
public int Height => RetrieveHeight();
private void RetrieveHeight()
{
//android specific code to retrieve the navigation bar height
//you can use the answer you linked to in your question
}
}

Gantt chart name suggestion - Length or Width

I'm not sure if this is the right Stack Exchange site to ask good name for bars in Gantt Chart.
Data Structure
public class GanttChartData
{
public ICollection<GanttBarData> BarCollection { get; set; }
}
public class GanttBarData
{
public string Name { get; set; }
public double Length { get; set; }
}
Question Should I use Length or Width to represent length of a bar? I know it would be horizontal bar always.
As Wikipedia states:
In geometric measurements, length is the longest dimension of an
object.
Hence your choice of BarLength is correct as the horizontal length is (almost!) always going to be the longest dimension of a bar in your chart.

Blackberry ListField: how to add Listener for list row click

Suppose i had a Screen class that has 10 ListField:
Vector v_prj_title,v_prj_mgr
// v_prj_title contains name of projects
// v_prj_mgr contains name of the project_manager of v_prj_title sequentially.
//Vector send_vector
//ListField myList
//ListCallBack callback
//It is clear from the code that in myList, I m inserting a vector send_vector ie callback.insert(send_vector,i), which contains 2 strings collected one from v_prj_title and other from v_prj_mgr.
for(int i=0;i<10;i++)
{
myList.insert(i);
t1 = v_prj_title.elementAt(i).toString();
send_vector = new Vector(2);
send_vector.addElement(t1);
t2 = v_prj_mgr.elementAt(i).toString();
send_vector.addElement(t2);
callback.insert(send_vector,i);
}
Now I'm getting confused how to add eventListener to particular ListField, e.g. suppose if I click the 3rd ListField,(suppose this is the displayed data below) a bitmap picture should be displayed in the 3rd ListField and the name of the project (Project_Social_Meeting) and project_manager (Tom Clerk) should be inserted into database (SQlLite)
1. a. Project_Chat_Master( project name)
b. Vyom Ryan (project manager)
2. a. Project_Online_Gaming
b. Vivek Roy
3. a. Project_Social_Meeting
b. Tom Clerk
.
.
etc.....
create a CustomField/Manager depending on your requirement.(which may contain images/strings/...)
then add them to the callback method
Ex:
step:1
//creating a custom field
class MYListFieldItem extends Field
{
//#override
paint(graphics g)
{
g.drawbitmap(bitmap,0,0);
g.drawtext(string,bitmap.getwidth()+5<padding>,Math.min(bitmap.getHeight(),getFont().getHeight()));
//#override
layout(....)
{
setExtent(Math.min(width,bitmap.getwidth()+padding+getfont.getadvance(stringtext)),
Math.min(height,Math.min(bitmap.getHeight,getFont().getHeight())));
}
}
step-2:
//create list items
MYListFieldItem [] fields[] = new MYListFieldItem [<numOfListItems>];
for(int i=0;i<fields.size;i++)
{
_callback = new MyListFieldCallBack();
_callback.insert(fields[i],i);
}
step-3:
//set listeners
mylistFielditem[i].setchangeListener(new fieldchangeListener(){
fieldChanged(field)
{
//do your action here.
});
//TIP: if the fields are just strings,
//#override
navigationclick()
{
if(status == keypadlistener.status_fourway)
{
MYListFieldItem fld = (ListField)getLiefFieldwithFocus();
//do your coding
}
}

String as a GeoCoordinate

I am developing an application which is storing places in database.
The problem is:
I have a main view of map, then I click add place button. The button goes to standard form which contains name, description and coordinates. Coordinates I am taking from map and changing to string. Then it saves in SQLite database.
But I have no idea how use geocoordinates from DB, because they are strings, not a GeoCoordinates, and simply I can't use them to for example pushpin with binding data
Considering there is no GeoCoordinate.Parse() method and assuming your string representation comes from GeoCoordinate.ToString(), you could use something like this:
public GeoCoordinate ParseGeoCoordinate(string input)
{
if (String.IsNullOrEmpty(input))
{
throw new ArgumentException("input");
}
if (input == "Unknown")
{
return GeoCoordinate.Unknown;
}
// GeoCoordinate.ToString() uses InvariantCulture, so the doubles will use '.'
// for decimal placement, even in european environments
string[] parts = input.Split(',');
if (parts.Length != 2)
{
throw new ArgumentException("Invalid format");
}
double latitude = Double.Parse(parts[0], CultureInfo.InvariantCulture);
double longitude = Double.Parse(parts[1], CultureInfo.InvariantCulture);
return new GeoCoordinate(latitude, longitude);
}

Resources