Quellcode durchsuchen

BRL.Collections. Initial Import.

woollybah vor 8 Jahren
Ursprung
Commit
97ccefe452

+ 147 - 0
collections.mod/collection.bmx

@@ -0,0 +1,147 @@
+SuperStrict
+
+Import "iterator.bmx"
+Import "errors.bmx"
+
+Interface ICollection<E> Extends IIterable<E>
+
+	Method Size:Int()
+	Method IsEmpty:Int()
+	Method Iterator:IIterator<E>()
+	Method ToArray:E[]()
+	Method Add:Int(element:E)
+	Method Remove:Int(element:E)
+	Method Contains:Int(element:E)
+	Method ContainsAll:Int(c:ICollection<E>)
+	Method AddAll:Int(c:ICollection<E>)
+	Method AddAll:Int(a:E[])
+	Method RemoveAll:Int(c:ICollection<E>)
+	Method RetainAll:Int(c:ICollection<E>)
+	Method Clear()
+	Method Equals:Int(o:Object)
+
+End Interface
+
+
+Type TAbstractCollection<E> Implements ICollection<E> Abstract
+
+	Method Iterator:IIterator<E>() Abstract
+	Method Size:Int() Abstract
+	
+	Method IsEmpty:Int()
+		Return Size() = 0
+	End Method
+
+	Method Add:Int(element:E)
+		Throw New TUnsupportedOperationException
+	End Method
+	
+	Method AddAll:Int(c:ICollection<E>)
+		Local isModified:Int
+		Local i:IIterator<E> = c.Iterator()
+		While i.HasNext()
+			If Add(i.NextElement()) Then
+				isModified = True
+			End If
+		Wend
+		Return isModified
+	End Method
+	
+	Method AddAll:Int(a:E[])
+		Local isModified:Int
+		If a Then
+			For Local i:E = EachIn a
+				If Add(i) Then
+					isModified = True
+				End If
+			Next
+		End If
+		Return isModified
+	End Method
+
+	Method Clear()
+		Local i:IIterator<E> = Iterator()
+		While i.HasNext()
+			i.NextElement()
+			i.Remove()
+		Wend
+	End Method
+	
+	Method Contains:Int(element:E)
+		Local i:IIterator<E> = Iterator()
+		If Not element Then
+			While i.HasNext()
+				If Not i.NextElement() Then
+					Return True
+				End If
+			Wend
+		Else
+			While i.HasNext()
+				If element = i.NextElement() Then
+					Return True
+				End If
+			Wend
+		End If
+		Return False
+	End Method
+	
+	Method ToArray:E[]()
+		' TODO
+	End Method
+	
+	Method RemoveAll:Int(c:ICollection<E>)
+		Local isModified:Int
+		Local i:IIterator<E> = Iterator()
+		While i.HasNext()
+			If c.Contains(i.NextElement()) Then
+				i.Remove()
+				isModified = True
+			End If
+		Wend
+		Return isModified
+	End Method
+	
+	Method Remove:Int(element:E)
+		Local i:IIterator<E> = Iterator()
+		If Not element Then
+			While i.HasNext()
+				If Not i.NextElement() Then
+					i.Remove()
+					Return True
+				End If
+			Wend
+		Else
+			While i.HasNext()
+				If element = i.NextElement() Then
+					i.Remove()
+					Return True
+				End If
+			Wend
+		End If
+		Return False
+	End Method
+	
+	Method ContainsAll:Int(c:ICollection<E>)
+		Local i:IIterator<E> = c.Iterator()
+		While i.HasNext()
+			If Not Contains(i.NextElement()) Then
+				Return False
+			End If
+		Wend
+		Return True
+	End Method
+	
+	Method RetainAll:Int(c:ICollection<E>)
+		Local isModified:Int
+		Local i:IIterator<E> = Iterator()
+		While i.HasNext()
+			If Not c.Contains(i.NextElement()) Then
+				i.Remove()
+				isModified = True
+			End If
+		Wend
+		Return isModified
+	End Method
+	
+End Type
+

+ 10 - 0
collections.mod/collections.bmx

@@ -0,0 +1,10 @@
+SuperStrict
+
+Rem
+bbdoc: 
+End Rem
+Module BRL.Collections
+
+Import "list.bmx"
+
+

+ 8 - 0
collections.mod/comparator.bmx

@@ -0,0 +1,8 @@
+SuperStrict
+
+Interface IComparator<T>
+
+	Method Compare:Int(t1:T, t2:T)
+
+End Interface
+

+ 44 - 0
collections.mod/errors.bmx

