CustomOpenHashSets now implement foreach and have less overhead.

This commit is contained in:
Speiger 2021-06-25 16:37:17 +02:00
parent b784041894
commit 84bc785cdc
3 changed files with 25 additions and 0 deletions

View File

@ -8,6 +8,7 @@
- Changed: Arrays Wrap is now accessible to Objects and now is ? extends TYPE instead of TYPE. - Changed: Arrays Wrap is now accessible to Objects and now is ? extends TYPE instead of TYPE.
- Added: OpenHashSets now implement foreach and have less overhead. - Added: OpenHashSets now implement foreach and have less overhead.
- Added: ImmutableOpenHashSet that is not editable (is linked by default for fast iteration) - Added: ImmutableOpenHashSet that is not editable (is linked by default for fast iteration)
- Added: CustomOpenHashSets now implement foreach and have less overhead.
### Version 0.3.1 ### Version 0.3.1
- Fixed: containsKey & containsValue in HashMaps were deprecated for Object Variants. - Fixed: containsKey & containsValue in HashMaps were deprecated for Object Variants.

View File

@ -2,6 +2,7 @@ package speiger.src.collections.PACKAGE.sets;
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.Comparator; import java.util.Comparator;
import java.util.function.Consumer;
#endif #endif
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
@ -18,6 +19,7 @@ import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS; import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
import speiger.src.collections.PACKAGE.utils.STRATEGY; import speiger.src.collections.PACKAGE.utils.STRATEGY;
import speiger.src.collections.utils.HashUtil; import speiger.src.collections.utils.HashUtil;
@ -516,6 +518,15 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
lastIndex = -1; lastIndex = -1;
} }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
int index = firstIndex;
while(index != -1){
action.accept(keys[index]);
index = (int)links[index];
}
}
@Override @Override
public LIST_ITERATOR KEY_GENERIC_TYPE iterator() { public LIST_ITERATOR KEY_GENERIC_TYPE iterator() {
return new SetIterator(); return new SetIterator();

View File

@ -4,6 +4,9 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
#if TYPE_OBJECT
import java.util.function.Consumer;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
@ -11,6 +14,7 @@ import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
import speiger.src.collections.PACKAGE.lists.LIST; import speiger.src.collections.PACKAGE.lists.LIST;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS; import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
import speiger.src.collections.PACKAGE.utils.STRATEGY; import speiger.src.collections.PACKAGE.utils.STRATEGY;
@ -448,6 +452,15 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
return size; return size;
} }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
if(size() <= 0) return;
if(containsNull) action.accept(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i])) action.accept(keys[i]);
}
}
private class SetIterator implements ITERATOR KEY_GENERIC_TYPE { private class SetIterator implements ITERATOR KEY_GENERIC_TYPE {
int pos = nullIndex; int pos = nullIndex;
int lastReturned = -1; int lastReturned = -1;