From 18a123ae320df83bff3e71062bc3b6aa539df954 Mon Sep 17 00:00:00 2001 From: Durieux Pol Date: Tue, 21 Nov 2023 12:07:09 +0100 Subject: [PATCH] More refactoring --- .../RandomMutantSelection.class.st | 24 +++++++++++++++---- .../RandomOperatorMutantSelection.class.st | 17 +++++++++---- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/MuTalk-Model/RandomMutantSelection.class.st b/src/MuTalk-Model/RandomMutantSelection.class.st index 6b387c02..bff8a5e6 100644 --- a/src/MuTalk-Model/RandomMutantSelection.class.st +++ b/src/MuTalk-Model/RandomMutantSelection.class.st @@ -13,6 +13,19 @@ RandomMutantSelection class >> using: aMutationsGenerationStrategy [ ^ self new mutationsGenerationStrategy: aMutationsGenerationStrategy ] +{ #category : #adding } +RandomMutantSelection >> addMutantIn: newColl from: oldColl using: random and: aNumber [ + + | index | + index := random nextIntegerBetween: 1 and: aNumber. + + "If the selected mutant has already been added, another one is chosen" + [ newColl includes: (oldColl at: index) ] whileTrue: [ + index := random nextIntegerBetween: 1 and: aNumber ]. + + newColl add: (oldColl at: index) +] + { #category : #'accessing-defaults' } RandomMutantSelection >> defaultMutationGenerationStrategy [ @@ -91,7 +104,7 @@ RandomMutantSelection >> nullLogger [ { #category : #generating } RandomMutantSelection >> selectMutantsFrom: aCollection [ - | size index newColl random | + | size newColl random | size := aCollection size. newColl := aCollection copyEmpty. random := Random new. @@ -100,10 +113,11 @@ RandomMutantSelection >> selectMutantsFrom: aCollection [ (self highLimitCondition: aCollection size) ifTrue: [ ^ aCollection ]. 1 to: (self totalNumberOfMutants: aCollection size) do: [ :i | - index := random nextIntegerBetween: 1 and: size. - [ newColl includes: (aCollection at: index) ] whileTrue: [ - index := random nextIntegerBetween: 1 and: size ]. - newColl add: (aCollection at: index) ]. + self + addMutantIn: newColl + from: aCollection + using: random + and: size ]. ^ newColl ] diff --git a/src/MuTalk-Model/RandomOperatorMutantSelection.class.st b/src/MuTalk-Model/RandomOperatorMutantSelection.class.st index 44ede5e1..6303b94f 100644 --- a/src/MuTalk-Model/RandomOperatorMutantSelection.class.st +++ b/src/MuTalk-Model/RandomOperatorMutantSelection.class.st @@ -21,6 +21,12 @@ RandomOperatorMutantSelection class >> percent: aPercentage [ (PercentRandomMutantSelection new percentageOfMutants: aPercentage) ] +{ #category : #adding } +RandomOperatorMutantSelection >> addMutantIn: newColl from: oldColl using: random and: aNumber [ + + randomMutantSelection addMutantIn: newColl from: oldColl using: random and: aNumber +] + { #category : #'accessing-defaults' } RandomOperatorMutantSelection >> defaultMutationGenerationStrategy [ @@ -108,7 +114,7 @@ RandomOperatorMutantSelection >> randomMutantSelection: anObject [ { #category : #generating } RandomOperatorMutantSelection >> selectMutantsFrom: aCollection [ - | size index newColl random dict operators operator | + | size newColl random dict operators operator | newColl := aCollection copyEmpty. random := Random new. @@ -128,10 +134,11 @@ RandomOperatorMutantSelection >> selectMutantsFrom: aCollection [ (random nextIntegerBetween: 1 and: operators size) ]. size := (dict at: operator) size. - index := random nextIntegerBetween: 1 and: size. - [ newColl includes: ((dict at: operator) at: index) ] whileTrue: [ - index := random nextIntegerBetween: 1 and: size ]. - newColl add: ((dict at: operator) at: index) ]. + self + addMutantIn: newColl + from: (dict at: operator) + using: random + and: size ]. ^ newColl ]