internal bool IsPlaceableTest([PexAssumeUnderTest] FreeSpace target, List <Card> cards)
        {
            bool result = target.IsPlaceable(cards);

            return(result);
            // TODO: add assertions to method FreeSpaceTest.IsPlaceableTest(FreeSpace, List`1<Card>)
        }
        internal int MaxNumberRemovableTest([PexAssumeUnderTest] FreeSpace target)
        {
            int result = target.MaxNumberRemovable();

            return(result);
            // TODO: add assertions to method FreeSpaceTest.MaxNumberRemovableTest(FreeSpace)
        }
        internal string ErrorMessageGetTest([PexAssumeUnderTest] FreeSpace target)
        {
            string result = target.ErrorMessage;

            return(result);
            // TODO: add assertions to method FreeSpaceTest.ErrorMessageGetTest(FreeSpace)
        }
        internal bool IsFreeTest([PexAssumeUnderTest] FreeSpace target)
        {
            bool result = target.IsFree();

            return(result);
            // TODO: add assertions to method FreeSpaceTest.IsFreeTest(FreeSpace)
        }
        internal FreeSpace ConstructorTest()
        {
            FreeSpace target = new FreeSpace();

            return(target);
            // TODO: add assertions to method FreeSpaceTest.ConstructorTest()
        }
Exemple #6
0
        public GameState GenerateNewNumber()
        {
            FreeSpace.Clear();
            GameState OKGameStateReturn = GameState.Continue;

            // fill with nulls
            for (int i = 0; i < Cols; i++)
            {
                for (int j = 0; j < Rows; j++)
                {
                    if (GameBoard[i, j] == 0)
                    {
                        FreeSpace.Add(new int[2] {
                            i, j
                        });
                    }
                    if (GameBoard[i, j] == 2048)
                    {
                        if (!ContinueGameMode)
                        {
                            OKGameStateReturn = GameState.Won;
                        }
                    }
                }
            }

            if (FreeSpace.Count > 0)
            {
                // 80% chance to generate one number, 20% chance to generate two numbers
                int repeatNewNumber = 1;
                if (RNG.Next(1, 6) == 5)
                {
                    repeatNewNumber = 2;
                }

                for (int k = 1; k <= repeatNewNumber; k++)
                {
                    // 80% chance to 2, 20% chance to 4
                    int numberValue = 2;
                    if (RNG.Next(1, 6) == 5)
                    {
                        numberValue = 4;
                    }

                    // select random free spot
                    int[] selectedSpot = FreeSpace[RNG.Next(0, FreeSpace.Count)];
                    // place the number to the spot
                    GameBoard[selectedSpot[0], selectedSpot[1]] = numberValue;
                }

                return(OKGameStateReturn);
            }
            else
            {
                return(GameState.Lost);
            }
        }
