From 00a4fd78d66ff29f52baf4d743edb43e6338a1c4 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Wed, 16 Mar 2016 14:05:34 +0000 Subject: [PATCH] changed Array to use 0-based index --- stix/kernel/Collection-Array.st | 13 +++++++---- stix/kernel/Process.st | 39 ++++++++++++++++----------------- stix/lib/exec.c | 14 ++++-------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/stix/kernel/Collection-Array.st b/stix/kernel/Collection-Array.st index f408101..463c684 100644 --- a/stix/kernel/Collection-Array.st +++ b/stix/kernel/Collection-Array.st @@ -5,6 +5,11 @@ ^self basicSize. } + #method ubound + { + ^(self basicSize - 1). + } + #method at: anInteger { ^self basicAt: anInteger. @@ -17,22 +22,22 @@ #method first { - ^self at: 1. + ^self at: 0. } #method last { - ^self at: self size. + ^self at: (self ubound). } #method do: aBlock { - 1 to: self size do: [:i | aBlock value: (self at: i)]. + 0 to: (self ubound) do: [:i | aBlock value: (self at: i)]. } #method copy: anArray { - 1 to: (anArray size) do: [:i | self at: i put: (anArray at: i) ]. + 0 to: (anArray ubound) do: [:i | self at: i put: (anArray at: i) ]. } } diff --git a/stix/kernel/Process.st b/stix/kernel/Process.st index 8498580..569073c 100644 --- a/stix/kernel/Process.st +++ b/stix/kernel/Process.st @@ -62,7 +62,7 @@ #method initialize { self.count := 0. - self.heapIndex := 0. + self.heapIndex := -1. self.fireTime := 0. } @@ -126,7 +126,10 @@ #method insert: aSemaphore { - self.size >= (self.arr size) ifTrue: [ + | index | + + index := self.size. + (index >= (self.arr size)) ifTrue: [ | newarr newsize | newsize := (self.arr size) * 2. newarr := Array new: newsize. @@ -134,19 +137,19 @@ self.arr := newarr. ]. + self.arr at: index put: aSemaphore. + aSemaphore heapIndex: index. self.size := self.size + 1. - self.arr at: self.size put: aSemaphore. - aSemaphore heapIndex: self.size. - ^self siftUp: self.size. + ^self siftUp: index } #method popTop { | top | - top := self.arr at: 1. - self deleteAt: 1. + top := self.arr at: 0. + self deleteAt: 0. ^top } @@ -161,11 +164,11 @@ item := self.arr at: anIndex. item heapIndex: -1. + self.size := self.size - 1. (anIndex == self.size) ifTrue: [ "the last item" self.arr at: self.size put: nil. - self.size := self.size - 1 ] ifFalse: [ | xitem | @@ -174,7 +177,6 @@ self.arr at: anIndex put: xitem. xitem heapIndex: anIndex. self.arr at: self.size put: nil. - self.size := self.size - 1. (xitem youngerThan: item) ifTrue: [self siftUp: anIndex ] @@ -184,27 +186,24 @@ #method parentIndex: anIndex { - ## ^(anIndex - 1) quo: 2 - ^anIndex quo: 2 + ^(anIndex - 1) quo: 2 } #method leftChildIndex: anIndex { - ## ^(anIndex * 2) + 1. - ^(anIndex * 2). + ^(anIndex * 2) + 1. } #method rightChildIndex: anIndex { - ## ^(anIndex * 2) + 2. - ^(anIndex * 2) + 1. + ^(anIndex * 2) + 2. } #method siftUp: anIndex { | pindex cindex par item stop | - (anIndex <= 1) ifTrue: [ ^anIndex ]. + (anIndex <= 0) ifTrue: [ ^anIndex ]. pindex := anIndex. item := self.arr at: anIndex. @@ -214,7 +213,7 @@ cindex := pindex. - (cindex > 1) + (cindex > 0) ifTrue: [ pindex := self parentIndex: cindex. par := self.arr at: pindex. @@ -240,8 +239,8 @@ { | base capa cindex item | - base := (self.size quo: 2) + 1. - (anIndex > base) ifTrue: [^anIndex]. + base := self.size quo: 2. + (anIndex >= base) ifTrue: [^anIndex]. cindex := anIndex. item := self.arr at: cindex. @@ -252,7 +251,7 @@ left := self leftChildIndex: cindex. right := self rightChildIndex: cindex. - ((right <= self.size) and: [(self.arr at: right) youngerThan: (self.arr at: left)]) + ((right < self.size) and: [(self.arr at: right) youngerThan: (self.arr at: left)]) ifTrue: [ younger := right ] ifFalse: [ younger := left ]. diff --git a/stix/lib/exec.c b/stix/lib/exec.c index a2985b2..1606fbf 100644 --- a/stix/lib/exec.c +++ b/stix/lib/exec.c @@ -1059,18 +1059,15 @@ static int prim_basic_at (stix_t* stix, stix_ooi_t nargs) pos = ACTIVE_STACK_GET(stix, stix->sp); if (stix_inttooow (stix, pos, &idx) <= 0) { - /* integer out of range or not integer */ + /* negative integer or not integer */ return 0; } - if (idx < 1 || idx > STIX_OBJ_GET_SIZE(rcv)) + if (idx >= STIX_OBJ_GET_SIZE(rcv)) { /* index out of range */ return 0; } - /* [NOTE] basicAt: and basicAt:put: uses a 1-based index. */ - idx = idx - 1; - switch (STIX_OBJ_GET_FLAGS_TYPE(rcv)) { case STIX_OBJ_TYPE_BYTE: @@ -1123,10 +1120,10 @@ static int prim_basic_at_put (stix_t* stix, stix_ooi_t nargs) if (stix_inttooow (stix, pos, &idx) <= 0) { - /* integer out of range or not integer */ + /* negative integer or not integer */ return 0; } - if (idx < 1 || idx > STIX_OBJ_GET_SIZE(rcv)) + if (idx >= STIX_OBJ_GET_SIZE(rcv)) { /* index out of range */ return 0; @@ -1140,9 +1137,6 @@ static int prim_basic_at_put (stix_t* stix, stix_ooi_t nargs) return 0; } - /* [NOTE] basicAt: and basicAt:put: uses a 1-based index. */ - idx = idx - 1; - switch (STIX_OBJ_GET_FLAGS_TYPE(rcv)) { case STIX_OBJ_TYPE_BYTE: