X-Git-Url: http://nitlanguage.org diff --git a/lib/dummy_array.nit b/lib/dummy_array.nit index adecf04..85440b4 100644 --- a/lib/dummy_array.nit +++ b/lib/dummy_array.nit @@ -1,24 +1,25 @@ # This file is part of NIT ( http://www.nitlanguage.org ). # # Copyright 2008 Floréal Morandat +# Copyright 2014 Alexandre Terrasa # # This file is free software, which comes along with NIT. This software is # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. You can modify it is you want, provided this header # is kept unaltered, and a notification of the changes is added. # You are allowed to redistribute it and sell it, alone or is a part of # another product. +# A `Set` that contains only integers. class DummyArray -special Set[Int] -special ArrayCapable[Int] - attr _capacity: Int - redef readable attr _length: Int - attr _keys: NativeArray[Int] - attr _values: NativeArray[Int] + super Set[Int] + private var capacity: Int + redef var length: Int + private var keys: NativeArray[Int] + private var values: NativeArray[Int] - redef meth add(value: Int) + redef fun add(value: Int) do assert full: _length < (_capacity-1) var l = _length @@ -27,9 +28,10 @@ special ArrayCapable[Int] _length = l + 1 end - redef meth remove(value: Int) + redef fun remove(value) do assert not is_empty + if not value isa Int then return var l = _length if l > 1 then var last = _values[l - 1] @@ -40,8 +42,9 @@ special ArrayCapable[Int] _length = l - 1 end - redef meth has(value: Int): Bool + redef fun has(value) do + if not value isa Int then return false assert value < _capacity var pos = _keys[value] if pos < _length then @@ -50,60 +53,61 @@ special ArrayCapable[Int] return false end - redef meth first: Int + redef fun first: Int do assert _length > 0 return _values[0] end - redef meth is_empty: Bool + redef fun is_empty: Bool do return not (_length > 0) end - redef meth clear + redef fun clear do _length = 0 end - redef meth iterator: DummyIterator + redef fun iterator: DummyIterator do return new DummyIterator(self) end - private meth value_at(pos: Int): Int + private fun value_at(pos: Int): Int do return _values[pos] end - init(capacity: Int) - do + # initialize a new DummyArray with `capacity`. + init(capacity: Int) is old_style_init do _capacity = capacity - _keys = calloc_array(capacity) - _values = calloc_array(capacity) + _keys = new NativeArray[Int](capacity) + _values = new NativeArray[Int](capacity) end end +# An iterator over a `DummyArray`. class DummyIterator -special Iterator[Int] - attr _array: DummyArray - attr _pos: Int + super Iterator[Int] + private var array: DummyArray + private var pos: Int - redef meth item: Int + redef fun item: Int do assert is_ok return _array.value_at(_pos) end - redef meth is_ok: Bool + redef fun is_ok: Bool do return _pos < _array.length end - redef meth next do _pos = _pos + 1 end + redef fun next do _pos = _pos + 1 end - init(array: DummyArray) - do + # Initialize an iterator for `array`. + init(array: DummyArray) is old_style_init do _pos = 0 _array = array end