Esempio n. 1
0
        public StackGeneric(int initialCapacity)
        {
            if (initialCapacity < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            // The internal collection is implemented as an array-based list.
            // See the ArrayList.cs for the list implementation.
            _collection = new ArrayListGeneric <T>(initialCapacity);
        }
        /// <summary>
        /// Heapifies the specified newCollection. Overrides the current heap.
        /// </summary>
        public void Initialize(IList <T> newCollection)
        {
            if (newCollection.Count > 0)
            {
                // Reset and reserve the size of the newCollection
                _collection = new ArrayListGeneric <T>(newCollection.Count);

                // Copy the elements from the newCollection to the inner collection
                for (int i = 0; i < newCollection.Count; ++i)
                {
                    _collection.InsertAt(newCollection[i], i);
                }

                // Build the heap
                _buildMaxHeap();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// A breadth first search traversal of the graph, starting from a specified vertex.
        /// Returns the visited vertices of the graph.
        /// </summary>
        public virtual IEnumerable <T> BreadthFirstWalk(T source)
        {
            if (VerticesCount == 0)
            {
                return(new ArrayListGeneric <T>());
            }
            else if (!HasVertex(source))
            {
                throw new Exception("The specified starting vertex doesn't exist.");
            }


            var visited     = new HashSet <T>();
            var queue       = new QueueGeneric <T>(VerticesCount);
            var listOfNodes = new ArrayListGeneric <T>(VerticesCount);

            listOfNodes.Add(source);
            visited.Add(source);

            queue.Enqueue(source);

            while (!queue.IsEmpty)
            {
                var current   = queue.Dequeue();
                var neighbors = Neighbours(current);

                foreach (var adjacent in neighbors)
                {
                    if (!visited.Contains(adjacent))
                    {
                        listOfNodes.Add(adjacent);
                        visited.Add(adjacent);
                        queue.Enqueue(adjacent);
                    }
                }
            }

            return(listOfNodes);
        }
Esempio n. 4
0
        /// <summary>
        /// Finds all the elements that match the typed Search Predicate.
        /// </summary>
        /// <returns>ArrayList<T> of matched elements. Empty list is returned if not element was found.</returns>
        /// <param name="searchMatch">Match predicate.</param>
        public ArrayListGeneric <T> FindAll(Predicate <T> searchMatch)
        {
            // Null Predicate functions are not allowed.
            if (searchMatch == null)
            {
                throw new ArgumentNullException();
            }

            ArrayListGeneric <T> matchedElements = new ArrayListGeneric <T>();

            // Begin searching, and add the matched elements to the new list.
            for (int i = 0; i < _size; ++i)
            {
                if (searchMatch(_collection[i]))
                {
                    matchedElements.Add(_collection[i]);
                }
            }

            // Return the new list of elements.
            return(matchedElements);
        }
        /// <summary>
        /// A depth first search traversal of the graph, starting from a specified vertex.
        /// Returns the visited vertices of the graph.
        /// </summary>
        public virtual IEnumerable <T> DepthFirstWalk(T source)
        {
            // Check for existence of source
            if (VerticesCount == 0)
            {
                return(new ArrayListGeneric <T>(0));
            }
            else if (!HasVertex(source))
            {
                throw new KeyNotFoundException("The source vertex doesn't exist.");
            }

            var visited     = new HashSet <T>();
            var stack       = new StackGeneric <T>();
            var listOfNodes = new ArrayListGeneric <T>(VerticesCount);

            stack.Push(source);

            while (!stack.IsEmpty)
            {
                var current = stack.Pop();

                if (!visited.Contains(current))
                {
                    listOfNodes.Add(current);
                    visited.Add(current);

                    foreach (var adjacent in Neighbours(current))
                    {
                        if (!visited.Contains(adjacent))
                        {
                            stack.Push(adjacent);
                        }
                    }
                }
            }

            return(listOfNodes);
        }
Esempio n. 6
0
        /// <summary>
        /// Get a range of elements, starting from an index..
        /// </summary>
        /// <returns>The range as ArrayList<T>.</returns>
        /// <param name="startIndex">Start index to get range from.</param>
        /// <param name="count">Count of elements.</param>
        public ArrayListGeneric <T> GetRange(int startIndex, int count)
        {
            // Handle the bound errors of startIndex
            if (startIndex < 0 || (uint)startIndex > (uint)_size)
            {
                throw new IndexOutOfRangeException("Please provide a valid starting index.");
            }

            // Handle the bound errors of count and startIndex with respect to _size
            if (count < 0 || startIndex > (_size - count))
            {
                throw new ArgumentOutOfRangeException();
            }

            var newArrayList = new ArrayListGeneric <T>(count);

            // Use Array.Copy to quickly copy the contents from this array to the new list's inner array.
            System.Array.Copy(_collection, startIndex, newArrayList._collection, 0, count);

            // Assign count to the new list's inner _size counter.
            newArrayList._size = count;

            return(newArrayList);
        }
 public BinaryMaxHeap(int capacity, Comparer <T> comparer)
 {
     _collection   = new ArrayListGeneric <T>(capacity);
     _heapComparer = comparer ?? Comparer <T> .Default;
 }
Esempio n. 8
0
 /// <summary>
 /// CONSTRUCTORS
 /// </summary>
 public StackGeneric()
 {
     // The internal collection is implemented as an array-based list.
     // See the ArrayList.cs for the list implementation.
     _collection = new ArrayListGeneric <T>();
 }