# Zk 12.2.2009 (yoyo check it out :)

<{ForumPost(poster="Santhos", timestamp=2009-02-12 19:53:41)}>
Hey,  
  
sup fellows? Lets rock:

 > 4120, 4096, 4104,...?
 > 
 > 
 > 
 > 
 >     (24 | 4096) & (~(1 << 4))
 > 
 > 
 > 
 > 4104

 > a) nezkompiluje se  
 > b) 1,2  
 > c) , (tedy pouze znak carka)  
 > d) runtime error recpectively Exception
 > 
 > 
 > 
 > 
 >     class Plus {
 >         public static void AddPlus() {
 >             Prg.AddOp("+",1);
 >         }
 >     }
 >     
 >     class Divide {
 >         public static void AddDivide() {
 >             Prg.AddOp("/",2);
 >         }
 >     }
 >     
 >     class Prg {
 >         static Dictionary<string, int> d=new Dictionary<string, int>();
 >     
 >         public static void AddOp(string s, int i) {
 >             d.Add(s,i);
 >         }
 >     
 >         static void Main() {                       
 >             Console.WriteLine(string.Format("{0},{1}",d["+"],d["-"]));
 >             new Plus();
 >             new Divide();
 >         }
 >     }
 >     
 > 
 > 
 > 
 > d)

 > 4B, 8B, 32B, 64B, 2^32, 2^64B, platform dependent? // B = Byte
 > 
 > 
 > 
 > 
 >     size of long
 >     
 > 
 > 
 > 
 > 8B

 > cannot be compiled, 32, 25... dunno actually
 > 
 > 
 > 
 > 
 >     delegate int F();
 >     ... a very nice code
 >     
 > 
 > 
 > 
 > ?

 > System.Object, object, decimal, some class, System.Int64
 > 
 > 
 > 
 > 
 >     value types
 >     
 > 
 > 
 > 
 > decimal, System.Int64

 > What can interface I1 enforce?
 > 
 > 
 > 
 > 
 >     a) operator *
 >     b) public method
 >     c) private method
 >     d) internal method
 >     e) interface I2
 >     f) indexer []
 >     g) static property
 >     
 > 
 > 
 > 
 > b), f) ... more?

 > Write 2 of the main reasons why to use immutable classes:  
 >   
 > 1) Immutable classes are thread safe  
 > 2) They can be used as hash values in hash tables such as Dictionary  
 >   
 > [http://codebetter.com/blogs/patricksmac ... -them.aspx](http://codebetter.com/blogs/patricksmacchia/archive/2008/01/13/immutable-types-understand-them-and-use-them.aspx)

 > Implement enque in priority queue made of doubly-linked list. Make it safe to use in any context (besides thread safe, exceptions).
 > 
 > 
 > 
 > 
 >     class Queue<T, P>
 >             {
 >                 _____Node
 >                 {
 >                     public Node prev, next;
 >                     public T item;
 >                     public P prio;
 >                     public Node(T i, P p) { item = t; prio = p; }
 >                 }
 >     
 >                 public void Enqueue(T item, P prio)
 >                 {
 >                     Node n = new Node(item, prio);
 >     
 >     
 >                 }
 >     
 >                 public T Dequeue()
 >                 {
 >                      ...  // count on correct implementation
 >                 }
 >             }
 >     
 > 
 > 
 > 
 > 
 > Answer:
 > 
 > 
 > 
 > 
 >     class Queue<T, P> where P:IComparable<P>
 >             {
 >                 class Node
 >                 {
 >                     public Node prev, next;
 >                     public T item;
 >                     public P prio;
 >                     public Node(T i, P p) { item = t; prio = p; }
 >                 }
 >     
 >                 Node first = null;
 >     
 >                 public void Enqueue(T item, P prio)
 >                 {
 >                     Node n = new Node(item, prio);
 >                     lock (this)
 >                     {
 >                         if (first == null)
 >                             first = n;
 >                         else
 >                         {
 >                             Node cur = first;
 >                             Node prev = null;
 >                             while (cur != null && prio.CompareTo(cur.prio) <= 0)
 >                             {
 >                                 prev = cur;
 >                                 cur = cur.next;
 >                             }
 >                             prev.next = n;
 >                             if (cur != null)
 >                                 cur.prev = n;
 >                         }
 >                     }
 >                 }
 >     
 >                 public T Dequeue()
 >                 {
 >                      ...
 >                 }

----

!NO WARRANTY GUARANTEED!
<{/ForumPost}>

<{ForumPost(poster="Tempest", timestamp=2010-02-07 17:17:30)}>


     public void Enqueue(T item, P prio)
                {
                    Node n = new Node(item, prio);
                    lock (this)
                    {
                        if (first == null)
                            first = n;
                        else
                        {
                            Node cur = first;
                            Node prev = null;
                            while (cur != null && prio.CompareTo(cur.prio) <= 0)
                            {
                                prev = cur;
                                cur = cur.next;
                            }
                            prev.next = n;
                            if (cur != null)
                                cur.prev = n;
                        }
                    }
                }

Jen pro pořádek, to lock(this) je špatně, už to tu párkrát bylo (např. [http://forum.matfyz.info/viewtopic.php?f=421&t=5004](http://forum.matfyz.info/viewtopic.php?f=421&t=5004)).
<{/ForumPost}>

<{ForumPost(poster="Jonáš", timestamp=2011-02-07 16:07:33)}>

 > What can interface I1 enforce?  
 >   
 > Kód: Vybrat vše  
 > a) operator *  
 > b) public method  
 > c) private method  
 > d) internal method  
 > e) interface I2  
 > f) indexer \[]  
 > g) static property  
 >   
 > b), f) ... more?

Není správně i e) interface I2?  
Pokud I1 dědí od I2, třída implementující I1 musí implementovat i vše co delkaruj I2.  
Je v nějakých případech rozdíl, jestli třída A imlementuje I1 který dědí od I2 nebo A implementuje přímo I1 a I2?
<{/ForumPost}>

