воскресенье, 25 октября 2009 г.

Analog ManualResetEvent in Java

class ManualResetEvent {

  private final Object monitor = new Object();
  private volatile boolean open = false;

  public ManualResetEvent(boolean open) {
    this.open = open;
  }

  public void waitOne() throws InterruptedException {
    synchronized (monitor) {
      while (open==false) {
          monitor.wait();
      }
    }
  }

  public void set() {//open start
    synchronized (monitor) {
      open = true;
      monitor.notifyAll();
    }
  }

  public void reset() {//close stop
    open = false;
  }
}


* This source code was highlighted with Source Code Highlighter.


ManualResetEvent alternative in Java

суббота, 24 октября 2009 г.

can not jdk uninstall Error 1723

for uninstall jdk

if you can not uninstall jdk update 16
1) install jdk update 15
2) create folder jdk1.6.0_16 and copy files from jdk1.6.0_15 to jdk1.6.0_16
in C:\Program Files (x86)\Java
3) install jdk 16 , it will remove old version of jdk 16

воскресенье, 9 августа 2009 г.

How to do dynamic loops For

Problem is
How to realize dynamic for-loops in source code

Visual Sudio solution you can dowload here

like this

for(int i = 0 ; i < 10 ; i++)
  for(int j = 0 ; j < 4 ; j++)
   for(int k = 0 ; k< 9 ; k++)
     ...........................
        ........................
          Console.Write( i , j , k ....)


* This source code was highlighted with Source Code Highlighter.



for this task i create class ManyFor

it simple to use (see ManyForTest)
ManyFor class generate all combination of i , j , k , ....

Example:

      int[] mm = new int[] { 3, 2, 4 };

      ManyFor mf = new ManyFor(mm);

      for (int s = 0; s < mf.Count(); s++)
      {
        ReadOnlyCollection<int> r = mf.Get(s);

        int i = r[0], j = r[1], k = r[2];

        Console.WriteLine(i + " " + j + " " + k);
      }


* This source code was highlighted with Source Code Highlighter.


OUTPUT :

0 0 0
0 0 1
0 0 2
0 0 3
0 1 0
0 1 1
0 1 2
0 1 3
1 0 0
1 0 1
1 0 2
1 0 3
1 1 0
1 1 1
1 1 2
1 1 3
2 0 0
2 0 1
2 0 2
2 0 3
2 1 0
2 1 1
2 1 2
2 1 3

// CLASS
  class ManyFor
  {
    readonly int[] id;    // all zero : 0 0 0 0 0 0
    readonly int[] max;   //     : 5 2 1 6 9 1
    readonly int[] div;

    int numberIteration = 1;
    int N;

    public ManyFor(int[] max)
    {
      
      N = max.Length;

      this.max = new int[N];
      id = new int[N];
      div = new int[N];

      for (int i = N - 1; i >= 0; i--)
      {
        if (max[i] < 0)
          throw new ArgumentOutOfRangeException("max", " must be >= 0 ");

        div[i] = numberIteration;
        this.max[i] = max[i];
        checked
        {
          numberIteration *= max[i];
        }
      }

      if (N == 0) numberIteration = 0;
    }

    public int Count()
    {
      return numberIteration;
    }

    public ReadOnlyCollection<int> Get(int n)
    {
      if (n < 0 || n >= numberIteration)
        throw new ArgumentOutOfRangeException("n", " n < 0 || n >= numberIteration n = " + n);

      for (int i = 0; i < N; i++)
      {
        id[i] = n / div[i];
        n -= id[i] * div[i];
      }

      return Array.AsReadOnly(id);
    }
  }


* This source code was highlighted with Source Code Highlighter.


//=========Class For Big Iterations=================================================
    private string GetString(int[] max )
    {
      m = new ManyForBig(max);

      StringBuilder s0 = new StringBuilder("");
      for (ReadOnlyCollection<int> r = m.Next(); r != null; r = m.Next())
        for (int j = 0; j < r.Count; j++)
          s0.Append(r[j]);

      return s0.ToString();
    }


* This source code was highlighted with Source Code Highlighter.

//=======================================================================
class ManyForBig
{
  readonly int[] id;    // all zero : 0 0 0 0 0 0
  readonly int[] max;   //     : 5 2 1 6 9 1

  private bool getFirst;
  private int N;


  private int index;  //  0 <= index < N