@@ -0,0 +1,44 @@
+SuperStrict
+
+Type TIndexOutOfBoundsException Extends TBlitzException
+
+	Method ToString:String()
+		Return "Attempt to index element out of bounds."
+	End Method
+
+End Type
+
+Type TNoSuchElementException Extends TBlitzException
+
+	Method ToString:String()
+		Return "No such Element."
+	End Method
+
+End Type
+
+Type TUnsupportedOperationException Extends TBlitzException
+
+	Method ToString:String()
+		Return "Unsupported operation."
+	End Method
+
+End Type
+
+Type TIllegalStateException Extends TBlitzException
+
+	Field message:String
+
+	Method New(message:String)
+		Self.message = message
+	End Method
+
+	Method ToString:String()
+		If message Then
+			Return message
+		End If
+		
+		Return "Illegal state."
+	End Method
+
+End Type
+

+ 25 - 0
collections.mod/examples/arraydeque.bmx

@@ -0,0 +1,25 @@
+SuperStrict
+
+Framework brl.standardio
+Import brl.collections
+
+Local stack:TArrayDeque<String> = New TArrayDeque<String>
+stack.Push("Hello")
+stack.Push("World")
+
+Print stack.Pop()
+Print stack.Pop()
+
+Local intStack:TArrayDeque<Int> = New TArrayDeque<Int>
+For Local i:Int = 0 Until 100
+	intStack.Push(i)
+Next
+
+Print "Size = " + intStack.Size()
+
+While Not intStack.IsEmpty()
+	Print intStack.Pop()
+Wend
+
+
+'Local list:TLinkedList<String>

+ 19 - 0
collections.mod/examples/arraylist.bmx

@@ -0,0 +1,19 @@
+SuperStrict
+
+Framework brl.standardio
+Import brl.collections
+
+Local countries:String[] = LoadText("countries.txt").Split("~n")
+
+Local list:IList<String> = New TArrayList<String>
+
+' load the list
+
+list.AddAll(countries)
+
+Print "Sizes : " + countries.length + " (array) = " + list.Size() + " (arraylist)"
+
+Print "Iceland is at position " + list.IndexOf("Iceland")
+
+Print "Has 'Borisland' ?   = " + list.Contains("Borisland")
+Print "Has 'New Zealand' ? = " + list.Contains("New Zealand")

+ 245 - 0
collections.mod/examples/countries.txt

