Example of Coding on haXe – Part 10

In this example you will see: feffects, feffects.Tween, flash.external.ExternalInterface.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Code Example: Example_10_Sprite2D.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description: In order to use Tweener you have to install the feffects
 * Install: neko-dev (Ubuntu: sudo apt-get Eneko-dev)
 * Execute: haxelib install feffects
 *	 Compile with flag: -lib feffects
 *	 Tweens available: feffects.easing.
 *	 Quint.easeIn, Quint.easeOut, Quint.easeInOut
 *	 Sine.easeIn, Sine.easeOut, Sine.easeInOut
 *	 Back.easeIn, Back.easeOut, Back.easeInOut
 *	 Bounce.easeIn, Bounce.easeOut, Bounce.easeInOut
 *	 Circ.easeIn, Circ.easeOut, Circ.easeInOut
 *	 Cubic.easeIn, Cubic.easeOut, Cubic.easeInOut
 *	 Elastic.easeIn, Elastic.easeOut, Elastic.easeInOut
 *	 Expo.easeIn, Expo.easeOut, Expo.easeInOut
 *	 Linear.easeIn, Linear.easeOut, Linear.easeInOut, Linear.easeNone
 *	 Quad.easeIn, Quad.easeOut, Quad.easeInOut
 *	 Quart.easeIn, Quart.easeOut, Quad.easeInOut
 *
 */

import feffects.Tween;
import feffects.easing.Bounce;
import feffects.easing.Circ;
import feffects.easing.Elastic;
import flash.text.TextField;
import flash.display.Sprite;

class NumericBall2D extends Sprite{
	private var text_field : TextField;
	private static var LINE_SIZE : Float = 1.0;
	private static var LINE_COLOR : Int = 0x000000;
	private static var FILL_COLOR : Int = 0x11FF33;
	private static var CIRCLE_X : Int = 0;
	private static var CIRCLE_Y : Int = 0;
	public function new(size : Float = 25){
		super();
		graphics.lineStyle(LINE_SIZE, LINE_COLOR);
    graphics.beginFill(FILL_COLOR);
	  graphics.drawCircle(CIRCLE_X, CIRCLE_Y, size);
    graphics.endFill();

	  text_field = new TextField();
		flash.Lib.current.addChild(text_field);
	}

	public function setText(text : String){
		text_field.text = text;
	  text_field.x = this.x - 12;
	  text_field.y = this.y - 40;
	}
}

class Example_10_Sprite2D{

    private var numericBall2D : NumericBall2D;
    private var tween : Tween;
		private static var MIN_Y : Int = 100;
		private static var MAX_Y : Int = 300;
		private static var EFFECT_DURATION : Int = 5000;

    public function new(?x : Float){
        numericBall2D = new NumericBall2D();
        numericBall2D.x = x;
        flash.Lib.current.addChild(numericBall2D);

        tween = new Tween(MIN_Y, MAX_Y, EFFECT_DURATION, Elastic.easeOut );
        tween.setTweenHandlers(moveY, msgFinished );
        trace ( 'tween staring at position: ' + MIN_Y);
        tween.start();
    }

    function moveY( position : Float ){
        numericBall2D.setText(Std.string(Math.round(position)));
        numericBall2D.y = position;
    }

    function msgFinished( position : Float ){
        trace ( 'tween finished at position: ' + position );
    }

}

example_10.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description:
 */

import flash.external.ExternalInterface;
import flash.Lib;

class Example_10{

  static function main(){
		var happy = new Example_10_Sprite2D(100);
  }	

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe – Part 9

In this example you will see: TextField, TextFormat, TextFormatAlign.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Actual Example:

Sorry, either Adobe flash is not installed or you do not have it enabled

Code example: Example_9.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description:
 */

import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextFormatDisplay;

class MyTextField extends flash.display.MovieClip{

	private var text_field : TextField;
	private var text_format : TextFormat;
	private var text_font : String;
	private var text_size : Int;
	private var text_color : Int;
	private var text : String;

	private function initTextFormat(){
		text_format = new TextFormat();
		text_format.font = text_font;
    text_format.size = text_size;
    text_format.color = text_color;
	}