  public ManyForBig(int[] max)
  {

    N = max.Length;

    this.max = (new List<int>(max)).ToArray();
    id = new int[N];

    index = N - 1;
    getFirst = true;


    if (N == 0) index = -1;

    foreach (int i in max)
    {
      if (i < 0) throw new ArgumentOutOfRangeException("Error ManyFor i< 0");

      if (i == 0)
      {
        index = -1;
        break;
      }
    }
  }

  public ReadOnlyCollection<int> Next()
  {
    if (index == -1) return null;

    if (getFirst)
    {
      getFirst = false;
      return Array.AsReadOnly(id);
    }
    else if (id[index] + 1 == max[index])
    {

      for (; index >= 0 && id[index] + 1 == max[index]; index--)
      { }

      if (index == -1) return null; //end

      id[index]++;

      for (int i = index + 1; i < N; i++)
        id[i] = 0;

      index = N - 1;
    }
    else
    {
      id[index]++;
    }

    return Array.AsReadOnly(id);
  }
}


* This source code was highlighted with Source Code Highlighter.

//=========================================================

tags:
Many number of for-loops , Quesiton about nesting for loops, A generator for any number of for loop ,

пятница, 19 июня 2009 г.

Math parse expressions. c# java

PS math parser .net , Math expression parser, Mathematical Expression Parser , .Net Math Parser, mathematical expression parsing

easy to use use and modify ( add functions , add your objects and so on)

If you can parse math expressions like this

5*4+3*7+8/3+abs(10-15 ) + max( 100 , 11, 1 ) + min ( 4 , 2) + X +X*X + ......

Visual studio solution you can download here http://stream.ifolder.ru/12757267
http://rapidshare.com/files/276525245/MathParse__1_.zip





1) generate list of tokens , where one token is object
2) create tree of tokens
3) create IValue object of math expression

1)
token is
  public class Token
  {
    public List<Token> tokens;
    public IValue ivalue;

    public string type;   
    public string value;  
    public string parseInfo;
  }

* This source code was highlighted with Source Code Highlighter.


2) 5+max(1 ,2 ) = INT ACTION FUNCTION

  public interface IValue
  {
    int Value( int x );
  }


* This source code was highlighted with Source Code Highlighter.


3)
public class INT : IValue
  {
    int i;

    public int I
    {
      set { i = value; }
      get { return i; }
    }

    public INT(int i)
    {
      this.i = i;
    }

    public int Value( int x )
    {
      return i;
    }
  }


  public class VARIABLE : IValue
  {
    public int Value( int x )
    {
      return x;
    }
  }

  class ACTION : IValue
  {
    public string action;
    public IValue v0;
    public IValue v1;

    public ACTION(IValue value0, string action, IValue value1)
    {
      this.v0 = value0;
      this.v1 = value1;
      this.action = string.Intern(action);
    }

    public int Value(int x)
    {
      switch (action)
      {
        case "&&":

          if (v0.Value(x) == 0) return 0;
          if (v1.Value(x) == 0) return 0;

          return 1;
        case "||":

          if (v0.Value(x) != 0) return 1;
          if (v1.Value(x) != 0) return 1;

          return 0;
      }

      int i0 = v0.Value(x);
      int i1 = v1.Value(x);

      switch (action)
      {
        case "+":
          return i0 + i1;
        case "-":
          return i0 - i1;
        case "*":
          return i0 * i1;
        case "/":
          return i0 / i1;
        case "%":
          return i0 % i1;

        case "<":
          return i0 < i1 ? 1 : 0;
        case ">":
          return i0 > i1 ? 1 : 0;
        case "<=":
          return i0 <= i1 ? 1 : 0;
        case ">=":
          return i0 >= i1 ? 1 : 0;
        case "==":
          return i0 == i1 ? 1 : 0;
        case "!=":
          return i0 != i1 ? 1 : 0;
      }

      throw new Exception("dsqwertyds");

    }

  }

  class FUNCTION : IValue
  {
    public List<IValue> value;
    public string nameFunction;

    public FUNCTION(string nameFunction, List<IValue> value)
    {
      this.nameFunction = string.Intern(nameFunction);
      this.value = value;
    }

    public int Value( int x)
    {
      int xx;

      switch (nameFunction)
      {
        case "max":

          if (value.Count == 0) throw new Exception("ERROR : function max dont have argument");

          xx = int.MinValue;
          int[] ii = new int[value.Count];

          for (int j = 0; j < value.Count; j++)
          {
            int a = ii[j] = value[j].Value(x);
            if (a > xx) xx = a;
          }
          return xx;
        case "min":

          if (value.Count == 0) throw new Exception("ERROR : function min dont have argument");

          xx = int.MaxValue;
          int[] iii = new int[value.Count];

          for (int j = 0; j < value.Count; j++)
          {
            int a = iii[j] = value[j].Value(x);
            if (a < xx) xx = a;
          }

          return xx;
        case "abs":

          if (value.Count != 1) throw new Exception("ERROR : function abs dont have argument");

          xx = value[0].Value(x);



          return (xx < 0) ? -xx : xx;
      }

      throw new Exception("function not exist : " + nameFunction);

    }

  }