@@ -0,0 +1,245 @@
+Afghanistan
+Albania
+Algeria
+American Samoa
+Andorra
+Angola
+Anguilla
+Antarctica
+Antigua And Barbuda
+Argentina
+Armenia
+Aruba
+Australia
+Austria
+Azerbaijan
+Bahamas
+Bahrain
+Bangladesh
+Barbados
+Belarus
+Belgium
+Belize
+Benin
+Bermuda
+Bhutan
+Bolivia
+Bosnia And Herzegovina
+Botswana
+Bouvet Island
+Brazil
+British Indian Ocean Territory
+Brunei Darussalam
+Bulgaria
+Burkina Faso
+Burundi
+Cambodia
+Cameroon
+Canada
+Cape Verde
+Cayman Islands
+Central African Republic
+Chad
+Chile
+China
+Christmas Island
+Cocos (keeling) Islands
+Colombia
+Comoros
+Congo
+The Democratic Republic Of The Congo
+Cook Islands
+Costa Rica
+Cote D'ivoire
+Croatia
+Cuba
+Cyprus
+Czech Republic
+Denmark
+Djibouti
+Dominica
+Dominican
+Republic
+East Timor
+Ecuador
+Egypt
+El Salvador
+Equatorial Guinea
+Eritrea
+Estonia
+Ethiopia
+Falkland Islands (malvinas)
+Faroe Islands
+Fiji
+Finland
+France
+French Guiana
+French Polynesia
+French Southern Territories
+Gabon
+Gambia
+Georgia
+Germany
+Ghana
+Gibraltar
+Greece
+Greenland
+Grenada
+Guadeloupe
+Guam
+Guatemala
+Guinea
+Guinea-bissau
+Guyana
+Haiti
+Heard Island And Mcdonald Islands
+Holy See (vatican City State)
+Honduras
+Hong Kong
+Hungary
+Iceland
+India
+Indonesia
+Islamic Republic Of Iran
+Iraq
+Ireland
+Israel
+Italy
+Jamaica
+Japan
+Jordan
+Kazakstan
+Kenya
+Kiribati
+Democratic People's Republic Of Korea
+Republic Of Korea
+Kosovo
+Kuwait
+Kyrgyzstan
+Lao People's Democratic Republic
+Latvia
+Lebanon
+Lesotho
+Liberia
+Libyan Arab Jamahiriya
+Liechtenstein
+Lithuania
+Luxembourg
+Macau
+The Former Yugoslav Republic Of Macedonia
+Madagascar
+Malawi
+Malaysia
+Maldives
+Mali
+Malta
+Marshall Islands
+Martinique
+Mauritania
+Mauritius
+Mayotte
+Mexico
+Federated States Of Micronesia
+Republic Of Moldova
+Monaco
+Mongolia
+Montserrat
+Montenegro
+Morocco
+Mozambique
+Myanmar
+Namibia
+Nauru
+Nepal
+Netherlands
+Netherlands Antilles
+New Caledonia
+New Zealand
+Nicaragua
+Niger
+Nigeria
+Niue
+Norfolk Island
+Northern Mariana Islands
+Norway
+Oman
+Pakistan
+Palau
+Palestinian Territory
+Occupied
+Panama
+Papua New Guinea
+Paraguay
+Peru
+Philippines
+Pitcairn
+Poland
+Portugal
+Puerto Rico
+Qatar
+Reunion
+Romania
+Russian Federation
+Rwanda
+Saint Helena
+Saint Kitts And Nevis
+Saint Lucia
+Saint Pierre And Miquelon
+Saint Vincent And The Grenadines
+Samoa
+San Marino
+Sao Tome And Principe
+Saudi Arabia
+Senegal
+Serbia
+Seychelles
+Sierra Leone
+Singapore
+Slovakia
+Slovenia
+Solomon
+Islands
+Somalia
+South Africa
+South Georgia And The South Sandwich Islands
+Spain
+Sri Lanka
+Sudan
+Suriname
+Svalbard And Jan Mayen
+Swaziland
+Sweden
+Switzerland
+Syrian Arab Republic
+Taiwan
+Tajikistan
+United Republic Of Tanzania
+Thailand
+Togo
+Tokelau
+Tonga
+Trinidad And Tobago
+Tunisia
+Turkey
+Turkmenistan
+Turks And Caicos Islands
+Tuvalu
+Uganda
+Ukraine
+United Arab Emirates
+United Kingdom
+United
+States
+United States Minor Outlying Islands
+Uruguay
+Uzbekistan
+Vanuatu
+Venezuela
+Viet Nam
+Virgin Islands British
+Virgin Islands U.s.
+Wallis And Futuna
+Western Sahara
+Yemen
+Zambia
+Zimbabwe

+ 31 - 0
collections.mod/iterator.bmx

@@ -0,0 +1,31 @@
+SuperStrict
+
+Rem
+bbdoc: A collection iterator.
+End Rem
+Interface IIterator<E>
+
+	Rem
+	bbdoc: Returns True if there are more elements.
+	End Rem
+	Method HasNext:Int()
+	
+	Rem
+	bbdoc: Gets the next element in the iteration.
+	End Rem
+	Method NextElement:E()
+	
+	Method Remove()
+
+End Interface
+
+
+Rem
+bbdoc: Implementing the @IIterable interface allows the object to used with an For/Eachin statement.
+End Rem
+Interface IIterable<T>
+
+	Method Iterator:IIterator<T>()
+
+End Interface
+

+ 523 - 0
collections.mod/list.bmx