	private function initTextField(){
		var text_field = new TextField();
		text_field.defaultTextFormat = text_format;
		text_field.text = text;
		text_field.x = x;
		text_field.y = y;
		flash.Lib.current.addChild(text_field);
	}

	public function new(){
		super();
		text_font = 'Helvetica';
		text_size = 12;
		text_color = 0x000000;
		initTextFormat();
		text = 'Default';
		initTextField();
	}
}

class MyTextField2D extends TextField{

	private var text_format : TextFormat;			

	public static var LEFT : TextFormatAlign = TextFormatAlign.LEFT;
	public static var RIGHT : TextFormatAlign = TextFormatAlign.RIGHT;
	public static var CENTER : TextFormatAlign = TextFormatAlign.CENTER;
	public static var JUSTIFY : TextFormatAlign = TextFormatAlign.JUSTIFY;	

	public function new(?text : String, ?font : String, ?size : Int, ?color : Int){
		super();
		text_format = new TextFormat();
		text_format.font = (font == null) ? 'Helvetica' : font;
		text_format.size = (size == null) ? 16 : size;
		text_format.color = (color == null) ? 0x000000 : color;
 		this.defaultTextFormat = text_format;
		this.text = 'Default 2D';
	}

	public function setFont(font : String) : Void{
		text_format.font = font;
		this.setTextFormat(text_format);
	}

	public function getFont() : String{
		return text_format.font;
	}

	public function setSize(size : Float) : Void{
		text_format.size = size;
		this.setTextFormat(text_format);
	}

	public function getSize() : Float {
		return text_format.size;
	}

	public function setColor(color : Int) : Void{
		text_format.color = color;
		this.setTextFormat(text_format);
	}

	public function getColor() : Int {
		return text_format.color;
	}

	public function setBold(boolean : Bool) : Void {
		text_format.bold = boolean;
		this.setTextFormat(text_format);
	}

	public function isBold() : Bool{
		return text_format.bold;
	}

	public function setItalic (boolean : Bool) : Void{
		text_format.italic = boolean;
		this.setTextFormat(text_format);
	}

	public function isItalic() : Bool{
		return text_format.italic;
	}

	public function setUnderline(boolean : Bool) : Void{
		text_format.underline = boolean;
	}

	public function isUnderline() : Bool{
		return text_format.underline;
	}

	public function setAlign(align : TextFormatAlign) : Void {
		text_format.align = align;
		this.setTextFormat(text_format);
	}

	public function getAlign() : TextFormatAlign{
		return text_format.align;
	}

	public function setIndent(indentation : Float) : Void{
		text_format.indent = indentation;
		this.setTextFormat(text_format);
	}

	public function getIndent() : Float {
		return text_format.indent;
	}

	public function setURL(url : String) : Void{
		text_format.url = url;
		this.setTextFormat(text_format);
	}

	public function getURL() : String{
		return text_format.url;
	}

	public function setLeftMargin(margin : Float) : Void{
		text_format.leftMargin;
		this.setTextFormat(text_format);
	}

	public function getLeftMargin() : Float{
		return text_format.leftMargin;
	}

	public function setRightMargin(margin : Float) : Void{
		text_format.rightMargin;
		this.setTextFormat(text_format);
	}

	public function getRightMargin() : Float{
		return text_format.rightMargin;
	}

	public function setLetterSpacing(spacing : Float) : Void{
		text_format.letterSpacing = spacing;
		this.setTextFormat(text_format);
	}

	public function getLetterSpacing() : Float{
		return text_format.letterSpacing;
	}

	public function setLeading(leading : Float) : Void{
		text_format.leading = leading;
		this.setTextFormat(text_format);
	}

	public function getLeading() : Float{
		return text_format.leading;
	}

	public function setKerning(boolean : Bool) : Void{
		text_format.kerning = boolean;
		this.setTextFormat(text_format);
	}

	public function isKerning() : Bool{
		return text_format.kerning;
	}

	public function setBullet(boolean : Bool) : Void{
		text_format.bullet = boolean;
		this.setTextFormat(text_format);
	}

	public function isBullet() : Bool{
		return text_format.bullet;
	}

	public function setBlockIndent(blockIndent : Float) : Void{
		text_format.blockIndent = blockIndent;
		this.setTextFormat(text_format);
	}

	public function getBlockIndex() : Float{
		return text_format.blockIndent;
	}

