private SortableSize allocateSize(int width, int height, int id)
        {
            if (mSortableSizeStack.Count > 0)
            {
                SortableSize size = mSortableSizeStack.Pop();
                size.width  = width;
                size.height = height;
                size.id     = id;

                return(size);
            }

            return(new SortableSize(width, height, id));
        }
        public int packRectangles(bool sort = true)
        {
            if (sort)
            {
                mInsertList.Sort((emp1, emp2) => emp1.width.CompareTo(emp2.width));
            }

            while (mInsertList.Count > 0)
            {
                SortableSize sortableSize = mInsertList.Pop();
                int          width        = sortableSize.width;
                int          height       = sortableSize.height;

                int index = getFreeAreaIndex(width, height);
                if (index >= 0)
                {
                    IntegerRectangle freeArea = mFreeAreas[index];
                    IntegerRectangle target   = allocateRectangle(freeArea.x, freeArea.y, width, height);
                    target.id = sortableSize.id;

                    // Generate the new free areas, these are parts of the old ones intersected or touched by the target
                    generateNewFreeAreas(target, mFreeAreas, mNewFreeAreas);

                    while (mNewFreeAreas.Count > 0)
                    {
                        mFreeAreas.Add(mNewFreeAreas.Pop());
                    }

                    mInsertedRectangles.Add(target);

                    if (target.right > mPackedWidth)
                    {
                        mPackedWidth = target.right;
                    }

                    if (target.bottom > mPackedHeight)
                    {
                        mPackedHeight = target.bottom;
                    }
                }

                freeSize(sortableSize);
            }

            return(rectangleCount);
        }
 private void freeSize(SortableSize size)
 {
     mSortableSizeStack.Add(size);
 }
        public void insertRectangle(int width, int height, int id)
        {
            SortableSize sortableSize = allocateSize(width, height, id);

            mInsertList.Add(sortableSize);
        }