@@ -0,0 +1,523 @@
+SuperStrict
+
+Import "collection.bmx"
+Import "queue.bmx"
+
+Interface IListIterator<E> Extends IIterator<E>
+	Method HasNext:Int()
+	Method NextElement:E()
+	Method HasPrevious:Int()
+	Method PreviousElement:E()
+	Method NextIndex:Int()
+	Method PreviousIndex:Int()
+	Method Remove()
+	Method Set(element:E)
+	Method Add(element:E)
+End Interface
+
+
+Interface IList<E> Extends ICollection<E>
+
+	Method Get:E(index:Int)
+	Method Set:E(index:Int, element:E)
+	Method Add(index:Int, element:E)
+	Method AddAll:Int(index:Int, c:ICollection<E>)
+	Method RemoveElement:E(index:Int)
+	Method ListIterator:IListIterator<E>()
+	Method ListIterator:IListIterator<E>(index:Int)
+	Method IndexOf:Int(element:E)
+	Method LastIndexOf:Int(element:E)
+
+End Interface
+
+Type TAbstractList<E> Extends TAbstractCollection<E> Implements IList<E> Abstract
+
+	Method Add:Int(element:E)
+		Add(Size(), element)
+		Return True
+	End Method
+	
+	Method AddAll:Int(index:Int, c:ICollection<E>)
+		If index < 0 Or index > Size() Then
+			Throw New TIndexOutOfBoundsException
+		End If
+
+		Local isModified:Int = False
+		Local i:IIterator<E> = c.Iterator()
+		While i.HasNext()
+			Add(index, i.NextElement())
+			index :+ 1
+			isModified = True
+		Wend
+		Return isModified
+	End Method
+	
+	Method Clear()
+		RemoveRange(0, Size())
+	End Method
+	
+	Method Iterator:IIterator<E>()
+		Return New TAbstractListIter<E>(Self)
+	End Method
+	
+	Method ListIterator:IListIterator<E>()
+		Return New TAbstractListListIter<E>(Self)
+	End Method
+	
+	Method ListIterator:IListIterator<E>(index:Int)
+		Return New TAbstractListListIter<E>(Self, index)
+	End Method
+	
+	Method Set:E(index:Int, element:E)
+		Throw New TUnsupportedOperationException
+	End Method
+	
+	Method Add(index:Int, element:E)
+		Throw New TUnsupportedOperationException
+	End Method
+
+	Method Remove:E(index:Int)
+		Throw New TUnsupportedOperationException
+	End Method
+	
+	Method IndexOf:Int(element:E)
+		Local iterator:IListIterator<E> = ListIterator()
+		If Not element Then
+			While iterator.HasNext()
+				If iterator.NextElement() = Null Then
+					Return iterator.PreviousIndex()
+				End If
+			Wend
+		Else
+			While iterator.HasNext()
+				If element = iterator.NextElement() Then
+					Return iterator.PreviousIndex()
+				End If
+			Wend
+		End If
+		
+		Return -1
+	End Method
+	
+	Method LastIndexOf:Int(element:E)
+		Local iterator:IListIterator<E> = ListIterator(Size())
+		If Not element Then
+			While iterator.HasPrevious()
+				If iterator.PreviousElement() = Null Then
+					Return iterator.NextIndex()
+				End If
+			Wend
+		Else
+			While iterator.HasPrevious()
+				If element = iterator.PreviousElement() Then
+					Return iterator.NextIndex()
+				End If
+			Wend
+		End If
+		
+		Return -1
+	End Method
+	
+	Method RemoveRange(indexFrom:Int, indexTo:Int)
+		Local iter:IIterator<E> = ListIterator(indexFrom)
+		Local n:Int = indexTo - indexFrom
+		For Local i:Int = 0 Until n
+			iter.NextElement()
+			iter.Remove()
+		Next
+	End Method
+
+	Type TAbstractListIter<E> Implements IIterator<E>
+		Field index:Int
+		Field lastReturned:Int = -1
+		
+		Field list:TAbstractList<E>
+		
+		Method New(list:TAbstractList<E>)
+			Self.list = list
+		End Method
+		
+		Method HasNext:Int()
+			Return index <> list.Size()
+		End Method
+		
+		Method NextElement:E()
+			Local i:Int = index
+			
+			Local n:E = list.Get(i)
+			lastReturned = i
+			index = i + 1
+			
+			Return n
+		End Method
+		
+		Method Remove()
+			If lastReturned < 0 Then
+				Throw New TIllegalStateException
+			End If
+			
+			list.Remove(lastReturned)
+			
+			If lastReturned < index Then
+				index :- 1
+			End If
+			
+			lastReturned = -1
+		End Method
+		
+	End Type
+	
+	Type TAbstractListListIter<E> Extends TAbstractListIter<E> Implements IListIterator<E>
+	
+		Method New(list:TAbstractList<E>, index:Int)
+			New(list)
+			Self.index = index
+		End Method
+	
+		Method HasPrevious:Int()
+			Return index <> 0
+		End Method
+		
+		Method PreviousElement:E()
+			Local i:Int = index = 1
+			Local previous:E = list.Get(i)
+			lastReturned = i
+			index = i
+			Return previous
+		End Method
+		
+		Method NextIndex:Int()
+			Return index
+		End Method
+		
+		Method PreviousIndex:Int()
+			Return index - 1
+		End Method
+		
+		Method Set(element:E)
+			If lastReturned < 0 Then
+				Throw New TIllegalStateException
+			End If
+			
+			list.Set(lastReturned, element)
+		End Method
+		
+		Method Add(element:E)
+			Local i:Int = index
+			
+			list.Add(i, element)
+			lastReturned = -1
+			
+			index = i + 1
+		End Method
+	
+	End Type
+
+End Type
+
+
+Type TArrayList<E> Extends TAbstractList<E> Implements IList<E>
+
+	Private
+	
+	Field data:E[]
+	Field count:Int
+	
+	Public
+
+	Method New(initial:Int = 16)
+		data = New E[initial]
+	End Method
+
+	Method Iterator:IIterator<E>()
+		Return New TArrayListIter<E>(Self)
+	End Method
+	
+	Method ListIterator:IListIterator<E>()
+		Return New TArrayListListIter<E>(Self, 0)
+	End Method
+
+	Method ListIterator:IListIterator<E>(index:Int)
+		Return New TArrayListListIter<E>(Self, index)
+	End Method
+
+	Method Size:Int()
+		Return count
+	End Method
+	
+	Method IsEmpty:Int()
+		Return count = 0
+	End Method
+
+	Method ToArray:E[]()
+		Return data[..count]
+	End Method
+
+	Method Add:Int(element:E)
+		ResizeAsNeeded(count + 1)
+		
+		If count < data.length Then
+			data[count] = element
+			count :+ 1
+		End If
+	End Method
+	
+	Method Add(index:Int, element:E)
+		If index < 0 Or index > count Then
+			Throw New TIndexOutOfBoundsException
+		End If
+
+		ResizeAsNeeded(count + 1)
+		
+		ArrayCopy(data, index, data, index + 1, count - index)
+		data[index] = element
+		
+		count :+ 1
+	End Method
+
+	Method Remove:Int(element:E)
+		For Local i:Int = 0 Until count
+			If data[i] = element Then
+				Return True
+			End If
+		Next
+		Return False
+	End Method
+
+	Method Contains:Int(element:E)
+		For Local i:Int = 0 Until count
+			If data[i] = element Then
+				Return True
+			End If
+		Next
+		Return False
+	End Method
+	
+	Method AddAll:Int(c:ICollection<E>)
+	
+		ResizeAsNeeded(count + c.Size())
+		
+		For Local i:E = EachIn c.ToArray()
+			Add(i)
+		Next
+	End Method
+	
+	Method AddAll:Int(a:E[])
+
+		ResizeAsNeeded(count + a.length)
+	
+		For Local i:E = EachIn a
+			Add(i)
+		Next
+	End Method
+	
+	Method AddAll:Int(index:Int, c:ICollection<E>)
+		' TODO
+	End Method
+
+	Method RemoveAll:Int(c:ICollection<E>)
+		' TODO
+	End Method
+
+	Method RetainAll:Int(c:ICollection<E>)
+		' TODO
+	End Method
+
+	Method Clear()
+		For Local i:Int = 0 Until count
+			data[i] = Null
+		Next
+		count = 0
+	End Method
+
+	Method Equals:Int(o:Object)
+		' TODO
+	End Method
+
+	Method Get:E(index:Int)
+		If index >= count Then
+			Throw New TIndexOutOfBoundsException
+		End If
+	
+		Return data[index]
+	End Method
+
+	Method Set:E(index:Int, element:E)
+		If index >= count Then
+			Throw New TIndexOutOfBoundsException
+		End If
+
+		Local old:E = data[index]
+		data[index] = element
+		Return old
+	End Method
+
+	Method RemoveElement:E(index:Int)
+		If index >= count Then
+			Throw New TIndexOutOfBoundsException
+		End If
+
+		Local old:E = data[index]
+		
+		Local moveCount:Int = count - index - 1
+		
+		If moveCount > 0 Then
+			ArrayCopy(data, index + 1, data, index, moveCount)
+		End If
+		
+		count :- 1
+		data[count] = Null
+		
+		Return old
+	End Method
+
+	Method ResizeAsNeeded(minCapacity:Int)
+		Local capacity:Int = data.length
+		If minCapacity > capacity Then
+			Local newCapacity:Int = (capacity * 3) / 2 + 1
+			If newCapacity < minCapacity Then
+				newCapacity = minCapacity
+			End If
+			data = data[..newCapacity]
+		End If
+	End Method
+
+	Method TrimToSize()
+		If count < data.Length Then
+			data = data[..count]
+		End If
+	End Method
+	
+	Method IndexOf:Int(element:E)
+		If Not element Then
+			For Local i:Int = 0 Until count
+				If data[i] = Null Then
+					Return i
+				End If
+			Next
+		Else
+			For Local i:Int = 0 Until count
+				If element = data[i] Then
+					Return i
+				End If
+			Next
+		End If
+		Return -1
+	End Method
+
+	Method LastIndexOf:Int(element:E)
+		If Not element Then
+			Local i:Int = count - 1
+			While i
+				If data[i] = Null Then
+					Return i
+				End If
+				i :- 1
+			Wend
+		Else
+			Local i:Int = count - 1
+			While i
+				If element = data[i] Then
+					Return i
+				End If
+				i :- 1
+			Wend
+		End If
+		Return -1
+	End Method
+
+	Type TArrayListIter<E> Implements IIterator<E>
+		Field index:Int
+		Field lastReturned:Int = -1
+		
+		Field list:TArrayList<E>
+		
+		Method New(list:TArrayList<E>)
+			Self.list = list
+		End Method
+		
+		Method HasNext:Int()
+			Return index <> list.count
+		End Method
+		
+		Method NextElement:E()
+			Local i:Int = index
+			If index >= list.count Then
+				Throw New TNoSuchElementException
+			End If
+			Local data:E[] = list.data
+			If i >= data.length Then
+				' throw error
+			End If
+			index = i + 1
+			lastReturned = i
+			Return data[lastReturned]
+		End Method
+		
+		Method Remove()
+			If lastReturned < 0 Then
+				Throw New TIllegalStateException
+			End If
+			
+			list.RemoveElement(lastReturned)
+			index = lastReturned
+			lastReturned = -1
+			
+		End Method
+		
+	End Type
+	
+	
+	
+	Type TArrayListListIter<E> Extends TArrayListIter<E> Implements IListIterator<E>
+	
+		Method New(list:TArrayList<E>, index:Int)
+			New(list)
+			Self.index = index
+		End Method
+	
+		Method HasPrevious:Int()
+			Return index <> 0
+		End Method
+		
+		Method PreviousElement:E()
+			Local i:Int = index - 1
+			If i < 0 Then
+				Throw New TNoSuchElementException
+			End If
+			
+			Local data:E[] = list.data
+			
+			index = i
+			lastReturned = i
+			Return data[i]
+		End Method
+		
+		Method NextIndex:Int()
+			Return index
+		End Method
+		
+		Method PreviousIndex:Int()
+			Return index - 1
+		End Method
+		
+		Method Set(element:E)
+			If lastReturned < 0 Then
+				Throw New TIllegalStateException
+			End If
+			
+			list.Set(lastReturned, element)
+		End Method
+		
+		Method Add(element:E)
+			Local i:Int = index
+			list.Add(i, element)
+			index = i + 1
+			lastReturned = -1
+		End Method
+		
+	End Type
+	
+
+End Type
+

