@@ -1628,7 +1628,6 @@ public void arrayListInClosure() {
16281628 " private static T array store" ,
16291629 " private static int nextFreeIndex = 0" ,
16301630 "" ,
1631- " // Memory management structures" ,
16321631 " private static constant int MAX_FREE_SECTIONS = 256" ,
16331632 " private static int array freeSectionStart" ,
16341633 " private static int array freeSectionCapacity" ,
@@ -1642,49 +1641,39 @@ public void arrayListInClosure() {
16421641 " static function getNextFreeIndex() returns int" ,
16431642 " return nextFreeIndex" ,
16441643 "" ,
1645- " /** Creates a new empty list with default capacity (16) */" ,
16461644 " construct()" ,
16471645 " allocateStorage(INITIAL_CAPACITY)" ,
16481646 "" ,
1649- " /** Creates a new list with specified initial capacity - RECOMMENDED for performance */" ,
16501647 " construct(int initialCapacity)" ,
16511648 " allocateStorage(initialCapacity)" ,
16521649 "" ,
1653- " /** Allocates storage section - tries to reuse freed sections first */" ,
16541650 " private function allocateStorage(int cap)" ,
1655- " // Try to find a freed section that fits" ,
16561651 " for i = 0 to freeSectionCount - 1" ,
16571652 " if freeSectionCapacity[i] >= cap" ,
16581653 " startIndex = freeSectionStart[i]" ,
16591654 " capacity = freeSectionCapacity[i]" ,
16601655 "" ,
1661- " // Remove this section from free list" ,
16621656 " for j = i to freeSectionCount - 2" ,
16631657 " freeSectionStart[j] = freeSectionStart[j + 1]" ,
16641658 " freeSectionCapacity[j] = freeSectionCapacity[j + 1]" ,
16651659 " freeSectionCount--" ,
16661660 " return" ,
16671661 "" ,
1668- " // No suitable free section, allocate new" ,
16691662 " if nextFreeIndex + cap > JASS_MAX_ARRAY_SIZE" ,
1670- " // Try to compact free sections" ,
16711663 " compactFreeList()" ,
16721664 "" ,
16731665 " if nextFreeIndex + cap > JASS_MAX_ARRAY_SIZE" ,
1674- " // Still not enough, wrap around (dangerous!)" ,
16751666 " print(\" ArrayList: WARNING - Memory store exhausted, wrapping around!\" )" ,
16761667 " nextFreeIndex = 0" ,
16771668 "" ,
16781669 " startIndex = nextFreeIndex" ,
16791670 " capacity = cap" ,
16801671 " nextFreeIndex += cap" ,
16811672 "" ,
1682- " /** Compacts the free list by merging adjacent sections */" ,
16831673 " private static function compactFreeList()" ,
16841674 " if freeSectionCount <= 1" ,
16851675 " return" ,
16861676 "" ,
1687- " // Sort free sections by start index using insertion sort" ,
16881677 " for i = 1 to freeSectionCount - 1" ,
16891678 " let keyStart = freeSectionStart[i]" ,
16901679 " let keyCap = freeSectionCapacity[i]" ,
@@ -1698,67 +1687,54 @@ public void arrayListInClosure() {
16981687 " freeSectionStart[j + 1] = keyStart" ,
16991688 " freeSectionCapacity[j + 1] = keyCap" ,
17001689 "" ,
1701- " // Merge adjacent sections" ,
17021690 " var writeIdx = 0" ,
17031691 " for readIdx = 0 to freeSectionCount - 1" ,
17041692 " if writeIdx > 0 and freeSectionStart[writeIdx - 1] + freeSectionCapacity[writeIdx - 1] == freeSectionStart[readIdx]" ,
1705- " // Merge with previous" ,
17061693 " freeSectionCapacity[writeIdx - 1] += freeSectionCapacity[readIdx]" ,
17071694 " else" ,
1708- " // Keep as separate section" ,
17091695 " if writeIdx != readIdx" ,
17101696 " freeSectionStart[writeIdx] = freeSectionStart[readIdx]" ,
17111697 " freeSectionCapacity[writeIdx] = freeSectionCapacity[readIdx]" ,
17121698 " writeIdx++" ,
17131699 "" ,
17141700 " freeSectionCount = writeIdx" ,
17151701 "" ,
1716- " // Update nextFreeIndex if last section extends to it" ,
17171702 " if freeSectionCount > 0" ,
17181703 " let lastIdx = freeSectionCount - 1" ,
17191704 " if freeSectionStart[lastIdx] + freeSectionCapacity[lastIdx] == nextFreeIndex" ,
17201705 " nextFreeIndex = freeSectionStart[lastIdx]" ,
17211706 " freeSectionCount--" ,
17221707 "" ,
1723- " /** Frees this list's storage section for reuse */" ,
17241708 " private function freeStorage()" ,
17251709 " if capacity <= 0" ,
17261710 " return" ,
17271711 "" ,
1728- " // Add to free list if there's space" ,
17291712 " if freeSectionCount < MAX_FREE_SECTIONS" ,
17301713 " freeSectionStart[freeSectionCount] = startIndex" ,
17311714 " freeSectionCapacity[freeSectionCount] = capacity" ,
17321715 " freeSectionCount++" ,
17331716 "" ,
1734- " // If this was at the end, we can reclaim it immediately" ,
17351717 " if startIndex + capacity == nextFreeIndex" ,
17361718 " nextFreeIndex = startIndex" ,
17371719 " freeSectionCount--" ,
17381720 " else" ,
1739- " // Free list full, try to compact" ,
17401721 " compactFreeList()" ,
17411722 "" ,
1742- " // Try again after compaction" ,
17431723 " if freeSectionCount < MAX_FREE_SECTIONS" ,
17441724 " freeSectionStart[freeSectionCount] = startIndex" ,
17451725 " freeSectionCapacity[freeSectionCount] = capacity" ,
17461726 " freeSectionCount++" ,
17471727 "" ,
1748- " /** Grows the capacity (doubles it) - EXPENSIVE OPERATION! */" ,
17491728 " private function grow()" ,
17501729 " let newCapacity = capacity * 2" ,
17511730 " let oldStart = startIndex" ,
17521731 " let oldCapacity = capacity" ,
17531732 "" ,
1754- " // Try to allocate new section" ,
17551733 " allocateStorage(newCapacity)" ,
17561734 "" ,
1757- " // Copy elements to new location" ,
17581735 " for i = 0 to size - 1" ,
17591736 " store[startIndex + i] = store[oldStart + i]" ,
17601737 "" ,
1761- " // Free old section" ,
17621738 " let tempStart = startIndex" ,
17631739 " let tempCap = capacity" ,
17641740 " startIndex = oldStart" ,
@@ -1768,25 +1744,12 @@ public void arrayListInClosure() {
17681744 " capacity = tempCap" ,
17691745 "" ,
17701746 " ondestroy" ,
1771- " // Clear references to allow garbage collection" ,
17721747 " for i = 0 to size - 1" ,
17731748 " store[startIndex + i] = null" ,
17741749 "" ,
17751750 " // Return storage to free pool" ,
17761751 " freeStorage()" ,
17771752 "" ,
1778- " /** Debug function to get memory layout info */" ,
1779- " function getMemoryInfo() returns string" ,
1780- " return \" Start: \" + startIndex.toString() + \" , Capacity: \" + capacity.toString() + \" , Size: \" + size.toString()" ,
1781- "" ,
1782- " /** Static function to get global memory state */" ,
1783- " static function getGlobalMemoryInfo() returns string" ,
1784- " return \" NextFree: \" + nextFreeIndex.toString() + \" , FreeSections: \" + freeSectionCount.toString() + \" , Used: \" + (nextFreeIndex - freeSectionCount).toString()" ,
1785- "" ,
1786- " // ============================================================================" ,
1787- " // BASIC OPERATIONS" ,
1788- " // ============================================================================" ,
1789- "" ,
17901753 " /** Adds one or more elements to the end of the list (amortized O(1)) */" ,
17911754 " function add(vararg T elems)" ,
17921755 " for elem in elems" ,
@@ -1801,23 +1764,6 @@ public void arrayListInClosure() {
18011764 " print(\" ArrayList: Index out of bounds: \" + index.toString())" ,
18021765 " return store[startIndex + index]" ,
18031766 "" ,
1804- " /** Sets the element at the specified index (O(1)) */" ,
1805- " function set(int index, T elem)" ,
1806- " if index < 0 or index >= size" ,
1807- " print(\" ArrayList: Index out of bounds: \" + index.toString())" ,
1808- " store[startIndex + index] = elem" ,
1809- "" ,
1810- " /** Returns the index of the specified element or -1 if it doesn't exist (O(n)) */" ,
1811- " function indexOf(T elem) returns int" ,
1812- " for i = 0 to size - 1" ,
1813- " if store[startIndex + i] == elem" ,
1814- " return i" ,
1815- " return -1" ,
1816- "" ,
1817- " /** Returns whether the list contains the specified element (O(n)) */" ,
1818- " function has(T elem) returns boolean" ,
1819- " return indexOf(elem) >= 0" ,
1820- "" ,
18211767 " /** Removes the element at the given index and returns it (O(n) - shifts elements) */" ,
18221768 " function removeAt(int index) returns T" ,
18231769 " if index < 0 or index >= size" ,
@@ -1832,28 +1778,6 @@ public void arrayListInClosure() {
18321778 " size--" ,
18331779 " return elem" ,
18341780 "" ,
1835- " /** Removes the element at the given index by swapping with last element (O(1) - DOES NOT PRESERVE ORDER!) */" ,
1836- " function removeSwap(int index) returns T" ,
1837- " if index < 0 or index >= size" ,
1838- " print(\" ArrayList: Index out of bounds: \" + index.toString())" ,
1839- "" ,
1840- " let elem = store[startIndex + index]" ,
1841- "" ,
1842- " // Replace with last element" ,
1843- " size--" ,
1844- " if index < size" ,
1845- " store[startIndex + index] = store[startIndex + size]" ,
1846- "" ,
1847- " return elem" ,
1848- "" ,
1849- " /** Removes the first occurrence of the element from the list (O(n)) */" ,
1850- " function remove(T elem) returns bool" ,
1851- " let index = indexOf(elem)" ,
1852- " if index >= 0" ,
1853- " removeAt(index)" ,
1854- " return true" ,
1855- " return false" ,
1856- "" ,
18571781 " /** Returns the size of the list (O(1)) */" ,
18581782 " function size() returns int" ,
18591783 " return size" ,
0 commit comments