From edc6cafb276c7e80c6795783d1cc908a5c3aa195 Mon Sep 17 00:00:00 2001 From: YaVanilko Date: Mon, 7 Sep 2015 00:04:59 +0300 Subject: [PATCH 1/2] Priority queue --- Encapsulation/Elena/ComplexNumber.cs | 96 +++++++++ Encapsulation/Elena/EncapsulationExercises.cs | 45 +++- Encapsulation/Elena/Money.cs | 120 +++++++++++ Encapsulation/Elena/Number.cs | 33 +++ Encapsulation/Elena/PriorityQueue.cs | 200 ++++++++++++++++++ 5 files changed, 491 insertions(+), 3 deletions(-) create mode 100644 Encapsulation/Elena/ComplexNumber.cs create mode 100644 Encapsulation/Elena/Money.cs create mode 100644 Encapsulation/Elena/Number.cs create mode 100644 Encapsulation/Elena/PriorityQueue.cs diff --git a/Encapsulation/Elena/ComplexNumber.cs b/Encapsulation/Elena/ComplexNumber.cs new file mode 100644 index 0000000..2b81670 --- /dev/null +++ b/Encapsulation/Elena/ComplexNumber.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Encapsulation.Elena +{ + class ComplexNumber + { + private float real; + private float imegion; + + public ComplexNumber() + { + real=0; + imegion=0; + } + + public ComplexNumber(float r, float i) + { + real=r; + imegion=i; + } + + public float Real + { + get { return real; } + } + + public float Imegion + { + get { return imegion; } + } + + public static ComplexNumber operator+(ComplexNumber operand1, ComplexNumber operand2) + { + return new ComplexNumber(operand1.real + operand2.real, operand1.imegion + operand2.imegion); + } + + public static ComplexNumber operator -(ComplexNumber operand1, ComplexNumber operand2) + { + return new ComplexNumber(operand1.real - operand2.real, operand1.imegion - operand2.imegion); + } + + public static ComplexNumber operator *(ComplexNumber operand1, int multiplier) + { + return new ComplexNumber(operand1.real *multiplier, operand1.imegion*multiplier); + } + + public static ComplexNumber operator *(ComplexNumber operand1, ComplexNumber operand2) + { + return new ComplexNumber((operand1.real * operand2.real-operand1.imegion*operand2.imegion), (operand1.imegion * operand2.real+operand1.imegion*operand2.imegion)); + } + + public static ComplexNumber operator /(ComplexNumber operand1, ComplexNumber operand2) + { + float real; + float imegion; + + real = (operand1.real * operand2.real + operand1.imegion * operand2.imegion) / (operand2.imegion * operand2.imegion + operand2.real * operand2.real); + imegion=(operand1.imegion*operand2.real-operand1.real*operand2.imegion)/ (operand2.imegion * operand2.imegion + operand2.real * operand2.real); + return new ComplexNumber(real, imegion); + } + + public static ComplexNumber operator /(ComplexNumber operand1, int div) + { + if (div != 0) + { + return new ComplexNumber(operand1.real / div, operand1.imegion / div); + } + else { return null; } + } + + public static bool operator ==(ComplexNumber operand1, ComplexNumber operand2) + { + bool returnValue = false; + if (operand1.real == operand2.real && operand1.imegion == operand2.imegion) + { returnValue = true; } + return returnValue; + } + + public static bool operator !=(ComplexNumber operand1, ComplexNumber operand2) + { + bool returnValue = false; + if (operand1.real != operand2.real && operand1.imegion != operand2.imegion) + { returnValue = true; } + return returnValue; + } + + public static implicit operator string(ComplexNumber operand1) + { + return String.Format("{0}+{1}i",operand1.Real,operand1.imegion); + } + } +} diff --git a/Encapsulation/Elena/EncapsulationExercises.cs b/Encapsulation/Elena/EncapsulationExercises.cs index 4585479..5d927d4 100644 --- a/Encapsulation/Elena/EncapsulationExercises.cs +++ b/Encapsulation/Elena/EncapsulationExercises.cs @@ -10,13 +10,52 @@ class EncapsulationExercises : IEncapsulationExercises { public void MoneyMoney() { - throw new NotImplementedException(); - } + + + Money m1 = new Money(); + Money m2 = new Money(25,51); + Money m3 = new Money(768); + + int hrivnas = m1.Hrivnas; + int kopecks = m1.Kopeks; + Money m4 = m1 + m3; + int totalKopecs = (int)m2; + float someMoney = (float)m2; + string amount = (string)m2; + + ComplexNumber c1 = new ComplexNumber(2,3); + ComplexNumber c2 = new ComplexNumber(-8, 5); + + ComplexNumber c3 = c1 + c2; + c3 = c1 - c2; + c3 = c1 * 2; + c3 = c1 * c2; + c3=c1/4; + c3 = c1 / c2; + string complexNumber = (string)c2; + + } public void WorkPriorityQueue() { - throw new NotImplementedException(); + Queue q=new Queue(); + PriorityQueue pq = new PriorityQueue(0,q ); + pq.Enqueue("Hi!", 2); + pq.Enqueue("WTF?", 2); + pq.Enqueue("Hello!", 1); + pq.Enqueue("W", 2); + pq.Enqueue(88, 1); + + object first = pq.First(); + object firstPriority = pq.First(2); + + object last = pq.Last(); + object lastPriority = pq.Last(1); + object count = pq.Count; + object countPriority = pq.GetCount(2); + + } } } diff --git a/Encapsulation/Elena/Money.cs b/Encapsulation/Elena/Money.cs new file mode 100644 index 0000000..26d331b --- /dev/null +++ b/Encapsulation/Elena/Money.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Encapsulation.Elena +{ + class Money + { + private int kopecks; + + public Money(){ } + + public Money(int amountInKopecks) + { + //if(amountInKopecks>0) + kopecks = amountInKopecks; + } + + public Money(int countCurrencyUnit, int countKopecks) + { + if (countKopecks > 0 && countKopecks < 100) + { + kopecks = countCurrencyUnit * 100 + countKopecks; + } + } + + public int Hrivnas + { + get {return kopecks / 100; } + } + + public int Kopeks + { + get {return kopecks % 100; } + } + + public static Money operator +(Money operand1, Money operand2) + { + return new Money(operand1.kopecks + operand2.kopecks); + } + + public static Money operator *(int multiplier, Money operand) + { + return new Money(operand.kopecks*multiplier); + } + + public static Money operator /(int div, Money operand) + { + return new Money(operand.kopecks / div); + } + + public static bool operator==(Money operand1, Money operand2) + { + bool returValue=false; + if (operand1.kopecks == operand2.kopecks) + { returValue= true; } + + return returValue; + } + + public static bool operator !=(Money operand1, Money operand2) + { + bool returValue = false; + if (operand1.kopecks != operand2.kopecks) + { returValue= true; } + return returValue; + } + + public static bool operator<(Money operand1, Money operand2) + { + bool returValue=false; + if(operand1.kopecks(Money operand1, Money operand2) + { + bool returValue = false; + if (operand1.kopecks > operand2.kopecks) + { returValue = true; } + return returValue; + } + + public static bool operator <=(Money operand1, Money operand2) + { + bool returValue = false; + if (operand1.kopecks <= operand2.kopecks) + { returValue = true; } + return returValue; + } + + public static bool operator >=(Money operand1, Money operand2) + { + bool returValue = false; + if (operand1.kopecks >= operand2.kopecks) + { returValue = true; } + return returValue; + } + + public static implicit operator float(Money operand1) + { + return ((float)operand1.kopecks / 100); + } + + public static implicit operator int(Money operand1) + { + return operand1.kopecks; + } + + public static implicit operator string(Money operand1) + { + return String.Format("{0} грн. {1} коп.", operand1.Hrivnas, operand1.Kopeks); + } + + + } +} diff --git a/Encapsulation/Elena/Number.cs b/Encapsulation/Elena/Number.cs new file mode 100644 index 0000000..9b3b8ec --- /dev/null +++ b/Encapsulation/Elena/Number.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Encapsulation.Elena +{ + abstract class Number + { + protected T value; + public Number() { } + public Number(T val) + { + value=val; + } + + public abstract T Add(T op1, T op2); + + } + + /* class Int : Number + { + int Add(int op) + { return value + op; } + } + + class Double : Number + { + double Add(double op) + { return value + op; } + }*/ +} diff --git a/Encapsulation/Elena/PriorityQueue.cs b/Encapsulation/Elena/PriorityQueue.cs new file mode 100644 index 0000000..df02c1c --- /dev/null +++ b/Encapsulation/Elena/PriorityQueue.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Encapsulation.Elena +{ + public static class SortedData + { + public static SortedSet OurSetWhithData = new SortedSet(new SortQueueByPriority()); + } + + public class PriorityQueue : IPriorityQueue + { + private int priority; + private Queue queueForPriority; + + public int Priority + { + get { return priority; } + } + + + public PriorityQueue(int p, Queue q) + { + priority = p; + queueForPriority = q; + } + + public void Enqueue(object val, int priority) + { + bool isPriorityExist = false; + foreach (PriorityQueue p in SortedData.OurSetWhithData) + + { + if (p.priority == priority) + { + isPriorityExist = true; + p.queueForPriority.Enqueue(val); + } + } + + if (!isPriorityExist) + { + Queue q = new Queue(); + q.Enqueue(val); + + SortedData.OurSetWhithData.Add(new PriorityQueue(priority, q)); + } + + + } + + public object Dequeue() + { + object decueue; + try + { + decueue = SortedData.OurSetWhithData.First().queueForPriority.Dequeue(); + } + + catch (InvalidOperationException) + { + decueue = null; + } + + return decueue; + } + + public object First() + { + object first; + try + { + first = SortedData.OurSetWhithData.First().queueForPriority.Peek(); + } + catch (InvalidOperationException) + { + first = null; + } + + return first; + } + + public object First(int priority) + { + object first =null; + try + { + + foreach (PriorityQueue p in SortedData.OurSetWhithData) + { + if (p.priority == priority) + { + first = p.queueForPriority.Peek(); + } + } + } + catch (InvalidOperationException) + { + first = null; + } + + return first; + } + + public object Last() + { + object last; + try + { + last = SortedData.OurSetWhithData.Last().queueForPriority.Last(); + } + catch (InvalidOperationException) + { + last = null; + } + return last; + } + + public object Last(int priority) + { + object last=null; + try + { + foreach (PriorityQueue p in SortedData.OurSetWhithData) + { + if (p.priority == priority) + { + last = p.queueForPriority.Last(); + } + } + } + + + catch (InvalidOperationException) + { + last = null; + } + return last; + } + + public int Count + { + get + { + int allCount=0; + try + { + foreach (PriorityQueue p in SortedData.OurSetWhithData) + { + + allCount += p.queueForPriority.Count(); + + } + } + + catch (InvalidOperationException) + { } + return allCount; + } + } + + public int GetCount(int priority) + { + int countInThisQoueu=0; + try + { + foreach (PriorityQueue p in SortedData.OurSetWhithData) + { + if (p.priority == priority) + { + countInThisQoueu = p.queueForPriority.Count(); + } + } + } + catch (InvalidOperationException) + { } + return countInThisQoueu; + } + } + + class SortQueueByPriority : IComparer + { + + public int Compare(PriorityQueue pq1, PriorityQueue pq2) + { + int resultOfCompare = 0; + if (pq1.Priority > pq2.Priority) + { resultOfCompare = 1; } + if (pq1.Priority < pq2.Priority) + { resultOfCompare = -1; } + return resultOfCompare; + } + + + } +} + From 87738db302284747bfc6e08a594845f42082ece9 Mon Sep 17 00:00:00 2001 From: YaVanilko Date: Mon, 14 Sep 2015 01:46:26 +0300 Subject: [PATCH 2/2] Bug fixing --- Encapsulation/Elena/ComplexNumber.cs | 12 +- Encapsulation/Elena/EncapsulationExercises.cs | 63 ++++- Encapsulation/Elena/MyCollection.cs | 48 ++++ Encapsulation/Elena/MyDictionary.cs | 18 ++ Encapsulation/Elena/PriorityQueue.cs | 240 +++++++++++------- Encapsulation/Encapsulation.csproj | 5 + 6 files changed, 277 insertions(+), 109 deletions(-) create mode 100644 Encapsulation/Elena/MyCollection.cs create mode 100644 Encapsulation/Elena/MyDictionary.cs diff --git a/Encapsulation/Elena/ComplexNumber.cs b/Encapsulation/Elena/ComplexNumber.cs index 2b81670..82851d5 100644 --- a/Encapsulation/Elena/ComplexNumber.cs +++ b/Encapsulation/Elena/ComplexNumber.cs @@ -69,23 +69,17 @@ public float Imegion { return new ComplexNumber(operand1.real / div, operand1.imegion / div); } - else { return null; } + else { throw new System.DivideByZeroException(); } } public static bool operator ==(ComplexNumber operand1, ComplexNumber operand2) { - bool returnValue = false; - if (operand1.real == operand2.real && operand1.imegion == operand2.imegion) - { returnValue = true; } - return returnValue; + return operand1.real == operand2.real && operand1.imegion == operand2.imegion; } public static bool operator !=(ComplexNumber operand1, ComplexNumber operand2) { - bool returnValue = false; - if (operand1.real != operand2.real && operand1.imegion != operand2.imegion) - { returnValue = true; } - return returnValue; + return operand1.real != operand2.real && operand1.imegion != operand2.imegion; } public static implicit operator string(ComplexNumber operand1) diff --git a/Encapsulation/Elena/EncapsulationExercises.cs b/Encapsulation/Elena/EncapsulationExercises.cs index 5d927d4..a3609ab 100644 --- a/Encapsulation/Elena/EncapsulationExercises.cs +++ b/Encapsulation/Elena/EncapsulationExercises.cs @@ -10,10 +10,8 @@ class EncapsulationExercises : IEncapsulationExercises { public void MoneyMoney() { - - Money m1 = new Money(); - Money m2 = new Money(25,51); + Money m2 = new Money(25, 51); Money m3 = new Money(768); int hrivnas = m1.Hrivnas; @@ -24,36 +22,73 @@ public void MoneyMoney() float someMoney = (float)m2; string amount = (string)m2; - ComplexNumber c1 = new ComplexNumber(2,3); + ComplexNumber c1 = new ComplexNumber(2, 3); ComplexNumber c2 = new ComplexNumber(-8, 5); ComplexNumber c3 = c1 + c2; c3 = c1 - c2; c3 = c1 * 2; c3 = c1 * c2; - c3=c1/4; + c3 = c1 / 4; c3 = c1 / c2; string complexNumber = (string)c2; + } + public void WorkPriorityQueue() { - Queue q=new Queue(); - PriorityQueue pq = new PriorityQueue(0,q ); + Queue q = new Queue(); + PriorityQueue pq = new PriorityQueue(0, q); + pq.OurSetWhithData = new SortedSet>(new SortQueueByPriority()); pq.Enqueue("Hi!", 2); pq.Enqueue("WTF?", 2); pq.Enqueue("Hello!", 1); pq.Enqueue("W", 2); - pq.Enqueue(88, 1); + pq.Enqueue("88", 1); + + string first = pq.First(); + + string last = pq.Last(); + int count = pq.Count; + int countPriority = pq.GetCount(2); + pq.Clear(); + + } - object first = pq.First(); - object firstPriority = pq.First(2); - object last = pq.Last(); - object lastPriority = pq.Last(1); - object count = pq.Count; - object countPriority = pq.GetCount(2); + public void WorkCollectionInheritedClasses() + { + MyDictionary dic = new MyDictionary(); + dic.Add(new Student() { FirstName = "Ivan", LastName = "Ivanov", ApplicationDate = new DateTime(2015, 01, 01), personalCode = 2012365566, Rating = 5, BirthDay = new DateTime(1990, 01, 05) }); + dic.Add(new Student() { FirstName = "Stanislav", LastName = "Sidorov", ApplicationDate = new DateTime(2010, 11, 22), personalCode = 265000041, Rating = 3, BirthDay = new DateTime(1970, 08, 15) }); + dic.Add(new Student() { FirstName = "Victoriya", LastName = "Stashatova", ApplicationDate = new DateTime(2014, 08, 21), personalCode = 2012445566, Rating = 5, BirthDay = new DateTime(1978, 09, 17) }); + Student serch = dic[new Tuple("Stanislav", "Sidorov", new DateTime(1970, 08, 15), 265000041)]; + + MyCollection collection = new MyCollection(); + collection.Add(new Student() { FirstName = "Ivan", LastName = "Ivanov", ApplicationDate = new DateTime(2015, 01, 01), personalCode = 2012365566, Rating = 5, BirthDay = new DateTime(1990, 01, 05) }); + collection.Add(new Student() { FirstName = "Stanislav", LastName = "Sidorov", ApplicationDate = new DateTime(2010, 11, 22), personalCode = 265000041, Rating = 3, BirthDay = new DateTime(1970, 08, 15) }); + collection.Add(new Student() { FirstName = "Victoriya", LastName = "Stashatova", ApplicationDate = new DateTime(2014, 08, 21), personalCode = 2012445566, Rating = 5, BirthDay = new DateTime(1978, 09, 17) }); + + collection.Insert(2, new Student() { FirstName = "Angelina", LastName = "Kucherova", ApplicationDate = new DateTime(2014, 12, 01), personalCode = 222025566, Rating = 4, BirthDay = new DateTime(1984, 12, 30) }); + collection.Remove(new Student() { FirstName = "Ivan", LastName = "Ivanov", ApplicationDate = new DateTime(2015, 01, 01), personalCode = 2012365566, Rating = 5, BirthDay = new DateTime(1990, 01, 05) }); + // collection. .Set(2, new Student() { FirstName = "Sergey", LastName = "Kuprin", ApplicationDate = new DateTime(2013, 12, 08), personalCode = 6545566, Rating = 1, BirthDay = new DateTime(1987, 02, 15) }); + collection.Clear(); + } + + class SortQueueByPriority : IComparer> + { + + public int Compare(PriorityQueue pq1, PriorityQueue pq2) + { + int resultOfCompare = 0; + if (pq1.Priority > pq2.Priority) + { resultOfCompare = 1; } + if (pq1.Priority < pq2.Priority) + { resultOfCompare = -1; } + return resultOfCompare; + } } diff --git a/Encapsulation/Elena/MyCollection.cs b/Encapsulation/Elena/MyCollection.cs new file mode 100644 index 0000000..948c336 --- /dev/null +++ b/Encapsulation/Elena/MyCollection.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Encapsulation.Elena +{ + struct Student + { + public string FirstName; + public string LastName; + public DateTime ApplicationDate; + public DateTime BirthDay; + public int Rating; + public int personalCode; + + } + + class MyCollection:Collection + { + protected override void ClearItems() + { + base.ClearItems(); + Console.WriteLine("Data on struct is cleaning!"); + } + + protected override void InsertItem(int index, Student item) + { + base.InsertItem(index, item); + Console.WriteLine("New item inserted"); + + } + + protected override void RemoveItem(int index) + { + base.RemoveItem(index); + Console.WriteLine("Item {0} is removed", index); + } + + protected override void SetItem(int index, Student item) + { + base.SetItem(index, item); + Console.WriteLine("Item is seting"); + } + } +} diff --git a/Encapsulation/Elena/MyDictionary.cs b/Encapsulation/Elena/MyDictionary.cs new file mode 100644 index 0000000..db89f27 --- /dev/null +++ b/Encapsulation/Elena/MyDictionary.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Encapsulation.Elena +{ + class MyDictionary:KeyedCollection, Student> + { + protected override Tuple GetKeyForItem(Student item) + { + Tuple returnTuple = new Tuple(item.FirstName, item.LastName, item.BirthDay, item.personalCode); + return returnTuple; + } + } +} diff --git a/Encapsulation/Elena/PriorityQueue.cs b/Encapsulation/Elena/PriorityQueue.cs index df02c1c..8f83e23 100644 --- a/Encapsulation/Elena/PriorityQueue.cs +++ b/Encapsulation/Elena/PriorityQueue.cs @@ -6,33 +6,30 @@ namespace Encapsulation.Elena { - public static class SortedData - { - public static SortedSet OurSetWhithData = new SortedSet(new SortQueueByPriority()); - } - public class PriorityQueue : IPriorityQueue + public class PriorityQueue : IPriorityQueue + , ICollection> { - private int priority; - private Queue queueForPriority; + private int priority; + private Queue queueForPriority; + public SortedSet> OurSetWhithData = new SortedSet>();//(new SortQueueByPriority()); public int Priority { get { return priority; } } - - public PriorityQueue(int p, Queue q) + + public PriorityQueue(int p, Queue q) { priority = p; queueForPriority = q; } - public void Enqueue(object val, int priority) + public void Enqueue(T val, int priority) { bool isPriorityExist = false; - foreach (PriorityQueue p in SortedData.OurSetWhithData) - + foreach (PriorityQueue p in OurSetWhithData) { if (p.priority == priority) { @@ -43,131 +40,108 @@ public void Enqueue(object val, int priority) if (!isPriorityExist) { - Queue q = new Queue(); + Queue q = new Queue(); q.Enqueue(val); - - SortedData.OurSetWhithData.Add(new PriorityQueue(priority, q)); + + OurSetWhithData.Add(new PriorityQueue(priority, q)); } - + } - public object Dequeue() + public T Dequeue() { - object decueue; - try + if (this.Count <= 0) { - decueue = SortedData.OurSetWhithData.First().queueForPriority.Dequeue(); + throw new InvalidOperationException(); } - catch (InvalidOperationException) - { - decueue = null; - } - - return decueue; + return OurSetWhithData.First>().queueForPriority.Dequeue(); } - public object First() + public T First() { - object first; - try + if (this.Count <= 0) { - first = SortedData.OurSetWhithData.First().queueForPriority.Peek(); - } - catch (InvalidOperationException) - { - first = null; + throw new InvalidOperationException(); } + return OurSetWhithData.First>().queueForPriority.Peek(); - return first; } - public object First(int priority) + public T First(int priority) { - object first =null; + try { - - foreach (PriorityQueue p in SortedData.OurSetWhithData) + foreach (PriorityQueue p in OurSetWhithData) { if (p.priority == priority) { - first = p.queueForPriority.Peek(); + return p.queueForPriority.Peek(); } - } - } - catch (InvalidOperationException) - { - first = null; + + } throw new InvalidOperationException(); } + catch + { throw new InvalidOperationException(); } - return first; } - public object Last() + public T Last() { - object last; - try - { - last = SortedData.OurSetWhithData.Last().queueForPriority.Last(); - } - catch (InvalidOperationException) + if (this.Count <= 0) { - last = null; + throw new InvalidOperationException(); } - return last; + return OurSetWhithData.Last>().queueForPriority.Last(); } - public object Last(int priority) + public T Last(int priority) { - object last=null; try { - foreach (PriorityQueue p in SortedData.OurSetWhithData) + foreach (PriorityQueue p in OurSetWhithData) { if (p.priority == priority) { - last = p.queueForPriority.Last(); + return p.queueForPriority.Last(); } - } - } - - catch (InvalidOperationException) - { - last = null; + } throw new InvalidOperationException(); } - return last; + catch + { throw new InvalidOperationException(); } + throw new InvalidOperationException(); } public int Count { - get + get { - int allCount=0; + int allCount = 0; try { - foreach (PriorityQueue p in SortedData.OurSetWhithData) + foreach (PriorityQueue p in OurSetWhithData) { - - allCount += p.queueForPriority.Count(); - + + allCount += p.queueForPriority.Count(); + } } catch (InvalidOperationException) - { } + { } return allCount; } } public int GetCount(int priority) { - int countInThisQoueu=0; + int countInThisQoueu = 0; try { - foreach (PriorityQueue p in SortedData.OurSetWhithData) + foreach (PriorityQueue p in OurSetWhithData) { if (p.priority == priority) { @@ -179,22 +153,116 @@ public int GetCount(int priority) { } return countInThisQoueu; } - } - class SortQueueByPriority : IComparer - { - - public int Compare(PriorityQueue pq1, PriorityQueue pq2) + + + public void Add(Tuple item) { - int resultOfCompare = 0; - if (pq1.Priority > pq2.Priority) - { resultOfCompare = 1; } - if (pq1.Priority < pq2.Priority) - { resultOfCompare = -1; } - return resultOfCompare; + this.Enqueue(item.Item2, item.Item1); } + public void Clear() + { + while (this.Count > 0) + { + OurSetWhithData.Clear(); + } + } + public bool Contains(Tuple item) + { + bool result = false; + if (GetCount(item.Item1) > 0) + { + foreach (PriorityQueue p in OurSetWhithData) + { + if (p.priority == item.Item1) + { + result = p.queueForPriority.Contains(item.Item2); + } + } + } + + else + { + throw new InvalidOperationException(); + } + + return result; + } + + public void CopyTo(Tuple[] array, int arrayIndex) + { + int countOfArray = this.Count; + for (int i = arrayIndex; i < countOfArray; i++) + { + array[i] = new Tuple(1, this.Dequeue()); + } + } + + public bool IsReadOnly + { + get { return false; } + } + + public bool Remove(Tuple item) + { + bool isRemove = false; + if (Contains(item)) + { + + } + + return isRemove; + } + + public IEnumerator> GetEnumerator() + { + Tuple t; //= new Tuple(); + int priority; + foreach (var p in this.OurSetWhithData) + { + priority = p.priority; + foreach (var element in p.queueForPriority) + { + t = new Tuple(priority, element); + yield return t; + } + } + + + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + Tuple t; //= new Tuple(); + int priority; + foreach (var p in this.OurSetWhithData) + { + priority = p.priority; + foreach (var element in p.queueForPriority) + { + t = new Tuple(priority, element); + yield return t; + } + } + } } + + /* class SortQueueByPriority : IComparer> + { + + public int Compare(PriorityQueue pq1, PriorityQueue pq2) + { + int resultOfCompare = 0; + if (pq1.Priority > pq2.Priority) + { resultOfCompare = 1; } + if (pq1.Priority < pq2.Priority) + { resultOfCompare = -1; } + return resultOfCompare; + } + + + }*/ } diff --git a/Encapsulation/Encapsulation.csproj b/Encapsulation/Encapsulation.csproj index 650c16d..3885fed 100644 --- a/Encapsulation/Encapsulation.csproj +++ b/Encapsulation/Encapsulation.csproj @@ -43,7 +43,12 @@ + + + + +