+ 417 - 0
collections.mod/queue.bmx

@@ -0,0 +1,417 @@
+SuperStrict
+
+Import "collection.bmx"
+
+
+Interface IQueue<E> Extends ICollection<E>
+
+	Method Add:Int(element:E)
+	Method Poll:E()
+	Method Peek:E()	
+
+End Interface
+
+Interface IDeque<E> Extends IQueue<E>
+
+	Method AddFirst(element:E)
+	Method AddLast(element:E)
+	Method RemoveFirst:E()
+	Method RemoveLast:E()
+	Method PollFirst:E()
+	Method PollLast:E()
+	Method PeekFirst:E()
+	Method PeekLast:E()
+	Method RemoveFirstOccurrence:Int(element:E)
+	Method RemoveLastOccurrence:Int(element:E)
+	Method Add:Int(element:E)
+	Method Poll:E()
+	Method Peek:E()
+	Method Push(element:E)
+	Method Pop:E()
+	Method Remove:Int(element:E)
+	Method Contains:Int(element:E)
+	Method Size:Int()
+	Method Iterator:IIterator<E>()
+	Method DescendingIterator:IIterator<E>()
+
+End Interface
+
+Type TAbstractQueue<E> Extends TAbstractCollection<E> Implements IQueue<E> Abstract
+
+	Method Clear()
+		While Poll() <> Null
+		Wend
+	End Method
+
+	Method AddAll:Int(c:ICollection<E>)
+		' TODO
+	End Method
+
+End Type
+
+Type TArrayDeque<E> Extends TAbstractCollection<E> Implements IDeque<E>
+
+	Private
+	
+	Field elements:E[]
+	Field head:Int
+	Field tail:Int
+	
+	Public
+	
+	Method New(initialCapacity:Int = 16)
+		If initialCapacity <> 16 Then
+			AllocateElements(initialCapacity)
+		Else
+			elements = New E[initialCapacity]
+		End If
+	End Method
+	
+	Method New(c:ICollection<E>)
+		AllocateElements(c.Size())
+		AddAll(c)
+	End Method
+	
+	Private
+	
+	Method AllocateElements(count:Int)
+		Local capacity:Int = 8
+		
+		If count >= capacity Then
+			capacity = count
+			capacity :| capacity Shr 1
+			capacity :| capacity Shr 2
+			capacity :| capacity Shr 4
+			capacity :| capacity Shr 8
+			capacity :| capacity Shr 16
+			capacity :+ 1
+			
+			If capacity < 0 Then
+				capacity :Shr 1
+			End If
+		End If
+		
+		elements = New E[capacity]
+	End Method
+	
+	Method IncreaseCapacity()
+		If head <> tail Then
+			Return
+		End If
+		
+		Local p:Int = head
+		Local n:Int = elements.length
+		Local r:Int = n - p
+
+		Local capacity:Int = n Shl 1
+
+		If capacity < 0 Then
+			Throw New TIllegalStateException("No space to increase deque capacity")
+		End If
+		
+		elements = elements[p..p+r] + elements[..p]
+		elements = elements[..capacity]
+		
+		head = 0
+		tail = n
+	End Method
+	
+	Public
+	
+	Method DeleteElement:Int(index:Int)
+		Local elements:E[] = Self.elements
+		Local mask:Int = elements.length - 1
+		Local h:Int = head
+		Local t:Int = tail
+		Local front:Int = (index - h) & mask
+		Local back:Int = (t - index) & mask
+		
+		If front < back Then
+			If h <= index Then
+				' TODO
+			Else
+				' TODO
+			End If
+		
+			elements[h] = Null
+			head = (h + 1) & mask
+			Return False
+		Else
+			' TODO
+			Return True
+		End If
+		
+	End Method
+
+	Method AddFirst(element:E)
+		head = (head - 1) & (elements.length - 1)
+		elements[head] = element
+		If head = tail Then
+			IncreaseCapacity()
+		End If
+	End Method
+	
+	Method AddLast(element:E)
+		elements[tail] = element
+		tail = (tail + 1) & (elements.length - 1)
+		If tail = head Then
+			IncreaseCapacity()
+		End If
+	End Method
+
+	Method RemoveFirst:E()
+		Local element:E = PollFirst()
+		' not found? TODO
+		Return element
+	End Method
+
+	Method RemoveLast:E()
+		Local element:E = PollLast()
+
+		Return element
+	End Method
+
+	Method PollFirst:E()
+		If Not Size() Then
+			Throw New TNoSuchElementException
+		End If
+	
+		Local h:Int = head
+		Local element:E = elements[h]
+		
+		elements[h] = Null
+		head = (h + 1) & (elements.length - 1)
+		
+		Return element
+	End Method
+
+	Method PollLast:E()
+		If Not Size() Then
+			Throw New TNoSuchElementException
+		End If
+
+		Local t:Int = (tail - 1) & (elements.length - 1)
+		Local element:E = elements[t]
+		elements[t] = Null
+		tail = t
+		
+		Return element
+	End Method
+
+	Method PeekFirst:E()
+		If Not Size() Then
+			Throw New TNoSuchElementException
+		End If
+		Return elements[head]
+	End Method
+
+	Method PeekLast:E()
+		If Not Size() Then
+			Throw New TNoSuchElementException
+		End If
+		Return elements[(tail - 1) & (elements.length - 1)]
+	End Method
+
+	Method RemoveFirstOccurrence:Int(element:E)
+		Local mask:Int = elements.length - 1
+		Local index:Int = head
+		
+		While index <> tail
+			If element = elements[index] Then
+				DeleteElement(index)
+				Return True
+			End If
+			
+			index = (index + 1) & mask
+		Wend
+		
+		Return False
+	End Method
+
+	Method RemoveLastOccurrence:Int(element:E)
+		Local mask:Int = elements.length - 1
+		Local index:Int = (tail - 1) & mask
+		
+		While index <> tail
+			If element = elements[index] Then
+				DeleteElement(index)
+				Return True
+			End If
+			
+			index = (index - 1) & mask
+		Wend
+		
+		Return False
+	End Method
+
+	Method Add:Int(element:E)
+		AddLast(element)
+		Return True
+	End Method
+
+	Method Poll:E()
+		Return PollFirst()
+	End Method
+
+	Method Peek:E()
+		Return PeekFirst()
+	End Method
+
+	Method Push(element:E)
+		AddFirst(element)
+	End Method
+
+	Method Pop:E()
+		Return RemoveFirst()
+	End Method
+
+	Method Remove:Int(element:E)
+		Return RemovefirstOccurrence(element)
+	End Method
+
+	Method Contains:Int(element:E)
+		' TODO
+	End Method
+
+	Method Size:Int()
+		Return (tail - head) & (elements.length - 1)
+	End Method
+
+	Method Iterator:IIterator<E>()
+		Return New TArrayDequeIterator<E>(Self)
+	End Method
+
+	Method DescendingIterator:IIterator<E>()
+		Return New TArrayDequeDescendingIterator<E>(Self)
+	End Method
+
+	Method Equals:Int(o:Object)
+	End Method
+	
+	Method ToArray:E[]()
+		Return elements[..] ' fixme
+	End Method
+	
+	Method IsEmpty:Int()
+		Return head = tail
+	End Method
+	
+	Method Clear()
+		Local h:Int = head
+		Local t:Int = tail
+		If h <> t Then
+			head = 0
+			tail = 0
+			Local index:Int = h
+			Local mask:Int = elements.length - 1
+			While index <> t
+				elements[index] = Null
+				index = (index + 1) & mask
+			Wend
+		End If
+	End Method
+	
+
+	Type TArrayDequeIterator<E> Implements IIterator<E>
+	
+		Field position:Int
+		Field last:Int
+		Field lastReturned:Int = -1
+		
+		Field deque:TArrayDeque<E>
+	
+		Method New(deque:TArrayDeque<E>)
+			Self.deque = deque
+			position = deque.head
+			last = deque.tail
+		End Method
+		
+		Method HasNext:Int()
+			Return position <> last
+		End Method
+		
+		Method NextElement:E()
+			If position = last Then
+				Throw New TNoSuchElementException
+			End If
+			
+			Local elements:E[] = deque.elements
+			
+			Local element:E = elements[position]
+			' TODO handle mods
+			
+			lastReturned = position
+			position = (position + 1) & (elements.length - 1)
+			
+			Return element
+		End Method
+		
+		Method Remove()
+			If lastReturned < 0 Then
+				Throw New TIllegalStateException
+			End If
+			
+			If deque.DeleteElement(lastReturned) Then
+				position = (position - 1) & (deque.elements.length - 1)
+				last = deque.tail
+			End If
+			
+			lastReturned = -1
+		End Method
+	
+	End Type
+	
+
+	Type TArrayDequeDescendingIterator<E> Implements IIterator<E>
+
+		Field position:Int
+		Field last:Int
+		Field lastReturned:Int = -1
+		
+		Field deque:TArrayDeque<E>
+	
+		Method New(deque:TArrayDeque<E>)
+			Self.deque = deque
+			position = deque.tail
+			last = deque.head
+		End Method
+	
+		Method HasNext:Int()
+			Return position <> last
+		End Method
+		
+		Method NextElement:E()
+			If position = last Then
+				Throw New TNoSuchElementException
+			End If
+			
+			Local elements:E[] = deque.elements
+			
+			position = (position - 1) & (elements.length - 1)
+	
+			Local element:E = elements[position]
+			
+			' TODO handle mods
+			
+			lastReturned = position
+			
+			Return element
+		End Method
+		
+		Method Remove()
+			If lastReturned < 0 Then
+				Throw New TIllegalStateException
+			End If
+			
+			If deque.DeleteElement(lastReturned) Then
+				position = (position + 1) & (deque.elements.length - 1)
+				last = deque.head
+			End If
+			
+			lastReturned = -1
+		End Method
+	
+	End Type
+
+End Type
+