* This source code was highlighted with Source Code Highlighter.

понедельник, 26 января 2009 г.

Compile memcached windows (Cygwin) (all tests passed !!!)

Memcached win32 32 bit you can download from here
all tests from folder "t" passed ,

but in libevent test

Simple DNS resolve: [Timed out] FAILED
IPv6 DNS resolve: [Timed out] SKIPPED
Simple reverse DNS resolve: [Timed out] FAILED


//==============================================================
if you want install youself

1) you need
http://win6.jp/Cygwin/cygwin-1.5.25-15-ipv6-0.22.zip
http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz

2) unpack cygwin-1.5.25-15-ipv6-0.22.zip - into you cygwin folder
about this files read site http://win6.jp/Cygwin/

3) in libevent http.c file add line ---> #define EAI_SYSTEM 11
now install libevent
./configure
make
make install

4) in memcached.c file add 3 lines --->

#define AI_PASSIVE 0x0020
#define AI_ADDRCONFIG 0x0001
#define EAI_SYSTEM 11
now install memcached

5) i run all tests and all tests passed !!!!!!! (unlike mamcached compile in visual studio )

воскресенье, 25 января 2009 г.

Compile memcached-1.2.6 under windows Visual Studio 2005 (use Jellycan Code )

1) i use files from http://code.jellycan.com/memcached/
get memcached-1.2.6-win32-bin.zip
libevent
2) need folder memcached and libevent put together
/src/libevent
/src/memcached

and event-config.h (from \libevent\WIN32-Code ) copy to "/src/libevent"
and in memcached properties VS2005 add
Additional Include Directiories - ../../libevent/
3) now you can compile memcached ,
4) all files here , VS2005 solution
http://stream.ifolder.ru/10219356

5) exe here - http://stream.ifolder.ru/10219544

TESTS (Cygwin)
1) if you have cygwin
you can test memcached , in folder "t" you have test perl files ,
for run test , open cygwin shell

form cygwin_lib_perl.rar - unpack to cygwin folder
OR install cygwin_Cache-Memcached-1.24.tar.gz and cygwin_String-CRC32-1.4.tar.gz

2)you can run test separately test like this
>perl udp.t
and you can run run_all_tests.bat and view *.txt fils
you will see like this


3) you see that tests

stats.t
incrdecr.t
64bit.t

FILED !!! i dont know why !!!
who have linux or unix , all tests passed ???

Compile libevent-1.4.9-stable under windows Visual Studio 2005

Compile libevent-1.4.9-stable under windows Visual Studio 2005

0) if you want get now solution go 7) ( and 6))
1) I take file "event-config.h" from http://code.jellycan.com/memcached/ - libevent-1.4.4-stable-win32.zip
2) copy event-config.h to ---> ...\libevent-1.4.9-stable\WIN32-Code
delete event-config.h from ---> ...\libevent-1.4.9-stable
3) now you can open ...\libevent-1.4.9-stable\WIN32-Prj\libevent.sln
and compile it
4) i write
#define exit(i) printf(" _____exit(%d) - hidden _____\n", i);
in file regress_dns.c
and write in function "main" before "return (0)"
printf("End tests ...");
while(1);
5) now you run regress project and will see like this



6)!!! you can see
Simple DNS resolve: [Timed out] FAILED
i dont know why this error !!!
7) now here the all source and solution for Visual Studio 2005
open ...\libevent-1.4.9-stable\WIN32-Prj\libevent.sln
compile and run regress test
8) libevent64.lib - libevent for 64 bit windows
libevent32.lib - libevent for 32 bit windows