Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Collection interfaces with mutableSublist producing a write through view #330

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
47 changes: 47 additions & 0 deletions rhombus/data/collection.rhm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#lang rhombus


export:
Collection
CollectionView
MutableCollection


import:
rhombus/data/sequence.Sequence


interface CollectionView:
extends Sequence
// read-only property
property size :: Int

method isEmpty() :: Boolean: size() == 0

method contains(element) :: Boolean:
let iterator = iterate()
fun loop():
iterator.hasNext() && (iterator.next() == element || loop())
loop()


interface Collection:
extends CollectionView
method add(element) :: Collection
method remove(element) :: Collection


interface MutableCollection:
extends CollectionView
method add(element) :: Void

method addAll(elements :: Sequence) :: Void:
let iterator = elements.iterate()
fun loop():
when iterator.hasNext()
| add(iterator.next())
loop()
loop()

method remove(element) :: Void
method clear() :: Void
35 changes: 35 additions & 0 deletions rhombus/data/list.rhm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#lang rhombus


export:
List
ListView
MutableList


import:
rhombus/data/private/array_list.ArrayList
rhombus/data/private/list
rhombus/data/private/list.ListView
rhombus/data/private/list_builder.ListBuilder


interface List:
extends list.List
annotation 'List': 'list.List'
expression 'List': 'of'
export: builder
fun builder(): ListBuilder()
fun of(element, ...):
let builder = List.builder()
for:
each v: Array(element, ...)
builder.add(v)
builder.build()


interface MutableList:
extends list.MutableList
annotation 'MutableList': 'list.MutableList'
expression 'MutableList': 'create'
fun create(): ArrayList()
206 changes: 206 additions & 0 deletions rhombus/data/private/array_list.rhm
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
#lang rhombus


export:
ArrayList


import:
rhombus/data/private/list.ListView
rhombus/data/private/list.MutableList
rhombus/data/sequence.Iterator
lib("racket/base.rkt").error


class ArrayList():
implements MutableList
internal PrivateArrayList

private field storage :: Array = Array()
private field currentSize :: Int = 0

// This field tracks how many structural modifications the list has undergone. Changing an element
// is not a structural modification, but adding or removing an element is.
private field version :: Int = 0

override method iterate(): ArrayListIterator(this)

override property size: currentSize

override method get(index):
checkIndex("List.get", index)
storage[index]

override method sublist(start, end):
checkSublistIndicies("List.sublist", start, end)
ArrayUnmodifiableSublist(this, start, end)

override method mutableSublist(start, end):
checkSublistIndicies("MutableList.mutableSublist", start, end)
ArrayMutableSublist(this, start, end)

override method set(index, element):
checkIndex("MutableList.set", index)
storage[index] := element

override method add(element):
version := version + 1
ensureCapacity(currentSize + 1)
storage[currentSize] := element
currentSize := currentSize + 1
version := version + 1

override method remove(element):
let i = find(element)
unless i == -1 | delete(i)

override method insert(index :: Int, element) :: Void:
checkInsertionIndex("MutableList.insert", index)
ensureCapacity(currentSize + 1)
fun loop(i = currentSize):
unless i == index
| storage[i] := storage[i - 1]
loop(i - 1)
loop()
storage[index] := element
currentSize := currentSize + 1
version := version + 1

override method delete(index :: Int) :: Void:
checkIndex("List.delete", index)
for:
each i: index + 1 .. currentSize
storage[i - 1] := storage[i]
storage[currentSize - 1] := #false
currentSize := currentSize - 1
version := version + 1

override method clear() :: Void:
storage := Array()
currentSize := 0
version := version + 1

private method find(element):
fun loop(i = 0):
cond
| i == currentSize: -1
| storage[i] == element: i
| ~else: loop(i + 1)
loop()

method ensureCapacity(requiredSize) :: Void:
when storage.length() < requiredSize
| let newStorage = Array.make(max(storage.length() * 2, requiredSize))
for:
each:
v: storage
i: 0..
newStorage[i] := v
storage := newStorage