Exemple #7
0
        public virtual int _GetUniqueIdentifier()
        {
            var hashCode = 399326290;

            hashCode = hashCode * -1521134295 + (Id?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (FreeSpace?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (Time?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (Distance?.GetHashCode() ?? 0);
            hashCode = hashCode * -1521134295 + (CurrentLoad?.GetHashCode() ?? 0);
            return(hashCode);
        }
        /// <summary>
        /// Marks an unallocated contiguous block as allocated, and then marks an allocated block as unallocated
        /// </summary>
        /// <param name="block">newly allocated block</param>
        /// <param name="shrinkTo">The length of the smaller free memory block</param>
        /// <param name="suppressSetLargest">True to not have this method update LargestFreeBlockSize</param>
        /// <remarks>
        /// Set suppressSetLargest when the caller will the LargestFreeBlockSize after this method is called</remarks>
        /// <remarks>
        /// Do not call this method directly, instead call ShrinkFreeMemoryBlock() or the FreeMemoryBlock()
        /// </remarks>
        private void ShrinkOrRemoveFreeMemoryBlock(FreeSpace block, long shrinkTo, bool suppressSetLargest)
        {
            //NOTE: Do not call this method directly, instead call ShrinkFreeMemoryBlock() or RemoveFreeBlock()

            //If shrinking confirm that suppressSetLargest is false
            System.Diagnostics.Debug.Assert((shrinkTo > 0 && suppressSetLargest == false) || shrinkTo == 0);

            System.Diagnostics.Debug.Assert(shrinkTo <= block.Length);
            System.Diagnostics.Debug.Assert(shrinkTo >= 0);

            dictStartLoc.Remove(block.Offset);
            dictEndLoc.Remove(block.End);

            bool        calcLargest = false;
            List <long> innerList   = freeBlocksList[block.Length];

            if (innerList.Count == 1)
            {
                freeBlocksList.Remove(block.Length);
                if (!suppressSetLargest && GetLargest() == block.Length)
                {
                    //The largest free block was removed, so there will be a new largest
                    calcLargest = true;
                }
            }
            else
            {
                //Find location of this block in the innerlist and remove it
                int index = innerList.BinarySearch(block.Offset);
                System.Diagnostics.Debug.Assert(index >= 0);
                innerList.RemoveAt(index);
            }

            if (shrinkTo > 0)
            {
                AddFreeBlock(block.Offset + (block.Length - shrinkTo), shrinkTo, calcLargest);
            }

            if (calcLargest)
            {
                //Get the true largest
                if (freeBlocksList.Count == 0)
                {
                    SetLargest(0);
                }
                else
                {
                    long[] indices = new long[freeBlocksList.Count];
                    freeBlocksList.Keys.CopyTo(indices, 0);
                    SetLargest(indices[indices.LongLength - 1]);
                }
            }
        }
Exemple #9
0
        private void UpdateSpace(object sender = null, EventArgs e = null)
        {
            if (Type == FileItemType.Computer)
            {
                int count = children.Count;
                for (int i = 0; i < count; i++)
                {
                    if (children[i] is RootItem root)
                    {
                        root.UpdateSpace(sender, e);
                    }
                }
            }

            // ShowTotalSpace means only the absolute root will show space
            // !ShowTotalSpace means only the file root will show space
            // We do this check because absolute roots can also be file roots
            bool baseShow = (IsAbsoluteRootType && scanning.ShowTotalSpace) ||
                            (IsFileRootType && !scanning.ShowTotalSpace);

            bool showFree    = scanning.ShowFreeSpace && baseShow;
            bool showUnknown = scanning.ShowUnknown && baseShow;

            // Create some children so we can lock things down
            EnsureChildren(2);

            lock (children) {
                if (showFree)
                {
                    FreeSpace.UpdateSize();
                }
                if (showUnknown)
                {
                    Unknown.UpdateSize(GetUsedSize());
                }
                AddOrRemove(FreeSpace, showFree);
                AddOrRemove(Unknown, showUnknown);

                // If nothing was added or removed and children was created,
                // children was never set back to empty.
                EnsureEmptyChildren();

                Invalidate();
            }

            if (IsAbsoluteRootType)
            {
                RootValidate();
            }
        }
Exemple #10
0
        public override void AddObjects()
        {
            var obstacleTemplate = new Obstacle(
                this.detailsConfiguration.ObstacleVisualization);
            //, this.detailsConfiguration.ObstacleForegroundColor
            //, this.detailsConfiguration.ObstacleBackgroundColor);

            var freeSpaceTemplate = new FreeSpace(
                this.detailsConfiguration.FreeSpaceVisualization);

            //, this.detailsConfiguration.FreeSpaceForegroundColor
            //, this.detailsConfiguration.FreeSpaceBackgroundColor);

            this.gameObjectsGenerator.GenerateObjects(this.labyrinth, obstacleTemplate, freeSpaceTemplate);
        }
Exemple #11
0
        public static void LoadContent(ContentManager content)
        {
            Gras.LoadContent(content, @"terrain\0");

            Holzfäller.LoadContent(content, @"buildings\holzfäller");

            MouseCursor.LoadContent(content, @"gui\cursor");
            FreeSpace.LoadContent(content, @"gui\freespace");
            BlockedSpace.LoadContent(content, @"gui\blockedspace");
            Bar.LoadContent(content, @"gui\bar");
            Holz.LoadContent(content, @"gui\holz");
            Stein.LoadContent(content, @"gui\stein");
            Werkzeug.LoadContent(content, @"gui\werkzeug");
            Pixel.LoadContent(content, @"gui\pixel");
        }
Exemple #12
0
        /// <summary>
        /// Resize the heap encreasing the size by the specified value mantaining all data
        /// in the same position.
        /// </summary>
        /// <param name="size">Grow size in bytes (the actual grow size could be bigger than the specified).</param>
        /// <param name="noOvergrowth">Force the allocator to grow the heap by the exact specified size.</param>
        /// <returns>Returns <see cref="true"/> if the memory allocation has been done.</returns>
        public virtual bool Grow(uint size, bool noOvergrowth = false)
        {
            // - Check if the heap can grow
            if (!CanGrow)
            {
                return(false);
            }

            // - Check if we reached the memory limit
            if (HeapSize + size > MaximumSize)
            {
                return(false);
            }

            // - Calculates the actual grow size
            uint growSize = noOvergrowth ? size : (uint)(size + GrowFactor * HeapSize);

            try
            {
                // - Realloc the heap memory
                RawMemory = Marshal.ReAllocHGlobal(RawMemory, new IntPtr(HeapSize + growSize));
            }
            catch (OutOfMemoryException)
            {
                return(false);
            }

            // - Gets the last avaiable chunk of memory
            var lastChunk = FreeSpace.LastOrDefault();

            // - Create a new memory pointer pointing to the last block if there's no free block at the end
            if (lastChunk == null || lastChunk.Offset + lastChunk.Size < HeapSize)
            {
                FreeSpace.Add(new MemoryPointer(this, growSize, HeapSize));
            }

            // - Modify the existing chunk if it's at the end of the heap
            if (lastChunk != null || lastChunk.Offset + lastChunk.Size >= HeapSize)
            {
                lastChunk.ChangeOffsetAndSize(lastChunk.Offset, lastChunk.Size + growSize);
            }

            // - Update the heap size
            HeapSize += growSize;

            return(true);
        }
Exemple #13
0
            public void Save(WidgetConfig config)
            {
                config["HostName"]       = HostName.ToString();
                config["LogonDomain"]    = LogonDomain.ToString();
                config["IpAddress"]      = IpAddress.ToString();
                config["MachineDomain"]  = MachineDomain.ToString();
                config["MacAddress"]     = MacAddress.ToString();
                config["BootTime"]       = BootTime.ToString();
                config["Cpu"]            = Cpu.ToString();
                config["DefaultGateway"] = DefaultGateway.ToString();
                config["DhcpServer"]     = DhcpServer.ToString();
                config["DnsServer"]      = DnsServer.ToString();
                config["FreeSpace"]      = FreeSpace.ToString();

                config["LabelColor"] = ColorTranslator.ToHtml(LabelColor);
                config["ValueColor"] = ColorTranslator.ToHtml(ValueColor);
            }
Exemple #14
0
        /// <summary>
        /// Shrinks the heap memory to the smallest memory that contains
        /// all the allocated blocks.
        /// </summary>
        /// <param name="keepBytes">Number of bytes to keep at the heap's tail.</param>
        /// <returns>Returns <see cref="true"/> if the shring goes well.</returns>
        public virtual bool FreeUnusedMemory(int keepBytes = -1)
        {
            // - Gets the last avaiable chunk of memory
            var lastChunk = FreeSpace.LastOrDefault();

            // - Create a new memory pointer pointing to the last block if there's no free block at the end
            if (lastChunk != null || lastChunk.Offset + lastChunk.Size >= HeapSize)
            {
                // - Automatically takes 10% of the whole size free if keepBytes is -1
                if (keepBytes < 0)
                {
                    keepBytes = (int)System.Math.Ceiling(HeapSize * 0.1);
                }

                // - Calculate the amount of memory to free
                uint shrinkSize = (uint)System.Math.Max(lastChunk.Size - keepBytes, 0);

                if (shrinkSize > 0)
                {
                    try
                    {
                        // - Realloc the heap memory
                        RawMemory = Marshal.ReAllocHGlobal(RawMemory, new IntPtr(HeapSize - shrinkSize));

                        // - Resize the last chunk
                        if (lastChunk.Size - shrinkSize > 0)
                        {
                            lastChunk.ChangeOffsetAndSize(lastChunk.Offset, lastChunk.Size - shrinkSize);
                        }
                        else
                        {
                            FreeSpace.Remove(lastChunk);
                        }

                        HeapSize -= shrinkSize;
                    }
                    catch (OutOfMemoryException)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemple #15
0
        public void GenerateObjects(Labyrinth labyrinth, Obstacle obstacle, FreeSpace freeSpace)
        {
            for (int i = 0; i < labyrinth.Columns; i++)
            {
                for (int j = 0; j < labyrinth.Rows; j++)
                {
                    int         randomNumber = this.randomGenerator.Next(2);
                    IGameObject currentObject;

                    if (randomNumber == 1)
                    {
                        currentObject = (Obstacle)obstacle.Clone();
                    }
                    else
                    {
                        currentObject = (FreeSpace)freeSpace.Clone();
                    }

                    labyrinth[i, j] = currentObject;
                }
            }
        }
Exemple #16
0
            public void Register(int blockInfoIndex, long offset, long size)
            {
                if (size < Helpers.MinFreeSuballocationSizeToRegister)
                {
                    return;
                }

                int bestIndex = -1;

                for (int i = 0; i < FreeSpaces.Length; ++i)
                {
                    ref FreeSpace space = ref FreeSpaces[i];

                    if (space.BlockInfoIndex == -1)
                    {
                        bestIndex = i;
                        break;
                    }

                    if (space.Size < size && (bestIndex == -1 || space.Size < FreeSpaces[bestIndex].Size))
                    {
                        bestIndex = i;
                    }
                }
 /// <summary>
 /// Marks an unallocated contiguous block as allocated, and then marks an allocated block as unallocated
 /// </summary>
 /// <param name="block">newly allocated block</param>
 /// <param name="shrinkTo">The length of the smaller free memory block</param>
 private void ShrinkFreeMemoryBlock(FreeSpace block, long shrinkTo)
 {
     ShrinkOrRemoveFreeMemoryBlock(block, shrinkTo, false);
 }
 /// <summary>
 /// Marks an unallocated contiguous block as allocated
 /// </summary>
 /// <param name="block">newly allocated block</param>
 /// <param name="suppressSetLargest">True to not have this method update LargestFreeBlockSize</param>
 /// <remarks>Set suppressSetLargest when the caller will the LargestFreeBlockSize after this method is called</remarks>
 private void RemoveFreeBlock(FreeSpace block, bool suppressSetLargest)
 {
     ShrinkOrRemoveFreeMemoryBlock(block, 0, suppressSetLargest);
 }
        /// <summary>
        /// Attempts to allocate a memory block of length between minLength and maxLength (both inclusive)
        /// </summary>
        /// <param name="minLength">The minimum acceptable length</param>
        /// <param name="maxLength">The maximum acceptable length</param>
        /// <param name="allocatedBlock">Allocated memory block</param>
        /// <returns>Length of allocated block if successful, zero otherwise</returns>
        /// <remarks>This overload is useful when multiple threads are concurrently working on the slab and the caller wants to allocate a block up to a desired size</remarks>
        public long TryAllocate(long minLength, long maxLength, out IMemoryBlock allocatedBlock)
        {
            if (minLength > maxLength)
            {
                throw new ArgumentException("minLength is greater than maxLength", "minLength");
            }
            if (minLength <= 0)
            {
                throw new ArgumentOutOfRangeException("minLength must be greater than zero", "minLength");
            }
            if (maxLength <= 0)
            {
                throw new ArgumentOutOfRangeException("maxLength must be greater than zero", "maxLength");
            }

            allocatedBlock = null;
            lock (sync)
            {
                if (freeBlocksList.Count == 0)
                {
                    return(0);
                }

                long[] keys = new long[freeBlocksList.Count];
                freeBlocksList.Keys.CopyTo(keys, 0);

                //Leave if the largest free block cannot hold minLength
                if (keys[keys.LongLength - 1] < minLength)
                {
                    return(0);
                }

                //search freeBlocksList looking for the smallest available free block than can fit maxLength
                long length = maxLength;
                int  index  = System.Array.BinarySearch <long>(keys, maxLength);
                if (index < 0)
                {
                    index = ~index;
                    if (index >= keys.Length)
                    {
                        //index is set to the largest free block which can hold minLength
                        index = keys.Length - 1;

                        //length is set to the size of that free block
                        length = keys[index];
                    }
                }


                //Grab the first memoryblock in the freeBlockList innerSortedDictionary
                //There is guanranteed to be at least one in the innerList
                FreeSpace foundBlock = dictStartLoc[freeBlocksList[keys[index]][0]];

                if (foundBlock.Length == length)
                {
                    //Perfect match:

                    //Remove existing free block -- and set Largest if need be
                    RemoveFreeBlock(foundBlock, false);

                    allocatedBlock = new MemoryBlock(foundBlock.Offset, foundBlock.Length, this);
                }
                else
                {
                    //FoundBlock is larger than requested block size

                    //Shrink the existing free memory block by the new allocation
                    ShrinkFreeMemoryBlock(foundBlock, foundBlock.Length - length);

                    allocatedBlock = new MemoryBlock(foundBlock.Offset, length, this);
                }

                return(length);
            }
        }
Exemple #20
0
 public void Initialize()
 {
     space = new FreeSpace();
 }
        /// <summary>
        /// Marks an unallocated contiguous block as allocated, and then marks an allocated block as unallocated
        /// </summary>
        /// <param name="block">newly allocated block</param>
        /// <param name="shrinkTo">The length of the smaller free memory block</param>
        /// <param name="suppressSetLargest">True to not have this method update LargestFreeBlockSize</param>
        /// <remarks>
        /// Set suppressSetLargest when the caller will the LargestFreeBlockSize after this method is called</remarks>
        /// <remarks>
        /// Do not call this method directly, instead call ShrinkFreeMemoryBlock() or the FreeMemoryBlock()
        /// </remarks>
        private void ShrinkOrRemoveFreeMemoryBlock(FreeSpace block, long shrinkTo, bool suppressSetLargest)
        {
            //NOTE: Do not call this method directly, instead call ShrinkFreeMemoryBlock() or RemoveFreeBlock()

            //If shrinking confirm that suppressSetLargest is false
            System.Diagnostics.Debug.Assert((shrinkTo > 0 && suppressSetLargest == false) || shrinkTo == 0);

            System.Diagnostics.Debug.Assert(shrinkTo <= block.Length);
            System.Diagnostics.Debug.Assert(shrinkTo >= 0 );

            dictStartLoc.Remove(block.Offset);
            dictEndLoc.Remove(block.End);

            bool calcLargest = false;
            List<long> innerList = freeBlocksList[block.Length];
            if (innerList.Count == 1)
            {
                freeBlocksList.Remove(block.Length);
                if (!suppressSetLargest && GetLargest() == block.Length)
                {
                    //The largest free block was removed, so there will be a new largest
                    calcLargest = true;
                }
            }
            else
            {
                //Find location of this block in the innerlist and remove it
                int index = innerList.BinarySearch(block.Offset);
                System.Diagnostics.Debug.Assert(index >= 0);
                innerList.RemoveAt(index);
            }

            if (shrinkTo > 0)
            {
                AddFreeBlock(block.Offset + (block.Length - shrinkTo), shrinkTo, calcLargest);
            }

            if (calcLargest)
            {
                //Get the true largest
                if (freeBlocksList.Count == 0)
                {
                    SetLargest(0);
                }
                else
                {
                    long[] indices = new long[freeBlocksList.Count];
                    freeBlocksList.Keys.CopyTo(indices, 0);
                    SetLargest(indices[indices.LongLength - 1]);
                }
            }
        }
 /// <summary>
 /// Marks an unallocated contiguous block as allocated, and then marks an allocated block as unallocated
 /// </summary>
 /// <param name="block">newly allocated block</param>
 /// <param name="shrinkTo">The length of the smaller free memory block</param>
 private void ShrinkFreeMemoryBlock(FreeSpace block, long shrinkTo)
 {
     ShrinkOrRemoveFreeMemoryBlock(block, shrinkTo, false);
 }
 /// <summary>
 /// Marks an unallocated contiguous block as allocated
 /// </summary>
 /// <param name="block">newly allocated block</param>
 /// <param name="suppressSetLargest">True to not have this method update LargestFreeBlockSize</param>
 /// <remarks>Set suppressSetLargest when the caller will the LargestFreeBlockSize after this method is called</remarks>
 private void RemoveFreeBlock(FreeSpace block, bool suppressSetLargest)
 {
     ShrinkOrRemoveFreeMemoryBlock(block, 0, suppressSetLargest);
 }