	public function setDisplay(display : TextFormatDisplay) : Void{
		text_format.display = display;
		this.setTextFormat(text_format);
	}

	public function getDisplay() : TextFormatDisplay{
		return text_format.display;
	}	

	public function setTarget(target : String) : Void{
		text_format.target = target;
		this.setTextFormat(text_format);
	}	

	public function getTartet() : String{
		return text_format.target;
	}

	public function setTabStops(tabStops : Array<UInt>) : Void{
		text_format.tabStops = tabStops;
		this.setTextFormat(text_format);
	}

	public function getTabStops() : Array<UInt>{
		return text_format.tabStops;
	}

	public function addAsChild() : Void {
		flash.Lib.current.addChild(this);
	}
} 

class Example_9 {

	static function main(){
		var myTextField : MyTextField = new MyTextField();

		var myTextField2D : MyTextField2D = new MyTextField2D();
		myTextField2D.x = 20.0;
		myTextField2D.y = 20.0;
		flash.Lib.current.addChild(myTextField2D);
		haxe.Timer.delay(function(){myTextField2D.setColor(0xAAFF22);}, 2000);
                haxe.Timer.delay(function(){myTextField2D.setColor(0x0000FF);}, 5000);
                haxe.Timer.delay(function(){myTextField2D.setColor(0x00FF00);}, 10000);
                haxe.Timer.delay(function(){myTextField2D.setColor(0xFF0000);}, 15000);
	}


Error: Attachment is not available or is not Flash content.
}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe – Part 8

In this example you will see: Class, extends, flash.display.MovieClip, flash.display.MovieClip.graphics, haxe.Timer.delay, flash.Lib.current.addChild

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Actual Example:

Sorry, either Adobe flash is not installed or you do not have it enabled

Code example: Example_8.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 * @description:
 */

import flash.display.MovieClip;

class Rectangle2D{
	private static var DEFAULT_BORDER_COLOR = 0x000000;
	private static var DEFAULT_BORDER_THICKNESS = 1.0;
	private var x : Int;
	private var y : Int;
	private var width : Int;
	private var height : Int;
	private var background_color : Null<Int>;
	private var border_color : Null<Int>;
	private var border_thickness : Null<Float>;
	private var movie_clip : flash.display.MovieClip;

	public function new(x : Int,
											y : Int,
											width : Int,
											height : Int,
											?background_color : Null<Int>,
											?border_color : Null<Int>,
											?border_thickness : Null<Float>){
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.background_color = (background_color == null) ? null : background_color;
		this.border_color = (border_color == null) ? DEFAULT_BORDER_COLOR : border_color;
		this.border_thickness = (border_thickness == null) ? DEFAULT_BORDER_THICKNESS : border_thickness;
	}

	public function draw(){
		movie_clip = flash.Lib.current;
		moveTo(x, y);
	}

	public function moveTo(x : Int, y : Int){
		this.x = x;
		this.y = y;
		resize(width, height);
	}

	public function resize(width : Int, height : Int){
		movie_clip.graphics.clear();

    if (background_color != null)
    	movie_clip.graphics.beginFill(background_color);

		if (border_thickness != null)
	    movie_clip.graphics.lineStyle(border_thickness, border_color);

		movie_clip.graphics.moveTo(x, y);
    movie_clip.graphics.lineTo(width,y);
    movie_clip.graphics.lineTo(width, height);
    movie_clip.graphics.lineTo(x, height);

    movie_clip.graphics.lineTo(x, y);

    if (background_color > -1)
    	movie_clip.graphics.endFill();

	}

}

class Shape2D extends MovieClip{
	private static var DEFAULT_BORDER_COLOR = 0x000000;
	private static var DEFAULT_BORDER_THICKNESS = 1.0;
	private var radius : Float;
	private var background_color : Null<Int>;
	private var border_color : Null<Int>;
	private var border_thickness : Null<Float>;

	public function new(x : Int,
											y : Int,
											?background_color : Null<Int>,
											?border_color : Null<Int>,
											?border_thickness : Null<Float>){
		super();
		this.x = x;
		this.y = y;
		this.background_color = (background_color == null) ? null : background_color;
		this.border_color = (border_color == null) ? DEFAULT_BORDER_COLOR : border_color;
		this.border_thickness = (border_thickness == null) ? DEFAULT_BORDER_THICKNESS : border_thickness;
	}

