essay on programming languages, computer science, information techonlogies and all.

Friday, July 5, 2019

Create instance of Type dynamically in C#

When applying RAII, constructor tends to have arguments to be fully autonomous afterward. When combined with factory method, there can be number of if-else with "new" and arguments like below pseudo code.

interface IWish
{
  void MakeTrue();
}

class EscapeCave : IWish
{
  public EscapeCave( string master, int nTh, string target ) {  ...  }
  public void MakeTrue() { ... }
}

class BecomePrince : IWish
{
  public BecomePrince( string master, int nTh, string target ) {  ...  }
  public void MakeTrue() { ... }
}

class BecomeHuman : IWish
{
  public BecomeHuman( string master, int nTh, string target ) {  ...  }
  public void MakeTrue() { ... }
}

class Genie
{
  static public IWish Create( string wish, string master, int nth, string target )
  {
    if( wish == "enscape cave" )
    {
      return new EscapeCave( master, nth, target );   // code repetition and become tedious and smelly 
    } 
    else if( wish == "become prince" )
    {
      return new BecomePrince( master, nth, target );
    } 
    else if( wish == "become human" )
    {
      return new BecomeHuman( master, nth, target );
    }  
    
    throw new ArgumentException( "Not supported wish" );
  }
}
To remove code repetition - "new" and arguments, Type can be used with System.ComponentModel.TypeDescriptor.CreateInstance(). Note that the Type should have same constructor - same number of arguments and types. It may be too restrictive but sometimes it is desirable as only difference should be differenct behaviour with same arguments.