+ 60 - 0
collections.mod/set.bmx

@@ -0,0 +1,60 @@
+SuperStrict
+
+Import "collection.bmx"
+Import "comparator.bmx"
+
+Interface ISet<E> Extends ICollection<E>
+
+End Interface
+
+Type TAbstractSet<E> Extends TAbstractCollection<E> Implements ISet<E> Abstract
+
+	Method Equals:Int(o:Object)
+		' TODO
+	End Method
+	
+	Method RemoveAll:Int(c:ICollection<E>)
+		Local modified:Int
+		If Size() > c.Size() Then
+			Local it:IIterator<E> = c.Iterator()
+			While it.HasNext()
+				modified :| Remove(it.NextElement())
+			Wend
+		Else
+		
+			Local it:IIterator<E> = Iterator()
+			While it.HasNext()
+				If c.Contains(it.NextElement()) Then
+					it.Remove()
+					modified = True
+				End If
+			Wend
+		End If
+		
+		Return modified
+	End Method
+
+End Type
+
+
+Interface INavigableSet<E> Extends ISortedSet<E>
+
+	Method Lower:E(element:E)
+	Method Floor:E(element:E)
+	Method Higher:E(element:E)
+	Method PollFirst:E()
+	Method PollLast:E()
+	Method DescendingSet:INavigableSet<E>()
+	Method DescentingIterator:IIterator<E>()
+
+End Interface
+
+Interface ISortedSet<E> Extends ISet<E>
+
+	Method Comparator:IComparator<E>()
+	Method First:E()
+	Method Last:E()
+
+End Interface
+
+