	public function drawCircle(radius : Float, ?x : Int, ?y : Int){
		if (x != null) this.x = x;
		if (x != null) this.y = y;
		this.radius = radius;
		beginDraw();
		graphics.drawCircle(x, y, radius);
		endDraw();
	}

	public function drawRectangle(width : Int, height : Int, ?x : Int, ?y : Int){
		if (x != null) this.x = x;
		if (x != null) this.y = y;
		this.width = width;
		this.height = height;
		beginDraw();
		graphics.drawRect(x, y, width, height);
		endDraw();
	}

	private function beginDraw(){
		graphics.clear();
		graphics.moveTo(x, y);

    if (background_color != null)
    	graphics.beginFill(background_color);

		if (border_thickness != null)
	    graphics.lineStyle(border_thickness, border_color);

	}

	private function endDraw(){
		 if (background_color > -1)
    	graphics.endFill();
	}

}

class Example_8{

 	static function main(){

		var rectangle : Rectangle2D = new Rectangle2D(100,100,100, 100, 0x334433, 0x44444);
		rectangle.draw();
		rectangle.resize(200, 200);
		haxe.Timer.delay(function(){rectangle.moveTo(0, 0);}, 2000);

		var shape2D : Shape2D = new Shape2D(100,100, null, 0x000000, 3.0);
		flash.Lib.current.addChild(shape2D);
		shape2D.drawCircle(45.0);

		haxe.Timer.delay(function(){shape2D.drawRectangle(100, 100);}, 2000);

 	}
}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe (SnapSack Algorithm) – Part 7

In this example you will see: Snapsack algorithm using multi-dimensional arrays

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

example_7.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 */

class Item{
	public var name : String;
	public var weight : Int;
  public var value : Int;
 	public function new(?name : String, ?weight : Int, ?value : Int){
		this.name = (name == null) ? '' : name;
		this.weight = (weight == null) ? 0 : weight;
		this.value = (value == null) ? 0 : value;
	}

	public function setName(name : String) : Void{
		this.name = (name == null) ? '' : name;
	}

	public function getName() : String{
		return this.name;
	}

	public function setWeight(weight : Int) : Void{
		this.weight = weight;
	}

	public function getWeight() : Int{
		return this.weight;
	}

	public function setValue(value : Int) : Void{
		this.value = value;
	}

	public function getValue() : Int{
		return this.value;
	}

}

/* Our dynamic programming table element.
 * We have the value that we can store, and
 * also a way to remember what we selected.
 */
class Prog{
  public var value : Int;
  public var prev_row : Int;
  public var prev_column : Int;
	public function new(?prev_row : Int, ?prev_column : Int, ?value : Int){
		this.value = (value == null) ? 0 : value;
		this.prev_row = (prev_row == null) ? -1 : prev_row;
		this.prev_column = (prev_column == null) ? -1 : prev_column;
	}

	public function setValue(value : Int) : Void{
		this.value = value;
	}

	public function getValue() : Int{
		return this.value;
	}

	public function setPrevRow(prev_row : Int) : Void{
		this.prev_row = prev_row;
	}

	public function getPrevRow() : Int{
		return this.prev_row;
	}

	public function setPrevColumn(prev_column : Int) : Void{
		this.value = prev_column;
	}

	public function getPrevColumn() : Int{
		return this.prev_column;
	}
}

class Snapsack{
	private var items : Array<Item>;
	private var selected_items : Array<Item>;
	private var table : Array<Array<Prog>>;

	private var max_weight : Int;
	private var num_items : Int;
	private var total_weight : Int;
	private var total_value : Int;

	private function initTable() : Void{
		trace('initTable()');
		var i : Int = 0;
		var j : Int = 0;
		for (i in 0...num_items) {
			table[i] = new Array<Prog>();
			for (j in 0...max_weight) {
				table[i][j] = new Prog(0, 0, 0);
			}
		}
	}

	private function initItems(){
		trace('initItems()');
		var i : Int = 0;
		for (i in 0...num_items){
			items[i] = new Item('', 0, 0);
		}
	}