class ArrayViewSublist(backingList :: PrivateArrayList, start :: Int, mutable end :: Int):
nonfinal
implements ListView

override method iterate():
ArraySublistIterator(this)

override property size:
end - start

override method contains(element):
find(element) != -1

override method get(index):
checkIndex("List.get", index)
backingList.get(index + start)

override method sublist(start, end):
checkSublistIndicies("List.sublist", start, end)
ArrayUnmodifiableSublist(backingList, this.start + start, this.start + end)

method find(element):
fun loop(i = start):
cond
| i == end: -1
| backingList.get(i) == element: i - start
| ~else: loop(i + 1)
loop()


class ArrayUnmodifiableSublist():
extends ArrayViewSublist


class ArrayMutableSublist():
extends ArrayViewSublist
implements MutableList

override method contains(element):
find(element) != -1

override method mutableSublist(start, end):
checkSublistIndicies("MutableList.mutableSublist", start, end)
ArrayMutableSublist(backingList, this.start + start, this.start + end)

override method set(index, element):
checkIndex("MutableList.set", index)
backingList.set(index + start, element)

override method add(element):
backingList.insert(end, element)

override method remove(element):
let i = find(element)
unless i == -1
| delete(i)

override method insert(index, element):
checkInsertionIndex("List.insert", index)
backingList.insert(index + start, element)
end := end + 1

override method delete(index):
checkIndex("List.delete", index)
backingList.delete(index + start)
end := end - 1

override method clear():
let elementsToShift = backingList.currentSize - end
let currentSize = size
for:
each i: start .. start + elementsToShift
backingList.storage[i] := backingList.storage[i + currentSize]
let newSize = backingList.size - currentSize
for:
each i: newSize .. backingList.currentSize
backingList.storage[i] := #false
backingList.currentSize := newSize
backingList.version := backingList.version + 1
end := 0


class ArrayListIterator(backingList :: ArrayList, private version :: Int = backingList.version):
implements Iterator

private field index :: Int: 0

override method hasNext():
when version < backingList.version | error("concurrent modification")
index < backingList.size

override method next():
when version < backingList.version | error("concurrent modification")
unless index < backingList.size | error("no such element")
let element = backingList.get(index)
index := index + 1
element


// TODO
class ArraySublistIterator(backingSublist :: ArrayViewSublist)


fun max(x, y):
if x < y | y | x
84 changes: 84 additions & 0 deletions rhombus/data/private/list.rhm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#lang rhombus


export:
List
ListView
MutableList


import:
rhombus/data/collection.Collection
rhombus/data/collection.CollectionView
rhombus/data/collection.MutableCollection
rhombus/private/precondition.checkArgument


interface ListView:
extends CollectionView

method get(index :: Int)
method sublist(start :: Int, end :: Int) :: ListView

method checkIndex(who, index):
checkArgument(
who,
index >= 0 && index < size,
"index out of bounds",
"index", index,
"size", size)

method checkInsertionIndex(who, index):
checkArgument(
who,
index >= 0 && index <= size,
"index out of bounds",
"index", index,
"size", size)

method checkSublistIndicies(who, start, end):
checkArgument(
who,
start >= 0 && start <= size,
"sublist start index out of bounds",
"start index", start,
"end index", end,
"size", size)
checkArgument(
who,
end >= 0 && end <= size,
"sublist end index out of bounds",
"start index", start,
"end index", end,
"size", size)
checkArgument(
who,
start <= end,
"sublist start index occurs after end index",
"start index", start,
"end index", end,
"size", size)


interface List:
extends:
ListView
Collection

method set(index :: Int, element) :: List
override add(element) :: List
override remove(element) :: List
method insert(index :: Int, element) :: List
method delete(index :: Int) :: List
override sublist(start :: Int, end :: Int) :: List


interface MutableList:
extends:
ListView
MutableCollection

method set(index :: Int, element) :: Void
method insert(index :: Int, element) :: Void
method delete(index :: Int) :: Void
method mutableSublist(start :: Int, end :: Int) :: MutableList
Loading