Added getFirst/getLast/removeFirst/removeLast to lists

This commit is contained in:
Speiger 2023-05-17 09:20:45 +02:00
parent ed9ce60af4
commit 2da4588430
6 changed files with 1086 additions and 1019 deletions

View File

@ -1,5 +1,8 @@
# Changelog of versions
### Version 0.8.0
- Added: getFirst/getLast/removeFirst/removeLast to Lists
### Version 0.8.0
- Added: ISizeProvider interface (Optimization Helper)
- Added: ISizeProvider into most Iterable implementations (Distinct/Filter/FlatMap/ArrayFlatMap don't support it, for obvious reasons)

View File

@ -69,7 +69,10 @@ public class ListModule extends BaseModule
protected void loadFunctions()
{
addFunctionMapper("GET_KEY", "get");
addFunctionMapper("REMOVE_LAST", "removeLast");
addFunctionMapper("GET_FIRST_KEY", "getFirst");
addFunctionMapper("GET_LAST_KEY", "getLast");
addFunctionMapper("REMOVE_FIRST_KEY", "removeFirst");
addFunctionMapper("REMOVE_LAST_KEY", "removeLast");
addFunctionMapper("REMOVE_SWAP", "swapRemove");
addFunctionMappers("REPLACE", keyType.isObject() ? "replaceObjects" : "replace%ss");
addFunctionMappers("SORT", "sort%ss");

View File

@ -314,21 +314,43 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#if DEQUEUE_FEATURE
@Override
public KEY_TYPE first() {
return GET_FIRST_KEY();
}
@Override
public KEY_TYPE last() {
return GET_LAST_KEY();
}
#endif
@Override
public KEY_TYPE GET_FIRST_KEY() {
if(first == null) throw new NoSuchElementException();
return first.value;
}
@Override
public KEY_TYPE last() {
public KEY_TYPE GET_LAST_KEY() {
if(last == null) throw new NoSuchElementException();
return last.value;
}
#endif
@Override
public KEY_TYPE REMOVE_FIRST_KEY() {
if(first == null) throw new NoSuchElementException();
return unlinkFirst(first);
}
@Override
public KEY_TYPE REMOVE_LAST_KEY() {
return pop();
}
@Override
public KEY_TYPE peek(int index) {
return GET_KEY((size() - 1) - index);
}
@Override
public KEY_TYPE GET_KEY(int index) {
checkRange(index);
@ -1146,8 +1168,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
Entry KEY_GENERIC_TYPE prev;
Entry KEY_GENERIC_TYPE next;
public Entry(KEY_TYPE value, Entry KEY_GENERIC_TYPE prev, Entry KEY_GENERIC_TYPE next)
{
public Entry(KEY_TYPE value, Entry KEY_GENERIC_TYPE prev, Entry KEY_GENERIC_TYPE next) {
this.value = value;
this.prev = prev;
this.next = next;

View File

@ -98,6 +98,46 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
*/
public boolean addAll(int index, LIST KEY_GENERIC_TYPE c);
/**
* Helper method that returns the first element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return first element of the list
*/
public default KEY_TYPE GET_FIRST_KEY() {
return GET_KEY(0);
}
/**
* Helper method that returns the last element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return last element of the list
*/
public default KEY_TYPE GET_LAST_KEY() {
return GET_KEY(size() - 1);
}
/**
* Helper method that removes and returns the first element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return first element of the list and removes it
*/
public default KEY_TYPE REMOVE_FIRST_KEY() {
return REMOVE(0);
}
/**
* Helper method that removes and returns the last element of a List.
* This function was introduced due to how annoying it is to get/remove the last element of a list.
* This simplifies this process a bit.
* @return last element of the list and removes it
*/
public default KEY_TYPE REMOVE_LAST_KEY() {
return REMOVE(size() - 1);
}
#if !TYPE_OBJECT
/**
* A Type-Specific get function to reduce (un)boxing