class Genie
{
  static public IWish Create( string wish, string master, int nth, string target )
  {
    var typeDict = new Dictionary
    {
      { "enscape cave", typeof(EscapeCave) },
      { "become prince", typeof(BecomePrince) },
      { "become human", typeof(BecomeHuman) },   // genie can add its capable wish by simply adding line in here 
    };
    var typeWish = typeDict[ wish ];

    return (IWish)System.ComponentModel.TypeDescriptor.CreateInstance(
                provider: null, // reflection
                objectType: typeWish,
                argTypes: new Type[] { typeof(string), typeof(int), typeof(string)},
                args: new object[] { master, nth, target }  
    }
}

Wednesday, June 26, 2019

Integer factorization and combination in C#

 
     F
R = --- 
     D 
When transmitting data in a certain rate ( R ), clock rate ( F ) has to be divided ( D ). And sometimes, it is desirable to have the rate as integer. Usually, F is given from the transmitting device. And we want to know every possible R.

This asks R to be integer and asks D to divide F without fraction. Then F has to be integer multiple of D. First F has to be factored into prime numbers. Then all the permutation of prime numbers has to be generated. Then each permuation can be D ( or R in here).

Prime numbers of clock can be calculated on the fly but not the point in this article. Assume prime numbers are already known like { 2, 3, 5, 7, 11, ... }. Then factors of R can be enumerated by continuously divide with each primes like below function.

IEnumerable< int> Factors(int n, IEnumerable< int> primes)
{
  foreach (int p in primes) {
    if (p * p > n) break;

    while (n % p == 0) {
      yield return p;
      n /= p;
    }
  }

  if (n > 1) yield return n;
}


Then each factors can be permuted and enumerated so that each dividible product can be calculated.

IEnumerable< int[]> Combination(Tuple< int, int>[] groups )
{
  var product = new int[groups.Count()];
  foreach (var p in RecursivelyCombine(product, groups, 0)) {
    yield return p;
  }
}

IEnumerable< int[]> RecursivelyCombine(int[] products, Tuple< int, int>[] factors, int index)
{
  var factor = factors[index];
  for (int i = 0; i <= factor.Item2; ++i) {
    products[index] = i;
    if (index == factors.Length - 1) {
      yield return products;
    }
    else {
      foreach (var p in EnumerateProduct(products, factors, index + 1)) {
        yield return p;
      }
    }
  }
  yield break;
}


Then here is function that enumerate all possible rates with a given clock F.

IEnumerable< int> Rates(int F) // F = 50
{
  auto factors = Factors( 500, new int [] {  2, 3, 5, 7 } );
  // factors = { 2, 2, 5, 5, 5 }
    
  var groups = factors.GroupBy(f => f).Select(g => new Tuple(g.Key, g.Count())).ToArray();
  // groups = { {2,2}, {5,3} }  <- 2^2 x 5^3

  foreach (int[] pm in Combination(groups)) { // {0,0}, {0,1}, {0,2}, {0,3}, {1,0}, .... , {2,3}

    // pm can be { 1, 2 } which means D = 2^1 x 5^2
    int D = 1;
    for (int i = 0; i < groups.Length; ++i) {
      D *= (int)Math.Pow(groups[i].Item1, pm[i]);
    }

    yield return D;
  }
}

Monday, June 17, 2019

Criticism vs Comparison

"Criticism is constructive, comparison is abuse" from The Rehearsal by Eleanor Catton.


When you have no other measure on the field, you just need to look into the object directly. But if there is any, you then stop looking at it and start to looking for difference only.

I happend to work on a project which rewrites legacy C++ code to C# component based code. And got a lot of feedback of comparison with old software. It goes like this.

'Hey, this new one does not generate output same as old one.'
'But does the new output cause an issue ?'
'No, but it is not what user have seen.'
'What should be seen ?'
'Don't ask me. Just make it same as before'.


Legacy code has its own reason of being put in such a way. And when it unearthed, it is not easy to see why it was shaped in such a way.

Not to burden the follower, when writing code, code should be clean and concise. And should show intention and possibly reasoning behind the code.

Though no matter how crystal-clear the code, better way is to leave a document. And when referred after 10 years, the document should tell what the software should do not what it does.

Monday, June 3, 2019

Bayesian Inference on program crash

One company consulting have been in trouble with abnormal program crash. The log says that it crashed just before accessing I/O board. And some SW engineers believed that the culprit is the I/O board and asked for replacement.

But it turned out that there are other steps before I/O which don't leave any log and caused the crash but left no trace. But when people doesn't see the IO log , they conclude that IO crashed the program so no log left on it. It is a classic case of 'Correleation is not the causation'

Thought about how to find out falut in systematic way and read about Bayesian inference and tried in here. Refer Beysian Inference at wiki

P( Bad IO | Crash ) = P( Crash | Bad IO ) * P( Bad IO ) / P( Crash )
P( Bad IO | Crash ) : The probability of Bad IO when Crash happens
P( Crash | Bad IO ) : The probability of Crash when IO goes nut
P( Bad IO ) : The probability of IO goes nut
P( Crash ) : The probability of crash


Say, there are 100 running of the program, 1 crash happens. And during the 100 running, IO probably accessed 1000 times and goes nut 1 time. If IO goes nut, it will definitely crash. Then,
P( Bad IO | Crash ) = 1 * 0.001 / 0.01 = 0.1
It is telling that the P( Bad IO ) is too small to tell that it is the culprit. One way to increase probability is to change hypothesis like 'Bad IO Writing'. IO writing may happen 500 times during 100 running and can go nut. Then P( Bad IO Writing ) can be 0.002 ( = 1 / 500 ) and the 'Bad IO writing' probability or confidence can be 0.2.

To increase confidence on a hypothesis, the hypothesis has to be very specific or the probability of the hypothesis - P ( Bad IO ) in here - is too small to be meaniningful. The process of refining hypothesis is the trouble shooting, I guess. More specific, more chance of having valid cause.

In the end, it is a keen eye to find out the bug - unsafe reentrant code in multi-threaded environment. This Beysian Inference is not strightforward to quantize - hard to tell what is probabilty of some hypothesis.

Monday, April 29, 2019

Composition using Generic

Composition or inheritance. Composition is perferred way in OOP. Though there is drawback like repetitive code. With Generic in C#, repetitive code can be factored into a generic class.

First here is IPlayer, DrJekyll and BruceBanner.

public interface IPlayer
{
  stirng LiveIn { get; }
  string Play();
}

public class DrJekyll : IPlayer
{
  public string LiveIn { get { return "London"; } }
  public string Play() { return "Strolling around " + LiveIn + " cautiously"; }
}

public class BruceBanner : IPlayer
{
  public string LiveIn { get { return "New York"; } }
  public string Study { get { return "gamma ray"; } } 
  public string Play() { return "Playing with " + Study + " and trying not to get angry in" + LiveIn; }
}


And DrJekyll turns to MrHyde, BruceBanner to Hulk using inheritance. But inheritance ask to put 'virtual' on the original source code. It may not be possible.

public class DrJekyll : IPlayer
{
...
  public virtual string Play() ... 
}

public class MrHyde : DrJekyll 
{
  public override string Play() { return "Sneaking around " + LiveIn + " viciously"; }
}


public class BruceBanner : IPlayer
{
...
  public virtual string Play() ...
}

public class Hulk : BruceBanner 
{
  public override string Play() { return "Hate " + Study + " and rampage around " + LiveIn; }
}


Or composition can be used like below. MrHyde has OtherSelf DrJekyll and Hulk BruceBanner. Though notice the repetitive codes - OtherSelf, LiveIn.

public class MrHyde : IPlayer
{
  public DrJekyll OtherSelf { get; private set; } 
  public string LiveIn { get { return OtherSelf.LiveIn; } }

  public MrHyde( DrJekyll p ) { OtherSelf = p; }
  public string Play() { return "Sneaking around " + LiveIn + " viciously"; }
}

public class Hulk : IPlayer
{
  public BruceBanner OtherSelf { get; private set; } 
  public string LiveIn { get { return OtherSelf.LiveIn; } }

