Skip to content

Commit

Permalink
1.1 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Neto committed Sep 26, 2023
1 parent e41caec commit 18a712a
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 97 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "net.jneto"
version = "1.1-snapshot"
version = "1.1-release"

repositories {
mavenCentral()
Expand Down
Binary file not shown.
51 changes: 27 additions & 24 deletions src/main/java/net/jneto/dataStructures/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

/**
* This is a classic ArrayList(Lista) implementation with Array
*
* @param <ITEM> The type of elements stored in the ArrayList.
*/
public class ArrayList<ITEM> implements DataStructure<ITEM> {
Expand All @@ -14,7 +15,7 @@ public class ArrayList<ITEM> implements DataStructure<ITEM> {
* Constructor empty list
*/
@SuppressWarnings("unchecked")
public ArrayList(){
public ArrayList() {
list = (ITEM[]) new Object[DEFAULT_SIZE];
size = 0;

Expand All @@ -35,13 +36,14 @@ public void add(ITEM item) {
}

/**
* A list logic you can put Objects in any position
* @param item - The object to be added
* A list logic you can put Objects in any position
*
* @param item - The object to be added
* @param index - index where object will be added
*/
public void add(ITEM item, int index) {
//INDEX VERIFY
if(index > list.length || index < 0){
if (index > list.length || index < 0) {
return;
}
//VERIFY SPACE
Expand All @@ -62,19 +64,20 @@ public void add(ITEM item, int index) {
//list[index==1] = new item
//[0,2,1,2,3,4,5, null, null, null, null, null, null, null, null, null, null]

for(int i = size; i > index; i--){
for (int i = size; i > index; i--) {
list[i] = list[i - 1];
}
list[index] = item;
size++;
}

/**
* A list logic you can get Objects in any position
* A list logic you can get Objects in any position
*
* @param index - index where object will be returned
*/
public ITEM get(int index) {
if(index > list.length || index < 0){
if (index > list.length || index < 0) {
return null;
}
return list[index];
Expand All @@ -89,25 +92,26 @@ public ITEM get(int index) {
@Override
public ITEM remove() {

if(isEmpty()){
if (isEmpty()) {
return null;
}
ITEM item = list[size - 1];
size--;

if((float) size / list.length <= 0.25){
if ((float) size / list.length <= 0.25) {
resize();
}

return item;
}

/**
* A list logic you can remove Objects in any position
* A list logic you can remove Objects in any position
*
* @param index - index where object will be removed
*/
public ITEM remove(int index) {
if(index > list.length || index < 0){
if (index > list.length || index < 0) {
return null;
}
//ARRAY REALOC
Expand All @@ -121,7 +125,7 @@ public ITEM remove(int index) {

ITEM removed = list[index];
//after 'remove' realoc after removed item
for(int i = index; i < size - 1; i++){
for (int i = index; i < size - 1; i++) {
list[i] = list[i + 1];

}
Expand All @@ -144,9 +148,9 @@ public ITEM remove(int index) {
* @return removed successfully.
* @throws IllegalStateException If the data structure is empty.
*/
public boolean remove(ITEM item){
for(int i = 0; i < size; i++){
if(list[i] == item || (list[i] != null && list[i].equals(item))){
public boolean remove(ITEM item) {
for (int i = 0; i < size; i++) {
if (list[i] == item || (list[i] != null && list[i].equals(item))) {
remove(i);
return true;
}
Expand All @@ -162,7 +166,7 @@ public boolean remove(ITEM item){
*/
@Override
public boolean isEmpty() {
return size <=0;
return size <= 0;
}

/**
Expand Down Expand Up @@ -199,7 +203,7 @@ public ITEM peek(int index) {
//[1] --size = 1 list.length = 2
//peek(0)
// 1 (list[0])
if(index > size || index < 0){
if (index > size || index < 0) {
return null;
}
return list[index - 1];
Expand Down Expand Up @@ -244,7 +248,6 @@ public String show() {
@Override
public String showReverse() {
StringBuilder builder = new StringBuilder();
String out = "";
boolean isFirst = true;
Stack<ITEM> aux = new Stack<ITEM>();
ITEM element;
Expand All @@ -253,7 +256,6 @@ public String showReverse() {

element = remove();
if (isFirst) {
//out = out + element;
builder.append(element);
isFirst = false;
} else {
Expand All @@ -270,21 +272,22 @@ public String showReverse() {
return "[" + builder.toString() + "]";
}

/*
/**
* Method to increase or decrease the list Array
*/
@SuppressWarnings("unchecked")
private void resize() {
if((float) size / list.length <= 0.25) { //Decrease
if ((float) size / list.length <= 0.25) { //Decrease
Common<ITEM> c = new Common<ITEM>();
list = c.resize(list,false);
list = c.resize(list, false);
return;
}
if(size >= list.length){ //increase
if (size >= list.length) { //increase
Common<ITEM> c = new Common<ITEM>();
list = c.resize(list,true);
list = c.resize(list, true);
}
}

@Override
public String toString() {
String out = " List size: " + size + " Internal array size: " + list.length;
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/net/jneto/dataStructures/Bag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* This is a classic Bag(Saco) implementation with Array
* t's an Array Bag, so it be ordered by Array Java logic
*
* @param <ITEM> The type of elements stored in the Bag.
*/
public class Bag<ITEM> implements DataStructure<ITEM> {
Expand All @@ -11,27 +12,28 @@ public class Bag<ITEM> implements DataStructure<ITEM> {
private int size; // Used array size

/**
* Constructor empty bag
* Constructor empty bag
*/
public Bag() {
bag = (ITEM[]) new Object[DEFAULT_SIZE];
size = 0;
}

/**
* Adds an object to the data structure.
*
* @param item The object to be added.
*/
@Override
public void add(ITEM item) {
if(size < bag.length){
if (size < bag.length) {
bag[size] = item;
size++;
}else{
} else {
resize();
add(item);
}

}

/**
Expand All @@ -41,7 +43,7 @@ public void add(ITEM item) {
*/
@Override
public ITEM remove() {
if (isEmpty()){
if (isEmpty()) {
return null;
}
ITEM item = bag[size - 1];
Expand All @@ -59,7 +61,7 @@ public ITEM remove() {
*/
@Override
public boolean isEmpty() {
return size ==0;
return size == 0;
}

/**
Expand All @@ -85,6 +87,7 @@ public ITEM peek() {
/**
* Returns a string of the bag.
* A bag dont have order... but its a ArrayBag implementation
*
* @return A string representation of the data structure.
*/
@Override
Expand Down Expand Up @@ -115,7 +118,8 @@ public String show() {

/**
* Returns a string of the data structure in reverse order.
* A bag dont have order... but its a ArrayBag implementation
* A bag don't have order... but it's a ArrayBag implementation
*
* @return A string representation of the reversed data structure.
*/
@Override
Expand Down Expand Up @@ -148,20 +152,19 @@ public String showReverse() {
}



/*
* Method to increase or decrease the bag Array
*
*/
private void resize() {
if((float) size / bag.length <= 0.25) { //Decrease
if ((float) size / bag.length <= 0.25) { //Decrease
Common<ITEM> c = new Common<ITEM>();
bag = c.resize(bag,false);
bag = c.resize(bag, false);
return;
}
if(size >= bag.length){ //increase
if (size >= bag.length) { //increase
Common<ITEM> c = new Common<ITEM>();
bag = c.resize(bag,true);
bag = c.resize(bag, true);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/jneto/dataStructures/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* Comons methods used by data structures
*
* @param <ITEM> The type of elements implemented.
*/
public class Common<ITEM> {
Expand All @@ -10,14 +11,14 @@ public class Common<ITEM> {
/**
* Default constructor to use not statics Common method
*/
public Common(){
public Common() {
//dont need initialization
}

/**
*
* The resize method is a common method used by data Structures
* to resize the Array size of a structure
*
* @param structure - data Structure typed
* @param increase - if True - increase Array, if false decrease Array
* @return structure - new sized structure
Expand Down
Loading

0 comments on commit 18a712a

Please sign in to comment.