santevid
santevid
Engineer
5 posts
Computer Science - Programming - OOP
Don't wanna be here? Send us removal request.
santevid 11 years ago
Text
James Altucher
An amazing guy, excellent writer, blogger, teacher and adviser.
http://www.jamesaltucher.com/
Some of his excellent articles:
http://www.jamesaltucher.com/2014/04/the-ultimate-cheat-sheet-for-investing-all-of-your-money/
Mr. Altucher reminds me of Nenad Baki膰, an Croatian entrepreneur and investor (http://trzistakapitala.wordpress.com/), but James is somehow more trustworthy.
0 notes
santevid 11 years ago
Text
Codility
Now, some words about Codility, this platform deserves it.
If you don't know what is Codility best explanation would be to click on this link: www.codility.com and read a frontpage.
There are several platforms on the net that provide tools for testing potential new software programmers/developers/engineers, but this is the best one, at least for now. Codility provides excellent reports, that will report a lot user actions while the user is solving a problem. i.e. when he run the code to test, time, execution times, complexity etc.
Except for recruiters, Codility is useful for someone who wants to train. For example if you are looking for a new job and you know that on interview you will be asked to solve algorithmic problems, then start train with Codility. They provide a lot of interesting problems.
Each month, they release a new challenge. So, people can test self on such challenges. If you manage to solve a challenge, then they will release a certificate:http://www.codility.com/cert/view/cert3G5VGX-KGDUSAF6XVCMWN8D/, which is nice.
0 notes
santevid 11 years ago
Text
Indexable Circular Buffer
Developers are familiar with the circular buffer data structure. It can be interpreted with end pointer (an index where a new item should be placed) and start pointer (an index where the oldest item is). When buffer is full (start== end), simply the oldest item is replaced with the newest (start= end++).
http://en.wikipedia.org/wiki/Circular_buffer
In some cases it is useful to have an indexable circular buffer, i.e.buffer[i] should access (start + i)-th item.
This can be simply implemented with modulo operation, as shown below in a C# implementation which implements IList<T> and IList interface.
public class CircularBuffer<T> : IList<T>, IList 聽聽聽 { 聽聽聽聽聽聽聽 private T[] buffer; 聽聽聽聽聽聽聽 private readonly int size; 聽聽聽聽聽聽聽 private int startIndex; 聽聽聽聽聽聽聽 private int endIndex; 聽聽聽聽聽聽聽 private int count; 聽聽聽聽聽聽聽 public CircularBuffer(int size, double trunckRatio) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 this.size = size; 聽聽聽聽聽聽聽聽聽聽聽 Init(); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void AddRange(IEnumerable<T> collection) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 foreach (var item in collection) 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Add(item); 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 private int CalculateTrunckSize() 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return Math.Min(Math.Max((int)(Math.Ceiling(TrunckRatio * size)), 1), size); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 private void Init() 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 buffer = new T[size]; 聽聽聽聽聽聽聽聽聽聽聽 startIndex = 0; 聽聽聽聽聽聽聽聽聽聽聽 endIndex = -1; 聽聽聽聽聽聽聽聽聽聽聽 count = 0; 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public int Size 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get { return size; } 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 /// <summary> 聽聽聽聽聽聽聽 /// Transforms from buffer index to CircularBuffer index 聽聽聽聽聽聽聽 /// </summary> 聽聽聽聽聽聽聽 /// <param name="index">Index in buffer</param> 聽聽聽聽聽聽聽 private int TransformInternalIndexToPublicIndex(int index) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return MathHelper.GetPositiveIntModulus(index - startIndex, size); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 private int TransformPublicIndexToInternalIndex(int index) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return (startIndex + index) % size; 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public int IndexOf(T item) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 for (int i = 0; i < Size; i++) 聽聽聽聽聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (buffer[i].Equals(item)) return TransformInternalIndexToPublicIndex(i); 聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return -1; 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void Insert(int index, T item) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (index > Count - 1) 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 throw new ArgumentOutOfRangeException("Index is not a valid index."); 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 int internalIndex = TransformPublicIndexToInternalIndex(index); 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 buffer[internalIndex] = item; 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void RemoveAt(int index) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 throw new NotSupportedException(); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public bool IsFixedSize 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get 聽聽聽聽聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return true; 聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public T this[int index] 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get 聽聽聽聽聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return buffer[TransformPublicIndexToInternalIndex(index)]; 聽聽聽聽聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽聽聽聽聽 set 聽聽聽聽聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 buffer[TransformPublicIndexToInternalIndex(index)] = value; 聽聽聽聽聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void Add(T item) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 AddInBuffer(item); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 private int AddInBuffer(T item) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 int index = IncreaseAndGetEndIndex(); 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (Count >= size) 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 startIndex = (startIndex + 1) % size; 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 else count++; 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 buffer[index] = item; 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return index; 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 private int IncreaseAndGetEndIndex() 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return endIndex = (endIndex + 1) % size; 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 private int IncreaseStartIndex() 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return startIndex = (startIndex + 1) % size; 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void Clear() 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Init(); 聽聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public bool Contains(T item) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return buffer.Contains(item); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void CopyTo(T[] array, int arrayIndex) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 foreach (T item in this) 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 array[arrayIndex++] = item; 聽聽聽聽聽聽聽聽聽 聽 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public int Count 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get { return count; } 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public bool IsReadOnly 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get { return false; } 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public bool Remove(T item) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 throw new NotSupportedException(); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public IEnumerator<T> GetEnumerator() 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return new CircularBufferEnumerator<T>(this); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return new CircularBufferEnumerator<T>(this); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public int Add(object value) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return this.AddInBuffer((T)value); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public bool Contains(object value) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return this.Contains((T)value); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public int IndexOf(object value) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 return this.IndexOf((T)value); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void Insert(int index, object value) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 this.Insert(index, (T)value); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void Remove(object value) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 this.Remove((T)value); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 object IList.this[int index] 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get 聽聽聽聽聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return this[index]; 聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽聽聽聽聽 set 聽聽聽聽聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 this[index] = (T)value; 聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public void CopyTo(Array array, int index) 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 this.CopyTo((T[])array, index); 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public bool IsSynchronized 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get { return false; } 聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽 public object SyncRoot 聽聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽聽 get { return null; } 聽聽聽聽聽聽聽 } 聽聽聽 }
0 notes
santevid 12 years ago
Text
ReadOnly Collections
A disadvantage of .NET is surely failed definition (and implementation) of read-only collections. From the beginning .NET creators simply ignored read-only collections. This brings a lot of misunderstanding among.NET developers.
Take a look at this class:
public class FirstFiveOddNumbers { 聽聽聽 List<int> numbers = new List<int>(new int[]{1,3,5,7,9}); . . . 聽聽聽 IList<int> GetNumbers() 聽聽聽 { 聽聽聽聽聽聽聽 return numbers; 聽聽聽 } }
Method GetNumbers() doesn't return read-only list, in fact caller can change, add, remove numbers in returned list.
Sure, method GetNumbers() can clone internal collection and return cloned one(new List<int>(numbers)), in such way method's class can ensure that caller's changes on collection will not affect its (internal) collection object. But cloning can be inefficient for large collections.
For that purpose Microsoft added in .NET 2.0 ReadOnlyCollection<T> wrapper:
[SerializableAttribute] [ComVisibleAttribute(false)] public class ReadOnlyCollection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
Constructor of ReadOnlyCollection<T> accepts IList<T> that it wraps:
public聽ReadOnlyCollection(IList<T> list)
It only wraps a collection, so it is O(1).
So lets change our code:
public class FirstFiveOddNumbers { 聽聽聽 List<int> numbers = new List<int>(new int[]{1,3,5,7,9}); . . . 聽聽聽 ReadOnlyCollection<int> GetNumbers() 聽聽聽 { 聽聽聽聽聽聽聽 return new ReadOnlyCollection(numbers); 聽聽聽 } }
Now, caller is unable to change returned collection, caller can't even cast returned collection to List<int> and then change items of collection.
It seems, like that is what we want. Is it !?
The problem is that ReadOnlyCollection<T> is not covariant in T, like any other implementation (only interfaces can be type covariant).
This code demostrates a problem:
public interface Shape { } public class Rectangle : Shape { } public interface Geometry { 聽聽聽 ReadOnlyCollection<Shape> GetShapes(); } public class RectangleGeomerty : Geometry { 聽聽聽 List<Rectangle> rectangles; 聽聽聽 ReadOnlyCollection<Shape> GetShapes() 聽聽聽 { 聽聽聽聽聽聽聽 return new ReadOnlyCollection<Shape>(rectangles); 聽聽聽 } }
This code is not valid, actually it produces a compiling error in method GetShapes(). Solution is to explicitly cast rectanges to shapes.
return new ReadOnlyCollection<Shape>(rectangels.Cast<Shape>());
But this is O(n) operation and we would like O(1).
So .NET creators bring interface IReadOnlyList<out T> in .NET 4.5. As you can see out T means it is covariant, so following code will do job as I wanted.
public interface Shape { } public class Rectangle : Shape { } public interface Geometry { 聽聽聽 IReadOnlyList<Shape> GetShapes(); } public class RectangleGeomerty : Geometry { 聽聽聽 List<Rectangle> rectangles; 聽聽聽 IReadOnlyList<Shape> GetShapes() 聽聽聽 { 聽聽聽聽聽聽聽 return rectangles; 聽聽聽 } }
Yeah, now seems like everything is solved. But then you are looking at specification of .NET 4.5 and find out that List<T> implements IReadOnlyList<T> which is OK, but IList<T> doesn't implement IReadOnlyList<out T>, which is not OK.
So code like this is not possible:
IList<Rectangle> rectangles; IReadOnlyList<Shapes> shapes = rectangles;
In fact, interface IList<T> is not covariant so there is a problem and they are unable to change IList<T> to IList<out T> because it would require moving some members from IList<T> to IReadOnlyCollection<out T> and this would bring a lot of problems in CLR 4.5. In fact, CLR 4.5 would be unable to run applications developed with frameworks .NET 1.0, .NET 2.0, .NET 3.0, .NET 3.5 and .NET 4.0
The problem can be overcome simply by implementing class that inherits IReadOnlyList<T> and is wrapper for IList<T> object,
public class ReadOnlyList<T> : IReadOnlyList<T> 聽聽聽 { 聽聽聽聽聽聽 private IList<T> list; 聽聽聽聽聽聽 public ReadOnlyList(IList<T> list) 聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽 this.list = list; 聽聽聽聽聽聽 } 聽聽聽聽聽聽 public T this[int index] 聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽 get { return list[index]; } 聽聽聽聽聽聽 } 聽聽聽聽聽聽 public int Count 聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽 get { return list.Count; } 聽聽聽聽聽聽 } 聽聽聽聽聽聽 public IEnumerator<T> GetEnumerator() 聽聽聽聽聽聽 { 聽聽聽聽聽聽聽聽聽聽 return list.GetEnumerator(); 聽聽聽聽聽聽 } 聽聽聽 }
or we can add extension method for IList<T>
public static IReadOnlyList<T> AsReadOnly<T>(this IList<T> collection) { 聽聽聽聽聽聽聽 return new ReadOnlyList<T>(collection); }
0 notes
santevid 12 years ago
Text
Dispose()
Usually you can read how programming in languages such as C# and Java should be a lot easier than C++. One argument is memory handling in C++ where you need to contrive a smart memory management system before you even start to implement any complex solution.
Yes, you don't need to think about freeing memory since you have Garbage Collector, but life is not much easier in modern OOP environments since there are disposable objects. Carelessly handling with disposable objects can turn your code in mess, especially if you are coding with other team members on a project.
Before starting any coding, you have to clear some things with your team. One of those is handling disposable objects.
There is a standard rule that everybody should follow:
"The object which creates a disposable object and owns it is responsible for disposing it.".
I even thought to implement something like Smart Pointers in C++ (http://en.wikipedia.org/wiki/Smart_pointer) for C# disposable objects, but soonly I've realized it's not a smart idea. The best is to follow upper rule 1.
If a method returns disposable object
TODO
0 notes