Removing elements from dynamic array

Reflexive via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 9 08:30:30 PDT 2015


I wrote import std.algorithm.remove ;, and I dont have any longer 
any error. But, it dont want to remove the item. Here is the 
source, the problem is in the shuffle() method :

// sabot.d
// version 0.0.1
import std.stdio ;
import std.random ;
import std.algorithm : remove ;


void main(){
	auto deck = new sabot ;
	deck.shuffle() ;

class sabot{
	card[] sabotarray ;

	(...)

	struct card {
		int id ;
		(...)
	}

	(...)

	this(){
		(the constructor builds up the array 'sabotarray'.)
}
	void shuffle(){
		card[] tempsabotarray ;
		card tempcard ;
		long id_card ;
		int counter ;
		while(this.sabotarray.length>1){
			if(counter>=60){break;}

			id_card = uniform(0,this.sabotarray.length-1) ;

			tempcard = this.sabotarray[id_card] ;
			this.sabotarray.remove(id_card) ;
			tempsabotarray ~= tempcard ;

			writeln ("Counter : " , counter, " ; id card : " , id_card, " 
; sabot size : ", this.sabotarray.length) ;
			++counter ;
		}
		this.sabotarray = tempsabotarray ;
	}

	(...)
}

And I get in the terminal :

$ dmd -run sabot.d
Counter : 0 ; id card : 18 ; sabot size : 52
Counter : 1 ; id card : 40 ; sabot size : 52
Counter : 2 ; id card : 14 ; sabot size : 52
Counter : 3 ; id card : 44 ; sabot size : 52
(...)

Until counter is 60. Sabot size remains 52 all the time.

Without the break statement, the computer goes out of memory.







More information about the Digitalmars-d-learn mailing list