	private function printTable() : Void{
		trace('printTable()');
		var str : String = '';
		var i : Int = 0;
		var j : Int = 0;
		for (i in 0...table.length) {
			for (j in 0...max_weight) {
				str += '[(' +
							table[i][j].prev_row +
							', ' +
							table[i][j].prev_column +
							') ' +
						  table[i][j].value +
						  ']';
			}
			trace(str);
			str = '';
		}
	}

	private function printItems(){
		trace('printItems()');
		var i : Int = 0;
		for (i in 0...num_items){
			trace('name: ' +
						items[i].name +
						', weight: ' +
						items[i].weight +
						', value: ' +
						items[i].value);
		}
	}

	public function new(num_items : Int, max_weight : Int){
		this.num_items = num_items;
		this.max_weight = ++max_weight;
		total_weight = 0;
		total_value = 0;
		items = new Array<Item>();
		table = new Array<Array<Prog>>();

		table.push(new Array<Prog>());
		for (i in 0...max_weight) {
			table[table.length - 1][i] = new Prog(-1, -1, 0);
		}
	}

	public function pushItem(name : String, weight : Int, value : Int){
		items.push(new Item(name, weight, value));
		table.push(new Array<Prog>());
		var i : Int = 0;
		for (i in 0...max_weight) {
			table[table.length - 1][i] = new Prog(-1, -1, 0);
		}
	}

	public function pickItems() : Void {
		printTable();
		printItems();
		var i : Int = 1;
		var w : Int = 0;

	  // Perform knapsack and find maximun value
	  for (i in 1...num_items + 1){
	    for (w in 0...max_weight){
	  		table[i][w].prev_row = i - 1;
			  table[i][w].prev_column = w;		

				// Check if item fit inside the knapsack
				if(items[i - 1].weight <= w) {
	        var diff_weight : Int = w - items[i - 1].weight;

	        // Check which value is higher
					table[i][w].value = Math.round(Math.max((items[i - 1].value + table[i - 1][diff_weight].value),
                              				             table[i - 1][w].value));

	        // Keep track of the previous column
					if(table[i][w].value > table[i - 1][w].value)
						table[i][w].prev_column = diff_weight;

				}else{
					table[i][w].value = table[i - 1][w].value;
				}
	    }
	  }

	  total_value = table[num_items - 1][max_weight - 1].value;

		total_weight = 0;
	  var w : Int = max_weight - 1;
  	var t : Int = 0;
  	selected_items = new Array<Item>();

  	var i : Int = num_items - 1;
  	while (i > 0){

			if(table[i][w].value != table[i - 1][w].value){

				total_weight += items[i - 1].weight;				

				selected_items.push(new Item(items[i - 1].name,
																		 items[i - 1].weight,
																		 items[i - 1].value));

		  }
			t = w;
		  w = table[i][w].prev_column;
		  i = table[i][t].prev_row;
		}

		printTable();
	}

	public function getTotalValue() : Int{
		return total_value;
	}

	public function getTotalWeight() : Int{
		return total_weight;
	}

	public function printResult() : Void{

		for (i in 0...selected_items.length)
			trace('name: ' +
						selected_items[i].name +
						', weight: ' +
						selected_items[i].weight +
						', value: ' +
						selected_items[i].value);
	}
}

class Example_7 {

	static function main(){

		trace('Creating Snapsack');
		var snapsack : Snapsack = new Snapsack(4, 5);

		trace('adding items to snapsack');
		snapsack.pushItem('item_A', 2, 3);
		snapsack.pushItem('item_B', 3, 4);
		snapsack.pushItem('item_C', 4, 5);
		snapsack.pushItem('item_D', 5, 6);
		snapsack.pickItems();
		trace('Total value: ' + snapsack.getTotalValue());
		trace('Total Weight: ' + snapsack.getTotalWeight());
		snapsack.printResult();

	}

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment

Example of Coding on haXe (Quicksort, Hoarse, Random) – Part 6

In this example you will see: Regular quicksort, quicksort using random partition, quicksort using Hoarse partition, quicksort using random partition together with Hoarse partition

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

example_6.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 */

enum PartitionType{
	DEFAULT;
	RANDOMIZED;
	HOARSE;
	RANDOMIZED_HOARSE;
}

class QuickSort{
	private var data : Array<Int>;
	private var partition_type : PartitionType;