  public Hulk ( BruceBanner p ) { Self = p; }
  public string Play() { return "Hate " + OtherSelf.Study + " and rampage around " + LiveIn; }
}


The repetitive code can be bundled up to a generic class OtherSelf. Then MrHyde and Hulk only needs its own Play. And it can refer its Self for any property.

public abstract class OtherSelf< T > : IPlayer where T : IPlayer
{
  public T Self{ get; private set; }
  public PlayerBase( T otherSelf ) { Self = otherSelf; }
  public string LiveIn { return Self.LiveIn; }
  public abstract string Play();
}

public class MrHyde : OtherSelf< DrJekyll >
{
  public MrHyde( DrJekyll p ) : base( p ) {}
  public override string Play() { return "Sneaking around " + LiveIn + " viciously"; }
}

public class Hulk : OtherSelf< BruceBanner >
{
  public Hulk( BruceBanner p ) : base( p ) {}
  public override string Play() { return "Hate " + Self.Study + " and jumping around " + LiveIn; }
}
I didn't bother to check syntax on above code so please treat above as pseudo code.

Sunday, March 10, 2019

C++ Pointer

Here you can refresh your memory about pointer.

Pointer and Reference

Pretty basic, Right ?

int a = 1234;
int *pa = &a;    // pa is pointer of a
*pa = 1111;      // assignment on *pa will change a also

int &ra = a;     // ra is reference of a
ra = 4321;       // assignment on ra will change a also

Function return value using reference or pointer

Which do you prefer ? * or & ?

int AddAndMultiply(int a, int b, int& mult)
{
  mult = a * b;
  return a + b;   
}

int AddAndMultiplyV3(int a, int b, int* mult)
{
  *mult = a * b;
  return a + b;
}

int mult = 0;
int sum = AddAndMultiply(1, 2, mult);
sum = AddAndMultiplyV3(1, 2, &mult);

Function return struct

I can read this easily. But isn't struct make code slow by copying memory ? Are you sure ?

struct AddMult
{
  int Add;
  int Mult;
};

AddMult AddAndMultiplyV3(int a, int b)
{
  AddMult r;
  r.Add = a + b;
  r.Mult = a * b;
  return r;
}

AddMult r = AddAndMultiplyV3(1, 2);
sum = r.Add;
mult = r.Mult;

const at before or after


const int* cpa = &a;
*cpa = 42;           // compile error : cpa is pointing a constant variable, so you can't change the value
cpa = &b;            // but you can make it points other variable

const int& cra = a;
cra = 42;            // compile error : cra is referencing a constant variable, so you can't change the value

int* const pca = &a; 
*pca = 42;           // no problem : pca is constantly pointing a variable, and you can change the value of variable.
*pca = &b;           // compile error : but pca can't pointing to other variable.

mutable can be changed on const method

Then what's the point of const if there is mutable ?

struct AddData
{
  int A, B;
  mutable int C = 0;  // ok to be changed on const method.

  int AddAll() const  // This isn't supposed to change member variable. 
  {
    if (C == 0) { C = A + B; }
    return C;
  }
};

Endianess

big-endian puts significant bytes at lower address, while little-endian puts significant bytes at higher address.

uint32_t *pa = new uint32_t[4];
pa[0] = 0x12345678;     // note the little-endian
pa[1] = 0x87654321;
pa[2] = 0x10101010;
pa[3] = 0xCAFEBEEF;

*pa = 0xFFFFFFFF;
*(pa + 1) = 0xEEEEEEEE;

delete[] pa;

64 bit ponter

Each pointer is 64bits.

Packing at struct

Do you know how variable is packed in a struct ?

struct Foo
{
  uint32_t a;     // 4 Bytes
  char b;         // 2 Bytes
  uint16_t c;     // 2 Bytes
  bool d;         // 4 Bytes
  const char * e; // 4 Bytes
};

Foo foo;
Foo* pfoo = &foo;
foo = { 0x12345678, 'a', 0xCAFE, true, nullptr };

Function pointer

This syntax of putting type name in between retrun type and argument type and *, (, ). Can you remember it ?

int TestBar(char a) {  return a + 42;  }
int TestToo(char a) {  return a + 22;  }

typedef int(*FooFn)(char);  // Here you go!

FooFn f = TestBar;
int ret = f('a');
f = TestToo;
ret = f('b');

STL function pointer

Or how about STL style ?

int TestBar(char a) {  return a + 42;  }
int TestToo(char a) {  return a + 22;  }

using FooFn = std::function< int(char) >;  // Here you go again !

FooFn fn = TestBar;
int ret = fn('a');
fn = TestToo;
ret = fn('b');