	private function _sort(start_index : Int, end_index : Int) : Void{
		if (start_index < end_index){
			var partition_index : Int = getPartitionIndex(start_index, end_index);
			_sort(start_index, partition_index - 1);
			_sort(partition_index + 1, end_index);
		}
	}

	private function getPartitionIndex(start_index : Int, end_index : Int) : Int{
		switch(partition_type){
			case DEFAULT:
				return partition(start_index, end_index);
			case RANDOMIZED:
				return randomPartition(start_index, end_index);
			case HOARSE:
				return hoarsePartition(start_index, end_index);
			case RANDOMIZED_HOARSE:
				return randomizedHoarsePartition(start_index, end_index);
		}
		return -1;
	}

	private function partition(start_index : Int, end_index : Int) : Int{
		var pivot_value : Int = data[end_index];
		var i : Int = start_index - 1;

		var index : Int = start_index;
		while (index < end_index){
			if (data[index] < pivot_value)
				exchange(++i, index);
			++index;
		}
		exchange(++i, end_index);
		return i;
	}

	private function randomPartition(start_index : Int, end_index : Int) : Int{
		exchange(getRandom(start_index, end_index), end_index);
		return partition(start_index, end_index);
	}

	private function getRandom(maximum_value : Int, minimum_value : Int) : Int{
		return Math.floor(Math.random() % (maximum_value - minimum_value)) + minimum_value;
	}

	private function hoarsePartition(start_index : Int, end_index : Int) : Int{
		var pivot_value : Int = data[start_index];
		var left_index : Int = start_index;
		var right_index : Int = end_index;
		while (left_index < right_index){
			while (data[left_index] < pivot_value)
				--right_index;
			while (data[left_index] > pivot_value)
				++left_index;
			if (left_index < right_index)
				exchange(left_index, right_index);
			else
				break;
			if (data[right_index] == data[left_index]){
				--right_index;
				++left_index;
			}
		}
		return right_index;
	}

	private function randomizedHoarsePartition(start_index : Int, end_index : Int) : Int{
		exchange(getRandom(start_index, end_index), end_index);
		return hoarsePartition(start_index, end_index);
	}	

	private function exchange(index_a : Int, index_b : Int) : Void{
		var temp_value : Int = data[index_a];
		data[index_a] = data[index_b];
		data[index_b] = temp_value;
	}

	// Constructor ( ? means that items is optional)
	public function new(?data : Array<Int>, ?partition_type : PartitionType){
		this.data = (data == null) ? new Array() : data;
		setPartitionType(partition_type);
	}

	public function getData() : Array<Int> {
		return data;
	}

	public function setData(data : Array<Int>) : Void {
		this.data = data;
	}

	public function sort() : Void{
		if(data.iterator().hasNext())
			_sort(0, data.length - 1);
	}

	public function setPartitionType(parition_type : PartitionType) : Void{
		this.partition_type = (partition_type == null) ? DEFAULT : partition_type;
	}

}

class Example_6 {

	static function main(){

		var data : Array<Int> = [3, 7, 1, -1];
		printData(data);

		trace('QuickSort using regular partition:');
		var quicksort = new QuickSort(data, DEFAULT);
		quicksort.sort();
		data = quicksort.getData();
		printData(data);		

		data = [3, 7, 1, -1];
		trace('QuickSort using randomized partition:');
		var quicksort_1 = new QuickSort(data, RANDOMIZED);
		quicksort_1.sort();
		data = quicksort_1.getData();
		printData(data);		

		data = [3, 7, 1, -1];
		trace('QuickSort using Hoarse partition:');
		var quicksort_2 = new QuickSort(data, HOARSE);
		quicksort_2.sort();
		data = quicksort_2.getData();
		printData(data);		

		data = [3, 7, 1, -1];
		trace('QuickSort using randomized Hoarse partition:');
		var quicksort_3 = new QuickSort(data, RANDOMIZED_HOARSE);
		quicksort_3.sort();
		data = quicksort_3.getData();
		printData(data);
	}

	private static function printData(data : Array<Int>){
		var iter = data.iterator();
		var index : Int = 0;
		trace('[DATA]');
		while (iter.hasNext()){
			trace('data[' + index++ + ']: ' + iter.next());
		}
	}

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

Share
Leave a comment