Compare commits

...

231 Commits

Author SHA1 Message Date
Speiger afdd27648e Updated Changelog and Readme to include Maven Central 2024-04-11 19:11:07 +02:00
Speiger 7e475b5472 Fixed a few bugs in the maven central script 2024-03-30 05:12:57 +01:00
Speiger e65fde736b Fixed 2024-03-30 01:08:38 +01:00
Speiger bf0b4172de Added Maven central Badge 2024-03-30 01:07:46 +01:00
Speiger 961b47a58c Fixed java8 compat 2024-03-30 01:02:42 +01:00
Speiger 330be87338 finalizing the maven central release. 2024-03-30 00:32:37 +01:00
Speiger 4b30ce12c9 Upgraded thread workers and disable GPG temporarly. 2024-03-29 22:35:44 +01:00
Speiger 0be7dba5d3 Fixed script bug 2024-03-29 22:26:34 +01:00
Speiger 6eaa992f5f Create SECURITY.md 2024-03-29 22:19:29 +01:00
Speiger 9b23d713ff Started maven publish plugin. 2024-03-29 22:17:38 +01:00
Speiger 85d230c561 Fixed a auto generation bug where iterators wouldn't accept settings 2023-07-04 17:29:32 +02:00
Speiger 6afed5e174 Version Bump 2023-06-29 22:52:44 +02:00
Speiger 640a1a8161 Fixed tests and added missing changelog 2023-06-29 20:04:45 +02:00
Speiger 274d37c4d6 New Features.
-Added: List.indexedIterator which allows you to create a iterator with a customized iteration indecies. Useful if you want to transform lists output.
-Added: PriorityQueue.contains is now a function
-Added: Iterators/Async Builders now support MapToPrimitiveType function on the object variant. So more processing can be done. (Will be expanded upon later versions)
-Updated: SimpleCodeGenerator 1.3.0 is now being used which allows for iterative code support.
2023-06-29 18:30:22 +02:00
Speiger a89c812c06 Added Infinite Iterators 2023-06-17 01:22:56 +02:00
Speiger 6af0656216 Adding coverage to master to make things simpler! 2023-06-15 18:53:08 +02:00
Speiger e76db94136 Upgrading Other dependencies! 2023-06-15 18:31:22 +02:00
Speiger 7011101b05 Upgraded Gradle to be java19/20 friendly 2023-06-15 18:19:12 +02:00
Speiger d0599b99ec Upgraded Build Script and support now java19/20 2023-06-15 18:09:14 +02:00
Speiger d7c5b9ad7d Optimized SelectionSort a tiny bit. 2023-06-15 18:08:57 +02:00
Speiger d2c81e7779 Fixed all the reported issues by the Unit testing. 2023-06-15 17:12:11 +02:00
Speiger ef5fdbd377 Finished Code Cleanup with Compute functions and added ReversedWrappers 2023-06-14 17:40:27 +02:00
Speiger 5e67e45910 Fixed failing tests/errors 2023-06-06 11:21:52 +02:00
Speiger 63ef68fb95 New Compute function and better toArray implementations 2023-06-05 23:40:48 +02:00
Speiger 0f9751bf70 Fixed that setValue wasn't working with ForEach implementations. 2023-05-18 23:02:27 +02:00
Speiger bcc2ffdc13 New features.
-Added: Improved Map documentation for compute methods
-Added: Dedicated toArray implementations to TreeSets
2023-05-17 09:58:55 +02:00
Speiger 2da4588430 Added getFirst/getLast/removeFirst/removeLast to lists 2023-05-17 09:20:45 +02:00
Speiger ed9ce60af4 Small Fixes to doc and Useless Imports 2023-05-17 09:01:09 +02:00
Speiger 4dd3a4a6e8 0.8.0 Release 2022-12-20 09:33:26 +01:00
Speiger efd29bbe7e First draft of the 0.8.0 patch. 2022-12-19 16:03:43 +01:00
Speiger 96458bd8b6 Changes:
- Added: RandomGenerator support (Java17), though requires self
compilation
- Added: Optimizations for HashUtils next power of function.
- Added: toArray() now returns a cached empty array if the collection is
empty.
- Added: toArray function for AsyncBuilder
- Updated: SCG to version 1.2.2
2022-12-16 18:17:51 +01:00
Speiger 477f3c9f40 Implemented ForEachIndexed into all collections 2022-12-16 13:03:28 +01:00
Speiger 3f6e7fbb88 Added forEachIndexed 2022-12-15 20:08:33 +01:00
Speiger 5650c6e69b Fixed tests don't force generate 2022-12-15 18:28:00 +01:00
Speiger 61df32b7b2 Fixed unit tests and java 8 incompat. 2022-12-15 18:10:59 +01:00
Speiger 859d00da16 Fixed Java9 or newer issues. 2022-12-15 17:13:34 +01:00
Speiger 9df95c0fc3 Supplier now uses javas function name. Tests will still fail. 2022-12-15 16:26:05 +01:00
Speiger d6d2c0a396 Changed that function names are closer to javas names. 2022-12-15 16:07:44 +01:00
Speiger 127eb71968 Massive refactor.
-Changed: Classes that used Primitive Collections Functions now use Java functions if possible to increase compat.
-Changed: Started that functions go closer to javas naming sceme.
2022-12-14 18:32:04 +01:00
Speiger 6005d0fd39 Updated SCG to version 1.2.1
This fixes that all expressions are supported.
2022-12-13 13:34:11 +01:00
Speiger 290e626f07 Updated to SCG 1.2.0 2022-12-13 13:07:24 +01:00
Speiger e30d011273 Fixed Unit Test and Added putIfAbsentTest 2022-12-08 10:40:21 +01:00
Speiger 4c52636c23 Fixed Unit Tests 2022-12-07 08:22:45 +01:00
Speiger aa580c1772 Merged branches hopefully 2022-12-07 07:36:25 +01:00
Speiger 8f7d49b280 Finished Function module and added breaking change. 2022-12-07 07:31:30 +01:00
Speiger 342b0cece9 Collection Module Works now. (Least Configurable for obvious Reasons) 2022-12-07 06:27:47 +01:00
Speiger 3ce52668df Added Priority Queue Module 2022-12-07 05:30:23 +01:00
Speiger ce9343348e Pair Module is now done. 2022-12-07 04:35:29 +01:00
Speiger 8d9f7a6761 Improved Settings a bit.
-Added: "Implementations" configuration to turn of all "Implementations" keeping the interfaces/wrappers/abstract classes. Reducing the amount of configurations that need to be set.
-Changed: removed "Sets/Maps/Lists" configuration and replaced it with "Wrappers" since they are wrappers.
-Changed: Category Specific settings (except "Enabled") is sorted by Name.
2022-12-07 04:07:11 +01:00
Speiger 1b1ec4b87a Improving Dependency Checks to be more recursive. 2022-12-07 03:30:59 +01:00
Speiger cc92ef953a Added Dependency Checks into the Module System.
Why a Dependency Check system was added?
2022-12-07 03:23:19 +01:00
Speiger 8d8c30c9a7 Finishing the Set configuration. 2022-12-07 02:50:42 +01:00
Speiger d44ad2d42e 99% of the Set Module done.
The 1% is the Distinct Iterator that relies on a Set implementation.
So I have to make a dedicated set implementation.
2022-12-06 04:57:43 +01:00
Speiger 2ed090e989 Fixed a few bugs and Started the Set compilation. 2022-12-06 04:09:40 +01:00
Speiger dafb162797 Fixed a small bug where generic types can not be disabled. 2022-12-06 03:21:48 +01:00
Speiger 57280b8285 Map Generation can now be turned off. 2022-12-06 03:05:58 +01:00
Speiger c9fc963670 Few changes.
-Removed: BooleanSet classes are gone. (Unused anyways)
-Removed: Boolean2xMaps are also now gone. (Unused Anyways)
-Added: GlobalFlags that are shared across packages.
2022-12-05 07:10:53 +01:00
Speiger cc87cae145 Implemented Lists can now be Disabled
Each List implementation can be now turned off.
Or all Lists can be turned off.
ListIterator can't be turned of since it is a IndexBasedIterator and not
a List and a few implementation use them.
2022-12-05 00:01:53 +01:00
Speiger f53d61a5bc Fixed bug in the module generator config. 2022-12-04 08:00:31 +01:00
Speiger b29874189c Finishing Framework and making Async Module Optional. 2022-12-04 07:56:04 +01:00
Speiger 6722f399db Implemented Module Configuration 2022-12-02 22:36:34 +01:00
Speiger 8e39acef45 Finished adding the modules and implemented sorting algorythm.
That this works is crazy...
2022-11-29 14:33:16 +01:00
Speiger b065ebe9ba Start of the Modularization
-Added: Base code to modularize the project.
-Added: Javas Base Module.
2022-11-27 13:16:21 +01:00
Speiger 25a7cd060a The beginning of modulizing Primitive Collections.
This will be a small rewrite of the "Builder".
Why this is done?
Basically this library is really big and having the Option to turn of
modules that you don't need is really important.

And right now enabling/disabling modules is really difficult since
everything is merged together.

So the first goal is to modularized where everything can be turned on or
of as wished.
2022-11-21 06:45:07 +01:00
Speiger 21f330260e The beginning of modulizing Primitive Collections.
This will be a small rewrite of the "Builder".
Why this is done?
2022-11-21 06:39:22 +01:00
Speiger 99e9afe7b1 Started working on the next patch.
- Added: ISizeProvider interface (Optimization Helper)
- Added: ISizeProvider into most Iterable implementations
(Distinct/Filter/FlatMap/ArrayFlatMap don't support it, for obvious
reasons)
- Added: ToArray function into Iterable which uses ISizeProvider to
reduce overhead of duplicating arrays.
- Fixed: putIfAbsent now replaces defaultValues
2022-11-20 08:56:23 +01:00
Speiger 5118ae8b1f Small Publish action fix.
-Fixed: Renamed files to junit_files since the original is deprecated
2022-07-16 05:00:50 +02:00
Speiger 03b23f0e3c Added New Iterable Feature
-Added: Repeating iterable/iterator/async feature which allows to repeate collections a desired amount of times.
-Added: Tests for it.
2022-07-16 05:00:22 +02:00
Speiger ca33c9eb9e Upgrading to TestResult Publish V2 2022-07-16 04:02:45 +02:00
Speiger b4374fdd4d Reverting build scripts because done with it. 2022-07-06 16:47:15 +02:00
Speiger 455ee64a88 Trying to expand memory a bit. 2022-07-06 14:44:06 +02:00
Speiger 893b371017 Adding a forced delay 2022-07-05 07:51:49 +02:00
Speiger 5fa9baae7a Testing Ram limits 2022-07-04 22:55:27 +02:00
Speiger 70d565c785 Hopefully Fixed setup-java 2022-07-04 22:02:49 +02:00
Speiger 5c27e332cd Attempt on including testruns. 2022-07-02 07:43:18 +02:00
Speiger f2d919bae5 Undoing what i have done so unit tests pass again. 2022-06-21 13:20:34 +02:00
Speiger 7d4c77332b Testing Improved Test Info Generator 2022-06-21 12:08:44 +02:00
Speiger d1186d4f82 Updated Readme and disabled auto sync. 2022-06-14 10:44:02 +02:00
Speiger 3b8a02ac51 Fixed syncer 2022-06-14 09:33:10 +02:00
Speiger f356d4ab57 Trying out a auto sync between branches. 2022-06-14 09:23:20 +02:00
Speiger b07bc85114 Oops 2022-06-14 09:04:38 +02:00
Speiger c37746cd84 Small Fixes
-Fixed: Unit Test Generator is now Silenced. Because 4k lines.
-Fixed: Reverted to JavaSetupV1 for build because java14 support.
-Added: Java18 support for build
2022-06-14 09:03:20 +02:00
Speiger fb7c417394 Refactor of the Project on Github.
-Added: Better System to decide what code should be generated.
-Added: Unit Tests are now included in the main branch.
-Added: Automatic System to test the main branch and ensure that tests work.
-Added: Pull Requests now run tests too.
2022-06-14 08:55:40 +02:00
Speiger ddc58ee221 Version 0.7.0 Release
- Added: Coverage Badge
- Updated: Changelog.
- Changed: Changelog now has info how to obtain the sourcecode.
- Added: Over 11 Million Unit Tests to this library to ensure quality.
- Added: ArrayList size constructor now throws IllegalStateException if
the size parameter is negative
- Added: EnumMap specialized forEach implementation.
- Added: AbstractMap.remove now delegates to its primitive counterpart.
- Added: ConcurrentHashMap now implements ITrimmable
- Refactor: Removed a lot of disabled code from ArraySet.
- Removed: LinkedList.addAll(index, List) now delegates to
LinkedList.addAll(index, Collection) due to no special optimization
required.
- Fixed: AbstractList.SubList.get/set/swapRemove didn't calculate their
List index Properly
- Fixed: AbstractList.SubList chains now properly if you create SubLists
within SubLists.
- Fixed: AbstractList.Iterator.add now respects
Immutable/UnmodifiableLists.
- Fixed: AbstractList.Iterator.skip/back now keep track of the last
returned value for remove function to work properly.
- Fixed: CopyOnWriteArrayList.extract/removeElements(int, int) does now
proper range checks and remove elements properly.
- Fixed: CopyOnWriteArrayList.SubList now works properly. (Reimplemented
entirely)
- Fixed: CopyOnWriteArrayList.Iterator.previous() was returning the
wrong values.
- Fixed: CopyOnWriteArrayList.Iterator.skip now skips the right amount
of elements and stops where it should.
- Fixed: LinkedList.first/last/dequeue/dequeueLast now throws
NoSuchElementException when empty instead of IllegalStateException.
- Fixed: LinkedList had an edge case where the entire reverse iterator
would break if the wrong element was removed.
- Fixed: LinkedList.extractElement now returns the correct values.
- Fixed: AbstractMap.entrySet().remove(Object) now returns true if
defaultReturnValue elements were removed.
- Fixed: ConcurrentHashMap.remove(Object, Object) checks if the type
matches before comparing against null Values.
- Fixed: LinkedHashMap.clearAndTrim() was checking the wrong value for
determining the full reset or clearing of a Map.
- Fixed: HashMap.trim/clearToTrim() was using the wrong value to
determin if something should be done.
- Fixed: HashMap now compares empty values (0) against nullKeys when
Object Variants of the type are used.
- Fixed: ImmutableMap now compares empty values (0) against nullKeys
when Object Variants of the type are used.
- Fixed: ArrayMap.iterator(key) now throws NoSuchElementException when
the element wasn't found.
- Fixed: Linked/EnumMap array constructor was creating the wrong size
values array.
- Fixed: LinkedEnumMap.getAndMoveToFirst/Last was moving elements even
if the element wasn't present.
- Fixed: AVL/RBTreeMap.getFirst/LastKey was not throwing a
NoSuchElementException if the map was empty.
- Fixed: Map.Builder wasn't throwing a IllegalStateException when
creating a negative size builder.
- Fixed: AVL/RBTreeSet.DecendingSet.subSet(from, fromInclusive, to,
toInclusive) was creating a corrupt asending subset.
- Fixed: ArraySet throws now a IllegalStateException when trying to
create it with a negative size.
- Fixed: ArraySet.addMoveToLast(key) was crashing when a key was already
present.
- Fixed: Immutable/LinkedHashSet now keep track of their iteration index
properly.
- Fixed: LinkedHashSet.moveToFirst/Last(key) would crash if the Set was
empty.
- Fixed: LinkedHashSet.clearAndTrim() was checking the wrong value for
determining the full reset or clearing of a Map.
- Fixed: HashSet.trim/clearToTrim() was using the wrong value to
determin if something should be done.
2022-06-12 14:31:45 +02:00
Speiger 8b5e5a75c1 Fixing bugs found when implementing Bidirectional Iterator unit tests.
-Fixed: AbstractList/ImmutableList/ArraySet/ArrayMap skip/back implementation was causing crashes and didn't update the last returned value.
-Fixed: ArraySet/ArrayMap previous was not subtracting before returning value.
-Fixed: BidirectionalIterator back was calling the object variant instead of the TypeSpecific Variant.
-Fixed: TreeSets/Maps Iterator now fully supports backwards Iterating.
-Added: Specialized skip/back function to improve speed in ImmutableHashSet/LinkedHashSet/CustomLinkedHashSet
2022-06-04 21:05:31 +02:00
Speiger c1862e6b05 Fixed a Few bugs with wrappers.
-Fixed: Maps.EmptyMap getOrDefault wasn't returning the default value.
-Fixed: Maps.UnmodifiableMap wasn't respecting the change of the DefaultReturnValue. (it has its own version of it so it doesn't break the rule of it, it wasn't returning the expected value.
-Added: A bunch of Methods for Unmodifiable/Empty maps that should fail right out but would still make it past a certain point. (Compute/Merge/Supply/etc)
2022-06-03 14:38:56 +02:00
Speiger c2c2780967 MapFixes found with unit tests.
-Fixed: CollectionWrapper.equals wasn't accounting for self.
-Fixed: MapWrapper.get didn't account for that it was a wrapper.
-Fixed: UnmodifiableMapWrapper was linking to synchronized maps due to a unknown reason.
-Added: UnmodifiableMapWrapper now has a lot more functions it right out says unsupported instead of indirect ways.
2022-06-02 16:21:48 +02:00
Speiger 086d933a0d Small fixes
-Fixed: NavigableMap.keySet is now a NavigableSet instead of a sorted one.
-Started: Trying to fix wrappers.
2022-06-02 14:59:39 +02:00
Speiger 1e7da394a1 Fixed Bugs found with wrapper collections
-Fixed: AbstractCollection.Iterator was changing the pointer before validating if the element was added. (ImmutableLists)
-Fixed: EmptyCollections containsAll should return true if it is compared against a emptyCollection.
-Fixed: HashCode/Equals for the Lists/Sets implementations.
-Changed: Iterators.wrap(Array) allows now dynamic arrays.
2022-06-02 11:02:19 +02:00
Speiger 9eb220194f Small fixes.
-Fixed: SynchronizedList.addElements was calling itself.
-Fixed: UnmodifiableSet was missing implementations that were causing fails.
2022-06-02 00:47:05 +02:00
Speiger 0baf141e4b New Snapshot builds xD 2022-05-31 19:27:20 +02:00
Speiger 569d4f5c86 Small Fix
-Fixed: HashSet/Map.reduce was using size to iterate instead of array.length
2022-05-31 19:05:54 +02:00
Speiger 36f24731b0 Script Cleanup, Changelog Update and Version Bump.
Added a lot of Unit Tests (Roughly 7 Million) to Primitive Collections
(look Debug Branch) and found some really big bugs.
The Coverage was moved from 1.9% to 34.7% so far.
So there were a lot bugs found and at this point I need a break so this
is the first fixpatch until new tests are added.
2022-05-28 23:34:37 +02:00
Speiger ea5ace0166 Another Coverage Try 2022-05-28 20:32:15 +02:00
Speiger 663809ff27 another test 2022-05-28 20:11:00 +02:00
Speiger 5d6adb5446 Added Jacoco reports 2022-05-28 19:58:54 +02:00
Speiger 212ad1ace2 Add coverage test 2022-05-28 19:56:28 +02:00
Speiger 707827929a Updated version number 2022-05-27 21:21:11 +02:00
Speiger 27ad01657d More fixes from Unit tests.
-Fixed: ArrayList/LinkedList extractElements crashing when 0 or less elements are desired.
-Fixed: TreeMap pollFirst/LastKey should return the defaultMin/max value instead of a Empty value.
-Fixed: TreeMap keySet implementation was missing the class type implementations to pass keySet tests.
-Fixed: TreeMap.SubMap Iterator (primitive Keys) was crashing because a Null was set on to a primitive.
2022-05-27 16:35:41 +02:00
Speiger e7da7acc08 Started massive unit testing library these are the first fixes.
-Fixed: AbstractCollection.retainAll didn't push removed values through the consumer.
-Fixed: AbstractCollection.toArray wouldn't reset the last entry if the input array was larger then the elements in the collection.
-Fixed: SubList didn't check for ranges properly or didn't use parent list to validate changes.
-Fixed: ArrayList.addElements didn't check input array fully and used the wrong variable to move the elements around.
-Fixed: LinkedList.addElements(EmptyInput) would crash.
-Fixed: LinkedList.swapRemove didn't account for if the removed element was the prelast one.
-Fixed: LinkedList.removeElements would break the implementation if the list was almost empty and the middle element was removed.
-Fixed: LinkedHashSet.addAndMoveToFirst wouldn't move elements to the first place.
2022-05-22 15:13:29 +02:00
Speiger 72943cb22c Added AsyncBuilder for arrays 2022-05-17 18:10:07 +02:00
Speiger 068cc4a4f7 Fixed Bugs with Java Iterators not throwing the correct exception. 2022-05-17 10:45:05 +02:00
Speiger 31b34f5de1 Fixed a couple bugs.
-Fixed: TreeMap.subMap().entrySet().remove() wouldn't check primitives properly.
-Fixed: SortedMap.sub/tail/headMap were looping into themselves.
-Changed: LinkedList.addBulk variable definition was triggering a false positive.
2022-05-17 07:59:57 +02:00
Speiger 4bded1af80 Small Change to sorting
-Added: Array only sorting function return the inputed array. This was done to allow for static final references to use the method in one go without having to make lambda wrappers. Cleaner code.
2022-04-30 22:41:11 +02:00
Speiger 100c7fe260 Updated yml Scripts
-Updated: yml Scripts and ReadMe since the gradle setup no longer requires 2 commands to work properly.
2022-04-28 19:09:42 +02:00
Speiger 04f628fcc1 Pushing release 2022-04-21 17:41:50 +02:00
Speiger 5fa26bfbf3 New Stuff
-Fixed: Crash with FIFOQueues peek function when loops were applied.
-Fixed: FIFOQueues clean function was doing unessesary extra work.
-Added: Stream Overrides functions now support sorted.
-Updated: Changelog.
-Added: A couple more badges because why not.
2022-04-21 17:25:23 +02:00
Speiger 4d3eaaf604 removing SolarCloud code 2022-04-19 17:16:53 +02:00
Speiger 33ab7c48bf hopefully doesn't filter anything. Last attempt 2022-04-19 17:10:57 +02:00
Speiger 1f4784c75e Testing single task requirement 2022-04-19 16:58:22 +02:00
Speiger ae5a4ea818 Readded caching 2022-04-19 16:33:16 +02:00
Speiger 5d66f7b453 Update Code compare 2022-04-19 16:11:20 +02:00
Speiger 12af656150 Updated Task Name 2022-04-19 16:04:20 +02:00
Speiger 603ff3df0f Fixed code compare 2022-04-19 15:52:41 +02:00
Speiger b712981718 Code quality test 2022-04-19 15:51:01 +02:00
Speiger 863d1a1027 Updated Readme. 2022-04-18 21:41:55 +02:00
Speiger 6287da8efe ReadMe restructuring. 2022-04-18 09:06:32 +02:00
Speiger c27e852ccb typo missed by the spellchecker... 2022-04-17 20:39:06 +02:00
Speiger 0157765628 Hotfix. 2022-04-17 20:24:31 +02:00
Speiger 8689037ceb Documentation update 2022-04-17 20:21:48 +02:00
Speiger 03a8914986 Completion Jitpack Integration 2022-04-14 09:51:36 +02:00
Speiger 26b9d6706d Fixed Classification 2022-04-14 09:34:16 +02:00
Speiger a21d4a9eb6 Fixing Jitpack Publishing 2022-04-14 09:24:13 +02:00
Speiger 212d614ebd Trying switching Jitpack Java version. 2022-04-14 09:19:27 +02:00
Speiger bcba3ccde0 Another try 2022-04-14 09:13:53 +02:00
Speiger b42ad76372 Jitpack Test 2022-04-14 09:12:03 +02:00
Speiger f95565771a 0.6.0 Release Commit 2022-04-14 07:56:39 +02:00
Speiger 3d6cbf5ac1 Finishing touches for the 0.6.0 release. 2022-04-14 07:29:32 +02:00
Speiger fc5d43e14b Updated Changelog. 2022-04-14 06:06:35 +02:00
Speiger b3264748cd Added Author to module Info. 2022-04-14 05:47:19 +02:00
Speiger ede40f06d0 Small fixes.
-Fixed: Some imports.
-Fixed: containsValue for OpenHashMap was not checking nullEntry properly
2022-04-14 05:37:41 +02:00
Speiger e69c675f82 Fixed bug with linux & cleanup of the java version detector 2022-04-13 02:58:35 +02:00
Speiger 17c1c90957 Added Module-Info support 2022-04-13 02:54:03 +02:00
Speiger c6f2f71f6c Switching to a new lock to make read operations actually concurrent. 2022-04-13 02:00:52 +02:00
Speiger d2c7c151bc Implemented tests.
-Added: ConcurrentHashMap test suite
-Fixed: Bugs found in ConcurrentHashMaps with the iterator and clear function.
2022-04-12 08:08:17 +02:00
Speiger 4448eca787 Finishing ConcurrentHashMap implementation, next tests 2022-04-12 06:06:18 +02:00
Speiger 0350a77dff Finishing the ConcurrentMap implementation
This though isn't finishing the rest because the interface still needs
to be implemented.
-Added: Rest of the ConcurrentMap implementation
-Fixed: LinkedOpenHashMap Count method was returning prematurely.
2022-04-11 03:20:28 +02:00
Speiger 4f98c599df New Features.
-Added: ArrayList.of(CLASS, size) function that allows to preallocate
the size of the generic list.
-Updated: ListTests suppressors only suprres what they need to.
-Added: Start of the ConcurrentHashMap implementation (based on Guavas
implementation)

Note that the ConcurrentHashMap implementation is just started not
finished and still needs a lot of work, but the base code - any
subclasses is technically finished.
The implementation is also using linkedMaps to have faster iteration
performance.
2022-04-10 15:49:16 +02:00
Speiger 103b2407d2 Updated Changelog 2022-04-09 04:23:57 +02:00
Speiger 059da9be12 New Features
-Added: CopyOnWriteArrayList
-Added: UnitTests for CopyOnWriteArrayLists
2022-04-09 03:56:54 +02:00
Speiger 3cac3a997e And hopefully the last javaDoc fixes 2022-04-08 00:24:37 +02:00
Speiger 3ffb001c73 More fixes
-Fixed: More javadoc fixes
-Fixed: BaseIntIterableTest no longer uses a deprecated function
2022-04-08 00:13:51 +02:00
Speiger 2810a6f952 Small fixes
-Fixed: JavaDoc
-Changed: callback is now called onCompletion
2022-04-08 00:00:45 +02:00
Speiger 166362334b Added gradle now uses the java version it runs on. 2022-04-07 20:26:53 +02:00
Speiger d3f9103ebf Adding Logger for testings 2022-04-07 20:19:47 +02:00
Speiger 1c5507d4a5 Switching to local properties 2022-04-07 20:16:59 +02:00
Speiger 5e3cd9f484 Trying out something with github actions 2022-04-07 20:14:23 +02:00
Speiger f27b884b6a Moving to a more up to date system.
-Changed: Moved to gradle 7.3 for java17 support of the project.
-Changed: Moved to maven-publish plugin and added a bit more info to the poms.
2022-04-07 16:00:58 +02:00
Speiger 769a9d8ea5 Upgraded gradle version to 6.3 for a test 2022-04-07 13:32:04 +02:00
Speiger 9499a7f0e0 Setup matrix so multiple java versions are tested. 2022-04-07 13:27:12 +02:00
Speiger ca38583f96 Fixed Java11 Incompat 2022-04-07 00:14:09 +02:00
Speiger 9557a0cfb5 Merge branch 'master' of git@github.com:Speiger/Primitive-Collections.git 2022-04-07 00:08:05 +02:00
Speiger c61e8bb7fc New Features
- Added: addOrGet for sets.
- Added: Async API which allows to easily execute Iterables/Collections
offthread without the complexity.
2022-04-07 00:06:49 +02:00
Speiger 4fbcaa47f8 Update README.md 2022-04-07 00:06:19 +02:00
Speiger 6f31fc5abb New Features
- Added: addOrGet for sets.
- Added: Async API which allows to easily execute Iterables/Collections
offthread without the complexity.
2022-04-07 00:04:52 +02:00
Speiger 10ec0e35d0
Update README.md 2021-12-27 21:29:51 +01:00
Speiger 29c4d253cf Disabling badge 2021-12-27 21:28:07 +01:00
Speiger 55c870d392 Adding Build Passing badge 2021-12-27 21:22:57 +01:00
Speiger cda6ec066d
Adding Build Validation
Create gradle.yml
2021-12-27 20:38:55 +01:00
Speiger c8b89e6847 Renaming file for what the action did. 2021-12-27 20:34:15 +01:00
Speiger b46a739008 Finishing Build Action 2021-12-27 20:28:45 +01:00
Speiger af62394651 Testing for secrets to be present 2021-12-27 20:04:50 +01:00
Speiger bc75a5cd97
Merge pull request #8 from Speiger/master
Moving Credentials Away from this file because now i need it
2021-12-27 20:00:58 +01:00
Speiger 7d8af3af08 Moving Credentials Away from this file because now i need it 2021-12-27 19:59:19 +01:00
Speiger ef8197b275
Update gradle.yml 2021-12-27 19:24:16 +01:00
Speiger 58d9ac7fdd
Update gradle.yml 2021-12-27 19:21:59 +01:00
Speiger 86a893916d
Update gradle.yml 2021-12-27 19:20:06 +01:00
Speiger e7a6292fe9
Update gradle.yml 2021-12-27 19:10:09 +01:00
Speiger 81209b0cf8
Update gradle.yml 2021-12-27 19:07:05 +01:00
Speiger 77b8147f9b
Update gradle.yml 2021-12-27 19:05:33 +01:00
Speiger 32abd7e14e
Update gradle.yml 2021-12-27 19:04:33 +01:00
Speiger 8a582f0b08
Create gradle.yml 2021-12-27 19:01:45 +01:00
Speiger 01be6f7d43
Delete gradle.yml 2021-12-27 18:56:49 +01:00
Speiger 9011700879
testing build only 2021-12-27 18:55:51 +01:00
Speiger 12cd4e5e5f
Update gradle.yml 2021-12-27 18:50:24 +01:00
Speiger 10201bcadb
hopefully this works 2021-12-27 18:44:47 +01:00
Speiger 3b4fcc0069
Merge pull request #6 from Speiger/actions
Update gradle.yml
2021-12-27 18:32:51 +01:00
Speiger 31a1ef74fb
Update gradle.yml 2021-12-27 18:32:23 +01:00
Speiger 9be3a1475f
another build try 2021-12-27 18:29:54 +01:00
Speiger 7591f396fd
getting into github actions
Adding a build test
2021-12-27 18:18:24 +01:00
Speiger 517b84042f Version Bump 2021-12-26 01:58:22 +01:00
Speiger dbfae33074 Fixed Bug with FIFOQueue performance 2021-12-26 00:08:19 +01:00
Speiger ead34009c6 Added pourAsList and pourAsSet 2021-12-25 20:27:33 +01:00
Speiger 7a4a0f05d1 Added subFrom (which is the inverse of addTo) 2021-12-25 20:07:37 +01:00
Speiger 8f32b2d887 Merge remote-tracking branch 'Github/master' 2021-12-25 03:49:45 +01:00
Speiger e6c9600b40 Refactoring and Deprecating SortedMap/Sets
- Added: OrderedMap/Set
- Added: All Relevant functions into Ordered interface
- Changed: Marked all Relevant SortedMap/Set functions Deprecated until
0.6.0
- Fixed: All code that was relevant to this
2021-12-25 03:46:51 +01:00
Speiger d5f2acb9ab
Update LICENSE 2021-12-19 21:19:28 +01:00
Speiger 3274e96355 Added Benchmark links 2021-12-16 14:03:39 +01:00
Speiger 78dfa286b0 New Version
- Fixed: Bugs with Queues starting with the wrong size
- Fixed: ArrayGrowth for Queues was +1 instead of +50%
- Added: Benchmarks with java and FastUtil
2021-12-16 13:46:55 +01:00
Speiger f32624d131 Build Release..
-Fixed: Doc error on gradle build.
-Fixed: SubSet.iterator had a ambiguity.
2021-12-14 09:05:25 +01:00
Speiger 59a417056c Added Missing method & fixed potential issue with TreeSetBiIterators 2021-12-13 13:56:28 +01:00
Speiger 18f6704ed7 Expanded Unit tests to IntLists/Sets
Maps are sadly not possible. It will require writing templates for
permutations effectively copying guavas Test library.
So this will take a lot longer
2021-12-13 13:45:35 +01:00
Speiger caf2f22e9f Finished the Basic Unit test frame work to ensure stablity.
-Added: 150k Unit tests.
-Fixed: Reworked the NavigableSet/Map implementations of RBTree/AVLTree/Array Sets/Maps
2021-12-13 10:41:20 +01:00
Speiger 865966db55 Build Fixes & Build Release Doc update 2021-12-11 13:27:33 +01:00
Speiger 6266a6293c Slight rewording 2021-12-11 12:58:02 +01:00
Speiger 70ea60aacf More doc fixes 2021-12-11 12:57:03 +01:00
Speiger 599cc44fff Fixed Doc 2021-12-11 12:56:01 +01:00
Speiger e1df59d512 We are now to 16k tests. Fixed loads of issues.
-Added: Tests for Lists and Sets.
-Fixed: SubLists are now stable (they weren't before)
-Fixed: All the bugs that the unit tests found so far.
-Updated: ReadMe/Changelog
2021-12-11 12:53:58 +01:00
Speiger eaa45976c7 Added over 6k tests...
- Fixed: ImmutableMaps issues thanks to the tests. Roughly the same as
the rest of the maps
- Fixed: RB/AVLTreeMaps issues. Roughly the same as the rest of the maps
2021-12-10 10:55:16 +01:00
Speiger 362838c434 Found something in FastUtil i needed to address too. BulkFixes coming.
- Added: Guava TestSuit
- Fixed: HashCode and toString method would crash if the Object
Key/Value was null
- Added: AbstractTypeCollection now delegates the contains check to
type-specific Collections if it detects it.
- Fixed: Map.Entry toString wasn't writing values not like it should do.
- Fixed: Set.hashCode now is the sum of the elements instead of a Unique
HashCode based on the elements.
- Fixed: Added missing NonNull Checks.
- Fixed: OpenHashMap.containsValue implementation was wrong.
- Fixed: OpenHashMap.compute/present/absent now works how it is
specified in the Java Documentation
- Fixed: OpenHashMap.merge/BulkMerge now works how it is specified in
the Java Documentation
- Fixed: OpenHashMap.keySet.remove was causing a infinite loop.
- Fixed: OpenHashMap.mapIterator now no longer crashes in certain cases.
2021-12-10 05:54:37 +01:00
Speiger 52caa9cdd2 Doc Update 2021-12-10 03:27:33 +01:00
Speiger c6afdb8762 New Tests & Fixes
- Added: Tests for the new Stream replace functions to ensure no bugs
are left.
- Fixed: Custom HashSet reduce function with a default value was
checking incorrectly for present keys.
2021-12-10 03:23:28 +01:00
Speiger c4964806f0 Loads of new features & fixes.
- Added: pour function directly into Iterable which allows to collect
all elements in the Iterable directly.
- Added: The new ToArray method from Java9 and newer into the library.
Using a functional interface. (Just a backport)
- Changed: Reworked how the Map Builder functions are created. They are
now in a SubClass that moves them out of the way. Less Clutter. (This
might break things if that was used before)
- Added: Map Builder that allows now to Build Maps like Guava
ImmutableMaps can be build. Note: This has a slight performance
overhead.
- Added: Unmodifiable and Synchronize wrapper functions direclty into
Collection Interfaces. This is mostly a quality of life thing.
- Added: Unmodifiable and Synchronized Wrapper Collections can now be
cloned. They clone the underlying map which doesn't break functionality.
(Had a usecase for it)
- Added: A boxed putAll array variant.
- Fixed: EnumMaps didn't keep track of their size and now got proper
care and implementations as needed. There might be more work required
but at least the core functionality is now up to date.
2021-12-09 09:14:55 +01:00
Speiger a2506b927a New Small feature
- Added: 2 Helper functions to find out how many bits are required to
store a Number.
2021-11-30 16:20:08 +01:00
Speiger 53061d9f78 Update Readme 2021-10-30 07:42:13 +02:00
Speiger 2555dc7fc2 Version Bump 2021-10-30 07:39:05 +02:00
Speiger 058087e15a Added some more tests for some of the new functions 2021-10-30 07:21:41 +02:00
Speiger c20c6393e5 Added Better forEach support for Iterables. 2021-10-29 17:23:52 +02:00
Speiger a25ec85ba2 Added Reduce function to all implementations 2021-10-29 16:03:39 +02:00
Speiger c930bda7a6 Added RemoveSwap 2021-10-28 23:39:19 +02:00
Speiger e7bc242292 Fixes and a lot new features. But still WIP
- Fixed: Supplier get function wasn't referencing original function.
- Added: addIfPresent/Absent to lists
- Added: distinct, limit and peek iterators
- Added: Iterable's can now reduce its contents
2021-10-27 14:09:19 +02:00
Speiger b90a9ec7d8 Added new Remove/RetainAll function to Collection 2021-10-24 01:11:11 +02:00
Speiger d18a35d9b7 Updated Readme to include deprecation of to old patches to save on space 2021-10-11 19:39:09 +02:00
Speiger ffc34a131f New Features and Bugfixes.
- Fixed: ObjectArrayList.of was causing crashes because of a Poor
implementation.
- Added: Unsorted HashMaps/Sets now throw Concurrent exceptions if they
were modified during a rehash.
- Added: Array/Collection version of enqueue and enqueueFirst to
PriorityQueues.
- Added: fillBuffer function into PrimitiveLists which allow to optimize
JavaNio buffers if needed.
2021-10-11 19:05:54 +02:00
Speiger a38e7b069a More Consistency and more features.
- Added: Wrapper now support the Optimized Lambda replacer functions to
improve performance.
- Added: FIFO Queue has now a minimum capacity and that is now checked
more consistently.
- Added: Changelog & Version Bump
2021-10-07 14:50:52 +02:00
Speiger 9c15980e68 Version Bump 2021-10-06 20:53:19 +02:00
Speiger a05689017e Update information 2021-10-06 20:33:11 +02:00
Speiger 54c9660145 New Tests & BugFixes
-Added: Tests for the Copying of Collections.
-Fixed: PriorityQueues didn't have hashCode/equals/toString implemented
2021-10-06 20:23:40 +02:00
Speiger 0c4ef7f6c4 Queues are now copyable 2021-10-06 18:47:15 +02:00
Speiger 61d7a88c82 Maps are now copyable too 2021-10-06 18:32:10 +02:00
Speiger dff173222d Collections are now copyable 2021-10-06 17:48:37 +02:00
Speiger 6eded1f4be Sets are now Copyable 2021-10-06 17:43:46 +02:00
Speiger 07b715dd4c Start of adding copyable collections. Starting with Lists. 2021-10-06 16:30:53 +02:00
Speiger 1f1aa995df Release of 0.4.1 2021-09-29 02:11:29 +02:00
Speiger 1e2703acf2 ChangelogUpdate 2021-09-28 13:23:41 +02:00
Speiger 07abba6312 New Features
-Added: Count method for Iterable
-Fixed: A couple of bugs with the new stream removers not working well in LinkedCollections
2021-09-28 13:20:05 +02:00
Speiger 3f872463b6 Fixed a Rule Break with the new ComputeIfAbsent function 2021-09-28 12:26:44 +02:00
Speiger 531443531d New Features.
-Added: Suppliers.
-Added: ComputeIfAbsent but value generator is a supplier
2021-09-28 12:06:51 +02:00
Speiger 49c5e9eadd Changes.
-Added: New Changes to Changelog
-Fixed: Missing Autogenerated Type
-Cleanup: Gradle file got cleaned up from things it no longer needs.
-Changed: Upgraded to SCG 1.0.5 for incoming new features.
2021-09-28 04:43:04 +02:00
Speiger 3c5769e0e2 Added java type support for Stream replacer functions. 2021-09-28 03:34:53 +02:00
Speiger 0e061921e9 New Features and improvements
-Added: addAll array function.
-Fixed: forEach with input now puts the input at the key instead of value
2021-09-28 03:23:21 +02:00
317 changed files with 74153 additions and 31616 deletions

View File

@ -38,7 +38,7 @@
<attribute name="gradle_used_by_scope" value="builder"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>

33
.github/workflows/build_action.yml vendored Normal file
View File

@ -0,0 +1,33 @@
name: Latest Build
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
strategy:
fail-fast: false
matrix:
jdk: [8, 11, 16, 17, 18, 19, 20]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK ${{ matrix.jdk }}
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ matrix.jdk }}
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew build

View File

@ -0,0 +1,84 @@
name: Unit Tests
on:
push:
branches: [ master ]
jobs:
build:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11
cache: gradle
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build and Test
run: |
./gradlew generateTestSource test jacocoTestReport --info -Dfull_test_suite=true
./gradlew --stop
- name: Publish Test Result
uses: EnricoMi/publish-unit-test-result-action@v2
id: test-results
if: always()
with:
junit_files: build/test-results/**/*.xml
fail_on: nothing
ignore_runs: true
json_thousands_separator: .
time_unit: milliseconds
- name: Create Badge Color
shell: bash
run: |
case ${{ fromJSON( steps.test-results.outputs.json ).conclusion }} in
success)
echo "BADGE_COLOR=31c653" >> $GITHUB_ENV
;;
failure)
echo "BADGE_COLOR=800000" >> $GITHUB_ENV
;;
neutral)
echo "BADGE_COLOR=696969" >> $GITHUB_ENV
;;
esac
- name: Create Test Badge
uses: emibcn/badge-action@v1.2.4
with:
label: Tests
status: '${{ fromJSON( steps.test-results.outputs.json ).conclusion }}, Passed: ${{ fromJSON( steps.test-results.outputs.json ).formatted.stats.tests_succ }}, Skipped: ${{ fromJSON( steps.test-results.outputs.json ).formatted.stats.tests_skip }}, Failed: ${{ fromJSON( steps.test-results.outputs.json ).formatted.stats.tests_fail }}'
color: ${{ env.BADGE_COLOR }}
path: tests.svg
- name: Create Coverage Badge
id: jacoco
uses: cicirello/jacoco-badge-generator@v2
with:
jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csv
badges-directory: null
intervals: 95 80 70 60 50 0
- name: Upload Test Badge
uses: exuanbo/actions-deploy-gist@v1
with:
token: ${{ secrets.GIST_TOKEN }}
gist_id: 280257cd19cbe1dda3789bebd4ff65cf
file_path: tests.svg
- name: Upload Coverage Badge
uses: exuanbo/actions-deploy-gist@v1
with:
token: ${{ secrets.GIST_TOKEN }}
gist_id: 280257cd19cbe1dda3789bebd4ff65cf
file_path: jacoco.svg

27
.github/workflows/coverage.yml vendored Normal file
View File

@ -0,0 +1,27 @@
name: Measure coverage
on:
push:
branches: [ debug ]
jobs:
build:
name: Code Quality Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 11
cache: 'gradle'
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build and analyze
run: ./gradlew generateTestSource test jacocoTestReport --info -Dfull_test_suite=true
- name: Upload to CodeCov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: bash <(curl -s https://codecov.io/bash) -t $CODECOV_TOKEN

View File

@ -0,0 +1,39 @@
name: Unit Tests
on:
pull_request:
branches: [ master ]
jobs:
build:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11
cache: gradle
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build and Test
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew generateTestSource test jacocoTestReport --info -Dfull_test_suite=true
- name: Publish Test Result
uses: EnricoMi/publish-unit-test-result-action@v2
id: test-results
if: always()
with:
junit_files: build/test-results/**/*.xml
fail_on: nothing
ignore_runs: true
json_thousands_separator: .
time_unit: milliseconds

15
.gitignore vendored
View File

@ -1,7 +1,6 @@
# ---> Gradle
.gradle
/build/
gradle.properties
# Ignore Gradle GUI config
gradle-app.setting
@ -31,5 +30,19 @@ gradle-app.setting
/src/main/java/speiger/src/collections/doubles/*
/src/main/java/speiger/src/collections/objects/*
#Generated Tests
/src/test/java/speiger/src/testers/booleans/*
/src/test/java/speiger/src/testers/bytes/*
/src/test/java/speiger/src/testers/shorts/*
/src/test/java/speiger/src/testers/chars/*
/src/test/java/speiger/src/testers/ints/*
/src/test/java/speiger/src/testers/longs/*
/src/test/java/speiger/src/testers/floats/*
/src/test/java/speiger/src/testers/doubles/*
/src/test/java/speiger/src/testers/objects/*
/src/test/java/speiger/src/tests/*
#Cache result
/src/builder/resources/speiger/assets/collections/cache.bin
/src/builder/resources/speiger/assets/testers/cache.bin
/src/builder/resources/speiger/assets/tests/cache.bin

View File

@ -1,5 +1,222 @@
# Changelog of versions
### Version 0.9.0
- Added: getFirst/getLast/removeFirst/removeLast to List.class.
- Added: Dedicated Set toArray implementations.
- Added: ToArray/pushTop functions to Stack.class.
- Added: ComputeNonDefault functions which will contain the current behavior of the Compute function, while the Compute will be changed to be more java compliant!
- Added: List.reversed, which returns a SubList that has all elements in reversed order and also inserts reversed.
- Added: Iterators.infinite as an option that will create a Infinite Iterator based on the inputed one.
- Added: List.indexedIterator which allows you to create a iterator with a customized iteration indecies. Useful if you want to transform lists output.
- Added: PriorityQueue.contains is now a function
- Added: Iterators/Async Builders now support MapToPrimitiveType function on the object variant. So more processing can be done. (Will be expanded upon later versions)
- Fixed: SetValue wasn't working on forEach implementations.
- Fixed: Compute functions now perform with primitives more java compliant. Meaning that getDefaultReturnValue function no longer is seen as null.
- Fixed: Supplier was using the wrong dataType in their function name.
- Updated: SimpleCodeGenerator 1.3.0 is now being used which allows for iterative code support.
- Breaking Change: Map.compute/IfAbsent/Present and Map.supplyIfAbsent if the value is a primitive it will no longer accept the defaultReturnValue() as "null" if that functionality is desired then use: computeNonDefault which contains the functionality!
### Version 0.8.1
- Added: getFirst/getLast/removeFirst/removeLast to List.class.
- Added: Dedicated Set toArray implementations.
- Added: ToArray/pushTop functions to Stack.class.
- Added: ComputeNonDefault functions which will contain the current behavior of the Compute function, while the Compute will be changed to be more java compliant!
- Added: List.reversed, which returns a SubList that has all elements in reversed order and also inserts reversed.
- Added: Iterators.infinite as an option that will create a Infinite Iterator based on the inputed one.
- Added: List.indexedIterator which allows you to create a iterator with a customized iteration indecies. Useful if you want to transform lists output.
- Added: PriorityQueue.contains is now a function
- Added: Iterators/Async Builders now support MapToPrimitiveType function on the object variant. So more processing can be done. (Will be expanded upon later versions)
- Fixed: SetValue wasn't working on forEach implementations.
- Fixed: Compute functions now perform with primitives more java compliant. Meaning that getDefaultReturnValue function no longer is seen as null.
- Fixed: Supplier was using the wrong dataType in their function name.
- Updated: SimpleCodeGenerator 1.3.0 is now being used which allows for iterative code support.
### Version 0.8.0
- Added: getFirst/getLast/removeFirst/removeLast to Lists
- Added: Dedicated implementations for toArray into TreeSets
- Fixed: forEach methods in Maps now can use "setValue" functions.
### 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)
- Added: ToArray function into Iterable which uses ISizeProvider to reduce overhead of duplicating arrays.
- Added: Functions that have the same type, Int2IntFunction as example, have now a identity function.
- Added: Functions of a BooleanValue have now alwaysTrue/False function.
- Added: ForEachIndexed for all Iterable implementations
- Added: RandomGenerator support (Java17), though requires self compilation
- Added: Optimizations for HashUtils next power of function.
- Added: toArray() now returns a cached empty array if the collection is empty.
- Added: toArray function for AsyncBuilder
- Added: Modularization to the library where feature can be disabled as needed. (Requires Self-Compilation)
- Fixed: putIfAbsent now replaces defaultValues
- Fixed: OpenHashSet/Map and their Custom Variants no longer rely on List implementations.
- Fixed: ObjectCopyOnWriteList.of did create a ObjectArrayList instead of the CopyOnWrite variant.
- Removed: BooleanSet and Maps that start with a Boolean classes since they can not be used anyways.
- Breaking Change: Function classes now use the "apply/applyAs/test" format from Java itself, instead of the "get" format. This cleans up a lot of things. But will break existing function class implementations
- Breaking Change: Classes that used PrimitiveCollection functions now default to java functions where applicable, this is to increase compat.
- Breaking Change: Some function classes now get closer to javas terms. (Predicate/UnaryOperator etc)
### Version 0.7.0
- Added: Over 11 Million Unit Tests to this library to ensure quality.
- Added: ArrayList size constructor now throws IllegalStateException if the size parameter is negative
- Added: EnumMap specialized forEach implementation.
- Added: AbstractMap.remove now delegates to its primitive counterpart.
- Added: ConcurrentHashMap now implements ITrimmable
- Refactor: Removed a lot of disabled code from ArraySet.
- Removed: LinkedList.addAll(index, List) now delegates to LinkedList.addAll(index, Collection) due to no special optimization required.
- Fixed: AbstractList.SubList.get/set/swapRemove didn't calculate their List index Properly
- Fixed: AbstractList.SubList chains now properly if you create SubLists within SubLists.
- Fixed: AbstractList.Iterator.add now respects Immutable/UnmodifiableLists.
- Fixed: AbstractList.Iterator.skip/back now keep track of the last returned value for remove function to work properly.
- Fixed: CopyOnWriteArrayList.extract/removeElements(int, int) does now proper range checks and remove elements properly.
- Fixed: CopyOnWriteArrayList.SubList now works properly. (Reimplemented entirely)
- Fixed: CopyOnWriteArrayList.Iterator.previous() was returning the wrong values.
- Fixed: CopyOnWriteArrayList.Iterator.skip now skips the right amount of elements and stops where it should.
- Fixed: LinkedList.first/last/dequeue/dequeueLast now throws NoSuchElementException when empty instead of IllegalStateException.
- Fixed: LinkedList had an edge case where the entire reverse iterator would break if the wrong element was removed.
- Fixed: LinkedList.extractElement now returns the correct values.
- Fixed: AbstractMap.entrySet().remove(Object) now returns true if defaultReturnValue elements were removed.
- Fixed: ConcurrentHashMap.remove(Object, Object) checks if the type matches before comparing against null Values.
- Fixed: LinkedHashMap.clearAndTrim() was checking the wrong value for determining the full reset or clearing of a Map.
- Fixed: HashMap.trim/clearToTrim() was using the wrong value to determin if something should be done.
- Fixed: HashMap now compares empty values (0) against nullKeys when Object Variants of the type are used.
- Fixed: ImmutableMap now compares empty values (0) against nullKeys when Object Variants of the type are used.
- Fixed: ArrayMap.iterator(key) now throws NoSuchElementException when the element wasn't found.
- Fixed: Linked/EnumMap array constructor was creating the wrong size values array.
- Fixed: LinkedEnumMap.getAndMoveToFirst/Last was moving elements even if the element wasn't present.
- Fixed: AVL/RBTreeMap.getFirst/LastKey was not throwing a NoSuchElementException if the map was empty.
- Fixed: Map.Builder wasn't throwing a IllegalStateException when creating a negative size builder.
- Fixed: AVL/RBTreeSet.DecendingSet.subSet(from, fromInclusive, to, toInclusive) was creating a corrupt asending subset.
- Fixed: ArraySet throws now a IllegalStateException when trying to create it with a negative size.
- Fixed: ArraySet.addMoveToLast(key) was crashing when a key was already present.
- Fixed: Immutable/LinkedHashSet now keep track of their iteration index properly.
- Fixed: LinkedHashSet.moveToFirst/Last(key) would crash if the Set was empty.
- Fixed: LinkedHashSet.clearAndTrim() was checking the wrong value for determining the full reset or clearing of a Map.
- Fixed: HashSet.trim/clearToTrim() was using the wrong value to determin if something should be done.
### Version 0.6.2
- Added: Array only sorting function return the inputed array. This was done to allow for static final references to use the method in one go without having to make lambda wrappers. Cleaner code.
- Added: Iterator Wrappers are now a bit more in Compliance with Java Standards.
- Added: AsyncBuilders now Support Array Inputs to create cleaner code.
- Changed: LinkedList.addBulk variable definition was triggering a false positive.
- Fixed: TreeMap.subMap().entrySet().remove() wouldn't check primitives properly.
- Fixed: SortedMap.sub/tail/headMap were looping into themselves.
- Fixed: AbstractCollection.retainAll didn't push removed values through the consumer.
- Fixed: AbstractCollection.toArray wouldn't reset the last entry if the input array was larger then the elements in the collection.
- Fixed: SubList didn't check for ranges properly or didn't use parent list to validate changes.
- Fixed: ArrayList.addElements didn't check input array fully and used the wrong variable to move the elements around.
- Fixed: LinkedList.addElements(EmptyInput) would crash.
- Fixed: LinkedList.swapRemove didn't account for if the removed element was the prelast one.
- Fixed: LinkedList.removeElements would break the implementation if the list was almost empty and the middle element was removed.
- Fixed: LinkedHashSet.addAndMoveToFirst wouldn't move elements to the first place.
- Fixed: ArrayList/LinkedList extractElements crashing when 0 or less elements are desired.
- Fixed: TreeMap pollFirst/LastKey should return the defaultMin/max value instead of a Empty value.
- Fixed: TreeMap keySet implementation was missing the class type implementations to pass keySet tests.
- Fixed: TreeMap.SubMap Iterator (primitive Keys) was crashing because a Null was set on to a primitive.
### Version 0.6.1
- Fixed: FIFO queue crashing when the last index is before the first index when peek is called.
- Fixed: FIFO queue only clears the array if it was in use.
- Added: Sorted Method for the stream replacing functions.
### Version 0.6.0
- Added: addOrGet for sets.
- Added: Async API which allows to easily execute Iterables/Collections offthread without the complexity.
- Added: CopyOnWriteArrayList and tests for it
- Added: Support up to Java17.
- Added: Build System now adds module-info if the Running JVM is 9 or higher
- Added: ArrayList.of(Class, size) that allows you to allocate a size right at the creation of the List without having to create a wrapper array.
- Added: A ConcurrentHashMap implementation.
- Fixed: containsValue in the HashMap wouldn't check the nullKey
- Removed: Deprecated functions from SortedMaps/Sets
### Version 0.5.3
- Added: OrderedMap/Set
- Added: Deprecation to Functions that are specific to Ordered interfaces in the SortedMap/Set
- Added: subFrom to Maps which is the counterpart of the addTo method
- Added: pourAsList and pourAsSet (booleans excluded for sets) to Iterable
- Fixed: ArrayList.grow had a small bug where it would trigger to early causing performance problems with exact sized collections.
- Fixed: FIFOQueue size constructor had a small bug where it would trigger a array enlargement when all elements were inserted.
### Version 0.5.2
- Fixed: Bugs with Queues starting with the wrong size
- Fixed: ArrayGrowth for Queues was +1 instead of +50%
- Added: Benchmarks with java and FastUtil
### Version 0.5.1
- Fixed: Reworked the NavigableSet/Map implementations of RBTree/AVLTree/Array Sets/Maps so they are now deemed stable.
- Added: Another 150k Unit tests.
- Added: List and Set Unit tests for Integer (or Primitives in this case) to ensure basic stability there. (Now covering all sets and lists)
- Fixed: Bugs with null values for primitive collections.
- Removed: ArraySet/Map subSet/subMap implementation was removed.
### Version 0.5.0
- Added: 2 Helper functions to find out how many bits are required to store a Number.
- Added: pour function directly into Iterable which allows to collect all elements in the Iterable directly.
- Added: The new ToArray method from Java9 and newer into the library. Using a functional interface. (Just a backport)
- Changed: Reworked how the Map Builder functions are created. They are now in a SubClass that moves them out of the way. Less Clutter. (This might break things if that was used before)
- Added: Map Builder that allows now to Build Maps like Guava ImmutableMaps can be build. Note: This has a slight performance overhead.
- Added: Unmodifiable and Synchronize wrapper functions direclty into Collection Interfaces. This is mostly a quality of life thing.
- Added: Unmodifiable and Synchronized Wrapper Collections can now be cloned. They clone the underlying map which doesn't break functionality. (Had a usecase for it)
- Added: A boxed putAll array variant.
- Fixed: EnumMaps didn't keep track of their size and now got proper care and implementations as needed. There might be more work required but at least the core functionality is now up to date.
- Added: Tests for the new Stream replace functions to ensure no bugs are left.
- Fixed: Custom HashSet reduce function with a default value was checking incorrectly for present keys.
- Added: Guava TestSuit
- Fixed: HashCode and toString method would crash if the Object Key/Value was null
- Added: AbstractTypeCollection now delegates the contains check to type-specific Collections if it detects it.
- Fixed: Map.Entry toString wasn't writing values not like it should do.
- Fixed: Set.hashCode now is the sum of the elements instead of a Unique HashCode based on the elements.
- Fixed: Added missing NonNull Checks.
- Fixed: Custom/OpenHashMap.containsValue implementation was wrong.
- Fixed: Custom/OpenHashMap.compute/present/absent now works how it is specified in the Java Documentation
- Fixed: Custom/OpenHashMap.merge/BulkMerge now works how it is specified in the Java Documentation
- Fixed: Custom/Linked/OpenHashMap.keySet.remove was causing a infinite loop.
- Fixed: Custom/Linked/OpenHashMap.entrySet.contains was not correctly comparing the entry.
- Fixed: Custom/OpenHashMap.mapIterator now no longer crashes in certain cases.
- Added: Custom/LinkedOpenHashMap now takes use of the improved Iterator it has for containsValue
- Fixed: CustomOpenHashMap.keySet.forEach was basically putting out keys even if they were present
- Fixed: ImmutableMaps issues thanks to the tests. Roughly the same as the rest of the maps
- Fixed: RB/AVLTreeMaps issues. Roughly the same as the rest of the maps
- Fixed: SubLists are now properly implemented.
- Fixed: HashSet Iterator bugs now fixed... That was Painful.
- Added: Tests for Lists and Sets
### Version 0.4.5
- Added: removeAll/retainAll(Collection c, Consumer r) which receives all the elements that got deleted from the collection
- Fixed: Supplier get function wasn't referencing original function.
- Added: addIfPresent/Absent to lists
- Added: distinct, limit and peek iterators
- Added: Iterable's can now reduce its contents
- Added: Better ForEach support for IterableWrappers so a Iterator chain is not created
- Added: SwapRemove to Lists which moves the last element into the desired space to be deleted
- Added: More Test cases
### Version 0.4.4
- Fixed: ObjectArrayList.of was causing crashes because of a Poor implementation.
- Added: Unsorted HashMaps/Sets now throw Concurrent exceptions if they were modified during a rehash.
- Added: Array/Collection version of enqueue and enqueueFirst to PriorityQueues.
- Added: fillBuffer function into PrimitiveLists which allow to optimize JavaNio buffers if needed.
### Version 0.4.3
- Added: Wrapper now support the Optimized Lambda replacer functions to improve performance.
- Added: FIFO Queue has now a minimum capacity and that is now checked more consistently.
### Version 0.4.2
- Added: Lists/Sets/Maps/PriorityQueues are now copy-able. with the new copy() function.
Note: subLists/subMaps/subSets or synchronize/unmodifyable wrappers do not support that function.
- Fixed: PriorityQueues didn't implement: hashCode/equals/toString
### Version 0.4.1
- Changed: ForEach with input now provides input, value instead of value, input, this improves the usage of method references greatly
- Added: addAll with Array-types in collections.
- Added: Java Iterator/Iterable support for Stream replacing methods
- Added: Suppliers.
- Added: SupplyIfAbsent. It is ComputeIfAbsent but using suppliers
- Added: Count feature into Iterable
- Fixed: A couple bugs with the new StreamReplacing functions in LinkedCollections Iterating to Infinity
### Version 0.4.0
- Changed: Iterable specific helper functions were moved out of Iterators and moved into Iterables

41
EXTRAS.md Normal file
View File

@ -0,0 +1,41 @@
### Extra Features
Primitive Collections comes with a few extra features that are disabled by default.
These will be enabled as soon they become relevant or never at all.
But some of these can be already unlocked when the target version changes.
If you compile the library for yourself you will automatically gain access to said features.
### Java17 Exclusive Features
Java17 has some new features that can sadly not really be back-ported but the library still supports them if it is compiled with java17
- RandomGenerator: Java17 has added [RandomGenerator.class](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/RandomGenerator.html).
This allows to use custom random implementations without having to re-implement them yourselves.
### ModuleSettings
Primitive Collections is a huge library.
But maybe you only use like 5-10 different classes.
Normally you would use tools like "Proguard" to get rid of classes that you don't use.
But since a lot of classes have dependencies on each other this would only do so much.
This is where the [ModuleSettings](ModuleSettings.json) come into play.
It allows you to turn of implementations as you wish and adjusts the code that everything still works.
There is 3 layers of control inside of the ModuleSettings.
- Modules directly at the top that turn off everything.
- Type Specific configurations, where you can for example turn of everything thats "Long" based.
- And then there is each type specific module settings.
Allowing for greater control without having to edit hundreds of lines of code.
On top of that:
Any Setting that isn't "Present" is counted as "Enabled".
So if you want to disable just 1 thing you can keep that 1 thing and delete the rest of the Setting.
It will still work as the same.
The default settings just come with everything so you can see what is controllable.
How to compile the Code with the ModuleSettings enabled:
```
/gradlew.bat generateLimitSource build -x test
```

343
LICENSE
View File

@ -1,208 +1,201 @@
Apache License
Apache License
Version 2.0, December 2021
http://www.apache.org/licenses/
Version 2.0, January 2021
http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION,
AND DISTRIBUTION
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"License" shall mean the terms and conditions for use, reproduction, and distribution
as defined by Sections 1 through 9 of this document.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Licensor" shall mean the copyright owner or entity authorized by the copyright
owner that is granting the License.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Legal Entity" shall mean the union of the acting entity and all other entities
that control, are controlled by, or are under common control with that entity.
For the purposes of this definition, "control" means (i) the power, direct
or indirect, to cause the direction or management of such entity, whether
by contract or otherwise, or (ii) ownership of fifty percent (50%) or more
of the outstanding shares, or (iii) beneficial ownership of such entity.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions
granted by this License.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
"Source" form shall mean the preferred form for making modifications, including
but not limited to software source code, documentation source, and configuration
files.
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
"Object" form shall mean any form resulting from mechanical transformation
or translation of a Source form, including but not limited to compiled object
code, generated documentation, and conversions to other media types.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
"Work" shall mean the work of authorship, whether in Source or Object form,
made available under the License, as indicated by a copyright notice that
is included in or attached to the work (an example is provided in the Appendix
below).
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
"Derivative Works" shall mean any work, whether in Source or Object form,
that is based on (or derived from) the Work and for which the editorial revisions,
annotations, elaborations, or other modifications represent, as a whole, an
original work of authorship. For the purposes of this License, Derivative
Works shall not include works that remain separable from, or merely link (or
bind by name) to the interfaces of, the Work and Derivative Works thereof.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
"Contribution" shall mean any work of authorship, including the original version
of the Work and any modifications or additions to that Work or Derivative
Works thereof, that is intentionally submitted to Licensor for inclusion in
the Work by the copyright owner or by an individual or Legal Entity authorized
to submit on behalf of the copyright owner. For the purposes of this definition,
"submitted" means any form of electronic, verbal, or written communication
sent to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, and
issue tracking systems that are managed by, or on behalf of, the Licensor
for the purpose of discussing and improving the Work, but excluding communication
that is conspicuously marked or otherwise designated in writing by the copyright
owner as "Not a Contribution."
Copyright 2021 Speiger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
of whom a Contribution has been received by Licensor and subsequently incorporated
within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this
License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
no-charge, royalty-free, irrevocable copyright license to reproduce, prepare
Derivative Works of, publicly display, publicly perform, sublicense, and distribute
the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License,
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
no-charge, royalty-free, irrevocable (except as stated in this section) patent
license to make, have made, use, offer to sell, sell, import, and otherwise
transfer the Work, where such license applies only to those patent claims
licensable by such Contributor that are necessarily infringed by their Contribution(s)
alone or by combination of their Contribution(s) with the Work to which such
Contribution(s) was submitted. If You institute patent litigation against
any entity (including a cross-claim or counterclaim in a lawsuit) alleging
that the Work or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses granted to You
under this License for that Work shall terminate as of the date such litigation
is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or
Derivative Works thereof in any medium, with or without modifications, and
in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy
of this License; and
(b) You must cause any modified files to carry prominent notices stating that
You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute,
all copyright, patent, trademark, and attribution notices from the Source
form of the Work, excluding those notices that do not pertain to any part
of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution,
then any Derivative Works that You distribute must include a readable copy
of the attribution notices contained within such NOTICE file, excluding those
notices that do not pertain to any part of the Derivative Works, in at least
one of the following places: within a NOTICE text file distributed as part
of the Derivative Works; within the Source form or documentation, if provided
along with the Derivative Works; or, within a display generated by the Derivative
Works, if and wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and do not modify the
License. You may add Your own attribution notices within Derivative Works
that You distribute, alongside or as an addendum to the NOTICE text from the
Work, provided that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and may provide
additional or different license terms and conditions for use, reproduction,
or distribution of Your modifications, or for any such Derivative Works as
a whole, provided Your use, reproduction, and distribution of the Work otherwise
complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any
Contribution intentionally submitted for inclusion in the Work by You to the
Licensor shall be under the terms and conditions of this License, without
any additional terms or conditions. Notwithstanding the above, nothing herein
shall supersede or modify the terms of any separate license agreement you
may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names,
trademarks, service marks, or product names of the Licensor, except as required
for reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to
in writing, Licensor provides the Work (and each Contributor provides its
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied, including, without limitation, any warranties
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR
A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness
of using or redistributing the Work and assume any risks associated with Your
exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether
in tort (including negligence), contract, or otherwise, unless required by
applicable law (such as deliberate and grossly negligent acts) or agreed to
in writing, shall any Contributor be liable to You for damages, including
any direct, indirect, special, incidental, or consequential damages of any
character arising as a result of this License or out of the use or inability
to use the Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all other commercial
damages or losses), even if such Contributor has been advised of the possibility
of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work
or Derivative Works thereof, You may choose to offer, and charge a fee for,
acceptance of support, warranty, indemnity, or other liability obligations
and/or rights consistent with this License. However, in accepting such obligations,
You may act only on Your own behalf and on Your sole responsibility, not on
behalf of any other Contributor, and only if You agree to indemnify, defend,
and hold each Contributor harmless for any liability incurred by, or claims
asserted against, such Contributor by reason of your accepting any such warranty
or additional liability. END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate
notice, with the fields enclosed by brackets "[]" replaced with your own identifying
information. (Don't include the brackets!) The text should be enclosed in
the appropriate comment syntax for the file format. We also recommend that
a file or class name and description of purpose be included on the same "printed
page" as the copyright notice for easier identification within third-party
archives.
Copyright 2021 Speiger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

2624
ModulSettings.json Normal file

File diff suppressed because it is too large Load Diff

118
README.md
View File

@ -1,82 +1,71 @@
![build](https://github.com/Speiger/Primitive-Collections/actions/workflows/build_action.yml/badge.svg)
[![Latest Release](https://jitpack.io/v/Speiger/Primitive-Collections.svg)](https://jitpack.io/#Speiger/Primitive-Collections)
![Maven Central Version](https://img.shields.io/maven-central/v/io.github.speiger/Primitive-Collections)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/Speiger/Primitive-Collections)
![Unit Tests](https://github.com/Speiger/Primitive-Collections/actions/workflows/build_tests_action.yml/badge.svg)
![Coverage](https://gist.githubusercontent.com/Speiger/280257cd19cbe1dda3789bebd4ff65cf/raw/405abd1d2f6c19ac70f20b8b1772176f42d5c5d3/jacoco.svg)
[![codecov](https://codecov.io/gh/Speiger/Primitive-Collections/branch/debug/graph/badge.svg?token=WSTSNJM0EN)](https://codecov.io/gh/Speiger/Primitive-Collections)
![Tests Done](https://gist.githubusercontent.com/Speiger/280257cd19cbe1dda3789bebd4ff65cf/raw/405abd1d2f6c19ac70f20b8b1772176f42d5c5d3/tests.svg)
# Primitive-Collections
This is a Simple Primitive Collections Library aimed to outperform Java's Collection Library and FastUtil.
Both in Performance and Quality of Life Features.
This is a Simple Primitive Collections Library i started as a hobby Project.
It is based on Java's Collection Library and FastUtil.
But its focus is a different one.
## Benchmarks
Benchmarks can be found here: [[Charts]](https://github.com/Speiger/Primitive-Collections-Benchmarks/blob/master/BENCHMARKS-CHARTS.md), [[Tables]](https://github.com/Speiger/Primitive-Collections-Benchmarks/blob/master/BENCHMARKS.md)
## Special Features
[Here](features.md) you find a set of features added to Primitive Collections.
These are designed to improve performance or to provide Quality of Life.
[Here](EXTRAS.md) you also find features that can be used when you compile the library for yourself.
These features are not used by default to have a wider range of compat, or require self compilation.
Such as pruning classes that are not needed in your code.
## Main Features:
- ArrayLists / LinkedLists
- HashSet/Map (Linked & HashControl)
- TreeSet/Map (RB & AVL)
- EnumMap
- ArrayLists / LinkedLists / CopyOnWriteLists
- HashSets/Maps (Linked & HashControl)
- TreeSets/Maps (RB & AVL)
- EnumMaps
- Immutable Maps/Lists/Sets
- Priority Queue
- Streams
- SplitIterators
- Iterators
- ConcurrentHashMaps
- Priority Queues
- Streams & Functional Queries
- Split/Iterators
- Pairs
## Specialized Functions
New Specialized functions that were added to increase performance or reduce allocations or Quality Of life.
To highlight things that may be wanted.
- Iterable:
- map/flatMap/arrayFlatMap: A Light weight version of Stream.map()
- findFirst: Allows to find the first element of a Predicated Iterable.
- filter: Allows to filter unwanted elements for wrapped Iterable
- matchAny/matchNone/matchAll: Allows to find elements in a collection.
- forEach: Allows to input a second element into a forEach move allowing for more flexibility for Method References
- Collection:
- containsAny: Allows to test if another collection contains an of the elements of the tested collection.
- primitiveStream: Provides access to the closest Java Stream Type.
- List:
- add/get/removeElements (From FastUtil): Allows to add/get/remove an Array into/from a list. Just with less overhead
- extractElements: Allows to remove a Range of elements from the List and get what was removed.
- Unstable Sort(From FastUtil): Uses a faster but not stable sort (Quick-Sort as example) to sort the list.
- SortedSet:
- addAndMoveToFirst/Last (From FastUtil but moved to Interface): Allows to add a element to the first/last position of a sorted set.
- moveToFirst/Last: Moves the desired element at the first/last position of the SortedSet.
- pollFirst/Last: Allows to poll the first/last element of the set.
- Map:
- putAll: putAll but in Array form.
- putAllIfAbsent: Puts only the elements that are absent.
- addTo (Only Primitives Values) (From FastUtil but moved to Interface): allows to add to the value of a given key. If not present it will be added. (Accumulator)
- addToAll: Same as addTo but bulkVersion.
- removeOrDefault: removes a Element and if not present returns the default value instead of the present value.
- mergeAll: BulkVersion of Merge function.
- Sorted Map:
- addAndMoveToFirst/Last (From FastUtil but moved to Interface): Allows to add a element to the first/last position of a sorted Map.
- moveToFirst/Last: Moves the desired element at the first/last position of the Map.
- getAndMoveToFirst/Last: gets the element and moves it to the first/last position. Replicating a Optional LinkedHashMap feature.
- pollFirst/LastKey: Allows to poll the first/last element.
- first/LastValue: Allows to get the first/last value from the Map.
- Unary/Functions
- Suppliers
- Bi/Consumers
- AsyncBuilders
# Notes about Versions
Any 0.x.0 version (Minor) can be reason for massive changes including API.
To ensure that problems can be dealt with even if it is breaking the current API.
Any breaking changes will be Documented (once 1.0 is released)
# How to install
Using Gradle:
```gradle
Using Jitpack Gradle
```groovy
repositories {
maven {
url = "https://maven.speiger.com/repository/main"
url = "https://jitpack.io"
}
}
dependencies {
compile 'de.speiger:Primitive-Collections:0.4.0'
implementation 'com.github.Speiger:Primitive-Collections:0.9.0'
}
```
Direct:
| Version | Jar | Sources | Java Doc |
|--------- |------------------------------------------------------------------------------------------------------------------------------ |-------------------------------------------------------------------------------------------------------------------------------------- |-------------------------------------------------------------------------------------------------------------------------------------- |
| 0.4.0 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.0/Primitive-Collections-0.4.0.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.0/Primitive-Collections-0.4.0-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.0/Primitive-Collections-0.4.0-javadoc.jar) |
| 0.3.6 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.6/Primitive-Collections-0.3.6.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.6/Primitive-Collections-0.3.6-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.6/Primitive-Collections-0.3.6-javadoc.jar) |
| 0.3.5 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.5/Primitive-Collections-0.3.5.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.5/Primitive-Collections-0.3.5-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.5/Primitive-Collections-0.3.5-javadoc.jar) |
| 0.3.4 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.4/Primitive-Collections-0.3.4.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.4/Primitive-Collections-0.3.4-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.4/Primitive-Collections-0.3.4-javadoc.jar) |
| 0.3.3 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.3/Primitive-Collections-0.3.3.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.3/Primitive-Collections-0.3.3-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.3/Primitive-Collections-0.3.3-javadoc.jar) |
Using Maven Central
```groovy
dependencies {
implementation 'io.github.speiger:Primitive-Collections:0.9.0'
}
```
# SourceCode
The generated Sourcecode can be automatically build,
but if you want to just browse around in it.
Check out the [Debug Branch](https://github.com/Speiger/Primitive-Collections/tree/debug/src/main/java/speiger/src/collections), which has the entire up to date code.
# Contributing
If you want to contribute.
@ -94,12 +83,11 @@ Please if you want to contribute follow the [Rule-Sheet](RuleSheet.md). It keeps
# How to Build
The SourceCode can be generated via:
```
/gradlew.bat generateSource
```
to build the jar:
to generate SourceCode and build the jar:
```
/gradlew.bat build
do not combine the commands because they can not be executed at the same time.
## Current Down Sides (Random order)
- Testing for Sub Maps/Sets/Lists are only in a very basic way tested
- Documentation is only present at the lowest level for most cases and needs a typo fixing.
```

15
SECURITY.md Normal file
View File

@ -0,0 +1,15 @@
# Security Policy
## Supported Versions
Due to how the releases work, only the latest versions will be supported.
## Reporting a Vulnerability
If you have discovered a security vulnerability in this project, please report it privately.
**Please refrain from posting in public issues.**
This gives me time to process issues that are being found, and reduces the possiblity of abuse while patches are being created.
Please disclose it [here](https://github.com/Speiger/Primitive-Collections/security/advisories/new).
Please consider that this project is developed by a single person.
So please provide a reasonable timeframe when reporting.

View File

@ -1,5 +1,7 @@
plugins {
id 'java-library'
id "jacoco"
// id "com.vanniktech.maven.publish" version "0.28.0"
}
tasks.withType(JavaCompile) {
@ -8,7 +10,8 @@ tasks.withType(JavaCompile) {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'signing'
repositories {
mavenCentral()
@ -18,9 +21,16 @@ repositories {
}
archivesBaseName = 'Primitive Collections'
version = '0.4.0';
version = RELEASE_VERSION;
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = JavaVersion.current();
System.out.println("Java Version: "+compileJava.sourceCompatibility)
java {
withJavadocJar()
withSourcesJar()
}
javadoc {
options.tags = [ "implSpec", "note" ]
@ -30,19 +40,7 @@ eclipse {
classpath {
downloadJavadoc = true
downloadSources = true
file {
whenMerged {
//Enforce a custom container and allowing access to the sun.misc package which is nessesary for EnumMaps
entries.find{ it.kind == 'con' && it.path.startsWith('org.eclipse.jdt')}.path = 'org.eclipse.jdt.launching.JRE_CONTAINER';
}
}
}
}
compileJava {
options.compilerArgs << '-XDignore.symbol.file'
options.fork = true
options.forkOptions.executable = 'javac' // may not needed on 1.8
}
sourceSets {
@ -54,9 +52,11 @@ configurations {
}
dependencies {
builderCompile 'de.speiger:Simple-Code-Generator:1.0.4'
runtimeOnly 'de.speiger:Simple-Code-Generator:1.0.4'
builderImplementation 'com.google.code.gson:gson:2.10'
builderImplementation 'de.speiger:Simple-Code-Generator:1.3.0'
testImplementation 'junit:junit:4.12'
testImplementation 'com.google.guava:guava-testlib:31.0.1-jre'
}
task generateSource(type: JavaExec) {
@ -66,50 +66,213 @@ task generateSource(type: JavaExec) {
main = 'speiger.src.builder.PrimitiveCollectionsBuilder'
}
task generateGithubSource(type: JavaExec) {
group = 'internal'
description = 'Builds the sourcecode for Github Actions'
classpath = sourceSets.builder.runtimeClasspath
main = 'speiger.src.builder.PrimitiveCollectionsBuilder'
args = ['silent']
}
task forceGenerateSource(type: JavaExec) {
group = 'internal'
description = 'Builds the sourcecode forceful'
classpath = sourceSets.builder.runtimeClasspath
main = 'speiger.src.builder.PrimitiveCollectionsBuilder'
args = ['true']
args = ['force']
}
task javadocJar(type: Jar) {
from javadoc
classifier = 'javadoc'
task generateTestSource(type: JavaExec) {
group = 'internal'
description = 'Builds the sourcecode for the Tests'
classpath = sourceSets.builder.runtimeClasspath
main = 'speiger.src.builder.PrimitiveCollectionsBuilder'
args = ['tests', 'silent']
}
task srcJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
task forceGenerateTestSource(type: JavaExec) {
group = 'internal'
description = 'Builds the sourcecode for the Tests'
classpath = sourceSets.builder.runtimeClasspath
main = 'speiger.src.builder.PrimitiveCollectionsBuilder'
args = ['tests', 'silent', 'force']
}
task generateLimitSource(type: JavaExec) {
group = 'internal'
description = 'Builds the Sourcecode with the ModuleSettings.json applied'
classpath = sourceSets.builder.runtimeClasspath
main = 'speiger.src.builder.PrimitiveCollectionsBuilder'
args = ['silent', 'load']
}
compileJava.dependsOn generateGithubSource
javadoc.failOnError = false
javadoc.options.memberLevel = JavadocMemberLevel.PUBLIC
javadoc.options.quiet()
artifacts {
archives javadocJar
archives srcJar
task testBooleans(type: Test) {
group 'tests'
description 'Tests all Boolean Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.booleans.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
}
task testBytes(type: Test) {
group 'tests'
description 'Tests all Byte Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.bytes.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
task testShorts(type: Test) {
group 'tests'
description 'Tests all Short Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.shorts.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
task testChars(type: Test) {
group 'tests'
description 'Tests all Character Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.chars.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
task testInts(type: Test) {
group 'tests'
description 'Tests all Int Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.ints.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
task testLongs(type: Test) {
group 'tests'
description 'Tests all Long Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.longs.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
task testFloats(type: Test) {
group 'tests'
description 'Tests all Float Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.floats.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
task testDoubles(type: Test) {
group 'tests'
description 'Tests all Double Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.doubles.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
task testObjects(type: Test) {
group 'tests'
description 'Tests all Object Collections'
filter {
excludeTestsMatching "speiger.src.testers.**.*"
includeTestsMatching "speiger.src.tests.objects.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
maxParallelForks = testThreads as Integer
}
if(System.getProperty("full_test_suite", "false").toBoolean()) {
test.dependsOn testBooleans
test.dependsOn testBytes
test.dependsOn testShorts
test.dependsOn testChars
test.dependsOn testInts
test.dependsOn testLongs
test.dependsOn testFloats
test.dependsOn testDoubles
test.dependsOn testObjects
}
test {
filter {
excludeTestsMatching "speiger.src.testers.**.*"
excludeTestsMatching "speiger.src.tests.**.*"
excludeTestsMatching "tests.**.*"
}
useJUnit()
ignoreFailures = true
maxHeapSize = maxMemory
}
uploadArchives {
repositories.mavenDeployer {
repository(url: 'https://maven.speiger.com/repository/main') {
authentication(userName: project.properties.mavenUser, password: project.properties.mavenPassword)
}
snapshotRepository(url: 'https://maven.speiger.com/repository/main') {
authentication(userName: project.properties.mavenUser, password: project.properties.mavenPassword)
jacocoTestReport {
executionData fileTree(project.buildDir.absolutePath).include("jacoco/*.exec")
reports {
xml.required = true
html.required = true
csv.required = true
}
}
publishing {
publications {
personal(MavenPublication) {
pom {
name = 'Primitive Collections'
description = 'A Primitive Collection library that reduces memory usage and improves performance'
url = 'https://github.com/Speiger/Primitive-Collections'
version = project.version
artifactId = project.archivesBaseName.replace(" ", "-")
groupId = 'de.speiger'
project {
from components.java
licenses {
license {
name = 'The Apache License, Version 2.0'
@ -120,10 +283,97 @@ uploadArchives {
developer {
id = 'speiger'
name = 'Speiger'
email = 'speiger@gmx.net'
}
}
scm {
url = 'https://github.com/Speiger/Primitive-Collections'
}
issueManagement {
system = 'github'
url = 'https://github.com/Speiger/Primitive-Collections/issues'
}
}
}
}
repositories {
maven {
name = "Speiger_Maven"
def auth = System.getenv("Speiger_Maven_Auth")?.split(';');
url version.endsWith('SNAPSHOT') ? "https://maven.speiger.com/repository/debug" : "https://maven.speiger.com/repository/main"
credentials(PasswordCredentials) {
username auth?[0]
password auth?[1]
}
}
}
}
tasks.withType(PublishToMavenRepository) {
def predicate = provider {
(repository == publishing.repositories.mavenCentral && publication == publishing.publications.maven) ||
(repository != publishing.repositories.mavenCentral && publication != publishing.publications.maven)
}
onlyIf("publishing binary to the external repository, or binary and sources to the internal one") {
predicate.get()
}
}
tasks.withType(PublishToMavenLocal) {
def predicate = provider {
publication == publishing.publications.personal
}
onlyIf("publishing binary and sources") {
predicate.get()
}
}
//Maven central Start
//Disabling due to java8 incompat, only needed to manually publishing anyways
//signing.useGpgCmd()
//
//import com.vanniktech.maven.publish.SonatypeHost
//import com.vanniktech.maven.publish.JavaLibrary
//import com.vanniktech.maven.publish.JavadocJar
//
//mavenPublishing {
// configure(new JavaLibrary(new JavadocJar.None(), true))
//}
//
//mavenPublishing {
// publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
//
// signAllPublications()
// pom {
// name = 'Primitive Collections'
// description = 'A Primitive Collection library that reduces memory usage and improves performance'
// url = 'https://github.com/Speiger/Primitive-Collections'
// version = project.version
// group = 'io.github.speiger'
// licenses {
// license {
// name = 'The Apache License, Version 2.0'
// url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
// }
// }
//
// developers {
// developer {
// id = 'speiger'
// name = 'Speiger'
// }
// }
//
// scm {
// connection = 'scm:git:git://github.com/Speiger/Primitive-Collections.git'
// developerConnection = 'scm:git:ssh://github.com:Speiger/Primitive-Collections.git'
// url = 'https://github.com/Speiger/Primitive-Collections'
// }
//
// issueManagement {
// system = 'github'
// url = 'https://github.com/Speiger/Primitive-Collections/issues'
// }
// }
//}
//

402
features.md Normal file
View File

@ -0,0 +1,402 @@
## Quality Of Life Features
New Specialized functions/classes that were added to increase performance and/or reduce allocations and/or for Quality of life.
# Functions
Functions that increase performance or are quality of life in their nature.
<details>
<summary>Iterable</summary>
<p>
## Functional Functions
Java adds themselves a lot of functional functions like,
- Stream:
- Map/FlatMap
- Filter/Distinct/Limit/Sorted
- Count/FindFirst/Collect
- Peek/ForEach/Reduce
- anyMatch/allMatch/NoneMatch
that allows to process a collection in a functional way.
But these require streams which have a lot of Overhead in their nature.
Luckly Primitive Collections adds replacement functions that provide the same functionality but with minimal overhead.
Here are some examples:
```java
public ObjectIterable<Path> toPath(ObjectIterable<String> iterable) {
return iterable.map(Paths::get).filter(Files::exist);
}
public Iterable<Path> toPath(Iterable<String> iterable) {
return ObjectIterables.map(iterable, Paths::get).filter(Files::exist);
}
public int sum(IntIterable iterable) {
return iterable.reduce(Integer::sum);
}
```
## AsyncAPI</summary>
The AsyncAPI is a Feature that simplifies the processing of Collections on a separate thread.
It uses the same concept as Javas Stream API but uses the light weight Functions from Primitive Collections to achieve the same thing.
Unlike Javas StreamAPI the AsyncAPI is always singleThreaded and more like Javas CompletableFuture, which you can await or let run Asynchronous.
The Goal is it to simplify the processing of Collections asynchronous.
Especially on tasks which don't have to be finished instantly but can be processed on the side.
Here is a example of how the API works.
```java
public void processFiles(ObjectCollection<String> potentialFiles) {
potentialFiles.asAsync()
.map(Paths::get).filter(Files::exists) //Modifies the collection (Optional)
.forEach(Files::delete) //Creates the action (Required)
.callback(T -> {}} //Callback on completion, still offthread (Optional)
.execute() //Starts the task. (Required)
}
```
</p>
</details>
<details>
<summary>Collection</summary>
<p>
These are functions specific to the Collections interface, stuff that everyone wished it was present to be in the first place.
## AddAll (Array)
Adding Elements to a Collection usually requires either a for loop or a Arrays.wrap().
This isn't an issue with Primitive Collections.
```java
public void addMonths(ObjectCollection<String> months) {
months.addAll("January", "February", "March", "April", "May", "June", "July", "August", "September, "October", November", "December");
}
public void addElements(ObjectCollection<String> result, String[] elements) {
result.addAll(elements, 0, 5); //elements, offset, length
}
```
## containsAny
Everyone hates comparing if 2 collections have part of each other included.
The solution usually requires for loops and keeping track if things were found or not.
And every Java Developer had this issue at least once and wished for a clean solution.
```java
public boolean hasMonths(ObjectCollection<Month> target, Collection<Month> toFind) {
return target.containsAny(toFind);
}
```
## Copy
Collections get copied every now and then. There is only 2 ways that this happens.
Javas Clone API or using Constructor that supports collections.
Javas Clone API is kinda in a Zombie state, where it is supported or not. Its not really clear if you should use it or not.
The Clone CloneNotSupportedException isn't helping either, causing more janky code.
While a Constructor can only support so much and testing for every case isn't really viable.
So the decision was made to straight out not support clone and instead add a copy function which doesn't use a checked exception.
It works exactly like the clone function. In a sense where it creates a shallow copy. (SubCollections do not work for obvious reasons)
```java
public IntCollection copy(IntCollection original) {
return original.copy();
}
```
## Primitive Streams
Since Javas Stream API is still really useful, even at its shortcomings, Primitive Collections provides easy access to it.
Generic Streams and the closest Primitive Stream will be provided. So a FloatCollection goes to a DoubleStream.
```java
public IntStream createStream(IntCollection source) {
return source.primitiveStream();
}
```
## RemoveAll/RetainAll with listener
Ever wanted use removeAll or retainAll and wanted to know what elements actually got deleted?
The usual solution is to create a copy and then apply it to the original and cross reference them.
Which leads to really messy code and just hasn't a clean solution.
Luckly Primitive Collections got you covered.
```java
public void removeInvalidFiles(ObjectCollections<Path> files, ObjectCollection<Path> toRemove) {
files.removeAll(toRemove, T -> System.out.println(T));
}
public void removeInvalidFiles(ObjectCollections<Path> files, ObjectCollection<Path> toKeep) {
files.retainFiles(toKeep, T -> System.out.println(T));
}
```
## ToArray
Primitive Collections supports primitive/generic toArray functions for its Primitive Collections.
On top of that the Object side gets a Java9 function ported back to java8, which uses a functional Interface to create the backing array.
```java
public Integer[] toArray(IntCollection c) {
return c.toArray(new Integer[c.size]);
}
public int[] toArray(IntCollection c) {
return c.toIntArray();
}
public String[] toArray(ObjectCollection<String> c) {
return c.toArray(String::new);
}
```
</p>
</details>
<details>
<summary>List</summary>
<p>
These functions are List specific functions, a couple of these are from FastUtil.
## add/get/remove/extractElements
These functions really useful helper functions. 3 of which are copied from FastUtil. (extract is from Primitive Collections)
They are basically array forms of addAll, getAll, removeRange and removeAndGetRange. This is the simplest way to describe it.
Here some example:
```java
public void addAll(DoubleList list) {
list.addElements(0D, 12.2D, 3.5D, 4.2D);
}
public double[] getAll(DoubleList list, int amount) {
double[] result = new double[amount];
list.getElements(0, result);
return result;
}
public void removeRange(FloatList list) {
list.removeElements(5, 14);
}
public float[] extractRange(FloatList list) {
return list.extractElements(5, 14); //Returns the removed elements
}
```
## addIfPresent/addIfAbsent
These two functions are simple helper functions that check internally if a element is present or absent before adding them to the List.
Removing the need for a contains or indexOf check every time you want to add a element.
While it is of course better to use a set, there is cases where this is still useful.
```java
public void addElements(IntList list, int... numbersToAdd) {
for(int e : numbersToAdd) {
list.addIfAbsent(e);
}
}
public void addExisting(ObjectList<String> list, String... textToAdd) {
for(String s : textToAdd) {
list.addIfPresent(s);
}
}
```
## SwapRemove
Lists when removing a Element shift usually the backing array to the left based to shrink the elements.
While that isn't computational expensive with LinkedLists, it is with ArrayLists.
Here comes swapRemove into play, which just removes the desired elements and instead of shifting left puts the last element in its place.
This reduces the data copying required down to 1 element instead of an array.
```java
public int remove(IntList elements, int indexToRemove) {
return elements.swapRemove(indexToRemove);
}
```
## Unstable Sort (From FastUtil)
Unstable Sort uses a Faster but not as stable sorting algorithm to sort the Collection.
Stable doesn't mean crashing, but more like that the result isn't exactly perfectly sorted.
```java
public void sort(List<Month> list, Comparator<Month> sorter) {
list.unstableSort(sorter);
}
```
</p>
</details>
<details>
<summary>Map</summary>
<p>
These functions are based on the Map interface. Useful functions you really would want.
## addTo/subFrom
addTo (from FastUtil) and subFrom are mathematically functions that either add or subtract from the value of a given key.
And if the key isn't present or would result in the default value it will either add or remove the entry from the Map. Given the circumstance.
This is a really useful function and I wish FastUtil made it accessible by default but sadly it isn't.
To simplify the explanation:
- addTo if no element is present puts in the desired number, otherwise it sums up the two values.
- subFrom if a element is present subtracts from it, if the element reaches the default value it removes the element from the map. If not present it will be ignored.
```java
public void addTo(Object2DoubleMap<Month> map, Month key, double averageTrainsRepaired) {
map.addTo(key, averageTrainsRepaired);
}
public void subFrom(Long2IntMap map, long key, double amount) {
map.subFrom(key, amount);
}
```
## addToAll
Simple bulk version of the addTo function since sometimes you want to merge 2 maps for summing.
Especially if your work is multi-threaded this can become useful.
```java
public void addTo(Object2DoubleMap<Month> map, Object2DoubleMap<Month> trainsRepaired) {
map.addToAll(trainsRepaired);
}
```
## mergeAll
This is a simple bulk version of merge since merging 2 maps is more frequent then people might think and leads to cleaner code too.
```java
public void merge(Long2ByteMap result, Long2ByteMap toMerge) {
result.mergeAll(toMerge);
}
```
## putAll (Array)
This allows to put keys and values as arrays instead of requiring a WrapperMap to insert the elements.
Not as useful as the Collections.addAll variant but still really useful.
```java
public void putAll(Int2DoubleMap map, int[] keys, double[] values) {
map.put(keys, values, 2, 15);
}
```
## putAllIfAbsent
putAll has this usual quirk where if a element is present it will replace the value, and sometimes this is not wanted.
While putIfAbsent exists it has no real mass form and makes iterative solutions really uneasy to use.
Here comes the helper function that gets rid of that problem.
```java
public void merge(Long2ObjectMap<String> regionFiles, Long2ObjectMap<String> toAdd) {
regionFiles.putAllIfAbsent(toAdd);
}
```
## removeOrDefault
getOrDefault is a really useful function that find use cases all the time.
Sadly by default there is no variant of removeOrDefault, while it has less cases still could be used every now and then.
This function basically tries to remove a element, if it is not present it will just return your desired default.
```java
public Path removeCache(Long2ObjectMap<Path> caches, long key) {
return caches.removeOrDefault(key, Paths.get("nuclearFun"));
}
```
## supplyIfAbsent
This one is one of my favorites. computeIfAbsent is a really useful function.
But in 90% of the cases I use it the value is a collection.
This becomes really annoying since methodReferences are faster/cleaner then Lambdas in my opinion.
supplyIfAbsent is basically computeIfAbsent but without a key, perfect for the default constructor of a collection.
This is the whole reason it exists.
```java
public void example(Int2ObjectMap<List<String>> map, Int2ObjectMap<String> toAdd) {
for(Entry<String> entry : toAdd.entrySet()) {
map.supplyIfAbsent(entry.getKey(), ObjectArrayList::new).add(entry.getValue());
}
}
```
</p>
</details>
# Interfaces
Interfaces that provide essential or quality of life features.
<details>
<summary>ITrimmable</summary>
<p>
The ITrimmable is Accessor interface that allows you to access a couple helper functions to control the size of your collections.
This was created for the constant casting requirement to implementations just to shrink collections which get annoying over time.
## trim
This function basically trims down the backing implementation to use as little memory as required to store the elements in the collection.
Optionally a desired minimum size can be provided as of how low it should go at worst.
## clearAndTrim
when you want to reset a Collection completely you have 2 options. Clear it and then call trim, or recreate the collection.
clearAndTrim solves this problem by clearing the collection and trimming it in one go, reducing overhead to achieve such a thing.
</p>
</details>
<details>
<summary>IArray</summary>
<p>
IArray is a Accessor interface that provides more access to collections by providing tools to grow your collection as needed.
While putAll/addAll try to ensure that you have enough room for your elements, this is not really a solution for all cases.
Sometimes you need to ensure the Collection is pre-initialized.
IArray grants you that control.
There is also a type specific that provides you access to the backing array implementation of Lists for faster Iteration but that is a really specific case.
## ensureCapacity
Ensures that your collection has enough storage for the elements you want to insert.
## elements (ITypeSpecificArray)
Allows you access to the backing array of a List which is for people who know what they are doing.
There is a lambda version of this function too which makes sure for synchronizedLists that you are the only one accessing the array.
</p>
</details>
<details>
<summary>OrderedMap</summary>
<p>
The OrderedMap is a real edge case interface that was born for a need.
FastUtil added functions that were like moveToFirst which were hardcoded to the implementation.
They didn't fit into something like a SortedMap because the Set wasn't sorted.
So OrderedMap was born, which isn't random but ordered in a specific way that can be changed.
## getAndMoveToFirst/getAndMoveToLast
Returns a desired element and removing it to the first/last spot in the Map. Moving the element that was at its spot after/before it.
## moveToFirst/moveToLast
Moves the element if present to the first/last spot in the Map. Moving the element that was at its spot after/before it.
Returns true if the element was actually moved.
## putAndMoveToFirst/putAndMoveToLast
Adds the desired element and moves it to first/last spot in the Map. Moving the element that was at its spot after/before it.
## firstKey/lastKey (Optional poll)
Provides access to the current first/last key of the Map.
Optionally can be polled if desired.
## firstValue/lastValue
Provides access to the current first/last value of the Map.
</p>
</details>
<details>
<summary>OrderedSet</summary>
<p>
The OrderedSet is a real edge case interface that was born for a need.
FastUtil added functions that were like moveToFirst which were hardcoded to the implementation.
They didn't fit into something like a SortedSet because the Set wasn't sorted.
So OrderedSet was born, which isn't random but ordered in a specific way that can be changed.
## addAndMoveToFirst/addAndMoveToLast
Adds the desired element and moves it to first/last spot in the Collection. Moving the element that was at its spot after/before it.
## moveToFirst/moveToLast
Moves the element if present to the first/last spot in the Collection. Moving the element that was at its spot after/before it.
Returns true if the element was actually moved.
## first/last (Optional poll)
Provides access to the current first/last element of the set.
Optionally can be polled if desired.
</p>
</details>

6
gradle.properties Normal file
View File

@ -0,0 +1,6 @@
org.gradle.jvmargs=-Xmx3G
maxMemory = 1024m
testThreads = 4
RELEASE_VERSION = 0.9.0

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

6
jitpack.yml Normal file
View File

@ -0,0 +1,6 @@
jdk:
- openjdk9
install:
- chmod +x ./gradlew
- ./gradlew build publishToMavenLocal

View File

@ -3,15 +3,15 @@ package speiger.src.builder;
@SuppressWarnings("javadoc")
public enum ClassType
{
BOOLEAN("boolean", "Boolean", "Boolean", "booleans", "BOOLEAN", "false"),
BYTE("byte", "Byte", "Byte", "bytes", "BYTE", "(byte)0"),
SHORT("short", "Short", "Short", "shorts", "SHORT", "(short)0"),
CHAR("char", "Character", "Char", "chars", "CHAR", "(char)0"),
INT("int", "Integer", "Int", "ints", "INT", "0"),
LONG("long", "Long", "Long", "longs", "LONG", "0L"),
FLOAT("float", "Float", "Float", "floats", "FLOAT", "0F"),
DOUBLE("double", "Double", "Double", "doubles", "DOUBLE", "0D"),
OBJECT("T", "T", "Object", "objects", "OBJECT", "null");
BOOLEAN("boolean", "Boolean", "Boolean", "booleans", "BOOLEAN", "false", "false"),
BYTE("byte", "Byte", "Byte", "bytes", "BYTE", "(byte)0", "(byte)-1"),
SHORT("short", "Short", "Short", "shorts", "SHORT", "(short)0", "(short)-1"),
CHAR("char", "Character", "Char", "chars", "CHAR", "(char)0", "(char)-1"),
INT("int", "Integer", "Int", "ints", "INT", "0", "-1"),
LONG("long", "Long", "Long", "longs", "LONG", "0L", "-1L"),
FLOAT("float", "Float", "Float", "floats", "FLOAT", "0F", "-1F"),
DOUBLE("double", "Double", "Double", "doubles", "DOUBLE", "0D", "-1D"),
OBJECT("T", "T", "Object", "objects", "OBJECT", "null", "null");
String keyType;
String classType;
@ -19,8 +19,9 @@ public enum ClassType
String pathType;
String capType;
String emptyValue;
String invalidValue;
private ClassType(String keyType, String classType, String fileType, String pathType, String capType, String emptyValue)
private ClassType(String keyType, String classType, String fileType, String pathType, String capType, String emptyValue, String invalidValue)
{
this.keyType = keyType;
this.classType = classType;
@ -28,6 +29,7 @@ public enum ClassType
this.pathType = pathType;
this.capType = capType;
this.emptyValue = emptyValue;
this.invalidValue = invalidValue;
}
public String getKeyType()
@ -50,6 +52,11 @@ public enum ClassType
return classType;
}
public String getClassPath()
{
return this == OBJECT ? "Object" : classType;
}
public String getClassType(boolean value)
{
return value && this == OBJECT ? "V" : classType;
@ -90,6 +97,11 @@ public enum ClassType
return emptyValue;
}
public String getInvalidValue()
{
return invalidValue;
}
public boolean isObject()
{
return this == OBJECT;
@ -102,7 +114,7 @@ public enum ClassType
public boolean needsCustomJDKType()
{
return this == BYTE || this == SHORT || this == CHAR || this == FLOAT;
return this == BOOLEAN || this == BYTE || this == SHORT || this == CHAR || this == FLOAT;
}
public boolean needsCast()
@ -121,6 +133,12 @@ public enum ClassType
}
}
public String getApply(ClassType other) {
if(other == BOOLEAN) return "test";
if(other == ClassType.OBJECT) return "apply";
return "applyAs"+other.getFileType();
}
public String getEquals(boolean not)
{
switch(this)

View File

@ -1,433 +0,0 @@
package speiger.src.builder;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.ArgumentMapper;
import speiger.src.builder.mappers.IMapper;
import speiger.src.builder.mappers.InjectMapper;
import speiger.src.builder.mappers.LineMapper;
import speiger.src.builder.mappers.SimpleMapper;
import speiger.src.builder.processor.TemplateProcess;
@SuppressWarnings("javadoc")
public class GlobalVariables
{
List<IMapper> operators = new ArrayList<>();
Set<String> flags = new LinkedHashSet<>();
ClassType type;
ClassType valueType;
public GlobalVariables(ClassType type, ClassType subType)
{
this.type = type;
valueType = subType;
}
public GlobalVariables createVariables()
{
addSimpleMapper("VALUE_PACKAGE", valueType.getPathType());
addSimpleMapper("PACKAGE", type.getPathType());
addSimpleMapper("CLASS_TYPE", type.getClassType());
addSimpleMapper("CLASS_VALUE_TYPE", valueType.getClassValueType());
addSimpleMapper("KEY_TYPE", type.getKeyType());
addSimpleMapper("KEY_SPECIAL_TYPE", type.isObject() ? "E" : type.getKeyType());
addSimpleMapper("VALUE_TYPE", valueType.getValueType());
addSimpleMapper("VALUE_SPECIAL_TYPE", valueType.isObject() ? "E" : valueType.getKeyType());
addSimpleMapper("EMPTY_KEY_VALUE", type.getEmptyValue());
addSimpleMapper("EMPTY_VALUE", valueType.getEmptyValue());
addSimpleMapper(" KEY_GENERIC_TYPE", type.isObject() ? "<"+type.getKeyType()+">" : "");
addSimpleMapper(" KEY_KEY_GENERIC_TYPE", type.isObject() ? "<"+type.getKeyType()+", "+type.getKeyType()+">" : "");
addSimpleMapper(" VALUE_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getValueType()+">" : "");
addSimpleMapper(" VALUE_VALUE_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getValueType()+", "+valueType.getValueType()+">" : "");
addSimpleMapper(" KEY_VALUE_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<"+type.getKeyType()+", "+valueType.getValueType()+">" : "<"+type.getKeyType()+">") : (valueType.isObject() ? "<"+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_VALUE_VALUE_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<"+type.getKeyType()+", "+valueType.getValueType()+", "+valueType.getValueType()+">" : "<"+type.getKeyType()+">") : (valueType.isObject() ? "<"+valueType.getValueType()+", "+valueType.getValueType()+">" : ""));
addSimpleMapper(" NO_GENERIC_TYPE", type.isObject() ? "<?>" : "");
addSimpleMapper(" NO_KV_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<?, ?>" : "<?>") : valueType.isObject() ? "<?>" : "");
addSimpleMapper(" KEY_COMPAREABLE_TYPE", type.isObject() ? "<"+type.getKeyType()+" extends Comparable<T>>" : "");
addSimpleMapper(" KEY_SUPER_GENERIC_TYPE", type.isObject() ? "<? super "+type.getKeyType()+">" : "");
addSimpleMapper(" VALUE_SUPER_GENERIC_TYPE", valueType.isObject() ? "<? super "+valueType.getValueType()+">" : "");
addSimpleMapper(" KEY_VALUE_SUPER_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<? super "+type.getKeyType()+", ? super "+valueType.getValueType()+">" : "<? super "+type.getKeyType()+">") : (valueType.isObject() ? "<? super "+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_ENUM_VALUE_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<"+type.getKeyType()+" extends Enum<"+type.getKeyType()+">, "+valueType.getValueType()+">" : "<"+type.getKeyType()+" extends Enum<"+type.getKeyType()+">>") : (valueType.isObject() ? "<"+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_VALUE_ENUM_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<"+type.getKeyType()+", "+valueType.getValueType()+" extends Enum<"+valueType.getValueType()+">>" : "<"+type.getKeyType()+">") : (valueType.isObject() ? "<"+valueType.getValueType()+" extends Enum<"+valueType.getValueType()+">>" : ""));
addInjectMapper(" KEY_SPECIAL_GENERIC_TYPE", type.isObject() ? "<%s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" VALUE_SPECIAL_GENERIC_TYPE", valueType.isObject() ? "<%s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" KSK_GENERIC_TYPE", type.isObject() ? "<%s, "+type.getKeyType()+">" : "<%s>").removeBraces().setBraceType("<>");
addInjectMapper(" KKS_GENERIC_TYPE", type.isObject() ? "<"+type.getKeyType()+", %s>" : "<%s>").removeBraces().setBraceType("<>");
addArgumentMapper(" KSS_GENERIC_TYPE", type.isObject() ? "<%1$s, %2$s>" : "<%2$s>").removeBraces().setBraceType("<>");
addInjectMapper(" VSV_GENERIC_TYPE", valueType.isObject() ? "<%s, "+valueType.getValueType()+">" : "<%s>").removeBraces().setBraceType("<>");
addInjectMapper(" VVS_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getValueType()+", %s>" : "<%s>").removeBraces().setBraceType("<>");
addArgumentMapper(" VSS_GENERIC_TYPE", valueType.isObject() ? "<%1$s, %2$s>" : "<%2$s>").removeBraces().setBraceType("<>");
addSimpleMapper(" GENERIC_KEY_BRACES", type.isObject() ? " <"+type.getKeyType()+">" : "");
addSimpleMapper(" GENERIC_VALUE_BRACES", valueType.isObject() ? " <"+valueType.getValueType()+">" : "");
addInjectMapper(" GENERIC_SPECIAL_KEY_BRACES", type.isObject() ? " <%s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" GENERIC_SPECIAL_VALUE_BRACES", valueType.isObject() ? " <%s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" GENERIC_KEY_SPECIAL_BRACES", type.isObject() ? " <"+type.getKeyType()+", %s>" : " <%s>").removeBraces().setBraceType("<>");
addInjectMapper(" GENERIC_VALUE_SPECIAL_BRACES", valueType.isObject() ? " <"+valueType.getKeyType()+", %s>" : " <%s>").removeBraces().setBraceType("<>");
addSimpleMapper(" GENERIC_KEY_VALUE_BRACES", type.isObject() ? (valueType.isObject() ? " <"+type.getKeyType()+", "+valueType.getValueType()+">" : " <"+type.getKeyType()+">") : (valueType.isObject() ? " <"+valueType.getValueType()+">" : ""));
addSimpleMapper(" COMPAREABLE_KEY_BRACES", type.isObject() ? " <"+type.getKeyType()+" extends Comparable<T>>" : "");
addSimpleMapper("KV_BRACES", type.isObject() || valueType.isObject() ? "<>" : "");
addSimpleMapper("BRACES", type.isObject() ? "<>" : "");
if(type.needsCustomJDKType())
{
addSimpleMapper("JAVA_TYPE", type.getCustomJDKType().getKeyType());
addSimpleMapper("SANITY_CAST", "castTo"+type.getFileType());
}
addSimpleMapper("JAVA_CLASS", type.getCustomJDKType().getClassType());
if(valueType.needsCustomJDKType())
{
addSimpleMapper("SANITY_CAST_VALUE", "castTo"+valueType.getFileType());
}
addComment("@ArrayType", "@param <%s> the type of array that the operation should be applied");
addComment("@Type", "@param <%s> the type of elements maintained by this Collection");
addValueComment("@ValueArrayType", "@param <%s> the type of array that the operation should be applied");
addValueComment("@ValueType", "@param <%s> the type of elements maintained by this Collection");
addAnnontion("@PrimitiveOverride", "@Override");
addSimpleMapper("@PrimitiveDoc", "");
addAnnontion("@Primitive", "@Deprecated");
addValueAnnontion("@ValuePrimitiveOverride", "@Override");
addValueAnnontion("@ValuePrimitive", "@Deprecated");
return this;
}
public GlobalVariables createHelperVariables()
{
createHelperVars(type, false, "KEY");
createHelperVars(valueType, true, "VALUE");
return this;
}
private void createHelperVars(ClassType type, boolean value, String fix)
{
addArgumentMapper("EQUALS_"+fix+"_TYPE", "Objects.equals(%2$s, "+(type.isObject() ? "%1$s" : fix+"_TO_OBJ(%1$s)")+")").removeBraces();
addInjectMapper(fix+"_EQUALS_NOT_NULL", type.getComparableValue()+" != "+(type.isPrimitiveBlocking() || type.needsCast() ? type.getEmptyValue() : "0")).removeBraces();
addInjectMapper(fix+"_EQUALS_NULL", type.getComparableValue()+" == "+(type.isPrimitiveBlocking() || type.needsCast() ? type.getEmptyValue() : "0")).removeBraces();
addArgumentMapper(fix+"_EQUALS_NOT", type.getEquals(true)).removeBraces();
addArgumentMapper(fix+"_EQUALS", type.getEquals(false)).removeBraces();
addArgumentMapper("COMPAREABLE_TO_"+fix, type.isObject() ? "((Comparable<"+type.getKeyType(value)+">)%1$s).compareTo(("+type.getKeyType(value)+")%2$s)" : type.getClassType(value)+".compare(%1$s, %2$s)").removeBraces();
addArgumentMapper("COMPARE_TO_"+fix, type.isObject() ? "%1$s.compareTo(%2$s)" : type.getClassType(value)+".compare(%1$s, %2$s)").removeBraces();
addInjectMapper(fix+"_TO_OBJ", type.isObject() ? "%s" : type.getClassType(value)+".valueOf(%s)").removeBraces();
addInjectMapper("OBJ_TO_"+fix, type.isObject() ? "%s" : "%s."+type.getKeyType(value)+"Value()").removeBraces();
addInjectMapper("CLASS_TO_"+fix, type.isObject() ? "("+type.getKeyType(value)+")%s" : "(("+type.getClassType(value)+")%s)."+type.getKeyType(value)+"Value()").removeBraces();
addInjectMapper(fix+"_TO_HASH", type.isObject() ? "%s.hashCode()" : type.getClassType(value)+".hashCode(%s)").removeBraces();
addInjectMapper(fix+"_TO_STRING", type.isObject() ? "%s.toString()" : type.getClassType(value)+".toString(%s)").removeBraces();
addSimpleMapper("CAST_"+fix+"_ARRAY ", type.isObject() ? "("+fix+"_TYPE[])" : "");
addSimpleMapper("EMPTY_"+fix+"_ARRAY", type.isObject() ? "("+fix+"_TYPE[])ARRAYS.EMPTY_ARRAY" : "ARRAYS.EMPTY_ARRAY");
addInjectMapper("NEW_"+fix+"_ARRAY", type.isObject() ? "("+fix+"_TYPE[])new Object[%s]" : "new "+fix+"_TYPE[%s]").removeBraces();
addInjectMapper("NEW_SPECIAL_"+fix+"_ARRAY", type.isObject() ? "(E[])new Object[%s]" : "new "+fix+"_TYPE[%s]").removeBraces();
addInjectMapper("NEW_CLASS"+(value ? "_VALUE" : "")+"_ARRAY", type.isObject() ? "(CLASS_TYPE[])new Object[%s]" : "new CLASS_TYPE[%s]").removeBraces();
}
public GlobalVariables createPreFunctions()
{
addSimpleMapper("ENTRY_SET", type.getFileType().toLowerCase()+"2"+valueType.getFileType()+"EntrySet");
return this;
}
public GlobalVariables createClassTypes()
{
addSimpleMapper("JAVA_PREDICATE", type.isPrimitiveBlocking() ? "" : type.getCustomJDKType().getFileType()+"Predicate");
addSimpleMapper("JAVA_CONSUMER", type.isPrimitiveBlocking() ? "" : "java.util.function."+type.getCustomJDKType().getFileType()+"Consumer");
addSimpleMapper("JAVA_FUNCTION", type.getFunctionClass(valueType));
addSimpleMapper("JAVA_BINARY_OPERATOR", type == ClassType.BOOLEAN ? "" : (type.isObject() ? "java.util.function.BinaryOperator" : "java.util.function."+type.getCustomJDKType().getFileType()+"BinaryOperator"));
addSimpleMapper("JAVA_UNARY_OPERATOR", type.isObject() ? "BinaryOperator" : type == ClassType.BOOLEAN ? "" : type.getCustomJDKType().getFileType()+"UnaryOperator");
addSimpleMapper("JAVA_SPLIT_ITERATOR", type.isPrimitiveBlocking() ? "Spliterator" : "Of"+type.getCustomJDKType().getFileType());
addSimpleMapper("JAVA_STREAM", type.isPrimitiveBlocking() ? "" : type.getCustomJDKType().getFileType()+"Stream");
//Final Classes
addClassMapper("ARRAY_LIST", "ArrayList");
addClassMapper("LINKED_LIST", "LinkedList");
addAbstractMapper("IMMUTABLE_LIST", "Immutable%sList");
addClassMapper("ARRAY_FIFO_QUEUE", "ArrayFIFOQueue");
addClassMapper("ARRAY_PRIORITY_QUEUE", "ArrayPriorityQueue");
addClassMapper("HEAP_PRIORITY_QUEUE", "HeapPriorityQueue");
addClassMapper("LINKED_CUSTOM_HASH_SET", "LinkedOpenCustomHashSet");
addClassMapper("LINKED_HASH_SET", "LinkedOpenHashSet");
addAbstractMapper("IMMUTABLE_HASH_SET", "Immutable%sOpenHashSet");
addClassMapper("CUSTOM_HASH_SET", "OpenCustomHashSet");
addClassMapper("HASH_SET", "OpenHashSet");
addAbstractBiMapper("IMMUTABLE_HASH_MAP", "Immutable%sOpenHashMap", "2");
addBiClassMapper("LINKED_CUSTOM_HASH_MAP", "LinkedOpenCustomHashMap", "2");
addBiClassMapper("LINKED_HASH_MAP", "LinkedOpenHashMap", "2");
addBiClassMapper("CUSTOM_HASH_MAP", "OpenCustomHashMap", "2");
addBiClassMapper("AVL_TREE_MAP", "AVLTreeMap", "2");
addBiClassMapper("RB_TREE_MAP", "RBTreeMap", "2");
addFunctionValueMappers("LINKED_ENUM_MAP", valueType.isObject() ? "LinkedEnum2ObjectMap" : "LinkedEnum2%sMap");
addFunctionValueMappers("ENUM_MAP", valueType.isObject() ? "Enum2ObjectMap" : "Enum2%sMap");
addBiClassMapper("HASH_MAP", "OpenHashMap", "2");
addBiClassMapper("ARRAY_MAP", "ArrayMap", "2");
addBiClassMapper("IMMUTABLE_PAIR", "ImmutablePair", "");
addBiClassMapper("MUTABLE_PAIR", "MutablePair", "");
addClassMapper("RB_TREE_SET", "RBTreeSet");
addClassMapper("AVL_TREE_SET", "AVLTreeSet");
addClassMapper("ARRAY_SET", "ArraySet");
//Abstract Classes
addAbstractMapper("ABSTRACT_COLLECTION", "Abstract%sCollection");
addAbstractMapper("ABSTRACT_SET", "Abstract%sSet");
addAbstractMapper("ABSTRACT_LIST", "Abstract%sList");
addAbstractBiMapper("ABSTRACT_MAP", "Abstract%sMap", "2");
addClassMapper("SUB_LIST", "SubList");
//Helper Classes
addClassMapper("LISTS", "Lists");
addClassMapper("SETS", "Sets");
addClassMapper("COLLECTIONS", "Collections");
addClassMapper("ARRAYS", "Arrays");
addClassMapper("PRIORITY_QUEUES", "PriorityQueues");
addClassMapper("SPLIT_ITERATORS", "Splititerators");
addClassMapper("ITERATORS", "Iterators");
addClassMapper("ITERABLES", "Iterables");
addBiClassMapper("MAPS", "Maps", "2");
//Interfaces
addClassMapper("LIST_ITERATOR", "ListIterator");
addClassMapper("BI_ITERATOR", "BidirectionalIterator");
addBiClassMapper("BI_CONSUMER", "Consumer", "");
addClassMapper("BI_OBJECT_CONSUMER", "ObjectConsumer");
addClassMapper("SPLIT_ITERATOR", "Splititerator");
addClassMapper("ITERATOR", "Iterator");
addClassMapper("ITERABLE", "Iterable");
addClassMapper("COLLECTION", "Collection");
addClassMapper("TO_OBJECT_FUNCTION", "2ObjectFunction");
addBiClassMapper("FUNCTION", "Function", "2");
addClassMapper("LIST_ITER", "ListIter");
addClassMapper("LIST", "List");
addBiClassMapper("NAVIGABLE_MAP", "NavigableMap", "2");
addBiClassMapper("SORTED_MAP", "SortedMap", "2");
addBiClassMapper("MAP", "Map", "2");
addClassMapper("NAVIGABLE_SET", "NavigableSet");
addBiClassMapper("PAIR", "Pair", "");
addClassMapper("PRIORITY_QUEUE", "PriorityQueue");
addClassMapper("PRIORITY_DEQUEUE", "PriorityDequeue");
addClassMapper("PREDICATE", "2BooleanFunction");
addClassMapper("SORTED_SET", "SortedSet");
addClassMapper("SET", "Set");
addClassMapper("STRATEGY", "Strategy");
addClassMapper("STACK", "Stack");
addBiClassMapper("UNARY_OPERATOR", "UnaryOperator", "");
if(type.isObject())
{
if(!valueType.isObject()) addSimpleMapper("VALUE_CONSUMER", valueType.getFileType()+"Consumer");
else addSimpleMapper("VALUE_CONSUMER", "Consumer");
addSimpleMapper("CONSUMER", "Consumer");
addSimpleMapper("COMPARATOR", "Comparator");
addSimpleMapper("IARRAY", "IObjectArray");
}
else
{
if(valueType.isObject())
{
addSimpleMapper("VALUE_CONSUMER", "Consumer");
addSimpleMapper("CONSUMER", type.getFileType()+"Consumer");
}
else addClassMapper("CONSUMER", "Consumer");
addClassMapper("COMPARATOR", "Comparator");
addFunctionMappers("IARRAY", "I%sArray");
}
return this;
}
public GlobalVariables createFunctions()
{
addSimpleMapper("APPLY_VALUE", valueType.isObject() ? "apply" : "applyAs"+valueType.getNonFileType());
addSimpleMapper("APPLY_CAST", "applyAs"+type.getCustomJDKType().getNonFileType());
addSimpleMapper("APPLY", type.isObject() ? "apply" : "applyAs"+type.getNonFileType());
addFunctionValueMapper("BULK_MERGE", "mergeAll");
addFunctionValueMappers("COMPUTE_IF_ABSENT", "compute%sIfAbsent");
addFunctionValueMappers("COMPUTE_IF_PRESENT", "compute%sIfPresent");
addFunctionValueMapper("COMPUTE", "compute");
addFunctionMapper("DEQUEUE_LAST", "dequeueLast");
addFunctionMapper("DEQUEUE", "dequeue");
addFunctionMappers("POLL_FIRST_ENTRY_KEY", "pollFirst%sKey");
addFunctionMappers("POLL_LAST_ENTRY_KEY", "pollLast%sKey");
addFunctionMapper("POLL_FIRST_KEY", "pollFirst");
addFunctionMapper("POLL_LAST_KEY", "pollLast");
addFunctionMappers("FIRST_ENTRY_KEY", "first%sKey");
addFunctionValueMappers("FIRST_ENTRY_VALUE", "first%sValue");
addFunctionMapper("FIRST_KEY", "first");
addFunctionMappers("LAST_ENTRY_KEY", "last%sKey");
addFunctionValueMappers("LAST_ENTRY_VALUE", "last%sValue");
addFunctionMappers("ENTRY_KEY", "get%sKey");
addFunctionValueMappers("ENTRY_VALUE", "get%sValue");
addFunctionMappers("KEY_ENTRY", "set%sKey");
addFunctionValueMappers("VALUE_ENTRY", "set%sValue");
addFunctionMapper("GET_KEY", "get");
if(type.isObject()) addFunctionValueMapper("GET_VALUE", valueType.isObject() ? "getObject" : "get");
else addSimpleMapper("GET_VALUE", "get");
addFunctionMapper("LAST_KEY", "last");
addFunctionValueMapper("MERGE", "merge");
addFunctionMapper("NEXT", "next");
addFunctionMapper("PREVIOUS", "previous");
if(type.isObject()) addFunctionMapper("REMOVE_VALUE", "rem");
else addSimpleMapper("REMOVE_VALUE", "remove");
addFunctionMapper("REMOVE_KEY", "rem");
addFunctionMapper("REMOVE_LAST", "removeLast");
addFunctionMapper("REMOVE", "remove");
addFunctionValueMappers("REPLACE_VALUES", valueType.isObject() ? "replaceObjects" : "replace%ss");
addFunctionMappers("REPLACE", type.isObject() ? "replaceObjects" : "replace%ss");
addFunctionMappers("SORT", "sort%ss");
addSimpleMapper("VALUE_TEST_VALUE", valueType.isObject() ? "getBoolean" : "get");
addSimpleMapper("TEST_VALUE", type.isObject() ? "getBoolean" : "get");
addSimpleMapper("NEW_STREAM", type.isPrimitiveBlocking() ? "" : type.getCustomJDKType().getKeyType()+"Stream");
addSimpleMapper("TO_ARRAY", "to"+type.getNonFileType()+"Array");
addSimpleMapper("[SPACE]", " ");
return this;
}
public GlobalVariables createFlags()
{
flags.add("TYPE_"+type.getCapType());
flags.add("VALUE_"+valueType.getCapType());
if(type == valueType) flags.add("SAME_TYPE");
if(type.hasFunction(valueType)) flags.add("JDK_FUNCTION");
if(!type.needsCustomJDKType()) flags.add("JDK_TYPE");
if(!type.isPrimitiveBlocking()) flags.add("PRIMITIVES");
if(!valueType.isPrimitiveBlocking()) flags.add("VALUE_PRIMITIVES");
if(valueType.needsCustomJDKType()) flags.add("JDK_VALUE");
return this;
}
public TemplateProcess create(String fileName, String splitter, boolean valueOnly)
{
TemplateProcess process = new TemplateProcess(String.format(fileName+".java", (splitter != null ? type.getFileType()+splitter+valueType.getFileType() : (valueOnly ? valueType : type).getFileType())));
process.setPathBuilder(new PathBuilder(type.getPathType()));
process.addFlags(flags);
process.addMappers(operators);
return process;
}
public ClassType getType()
{
return type;
}
private void addClassMapper(String pattern, String replacement)
{
operators.add(new SimpleMapper(type.name()+"[VALUE_"+pattern+"]", "VALUE_"+pattern, valueType.getFileType()+replacement));
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, type.getFileType()+replacement));
}
private void addBiClassMapper(String pattern, String replacement, String splitter)
{
operators.add(new SimpleMapper(type.name()+"[KEY_"+pattern+"]", "KEY_"+pattern, type.getFileType()+splitter+type.getFileType()+replacement));
operators.add(new SimpleMapper(type.name()+"[VALUE_"+pattern+"]", "VALUE_"+pattern, valueType.getFileType()+splitter+valueType.getFileType()+replacement));
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, type.getFileType()+splitter+valueType.getFileType()+replacement));
}
private void addAbstractMapper(String pattern, String replacement)
{
operators.add(new SimpleMapper(type.name()+"[VALUE_"+pattern+"]", "VALUE_"+pattern, String.format(replacement, valueType.getFileType())));
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, String.format(replacement, type.getFileType())));
}
private void addAbstractBiMapper(String pattern, String replacement, String splitter)
{
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, String.format(replacement, type.getFileType()+splitter+valueType.getFileType())));
}
private void addFunctionMapper(String pattern, String replacement)
{
operators.add(new SimpleMapper(type.name()+"[VALUE_"+pattern+"]", "VALUE_"+pattern, replacement+valueType.getNonFileType()));
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, replacement+type.getNonFileType()));
}
private void addFunctionValueMapper(String pattern, String replacement)
{
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, replacement+valueType.getNonFileType()));
}
private void addFunctionMappers(String pattern, String replacement)
{
operators.add(new SimpleMapper(type.name()+"[VALUE_"+pattern+"]", "VALUE_"+pattern, String.format(replacement, valueType.getNonFileType())));
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, String.format(replacement, type.getNonFileType())));
}
private void addFunctionValueMappers(String pattern, String replacement)
{
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, String.format(replacement, valueType.getNonFileType())));
}
private void addSimpleMapper(String pattern, String replacement)
{
operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, replacement));
}
private void addAnnontion(String pattern, String value)
{
if(type == ClassType.OBJECT) operators.add(new LineMapper(type.name()+"["+pattern+"]", pattern));
else operators.add(new SimpleMapper(type.name()+"["+pattern+"]", pattern, value));
}
private void addValueAnnontion(String pattern, String value)
{
if(valueType == ClassType.OBJECT) operators.add(new LineMapper(valueType.name()+"["+pattern+"]", pattern));
else operators.add(new SimpleMapper(valueType.name()+"["+pattern+"]", pattern, value));
}
private void addComment(String pattern, String value)
{
if(type == ClassType.OBJECT) operators.add(new InjectMapper(type.name()+"["+pattern+"]", pattern, value).removeBraces());
else operators.add(new LineMapper(type.name()+"["+pattern+"]", pattern));
}
private void addValueComment(String pattern, String value)
{
if(valueType == ClassType.OBJECT) operators.add(new InjectMapper(valueType.name()+"["+pattern+"]", pattern, value).removeBraces());
else operators.add(new LineMapper(valueType.name()+"["+pattern+"]", pattern));
}
private InjectMapper addInjectMapper(String pattern, String replacement)
{
InjectMapper mapper = new InjectMapper(type.name()+"["+pattern+"]", pattern, replacement);
operators.add(mapper);
return mapper;
}
private ArgumentMapper addArgumentMapper(String pattern, String replacement)
{
return addArgumentMapper(pattern, replacement, ", ");
}
private ArgumentMapper addArgumentMapper(String pattern, String replacement, String splitter)
{
ArgumentMapper mapper = new ArgumentMapper(type.name()+"["+pattern+"]", pattern, replacement, splitter);
operators.add(mapper);
return mapper;
}
class PathBuilder implements UnaryOperator<Path>
{
String before;
public PathBuilder(String before)
{
this.before = before;
}
@Override
public Path apply(Path t)
{
return t.subpath(0, 6).resolve(before).resolve(t.subpath(6, t.getNameCount()));
}
}
}

View File

@ -0,0 +1,155 @@
package speiger.src.builder;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.IMapper;
import speiger.src.builder.processor.TemplateProcess;
@SuppressWarnings("javadoc")
public class ModulePackage
{
private static final BiConsumer<String, RequiredType> VOID = (K, V) -> {};
public static final ClassType[] TYPE = ClassType.values();
final ClassType keyType;
final ClassType valueType;
Set<String> blocked = new HashSet<>();
Map<String, String> nameRemapper = new HashMap<>();
Map<String, String> splitters = new HashMap<>();
List<Predicate<String>> blockedFilters = new ArrayList<>();
List<IMapper> mappers = new ArrayList<>();
Set<String> flags = new LinkedHashSet<>();
Set<String> globalFlags;
Map<String, Integer> flaggedValues = new HashMap<>();
BiConsumer<String, RequiredType> requirements = VOID;
public ModulePackage(Set<String> globalFlags, ClassType keyType, ClassType valueType) {
this.globalFlags = globalFlags;
this.keyType = keyType;
this.valueType = valueType;
}
public void finish() {
requirements = VOID;
mappers.sort(Comparator.comparing(IMapper::getSearchValue, Comparator.comparingInt(String::length).reversed()));
mappers.sort(Comparator.comparing(IMapper::getSearchValue, this::sort));
}
public void setRequirements(BiConsumer<String, RequiredType> requirements) {
this.requirements = requirements;
}
public boolean isSame() {
return keyType == valueType;
}
public boolean isEnumValid() {
return keyType == ClassType.OBJECT;
}
public ClassType getKeyType() {
return keyType;
}
public ClassType getValueType() {
return valueType;
}
public void addFlag(String flag) {
flags.add(flag);
}
public void addGlobalFlag(String flag) {
globalFlags.add(flag);
}
public void addValue(String key, int value) {
flaggedValues.put(key, value);
}
public void addRequirement(String fileName, RequiredType type) {
requirements.accept(fileName, type);
}
public void addMapper(IMapper mapper) {
mappers.add(mapper);
}
public void addBlockedFilter(Predicate<String> filter) {
blockedFilters.add(filter);
}
public void addBlockedFiles(String... names) {
blocked.addAll(Arrays.asList(names));
}
public void addSplitter(String fileName, String splitter) {
splitters.put(fileName, splitter);
}
public void addRemapper(String fileName, String actualName) {
nameRemapper.put(fileName, actualName);
}
public void process(String fileName, Consumer<TemplateProcess> result) {
if(isBlocked(fileName)) return;
String splitter = String.format(splitters.getOrDefault(fileName, keyType.getFileType()), keyType.getFileType(), valueType.getFileType());
String newName = String.format(nameRemapper.getOrDefault(fileName, "%s"+fileName), splitter);
TemplateProcess process = new TemplateProcess(newName+".java");
process.setPathBuilder(new PathBuilder(keyType.getPathType()));
process.addFlags(flags);
process.addFlags(globalFlags);
process.addMappers(mappers);
process.addValues(flaggedValues);
result.accept(process);
}
private boolean isBlocked(String fileName) {
if(blocked.contains(fileName)) return true;
for(int i = 0,m=blockedFilters.size();i<m;i++) {
if(blockedFilters.get(i).test(fileName)) return true;
}
return false;
}
public static List<ModulePackage> createPackages(Set<String> globalFlags) {
List<ModulePackage> list = new ArrayList<>();
for(ClassType key : TYPE) {
for(ClassType value : TYPE) {
list.add(new ModulePackage(globalFlags, key, value));
}
}
return list;
}
private int sort(String key, String value) {
if(value.contains(key)) return 1;
else if(key.contains(value)) return -1;
return 0;
}
class PathBuilder implements UnaryOperator<Path> {
String before;
public PathBuilder(String before) {
this.before = before;
}
@Override
public Path apply(Path t) {
return t.subpath(0, 6).resolve(before).resolve(t.subpath(6, t.getNameCount()));
}
}
}

View File

@ -1,177 +1,227 @@
package speiger.src.builder;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.Consumer;
import java.util.stream.Stream;
import speiger.src.builder.modules.AsyncModule;
import speiger.src.builder.modules.BaseModule;
import speiger.src.builder.modules.CollectionModule;
import speiger.src.builder.modules.FunctionModule;
import speiger.src.builder.modules.JavaModule;
import speiger.src.builder.modules.ListModule;
import speiger.src.builder.modules.MapModule;
import speiger.src.builder.modules.PairModule;
import speiger.src.builder.modules.PrioQueueModule;
import speiger.src.builder.modules.SetModule;
import speiger.src.builder.processor.TemplateProcess;
import speiger.src.builder.processor.TemplateProcessor;
@SuppressWarnings("javadoc")
public class PrimitiveCollectionsBuilder extends TemplateProcessor
{
Map<String, EnumSet<ClassType>> blocked = new HashMap<>();
Map<String, String> nameRemapper = new HashMap<>();
Map<String, String> biRequired = new HashMap<>();
Set<String> enumRequired = new HashSet<>();
public static final ClassType[] TYPE = ClassType.values();
List<GlobalVariables> variables = new ArrayList<>();
List<GlobalVariables> biVariables = new ArrayList<>();
List<GlobalVariables> enumVariables = new ArrayList<>();
private static final int SPECIAL = 0x1; //Detects if the Builder is generating tests
private static final int LOAD = 0x2; //If Configs should be loaded
private static final int ANTI_SAVE = SPECIAL | LOAD; //If save should be disabled since load/save shouldn't happen at the same time.
private static final int SAVE = 0x4; //if the configuration should be created
Set<String> globalFlags = new HashSet<>();
List<ModulePackage> simplePackages = new ArrayList<>();
List<ModulePackage> biPackages = new ArrayList<>();
List<ModulePackage> enumPackages = new ArrayList<>();
Map<String, RequiredType> requirements = new HashMap<>();
SettingsManager manager = new SettingsManager();
int flags;
public PrimitiveCollectionsBuilder()
{
super(Paths.get("src/builder/resources/speiger/assets/collections/templates/"), Paths.get("src/main/java/speiger/src/collections/"), Paths.get("src/builder/resources/speiger/assets/collections/"));
public PrimitiveCollectionsBuilder() {
this(false);
}
public PrimitiveCollectionsBuilder(Path sourceFolder, Path outputFolder, Path dataFolder)
{
super(sourceFolder, outputFolder, dataFolder);
public PrimitiveCollectionsBuilder(boolean silencedSuccess) {
super(silencedSuccess, Paths.get("src/builder/resources/speiger/assets/collections/templates/"), Paths.get("src/main/java/speiger/src/collections/"), Paths.get("src/builder/resources/speiger/assets/collections/"));
}
public PrimitiveCollectionsBuilder(Path sourceFolder, Path outputFolder, Path dataFolder) {
this(false, sourceFolder, outputFolder, dataFolder);
}
public PrimitiveCollectionsBuilder(boolean silencedSuccess, Path sourceFolder, Path outputFolder, Path dataFolder) {
super(silencedSuccess, sourceFolder, outputFolder, dataFolder);
}
private PrimitiveCollectionsBuilder setFlags(int flags) {
this.flags = flags;
if((flags & ANTI_SAVE) != 0) {
this.flags &= ~SAVE;
}
return this;
}
private static PrimitiveCollectionsBuilder createTests(boolean silent, int flags) {
return new PrimitiveCollectionsBuilder(silent,
Paths.get("src/builder/resources/speiger/assets/tests/templates/"),
Paths.get("src/test/java/speiger/src/tests/"),
Paths.get("src/builder/resources/speiger/assets/tests/")).setFlags(flags | SPECIAL);
}
private static PrimitiveCollectionsBuilder createTesters(boolean silent, int flags) {
return new PrimitiveCollectionsBuilder(silent,
Paths.get("src/builder/resources/speiger/assets/testers/templates/"),
Paths.get("src/test/java/speiger/src/testers/"),
Paths.get("src/builder/resources/speiger/assets/testers/")).setFlags(flags | SPECIAL);
}
@Override
protected boolean isFileValid(Path fileName)
{
return true;
protected boolean isFileValid(Path fileName) { return true; }
@Override
protected boolean relativePackages() { return true; }
@Override
protected boolean debugUnusedMappers() { return false; }
@Override
protected void afterFinish() {
if((flags & SPECIAL) == 0 && getVersion() > 8) {
Path basePath = Paths.get("src/main/java");
try(BufferedWriter writer = Files.newBufferedWriter(basePath.resolve("module-info.java"))) {
writer.write(getModuleInfo(basePath));
}
catch(Exception e) { e.printStackTrace(); }
}
}
public List<BaseModule> createModules() {
List<BaseModule> modules = new ArrayList<>();
modules.add(JavaModule.INSTANCE);
modules.add(FunctionModule.INSTANCE);
modules.add(CollectionModule.INSTANCE);
modules.add(PrioQueueModule.INSTANCE);
modules.add(ListModule.INSTANCE);
modules.add(SetModule.INSTANCE);
modules.add(MapModule.INSTANCE);
modules.add(PairModule.INSTANCE);
modules.add(AsyncModule.INSTANCE);
return modules;
}
@Override
protected void init() {
prepPackages();
//Init Modules here
addModules(createModules());
finishPackages();
}
public void addModules(List<BaseModule> modules) {
for(int i = 0,m=modules.size();i<m;i++) {
modules.get(i).setManager(manager);
}
for(int i = 0,m=modules.size();i<m;i++) {
biPackages.forEach(modules.get(i)::init);
}
modules.forEach(BaseModule::cleanup);
}
private void finishPackages() {
biPackages.forEach(ModulePackage::finish);
if((flags & SAVE) != 0) manager.save();
}
private void prepPackages() {
if((flags & LOAD) != 0) manager.load();
for(ModulePackage entry : ModulePackage.createPackages(globalFlags)) {
entry.setRequirements(requirements::put);
biPackages.add(entry);
if(entry.isSame()) simplePackages.add(entry);
if(entry.isEnumValid()) enumPackages.add(entry);
}
}
@Override
protected boolean relativePackages()
{
return true;
public void createProcesses(String fileName, Consumer<TemplateProcess> process) {
List<ModulePackage> packages = getPackagesByRequirement(requirements.get(fileName));
for(int i = 0,m=packages.size();i<m;i++) {
packages.get(i).process(fileName, process);
}
}
@Override
protected boolean debugUnusedMappers()
{
protected List<ModulePackage> getPackagesByRequirement(RequiredType type) {
if(type == null) return simplePackages;
if(type == RequiredType.BI_CLASS) return biPackages;
if(type == RequiredType.ENUM) return enumPackages;
return Collections.emptyList();
}
private String getModuleInfo(Path basePath) {
StringJoiner joiner = new StringJoiner("\n", "", "\n");
try(Stream<Path> stream = Files.walk(getOutputFolder())) {
stream.filter(Files::isDirectory)
.filter(this::containsFiles)
.map(basePath::relativize)
.map(Path::toString)
.map(this::sanitize)
.forEach(T -> joiner.add("\texports "+T+";"));
}
catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
StringBuilder builder = new StringBuilder();
builder.append("/** @author Speiger */\n");
builder.append("module ").append(sanitize(basePath.relativize(getOutputFolder()).toString())).append(" {\n");
builder.append(joiner.toString()).append("}");
return builder.toString();
}
private String sanitize(String input) {
return input.replace("\\", ".").replace("/", ".");
}
private boolean containsFiles(Path path) {
try(Stream<Path> stream = Files.walk(path, 1)) {
return stream.filter(Files::isRegularFile).findFirst().isPresent();
}
catch(Exception e) { e.printStackTrace(); }
return false;
}
@Override
protected void init()
{
variables.clear();
for(ClassType clzType : TYPE)
{
for(ClassType subType : TYPE)
{
create(clzType, subType);
}
}
enumRequired.add("EnumMap");
enumRequired.add("LinkedEnumMap");
biRequired.put("BiConsumer", "");
biRequired.put("UnaryOperator", "");
biRequired.put("Pair", "");
biRequired.put("MutablePair", "");
biRequired.put("ImmutablePair", "");
addBiClass("Function", "Maps", "Map", "SortedMap", "NavigableMap", "AbstractMap", "ImmutableOpenHashMap", "OpenHashMap", "LinkedOpenHashMap", "OpenCustomHashMap", "LinkedOpenCustomHashMap", "ArrayMap", "RBTreeMap", "AVLTreeMap");
nameRemapper.put("BiConsumer", "%sConsumer");
nameRemapper.put("IArray", "I%sArray");
nameRemapper.put("AbstractMap", "Abstract%sMap");
nameRemapper.put("AbstractCollection", "Abstract%sCollection");
nameRemapper.put("AbstractSet", "Abstract%sSet");
nameRemapper.put("AbstractList", "Abstract%sList");
nameRemapper.put("EnumMap", "Enum2%sMap");
nameRemapper.put("LinkedEnumMap", "LinkedEnum2%sMap");
nameRemapper.put("ImmutableList", "Immutable%sList");
nameRemapper.put("ImmutableOpenHashSet", "Immutable%sOpenHashSet");
nameRemapper.put("ImmutableOpenHashMap", "Immutable%sOpenHashMap");
addBlockage(ClassType.OBJECT, "Consumer", "Comparator", "Stack");
addBlockage(ClassType.BOOLEAN, "ArraySet", "AVLTreeSet", "RBTreeSet", "SortedSet", "NavigableSet", "OpenHashSet", "OpenCustomHashSet", "LinkedOpenHashSet", "LinkedOpenCustomHashSet");
addBlockage(ClassType.BOOLEAN, "ImmutableOpenHashMap", "ImmutableOpenHashSet", "SortedMap", "NavigableMap", "OpenHashMap", "LinkedOpenHashMap", "OpenCustomHashMap", "LinkedOpenCustomHashMap", "ArrayMap", "RBTreeMap", "AVLTreeMap");
private int getVersion() {
String version = System.getProperty("java.version");
if(version.startsWith("1.")) return Integer.parseInt(version.substring(2, 3));
int dot = version.indexOf(".");
return Integer.parseInt(dot != -1 ? version.substring(0, dot) : version);
}
protected void create(ClassType mainType, ClassType subType)
{
GlobalVariables type = new GlobalVariables(mainType, subType);
type.createFlags();
type.createHelperVariables();
type.createVariables();
type.createPreFunctions();
type.createClassTypes();
type.createFunctions();
if(mainType == subType) variables.add(type);
biVariables.add(type);
if(mainType.isObject()) enumVariables.add(type);
}
protected void addBiClass(String...classNames)
{
for(String s : classNames)
{
biRequired.put(s, "2");
}
}
protected void addBlockage(ClassType type, String...args)
{
for(String s : args)
{
EnumSet<ClassType> set = blocked.get(s);
if(set == null)
{
set = EnumSet.noneOf(ClassType.class);
blocked.put(s, set);
}
set.add(type);
}
}
@Override
public void createProcesses(String name, Consumer<TemplateProcess> acceptor)
{
String splitter = biRequired.get(name);
boolean valueRequired = enumRequired.contains(name);
List<GlobalVariables> vars = getVariablesByClass(name, splitter != null);
EnumSet<ClassType> types = blocked.get(name);
for(int i = 0,m=vars.size();i<m;i++)
{
GlobalVariables type = vars.get(i);
if(types == null || !types.contains(type.getType()))
{
acceptor.accept(type.create(nameRemapper.getOrDefault(name, "%s"+name), splitter, valueRequired));
}
}
}
protected List<GlobalVariables> getVariablesByClass(String name, boolean bi) {
if(enumRequired.contains(name)) return enumVariables;
if(bi) return biVariables;
return variables;
}
public static void main(String...args)
{
public static void main(String...args) {
try
{
if(args.length == 0) {
new PrimitiveCollectionsBuilder().process(false);
} else if(args.length == 1) {
new PrimitiveCollectionsBuilder().process(Boolean.parseBoolean(args[0]));
} else if(args.length == 3) {
new PrimitiveCollectionsBuilder(Paths.get(args[0]), Paths.get(args[1]), Paths.get(args[2])).process(false);
} else if(args.length == 4) {
new PrimitiveCollectionsBuilder(Paths.get(args[0]), Paths.get(args[1]), Paths.get(args[2])).process(Boolean.parseBoolean(args[3]));
} else {
System.out.println("Invalid argument count passed in");
System.exit(1);
Set<String> flags = new HashSet<>(Arrays.asList(args));
boolean silent = flags.contains("silent");
boolean force = flags.contains("force");
boolean tests = flags.contains("tests");
boolean forceTests = flags.contains("force-tests");
boolean load = flags.contains("load");
boolean save = flags.contains("save");
int flag = (load ? LOAD : 0) | (save ? SAVE : 0);
new PrimitiveCollectionsBuilder(silent).setFlags(flag).process(force);
if(tests) {
createTests(silent, flag).process(force || forceTests);
createTesters(silent, flag).process(force || forceTests);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
catch(IOException e)
catch(InterruptedException | IOException e)
{
e.printStackTrace();
}

View File

@ -0,0 +1,8 @@
package speiger.src.builder;
@SuppressWarnings("javadoc")
public enum RequiredType
{
BI_CLASS,
ENUM
}

View File

@ -0,0 +1,125 @@
package speiger.src.builder;
import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;
import java.util.TreeSet;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter;
import speiger.src.builder.modules.BaseModule;
@SuppressWarnings("javadoc")
public class SettingsManager
{
boolean loaded;
JsonObject data = new JsonObject();
Set<String> moduleNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
public boolean isModuleEnabled(BaseModule base, ClassType keyType, ClassType valueType) {
if(!loaded) return true;
if(!isEnabled(data, base.getModuleName())) return false;
JsonObject result = getObject(data, keyType.getClassPath(), false);
if(!isEnabled(result, "Enabled")) return false;
if(base.isBiModule()) {
result = getObject(result, valueType.getClassPath(), false);
if(!isEnabled(result, "Enabled")) return false;
}
result = getObject(result, base.getModuleName(), false);
return (result.size() <= 0 || isEnabled(result, "Enabled")) && base.areDependenciesLoaded();
}
public boolean isModuleEnabled(BaseModule base, ClassType keyType, ClassType valueType, String entry)
{
if(!loaded) return true;
if(!isEnabled(data, base.getModuleName())) return false;
JsonObject result = getObject(data, keyType.getClassPath(), false);
if(!isEnabled(result, "Enabled")) return false;
if(base.isBiModule()) {
result = getObject(result, valueType.getClassPath(), false);
if(!isEnabled(result, "Enabled")) return false;
}
result = getObject(result, base.getModuleName(), false);
return (result.size() <= 0 || (isEnabled(result, "Enabled") && isEnabled(result, entry))) && base.areDependenciesLoaded();
}
public void addModule(BaseModule module) {
if(loaded) return;
String moduleName = module.getModuleName();
moduleNames.add(moduleName);
data.addProperty(moduleName, true);
if(module.isBiModule()) {
for(ClassType keyType : ModulePackage.TYPE) {
for(ClassType valueType : ModulePackage.TYPE) {
if(!module.isModuleValid(keyType, valueType)) continue;
JsonObject obj = new JsonObject();
obj.addProperty("Enabled", true);
for(String key : module.getModuleKeys(keyType, valueType)) {
obj.addProperty(key, true);
}
addModule(keyType, valueType, true, moduleName, obj);
}
}
return;
}
for(ClassType keyType : ModulePackage.TYPE) {
if(!module.isModuleValid(keyType, keyType)) continue;
JsonObject obj = new JsonObject();
obj.addProperty("Enabled", true);
for(String key : module.getModuleKeys(keyType, keyType)) {
obj.addProperty(key, true);
}
addModule(keyType, keyType, false, moduleName, obj);
}
}
public void load() {
try(BufferedReader reader = Files.newBufferedReader(Paths.get("ModulSettings.json"))) {
data = JsonParser.parseReader(reader).getAsJsonObject();
loaded = true;
}
catch(Exception e) { e.printStackTrace(); }
}
public void save() {
data.asMap().keySet().removeAll(moduleNames);
JsonObject result = new JsonObject();
for(String s : moduleNames) {
result.addProperty(s, true);
}
result.asMap().putAll(data.asMap());
try(JsonWriter writer = new JsonWriter(Files.newBufferedWriter(Paths.get("ModulSettings.json")))) {
writer.setIndent("\t");
Streams.write(result, writer);
}
catch(Exception e) { e.printStackTrace(); }
}
private void addModule(ClassType keyType, ClassType valueType, boolean bi, String moduleName, JsonObject obj) {
JsonObject result = getObject(data, keyType.getClassPath(), true);
if(bi) {
result = getObject(result, valueType.getClassPath(), true);
}
result.add(moduleName, obj);
}
private JsonObject getObject(JsonObject data, String name, boolean create) {
JsonObject obj = data.getAsJsonObject(name);
if(obj == null) {
obj = new JsonObject();
data.add(name, obj);
if(create) obj.addProperty("Enabled", true);
}
return obj;
}
private boolean isEnabled(JsonObject obj, String key) {
if(obj.has(key)) return obj.getAsJsonPrimitive(key).getAsBoolean();
return true;
}
}

View File

@ -0,0 +1,45 @@
package speiger.src.builder.modules;
@SuppressWarnings("javadoc")
public class AsyncModule extends BaseModule
{
public static final BaseModule INSTANCE = new AsyncModule();
@Override
public String getModuleName() { return "Async"; }
@Override
protected void loadVariables() {}
@Override
protected void loadRemappers() {}
@Override
protected void loadTestClasses() {}
@Override
protected void loadFunctions() {}
@Override
public boolean areDependenciesLoaded() { return isDependencyLoaded(CollectionModule.INSTANCE); }
@Override
protected void loadBlockades() {
if(!isModuleEnabled()) {
addBlockedFiles("AsyncBuilder", "Task");
}
}
@Override
protected void loadFlags() {
if(isModuleEnabled()) {
addKeyFlag("ASYNC_MODULE");
}
}
@Override
protected void loadClasses()
{
//Implementation Classes
addClassMapper("ASYNC_BUILDER", "AsyncBuilder");
//Abstract Classes
addAbstractMapper("BASE_TASK", "Base%sTask");
//Interfaces
addClassMapper("TASK", "Task");
}
}

View File

@ -0,0 +1,208 @@
package speiger.src.builder.modules;
import java.util.Collections;
import java.util.Set;
import java.util.function.Predicate;
import speiger.src.builder.ClassType;
import speiger.src.builder.ModulePackage;
import speiger.src.builder.RequiredType;
import speiger.src.builder.SettingsManager;
import speiger.src.builder.mappers.ArgumentMapper;
import speiger.src.builder.mappers.InjectMapper;
import speiger.src.builder.mappers.LineMapper;
import speiger.src.builder.mappers.SimpleMapper;
@SuppressWarnings("javadoc")
public abstract class BaseModule
{
SettingsManager manager;
ModulePackage entry;
protected ClassType keyType;
protected ClassType valueType;
public final void setManager(SettingsManager manager) {
this.manager = manager;
manager.addModule(this);
}
public final void init(ModulePackage entry) {
this.entry = entry;
keyType = entry.getKeyType();
valueType = entry.getValueType();
loadVariables();
loadClasses();
loadTestClasses();
loadFunctions();
loadRemappers();
loadBlockades();
loadFlags();
}
public final void cleanup() {
entry = null;
keyType = null;
valueType = null;
manager = null;
}
protected abstract void loadVariables();
protected abstract void loadClasses();
protected abstract void loadTestClasses();
protected abstract void loadFunctions();
protected abstract void loadRemappers();
protected abstract void loadBlockades();
protected abstract void loadFlags();
public abstract String getModuleName();
public boolean isBiModule() { return false; }
public Set<String> getModuleKeys(ClassType keyType, ClassType valueType) { return Collections.emptySet(); }
public boolean isModuleValid(ClassType keyType, ClassType valueType) { return true; }
public ClassType keyType() { return keyType; }
public ClassType valueType() { return valueType; }
protected boolean isModuleEnabled() {
return manager == null || manager.isModuleEnabled(this, keyType, valueType);
}
protected boolean isModuleEnabled(String name) {
return manager == null || manager.isModuleEnabled(this, keyType, valueType, name);
}
protected boolean isDependencyLoaded(BaseModule module) {
return isDependencyLoaded(module, true);
}
protected boolean isDependencyLoaded(BaseModule module, boolean key) {
return manager == null || (module.isBiModule() ? manager.isModuleEnabled(module, keyType, valueType) : (key ? manager.isModuleEnabled(module, keyType, keyType) : manager.isModuleEnabled(module, valueType, valueType)));
}
public boolean areDependenciesLoaded() {
return true;
}
protected void addFlag(String name) {
entry.addFlag(name);
}
protected void addValue(String name, int value) {
entry.addValue(name, value);
}
protected void addKeyFlag(String name) {
entry.addFlag(name);
entry.addGlobalFlag(keyType.getCapType()+"_"+name);
}
protected void addBiRequirement(String fileName) {
entry.addRequirement(fileName, RequiredType.BI_CLASS);
entry.addSplitter(fileName, "%1$s2%2$s");
}
protected void addEnumRequirement(String fileName) {
entry.addRequirement(fileName, RequiredType.ENUM);
entry.addSplitter(fileName, "%2$s");
}
protected void addBiRequirement(String fileName, String splitter) {
entry.addRequirement(fileName, RequiredType.BI_CLASS);
entry.addSplitter(fileName, "%1$s"+splitter+"%2$s");
}
protected void addRequirement(String fileName, String splitter, RequiredType type) {
entry.addRequirement(fileName, type);
entry.addSplitter(fileName, splitter);
}
protected void addRemapper(String fileName, String actualName) {
entry.addRemapper(fileName, actualName);
}
protected void addBlockedFiles(String... name) {
entry.addBlockedFiles(name);
}
protected void addBlockedFilter(Predicate<String> filter) {
entry.addBlockedFilter(filter);
}
protected void addClassMapper(String pattern, String replacement) {
entry.addMapper(new SimpleMapper("VALUE_"+pattern, "VALUE_"+pattern, valueType.getFileType()+replacement));
entry.addMapper(new SimpleMapper(pattern, pattern, keyType.getFileType()+replacement));
}
protected void addBiClassMapper(String pattern, String replacement, String splitter) {
entry.addMapper(new SimpleMapper("KEY_"+pattern, "KEY_"+pattern, keyType.getFileType()+splitter+keyType.getFileType()+replacement));
entry.addMapper(new SimpleMapper("VALUE_"+pattern, "VALUE_"+pattern, valueType.getFileType()+splitter+valueType.getFileType()+replacement));
entry.addMapper(new SimpleMapper(pattern, pattern, keyType.getFileType()+splitter+valueType.getFileType()+replacement));
}
protected void addAbstractMapper(String pattern, String replacement) {
entry.addMapper(new SimpleMapper("VALUE_"+pattern, "VALUE_"+pattern, String.format(replacement, valueType.getFileType())));
entry.addMapper(new SimpleMapper(pattern, pattern, String.format(replacement, keyType.getFileType())));
}
protected void addAbstractBiMapper(String pattern, String replacement, String splitter) {
entry.addMapper(new SimpleMapper(pattern, pattern, String.format(replacement, keyType.getFileType()+splitter+valueType.getFileType())));
}
protected void addFunctionMapper(String pattern, String replacement) {
entry.addMapper(new SimpleMapper("VALUE_"+pattern, "VALUE_"+pattern, replacement+valueType.getNonFileType()));
entry.addMapper(new SimpleMapper(pattern, pattern, replacement+keyType.getNonFileType()));
}
protected void addFunctionValueMapper(String pattern, String replacement) {
entry.addMapper(new SimpleMapper(pattern, pattern, replacement+valueType.getNonFileType()));
}
protected void addFunctionMappers(String pattern, String replacement) {
entry.addMapper(new SimpleMapper("VALUE_"+pattern, "VALUE_"+pattern, String.format(replacement, valueType.getNonFileType())));
entry.addMapper(new SimpleMapper(pattern, pattern, String.format(replacement, keyType.getNonFileType())));
}
protected void addFunctionValueMappers(String pattern, String replacement) {
entry.addMapper(new SimpleMapper(pattern, pattern, String.format(replacement, valueType.getNonFileType())));
}
protected void addSimpleMapper(String pattern, String replacement) {
entry.addMapper(new SimpleMapper(pattern, pattern, replacement));
}
protected void addAnnontion(String pattern, String value) {
if(keyType == ClassType.OBJECT) entry.addMapper(new LineMapper(pattern, pattern));
else entry.addMapper(new SimpleMapper(pattern, pattern, value));
}
protected void addValueAnnontion(String pattern, String value) {
if(valueType == ClassType.OBJECT) entry.addMapper(new LineMapper(pattern, pattern));
else entry.addMapper(new SimpleMapper(pattern, pattern, value));
}
protected void addComment(String pattern, String value) {
if(keyType == ClassType.OBJECT) entry.addMapper(new InjectMapper(pattern, pattern, value).removeBraces());
else entry.addMapper(new LineMapper(pattern, pattern));
}
protected void addValueComment(String pattern, String value) {
if(valueType == ClassType.OBJECT) entry.addMapper(new InjectMapper(pattern, pattern, value).removeBraces());
else entry.addMapper(new LineMapper(pattern, pattern));
}
protected InjectMapper addInjectMapper(String pattern, String replacement) {
InjectMapper mapper = new InjectMapper(pattern, pattern, replacement);
entry.addMapper(mapper);
return mapper;
}
protected ArgumentMapper addArgumentMapper(String pattern, String replacement) {
return addArgumentMapper(pattern, replacement, ", ");
}
protected ArgumentMapper addArgumentMapper(String pattern, String replacement, String splitter) {
ArgumentMapper mapper = new ArgumentMapper(pattern, pattern, replacement, splitter);
entry.addMapper(mapper);
return mapper;
}
}

View File

@ -0,0 +1,128 @@
package speiger.src.builder.modules;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc")
public class CollectionModule extends BaseModule
{
public static final BaseModule INSTANCE = new CollectionModule();
@Override
public String getModuleName() { return "Collection"; }
@Override
protected void loadVariables() {}
@Override
public boolean areDependenciesLoaded(){ return isDependencyLoaded(JavaModule.INSTANCE); }
@Override
public Set<String> getModuleKeys(ClassType keyType, ClassType valueType)
{
return new TreeSet<>(Arrays.asList("Streams", "Splititerators", "IArray", "Strategy"));
}
@Override
protected void loadFlags() {
if(isModuleEnabled()) addKeyFlag("COLLECTION_MODULE");
if(isModuleEnabled("Streams")) addKeyFlag("STREAM_FEATURE");
if(isModuleEnabled("Splititerators")) addKeyFlag("SPLIT_ITERATOR_FEATURE");
if(isModuleEnabled("IArray")) addKeyFlag("IARRAY_FEATURE");
}
@Override
protected void loadBlockades() {
if(!isModuleEnabled()) {
addBlockedFiles("Iterable", "Iterables", "Iterator", "Iterators", "BidirectionalIterator", "ListIterator");
addBlockedFiles("Arrays", "Collection", "AbstractCollection", "Collections", "Stack");
}
if(!isModuleEnabled("Splititerators")) addBlockedFiles("Splititerator", "Splititerators");
if(!isModuleEnabled("IArray")) addBlockedFiles("IArray");
if(!isModuleEnabled("Strategy")) addBlockedFiles("Strategy");
if(keyType.isObject()) {
addBlockedFiles("Stack");
addBlockedFiles("CollectionStreamTester");
}
if(keyType == ClassType.BOOLEAN) {
addBlockedFiles("CollectionRemoveIfTester", "CollectionStreamTester");
addBlockedFilter(T -> T.endsWith("Tester") && T.startsWith("Iterable"));
}
}
@Override
protected void loadRemappers()
{
//Main Classes
addRemapper("IArray", "I%sArray");
addRemapper("AbstractCollection", "Abstract%sCollection");
//Test Classes
addRemapper("AbstractIteratorTester", "Abstract%sIteratorTester");
addRemapper("MinimalCollection", "Minimal%sCollection");
addRemapper("TestCollectionGenerator", "Test%sCollectionGenerator");
addRemapper("AbstractContainerTester", "Abstract%sContainerTester");
addRemapper("AbstractCollectionTester", "Abstract%sCollectionTester");
addRemapper("SimpleTestGenerator", "Simple%sTestGenerator");
}
@Override
protected void loadFunctions()
{
addFunctionMapper("NEXT", "next");
addSimpleMapper("NEW_STREAM", keyType.isPrimitiveBlocking() ? "" : keyType.getCustomJDKType().getKeyType()+"Stream");
addFunctionMapper("PREVIOUS", "previous");
addFunctionMapper("REMOVE_KEY", "rem");
addSimpleMapper("TO_ARRAY", "to"+keyType.getNonFileType()+"Array");
addSimpleMapper("VALUE_TO_ARRAY", "to"+valueType.getNonFileType()+"Array");
}
@Override
protected void loadClasses()
{
//Abstract Classes
addAbstractMapper("ABSTRACT_COLLECTION", "Abstract%sCollection");
//Helper Classes
addClassMapper("ARRAYS", "Arrays");
addClassMapper("COLLECTIONS", "Collections");
addClassMapper("ITERABLES", "Iterables");
addClassMapper("SPLIT_ITERATORS", "Splititerators");
addClassMapper("ITERATORS", "Iterators");
//Interfaces
addClassMapper("COLLECTION", "Collection");
addClassMapper("ITERABLE", "Iterable");
addClassMapper("SPLIT_ITERATOR", "Splititerator");
addClassMapper("LIST_ITERATOR", "ListIterator");
addClassMapper("BI_ITERATOR", "BidirectionalIterator");
addClassMapper("ITERATOR", "Iterator");
if(keyType.isObject()) addSimpleMapper("STACK", "Stack");
else addClassMapper("STACK", "Stack");
addClassMapper("STRATEGY", "Strategy");
}
@Override
protected void loadTestClasses()
{
//Implementation Classes
addAbstractMapper("MINIMAL_COLLECTION", "Minimal%sCollection");
addClassMapper("BIDIRECTIONAL_ITERATOR_TESTER", "BidirectionalteratorTester");
addClassMapper("LIST_ITERATOR_TESTER", "ListIteratorTester");
addClassMapper("ITERATOR_TESTER", "IteratorTester");
addClassMapper("COLLECTION_TEST_BUILDER", "CollectionTestSuiteBuilder");
addClassMapper("COLLECTION_CONSTRUCTOR_TESTS", "CollectionConstructorTests");
//Abstract Classes
addAbstractMapper("ABSTRACT_COLLECTION_TESTER", "Abstract%sCollectionTester");
addAbstractMapper("ABSTRACT_CONTAINER_TESTER", "Abstract%sContainerTester");
addAbstractMapper("ABSTRACT_ITERATOR_TESTER", "Abstract%sIteratorTester");
//Helper Classes
addAbstractMapper("TEST_COLLECTION_GENERATOR", "Test%sCollectionGenerator");
addAbstractMapper("SIMPLE_TEST_GENERATOR", "Simple%sTestGenerator");
}
}

View File

@ -0,0 +1,107 @@
package speiger.src.builder.modules;
import speiger.src.builder.ClassType;
import speiger.src.builder.RequiredType;
@SuppressWarnings("javadoc")
public class FunctionModule extends BaseModule
{
public static final BaseModule INSTANCE = new FunctionModule();
@Override
public String getModuleName() { return "Function"; }
@Override
public boolean isBiModule() { return true; }
@Override
protected void loadVariables() {}
@Override
protected void loadFlags() {}
@Override
protected void loadTestClasses() {}
@Override
protected void loadBlockades()
{
if(keyType.isObject()) addBlockedFiles("Consumer", "Comparator");
}
@Override
protected void loadRemappers()
{
addBiRequirement("BiConsumer", "");
addBiRequirement("UnaryOperator", "");
if(valueType == ClassType.BOOLEAN) {
addRequirement("Function", "%1$s", RequiredType.BI_CLASS);
addRemapper("Function", (keyType.isObject() ? "" : "%s")+"Predicate");
}
else if(keyType.isObject() && !valueType.isObject()) {
addRequirement("Function", "%2$s", RequiredType.BI_CLASS);
addRemapper("Function", "To%sFunction");
}
else if(keyType == valueType) {
addRequirement("Function", "%1$s", RequiredType.BI_CLASS);
addRemapper("Function", (keyType.isObject() ? "" : "%s")+"UnaryOperator");
}
else if(valueType.isObject()) {
addRequirement("Function", "%1$s", RequiredType.BI_CLASS);
addRemapper("Function", "%sFunction");
}
else addBiRequirement("Function");
addRemapper("BiConsumer", "%sConsumer");
}
@Override
protected void loadFunctions()
{
addSimpleMapper("APPLY", keyType.getApply(valueType));
addSimpleMapper("SUPPLY_GET", keyType.isObject() ? "get" : "getAs"+keyType.getNonFileType());
addSimpleMapper("VALUE_SUPPLY_GET", valueType.isObject() ? "get" : "getAs"+valueType.getNonFileType());
}
@Override
protected void loadClasses()
{
//Interfaces
addBiClassMapper("BI_CONSUMER", "Consumer", "");
addClassMapper("BI_TO_OBJECT_CONSUMER", "ObjectConsumer");
addAbstractMapper("BI_FROM_OBJECT_CONSUMER", "Object%sConsumer");
addAbstractMapper("BI_FROM_INT_CONSUMER", "Int%sConsumer");
if(keyType.isObject()) {
addSimpleMapper("TO_OBJECT_FUNCTION", keyType.getNonFileType()+"UnaryOperator");
addSimpleMapper("VALUE_TO_OBJECT_FUNCTION", valueType.isObject() ? "UnaryOperator" : valueType.getFileType()+"Function");
}
else {
addSimpleMapper("TO_OBJECT_FUNCTION", keyType.getNonFileType()+"Function");
addSimpleMapper("VALUE_TO_OBJECT_FUNCTION", valueType.isObject() ? "UnaryOperator" : valueType.getFileType()+"Function");
}
if(valueType == ClassType.BOOLEAN) addFunctionMappers("FUNCTION", "%sPredicate");
else if(keyType.isObject() && !valueType.isObject()) addFunctionValueMappers("FUNCTION", "To%sFunction");
else if(keyType == valueType) addFunctionMappers("FUNCTION", "%sUnaryOperator");
else if(valueType.isObject()) addFunctionMappers("FUNCTION", "%sFunction");
else addBiClassMapper("FUNCTION", "Function", "2");
addFunctionMappers("PREDICATE", "%sPredicate");
addClassMapper("SUPPLIER", "Supplier");
addAbstractMapper("SINGLE_UNARY_OPERATOR", "%1$s%1$sUnaryOperator");
addBiClassMapper("UNARY_OPERATOR", "UnaryOperator", "");
if(keyType.isObject())
{
if(!valueType.isObject()) addSimpleMapper("VALUE_CONSUMER", valueType.getFileType()+"Consumer");
else addSimpleMapper("VALUE_CONSUMER", "Consumer");
addSimpleMapper("CONSUMER", "Consumer");
addSimpleMapper("IARRAY", "IObjectArray");
}
else
{
if(valueType.isObject())
{
addSimpleMapper("VALUE_CONSUMER", "Consumer");
addSimpleMapper("CONSUMER", keyType.getFileType()+"Consumer");
}
else addClassMapper("CONSUMER", "Consumer");
addFunctionMappers("IARRAY", "I%sArray");
}
addSimpleMapper("VALUE_COMPARATOR", valueType.isObject() ? "Comparator" : String.format("%sComparator", valueType.getNonFileType()));
addSimpleMapper("COMPARATOR", keyType.isObject() ? "Comparator" : String.format("%sComparator", keyType.getNonFileType()));
}
}

View File

@ -0,0 +1,221 @@
package speiger.src.builder.modules;
import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc")
public class JavaModule extends BaseModule
{
public static final BaseModule INSTANCE = new JavaModule();
@Override
public String getModuleName() { return "Base"; }
@Override
protected void loadVariables()
{
createHelperVars(keyType, false, "KEY");
createHelperVars(valueType, true, "VALUE");
loadBaseVariables();
}
@Override
protected void loadFlags()
{
addFlag("TYPE_"+keyType.getCapType());
addFlag("VALUE_"+valueType.getCapType());
addValue("JAVA_VERSION", getVersion());
if(keyType == valueType) addFlag("SAME_TYPE");
if(keyType.hasFunction(valueType)) addFlag("JDK_FUNCTION");
if(!keyType.needsCustomJDKType()) addFlag("JDK_TYPE");
if(!keyType.isPrimitiveBlocking()) addFlag("PRIMITIVES");
if(!valueType.isPrimitiveBlocking()) addFlag("VALUE_PRIMITIVES");
if(!valueType.needsCustomJDKType()) addFlag("JDK_VALUE");
}
private int getVersion() {
String version = System.getProperty("java.version");
if(version.startsWith("1.")) return Integer.parseInt(version.substring(2, 3));
int dot = version.indexOf(".");
return Integer.parseInt(dot != -1 ? version.substring(0, dot) : version);
}
@Override
protected void loadRemappers() {}
@Override
protected void loadBlockades() {}
@Override
protected void loadFunctions()
{
addSimpleMapper("APPLY_KEY_VALUE", keyType.isObject() ? "apply" : "applyAs"+keyType.getNonFileType());
addSimpleMapper("APPLY_VALUE", valueType.isObject() ? "apply" : "applyAs"+valueType.getNonFileType());
addSimpleMapper("APPLY_CAST", "applyAs"+keyType.getCustomJDKType().getNonFileType());
//Shared by Maps and Pairs so moved to java.
addFunctionMappers("ENTRY_KEY", "get%sKey");
addFunctionValueMappers("ENTRY_VALUE", "get%sValue");
addFunctionMappers("KEY_ENTRY", "set%sKey");
addFunctionValueMappers("VALUE_ENTRY", "set%sValue");
}
@Override
protected void loadClasses()
{
if(getVersion() >= 17) addSimpleMapper("RANDOM", "RandomGenerator");
else addSimpleMapper("RANDOM", "Random");
addSimpleMapper("JAVA_PREDICATE", keyType.isPrimitiveBlocking() ? "" : keyType.getCustomJDKType().getFileType()+"Predicate");
addSimpleMapper("JAVA_CONSUMER", keyType.isPrimitiveBlocking() ? "" : "java.util.function."+keyType.getCustomJDKType().getFileType()+"Consumer");
addSimpleMapper("JAVA_SUPPLIER", keyType.isPrimitiveBlocking() ? "" : "java.util.function."+keyType.getCustomJDKType().getFileType()+"Supplier");
addSimpleMapper("JAVA_FUNCTION", keyType.getFunctionClass(valueType));
addSimpleMapper("JAVA_BINARY_OPERATOR", keyType == ClassType.BOOLEAN ? "" : (keyType.isObject() ? "java.util.function.BinaryOperator" : "java.util.function."+keyType.getCustomJDKType().getFileType()+"BinaryOperator"));
addSimpleMapper("JAVA_UNARY_OPERATOR", keyType.isObject() ? "BinaryOperator" : keyType == ClassType.BOOLEAN ? "" : keyType.getCustomJDKType().getFileType()+"UnaryOperator");
addSimpleMapper("JAVA_SPLIT_ITERATOR", keyType.isPrimitiveBlocking() ? "Spliterator" : "Of"+keyType.getCustomJDKType().getFileType());
addSimpleMapper("JAVA_STREAM", keyType.isPrimitiveBlocking() ? "" : keyType.getCustomJDKType().getFileType()+"Stream");
addSimpleMapper("JAVA_BUFFER", keyType.getFileType()+"Buffer");
}
@Override
protected void loadTestClasses()
{
addClassMapper("HELPERS", "Helpers");
addClassMapper("SAMPLE_ELEMENTS", "Samples");
}
private void loadBaseVariables()
{
addSimpleMapper("VALUE_PACKAGE", valueType.getPathType());
addSimpleMapper("PACKAGE", keyType.getPathType());
addSimpleMapper("CLASS_TYPE", keyType.getClassType());
addSimpleMapper("CLASS_VALUE_TYPE", valueType.getClassValueType());
addSimpleMapper("KEY_TYPE", keyType.getKeyType());
addSimpleMapper("KEY_OBJECT_TYPE", keyType.isObject() ? "Object" : keyType.getKeyType());
addSimpleMapper("KEY_STRING_TYPE", keyType.isObject() ? "String" : keyType.getKeyType());
addSimpleMapper("KEY_SPECIAL_TYPE", keyType.isObject() ? "E" : keyType.getKeyType());
addSimpleMapper("CLASS_OBJECT_TYPE", keyType.getClassType());
addSimpleMapper("CLASS_OBJECT_VALUE_TYPE", valueType.getClassValueType());
addSimpleMapper("CLASS_STRING_TYPE", keyType.isObject() ? "String" : keyType.getClassType());
addSimpleMapper("CLASS_STRING_VALUE_TYPE", valueType.isObject() ? "String" : valueType.getClassValueType());
addSimpleMapper("VALUE_TYPE", valueType.getValueType());
addSimpleMapper("VALUE_OBJECT_TYPE", valueType.isObject() ? "Object" : valueType.getValueType());
addSimpleMapper("VALUE_STRING_TYPE", valueType.isObject() ? "String" : valueType.getValueType());
addSimpleMapper("VALUE_SPECIAL_TYPE", valueType.isObject() ? "E" : valueType.getKeyType());
addSimpleMapper("KEY_JAVA_TYPE", keyType.getCustomJDKType().getKeyType());
addSimpleMapper("VALUE_JAVA_TYPE", keyType.getCustomJDKType().getKeyType());
addSimpleMapper("EMPTY_KEY_VALUE", keyType.getEmptyValue());
addSimpleMapper("EMPTY_VALUE", valueType.getEmptyValue());
addSimpleMapper("INVALID_KEY_VALUE", keyType.getInvalidValue());
addSimpleMapper("INVALID_VALUE", valueType.getInvalidValue());
addSimpleMapper(" KEY_STRING_GENERIC_TYPE", keyType.isObject() ? "<String>" : "");
addSimpleMapper(" VALUE_STRING_GENERIC_TYPE", valueType.isObject() ? "<String>" : "");
addSimpleMapper(" KEY_VALUE_STRING_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<String, String>" : "<String>") : (valueType.isObject() ? "<String>" : ""));
addSimpleMapper(" KEY_SAME_GENERIC_TYPE", keyType.isObject() ? "<T, T>" : "");
addSimpleMapper(" VALUE_SAME_GENERIC_TYPE", keyType.isObject() ? "<V, V>" : "");
addSimpleMapper(" KEY_GENERIC_TYPE", keyType.isObject() ? "<"+keyType.getKeyType()+">" : "");
addSimpleMapper(" KEY_KEY_GENERIC_TYPE", keyType.isObject() ? "<"+keyType.getKeyType()+", "+keyType.getKeyType()+">" : "");
addSimpleMapper(" KEY_CLASS_GENERIC_TYPE", keyType.isObject() ? "<"+keyType.getClassType()+">" : "");
addSimpleMapper(" VALUE_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getValueType()+">" : "");
addSimpleMapper(" VALUE_VALUE_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getValueType()+", "+valueType.getValueType()+">" : "");
addSimpleMapper(" VALUE_CLASS_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getClassValueType()+">" : "");
addSimpleMapper(" KEY_VALUE_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<"+keyType.getKeyType()+", "+valueType.getValueType()+">" : "<"+keyType.getKeyType()+">") : (valueType.isObject() ? "<"+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_VALUE_VALUE_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<"+keyType.getKeyType()+", "+valueType.getValueType()+", "+valueType.getValueType()+">" : "<"+keyType.getKeyType()+">") : (valueType.isObject() ? "<"+valueType.getValueType()+", "+valueType.getValueType()+">" : ""));
addInjectMapper(" KEY_VALUE_SPECIAL_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<"+keyType.getKeyType()+", "+valueType.getValueType()+", %s>" : "<"+keyType.getKeyType()+", %s>") : (valueType.isObject() ? "<"+valueType.getValueType()+", %s>" : "<%s>")).setBraceType("<>").removeBraces();
addSimpleMapper(" NO_GENERIC_TYPE", keyType.isObject() ? "<?>" : "");
addSimpleMapper(" NO_KV_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<?, ?>" : "<?>") : valueType.isObject() ? "<?>" : "");
addSimpleMapper(" KEY_COMPAREABLE_TYPE", keyType.isObject() ? "<"+keyType.getKeyType()+" extends Comparable<T>>" : "");
addSimpleMapper(" KEY_SUPER_GENERIC_TYPE", keyType.isObject() ? "<? super "+keyType.getKeyType()+">" : "");
addSimpleMapper(" VALUE_SUPER_GENERIC_TYPE", valueType.isObject() ? "<? super "+valueType.getValueType()+">" : "");
addSimpleMapper(" KEY_VALUE_SUPER_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<? super "+keyType.getKeyType()+", ? super "+valueType.getValueType()+">" : "<? super "+keyType.getKeyType()+">") : (valueType.isObject() ? "<? super "+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_UNKNOWN_GENERIC_TYPE", keyType.isObject() ? "<? extends "+keyType.getKeyType()+">" : "");
addSimpleMapper(" VALUE_UNKNOWN_GENERIC_TYPE", valueType.isObject() ? "<? extends "+valueType.getValueType()+">" : "");
addSimpleMapper(" KEY_VALUE_UNKNOWN_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<? extends "+keyType.getKeyType()+", ? extends "+valueType.getValueType()+">" : "<? extends "+keyType.getKeyType()+">") : (valueType.isObject() ? "<? extends "+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_ENUM_VALUE_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<"+keyType.getKeyType()+" extends Enum<"+keyType.getKeyType()+">, "+valueType.getValueType()+">" : "<"+keyType.getKeyType()+" extends Enum<"+keyType.getKeyType()+">>") : (valueType.isObject() ? "<"+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_VALUE_ENUM_GENERIC_TYPE", keyType.isObject() ? (valueType.isObject() ? "<"+keyType.getKeyType()+", "+valueType.getValueType()+" extends Enum<"+valueType.getValueType()+">>" : "<"+keyType.getKeyType()+">") : (valueType.isObject() ? "<"+valueType.getValueType()+" extends Enum<"+valueType.getValueType()+">>" : ""));
addInjectMapper(" KEY_SPECIAL_GENERIC_TYPE", keyType.isObject() ? "<%s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" VALUE_SPECIAL_GENERIC_TYPE", valueType.isObject() ? "<%s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" KSK_GENERIC_TYPE", keyType.isObject() ? "<%s, "+keyType.getKeyType()+">" : "<%s>").removeBraces().setBraceType("<>");
addInjectMapper(" KKS_GENERIC_TYPE", keyType.isObject() ? "<"+keyType.getKeyType()+", %s>" : "<%s>").removeBraces().setBraceType("<>");
addArgumentMapper(" KSS_GENERIC_TYPE", keyType.isObject() ? "<%1$s, %2$s>" : "<%2$s>").removeBraces().setBraceType("<>");
addInjectMapper(" SK_GENERIC_TYPE", keyType.isObject() ? "<%s, "+keyType.getKeyType()+">" : "").removeBraces().setBraceType("<>");
addInjectMapper(" KS_GENERIC_TYPE", keyType.isObject() ? "<"+keyType.getKeyType()+", %s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" VSV_GENERIC_TYPE", valueType.isObject() ? "<%s, "+valueType.getValueType()+">" : "<%s>").removeBraces().setBraceType("<>");
addInjectMapper(" VVS_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getValueType()+", %s>" : "<%s>").removeBraces().setBraceType("<>");
addArgumentMapper(" VSS_GENERIC_TYPE", valueType.isObject() ? "<%1$s, %2$s>" : "<%2$s>").removeBraces().setBraceType("<>");
addInjectMapper(" SV_GENERIC_TYPE", valueType.isObject() ? "<%s, "+valueType.getValueType()+">" : "").removeBraces().setBraceType("<>");
addInjectMapper(" VS_GENERIC_TYPE", valueType.isObject() ? "<"+valueType.getValueType()+", %s>" : "").removeBraces().setBraceType("<>");
addSimpleMapper(" GENERIC_KEY_BRACES", keyType.isObject() ? " <"+keyType.getKeyType()+">" : "");
addSimpleMapper(" GENERIC_VALUE_BRACES", valueType.isObject() ? " <"+valueType.getValueType()+">" : "");
addInjectMapper(" GENERIC_SPECIAL_KEY_BRACES", keyType.isObject() ? " <%s>" : "").removeBraces().setBraceType("<>");
addInjectMapper(" GENERIC_SPECIAL_VALUE_BRACES", valueType.isObject() ? " <%s>" : "").removeBraces().setBraceType("<>");
addSimpleMapper(" GENERIC_KEY_ENUM_VALUE_BRACES", keyType.isObject() ? (valueType.isObject() ? " <"+keyType.getKeyType()+" extends Enum<"+keyType.getKeyType()+">, "+valueType.getValueType()+">" : " <"+keyType.getKeyType()+" extends Enum<"+keyType.getKeyType()+">>") : (valueType.isObject() ? " <"+valueType.getValueType()+">" : ""));
addInjectMapper(" GENERIC_KEY_SPECIAL_BRACES", keyType.isObject() ? " <"+keyType.getKeyType()+", %s>" : " <%s>").removeBraces().setBraceType("<>");
addInjectMapper(" GENERIC_VALUE_SPECIAL_BRACES", valueType.isObject() ? " <"+valueType.getKeyType()+", %s>" : " <%s>").removeBraces().setBraceType("<>");
addSimpleMapper(" GENERIC_KEY_VALUE_BRACES", keyType.isObject() ? (valueType.isObject() ? " <"+keyType.getKeyType()+", "+valueType.getValueType()+">" : " <"+keyType.getKeyType()+">") : (valueType.isObject() ? " <"+valueType.getValueType()+">" : ""));
addSimpleMapper(" COMPAREABLE_KEY_BRACES", keyType.isObject() ? " <"+keyType.getKeyType()+" extends Comparable<T>>" : "");
addSimpleMapper("KV_BRACES", keyType.isObject() || valueType.isObject() ? "<>" : "");
addSimpleMapper("VALUE_BRACES", valueType.isObject() ? "<>" : "");
addSimpleMapper("BRACES", keyType.isObject() ? "<>" : "");
if(keyType.needsCustomJDKType())
{
addSimpleMapper("JAVA_TYPE", keyType.getCustomJDKType().getKeyType());
addSimpleMapper("SANITY_CAST", "castTo"+keyType.getFileType());
}
addSimpleMapper("JAVA_CLASS", keyType.getCustomJDKType().getClassType());
if(valueType.needsCustomJDKType())
{
addSimpleMapper("SANITY_CAST_VALUE", "castTo"+valueType.getFileType());
}
addSimpleMapper("[SPACE]", " ");
addComment("@ArrayType", "@param <%s> the keyType of array that the operation should be applied");
addComment("@Type", "@param <%s> the keyType of elements maintained by this Collection");
addValueComment("@ValueArrayType", "@param <%s> the keyType of array that the operation should be applied");
addValueComment("@ValueType", "@param <%s> the keyType of elements maintained by this Collection");
addAnnontion("@PrimitiveOverride", "@Override");
addSimpleMapper("@PrimitiveDoc", "");
addAnnontion("@Primitive", "@Deprecated");
addValueAnnontion("@ValuePrimitiveOverride", "@Override");
addValueAnnontion("@ValuePrimitive", "@Deprecated");
}
private void createHelperVars(ClassType type, boolean value, String fix)
{
addArgumentMapper("EQUALS_"+fix+"_TYPE", "Objects.equals(%2$s, "+(type.isObject() ? "%1$s" : fix+"_TO_OBJ(%1$s)")+")").removeBraces();
addInjectMapper(fix+"_EQUALS_NOT_NULL", type.getComparableValue()+" != "+(type.isPrimitiveBlocking() || type.needsCast() ? type.getEmptyValue() : "0")).removeBraces();
addInjectMapper(fix+"_EQUALS_NULL", type.getComparableValue()+" == "+(type.isPrimitiveBlocking() || type.needsCast() ? type.getEmptyValue() : "0")).removeBraces();
addArgumentMapper(fix+"_EQUALS_NOT", type.getEquals(true)).removeBraces();
addArgumentMapper(fix+"_EQUALS", type.getEquals(false)).removeBraces();
addSimpleMapper("FILE_"+fix+"_TYPE", type.getFileType());
addArgumentMapper("COMPAREABLE_TO_"+fix, type.isObject() ? "((Comparable<"+type.getKeyType(value)+">)%1$s).compareTo(("+type.getKeyType(value)+")%2$s)" : type.getClassType(value)+".compare(%1$s, %2$s)").removeBraces();
addArgumentMapper("COMPARE_TO_"+fix, type.isObject() ? "%1$s.compareTo(%2$s)" : type.getClassType(value)+".compare(%1$s, %2$s)").removeBraces();
addInjectMapper(fix+"_TO_OBJ", type.isObject() ? "%s" : type.getClassType(value)+".valueOf(%s)").removeBraces();
addInjectMapper("OBJ_TO_"+fix, type.isObject() ? "%s" : "%s."+type.getKeyType(value)+"Value()").removeBraces();
addInjectMapper("CLASS_TO_"+fix, type.isObject() ? "("+type.getKeyType(value)+")%s" : "(("+type.getClassType(value)+")%s)."+type.getKeyType(value)+"Value()").removeBraces();
addInjectMapper(fix+"_TO_HASH", type.isObject() ? "Objects.hashCode(%s)" : type.getClassType(value)+".hashCode(%s)").removeBraces();
addInjectMapper(fix+"_TO_STRING", type.isObject() ? "Objects.toString(%s)" : type.getClassType(value)+".toString(%s)").removeBraces();
addSimpleMapper("CAST_"+fix+"_ARRAY ", type.isObject() ? "("+fix+"_TYPE[])" : "");
addSimpleMapper("EMPTY_"+fix+"_ARRAY", type.isObject() ? "("+fix+"_TYPE[])ARRAYS.EMPTY_ARRAY" : "ARRAYS.EMPTY_ARRAY");
addInjectMapper("NEW_"+fix+"_ARRAY", type.isObject() ? "("+fix+"_TYPE[])new Object[%s]" : "new "+fix+"_TYPE[%s]").removeBraces();
addInjectMapper("NEW_SPECIAL_"+fix+"_ARRAY", type.isObject() ? "(E[])new Object[%s]" : "new "+fix+"_TYPE[%s]").removeBraces();
if(value) addInjectMapper("NEW_CLASS_VALUE_ARRAY", type.isObject() ? "(CLASS_VALUE_TYPE[])new Object[%s]" : "new CLASS_VALUE_TYPE[%s]").removeBraces();
else addInjectMapper("NEW_CLASS_ARRAY", type.isObject() ? "(CLASS_TYPE[])new Object[%s]" : "new CLASS_TYPE[%s]").removeBraces();
}
}

View File

@ -0,0 +1,118 @@
package speiger.src.builder.modules;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc")
public class ListModule extends BaseModule
{
public static final BaseModule INSTANCE = new ListModule();
@Override
public String getModuleName() { return "List"; }
@Override
protected void loadVariables() {}
@Override
protected void loadFlags() {
if(isModuleEnabled()) addKeyFlag("LIST_MODULE");
if(isModuleEnabled("Wrappers")) addKeyFlag("LISTS_FEATURE");
boolean implementations = isModuleEnabled("Implementations");
if(implementations && isModuleEnabled("ArrayList")) addKeyFlag("ARRAY_LIST_FEATURE");
if(implementations && isModuleEnabled("LinkedList")) addKeyFlag("LINKED_LIST_FEATURE");
if(implementations && isModuleEnabled("ImmutableList")) addKeyFlag("IMMUTABLE_LIST_FEATURE");
if(implementations && isModuleEnabled("CopyOnWriteList")) addKeyFlag("COPY_ON_WRITE_LIST_FEATURE");
}
@Override
protected void loadBlockades()
{
if(!isModuleEnabled("Wrappers")) addBlockedFiles("Lists");
boolean implementations = !isModuleEnabled("Implementations");
if(implementations || !isModuleEnabled("ArrayList")) addBlockedFiles("ArrayList");
if(implementations || !isModuleEnabled("LinkedList")) addBlockedFiles("LinkedList");
if(implementations || !isModuleEnabled("ImmutableList")) addBlockedFiles("ImmutableList");
if(implementations || !isModuleEnabled("CopyOnWriteList")) addBlockedFiles("CopyOnWriteList");
if(!isModuleEnabled()) addBlockedFiles("List", "AbstractList");
if(keyType.isObject()) addBlockedFiles("ListFillBufferTester");
if(keyType == ClassType.BOOLEAN) addBlockedFiles("ListFillBufferTester", "ListReplaceAllTester");
}
@Override
public Set<String> getModuleKeys(ClassType keyType, ClassType valueType) {
return new TreeSet<>(Arrays.asList("Implementations", "Wrappers", "ArrayList", "LinkedList", "ImmutableList", "CopyOnWriteList"));
}
@Override
public boolean areDependenciesLoaded() {
return isDependencyLoaded(CollectionModule.INSTANCE);
}
@Override
protected void loadRemappers()
{
//Main Classes
addRemapper("AbstractList", "Abstract%sList");
addRemapper("ImmutableList", "Immutable%sList");
addRemapper("CopyOnWriteList", "CopyOnWrite%sArrayList");
//Test Classes
addRemapper("AbstractListTester", "Abstract%sListTester");
addRemapper("AbstractListIndexOfTester", "Abstract%sListIndexOfTester");
addRemapper("TestListGenerator", "Test%sListGenerator");
}
@Override
protected void loadFunctions()
{
addFunctionMapper("GET_KEY", "get");
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");
}
@Override
protected void loadClasses()
{
//Implementation Classes
addClassMapper("ARRAY_LIST", "ArrayList");
addAbstractMapper("COPY_ON_WRITE_LIST", "CopyOnWrite%sArrayList");
addClassMapper("LINKED_LIST", "LinkedList");
addAbstractMapper("IMMUTABLE_LIST", "Immutable%sList");
//Abstract Classes
addAbstractMapper("ABSTRACT_LIST", "Abstract%sList");
//SubClasses
addClassMapper("SUB_LIST", "SubList");
addClassMapper("LIST_ITER", "ListIter");
//Helper Classes
addClassMapper("LISTS", "Lists");
//Interfaces
addClassMapper("LIST", "List");
}
@Override
protected void loadTestClasses()
{
//Implementation Classes
addClassMapper("LIST_TEST_BUILDER", "ListTestSuiteBuilder");
addClassMapper("LIST_TESTS", "ListTests");
//Abstract Classes
addAbstractMapper("ABSTRACT_LIST_INDEX_OF_TESTER", "Abstract%sListIndexOfTester");
addAbstractMapper("ABSTRACT_LIST_TESTER", "Abstract%sListTester");
//Helper classes
addAbstractMapper("TEST_LIST_GENERATOR", "Test%sListGenerator");
}
}

View File

@ -0,0 +1,288 @@
package speiger.src.builder.modules;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc")
public class MapModule extends BaseModule
{
public static final BaseModule INSTANCE = new MapModule();
@Override
public String getModuleName() { return "Map"; }
@Override
public boolean isBiModule() { return true; }
@Override
protected void loadVariables() {}
@Override
public boolean isModuleValid(ClassType keyType, ClassType valueType) { return keyType != ClassType.BOOLEAN; }
@Override
public boolean areDependenciesLoaded() { return isDependencyLoaded(SetModule.INSTANCE) && isDependencyLoaded(CollectionModule.INSTANCE, false); }
@Override
public Set<String> getModuleKeys(ClassType keyType, ClassType valueType) {
Set<String> sets = new TreeSet<>();
sets.addAll(Arrays.asList("Wrappers", "Implementations"));
sets.addAll(Arrays.asList("OrderedMap", "SortedMap"));
sets.addAll(Arrays.asList("ArrayMap", "ConcurrentMap", "ImmutableMap"));
sets.addAll(Arrays.asList("HashMap", "LinkedHashMap"));
sets.addAll(Arrays.asList("CustomHashMap", "LinkedCustomHashMap"));
sets.addAll(Arrays.asList("EnumMap", "LinkedEnumMap"));
sets.addAll(Arrays.asList("AVLTreeMap", "RBTreeMap"));
return sets;
}
@Override
protected void loadFlags()
{
if(isModuleEnabled()) addFlag("MAP_MODULE");
if(isModuleEnabled("Wrappers")) addFlag("MAPS_FEATURE");
boolean implementations = isModuleEnabled("Implementations");
boolean hashMap = implementations && isModuleEnabled("HashMap");
boolean customHashMap = implementations && isModuleEnabled("CustomHashMap");
boolean enumMap = implementations && isModuleEnabled("EnumMap");
if(isModuleEnabled("OrderedMap")) {
addFlag("ORDERED_MAP_FEATURE");
if(isModuleEnabled("ArrayMap")) addFlag("ARRAY_MAP_FEATURE");
if(hashMap && isModuleEnabled("LinkedHashMap")) addFlag("LINKED_MAP_FEATURE");
if(customHashMap && isModuleEnabled("LinkedCustomHashMap")) addFlag("LINKED_CUSTOM_MAP_FEATURE");
if(enumMap && isModuleEnabled("LinkedEnumMap")) addFlag("LINKED_ENUM_MAP_FEATURE");
}
if(isModuleEnabled("SortedMap")) {
addFlag("SORTED_MAP_FEATURE");
if(implementations && isModuleEnabled("AVLTreeMap")) addFlag("AVL_TREE_MAP_FEATURE");
if(implementations && isModuleEnabled("RBTreeMap")) addFlag("RB_TREE_MAP_FEATURE");
}
if(implementations && isModuleEnabled("ConcurrentMap")) addFlag("CONCURRENT_MAP_FEATURE");
if(implementations && isModuleEnabled("ImmutableMap")) addFlag("IMMUTABLE_MAP_FEATURE");
if(hashMap) addFlag("MAP_FEATURE");
if(customHashMap) addFlag("CUSTOM_MAP_FEATURE");
if(enumMap) addFlag("ENUM_MAP_FEATURE");
}
@Override
protected void loadBlockades()
{
if(!isModuleEnabled()) addBlockedFiles("Map", "AbstractMap");
if(!isModuleEnabled("Wrappers")) addBlockedFiles("Maps");
boolean implementations = !isModuleEnabled("Implementations");
if(implementations || !isModuleEnabled("ImmutableMap")) addBlockedFiles("ImmutableOpenHashMap");
if(implementations || !isModuleEnabled("ConcurrentMap")) addBlockedFiles("ConcurrentMap", "ConcurrentOpenHashMap");
boolean ordered = !isModuleEnabled("OrderedMap");
if(ordered) addBlockedFiles("OrderedMap");
boolean hashMap = implementations || !isModuleEnabled("HashMap");
if(hashMap) addBlockedFiles("OpenHashMap");
if(hashMap || ordered || !isModuleEnabled("LinkedHashMap")) addBlockedFiles("LinkedOpenHashMap");
boolean customHashMap = implementations || !isModuleEnabled("CustomHashMap");
if(customHashMap) addBlockedFiles("OpenCustomHashMap");
if(customHashMap || ordered || !isModuleEnabled("LinkedCustomHashMap")) addBlockedFiles("LinkedOpenCustomHashMap");
boolean enumMap = implementations || !isModuleEnabled("EnumMap");
if(enumMap) addBlockedFiles("EnumMap");
if(enumMap || ordered || !isModuleEnabled("LinkedEnumMap")) addBlockedFiles("LinkedEnumMap");
if(ordered || !isModuleEnabled("ArrayMap")) addBlockedFiles("ArrayMap");
boolean sorted = !isModuleEnabled("SortedMap");
if(sorted) addBlockedFiles("SortedMap", "NavigableMap");
if(implementations || sorted || !isModuleEnabled("AVLTreeMap")) addBlockedFiles("AVLTreeMap");
if(implementations || sorted || !isModuleEnabled("RBTreeMap")) addBlockedFiles("RBTreeMap");
if(keyType == ClassType.BOOLEAN)
{
//Main Classes
addBlockedFiles("SortedMap", "NavigableMap", "RBTreeMap", "AVLTreeMap");
addBlockedFiles("OrderedMap", "ArrayMap", "LinkedOpenHashMap", "LinkedOpenCustomHashMap");
addBlockedFiles("ConcurrentMap", "ConcurrentOpenHashMap");
addBlockedFiles("Map", "Maps", "AbstractMap", "ImmutableOpenHashMap", "OpenHashMap", "OpenCustomHashMap");
//Test Classes
addBlockedFiles("TestMap", "MapTests", "MapTestSuiteBuilder", "MapConstructorTests", "TestMapGenerator", "SimpleMapTestGenerator", "DerivedMapGenerators", "AbstractMapTester");
addBlockedFiles("TestSortedMapGenerator", "OrderedMapTestSuiteBuilder", "NavigableMapTestSuiteBuilder", "SortedMapTestSuiteBuilder");
addBlockedFiles("TestOrderedMapGenerator");
addBlockedFilter(T -> T.endsWith("Tester") && (T.startsWith("Map") || T.startsWith("OrderedMap") || T.startsWith("SortedMap") || T.startsWith("NavigableMap")));
}
if(valueType == ClassType.OBJECT) {
addBlockedFiles("MapComputeIfAbsentNonDefaultTester", "MapComputeIfPresentNonDefaultTester", "MapComputeNonDefaultTester", "MapSupplyIfAbsentNonDefaultTester");
}
}
@Override
protected void loadRemappers()
{
//Main Classes
addBiRequirement("Map");
addBiRequirement("SortedMap");
addBiRequirement("OrderedMap");
addBiRequirement("NavigableMap");
addBiRequirement("ConcurrentMap");
addBiRequirement("AbstractMap");
addEnumRequirement("EnumMap");
addEnumRequirement("LinkedEnumMap");
addBiRequirement("ConcurrentOpenHashMap");
addBiRequirement("ImmutableOpenHashMap");
addBiRequirement("OpenHashMap");
addBiRequirement("LinkedOpenHashMap");
addBiRequirement("OpenCustomHashMap");
addBiRequirement("LinkedOpenCustomHashMap");
addBiRequirement("ArrayMap");
addBiRequirement("RBTreeMap");
addBiRequirement("AVLTreeMap");
addBiRequirement("Maps");
addRemapper("AbstractMap", "Abstract%sMap");
addRemapper("EnumMap", "Enum2%sMap");
addRemapper("LinkedEnumMap", "LinkedEnum2%sMap");
addRemapper("ImmutableOpenHashMap", "Immutable%sOpenHashMap");
//Test Classes
addBiRequirement("TestMapGenerator");
addBiRequirement("TestSortedMapGenerator");
addBiRequirement("TestOrderedMapGenerator");
addBiRequirement("SimpleMapTestGenerator");
addBiRequirement("DerivedMapGenerators");
addBiRequirement("AbstractMapTester");
addBiRequirement("MapTestSuiteBuilder");
addBiRequirement("SortedMapTestSuiteBuilder");
addBiRequirement("NavigableMapTestSuiteBuilder");
addBiRequirement("OrderedMapTestSuiteBuilder");
addBiRequirement("MapTests");
addBiRequirement("MapConstructorTests");
addBiRequirement("TestMap");
addBiRequirement("MapAddToTester");
addBiRequirement("MapSubFromTester");
addBiRequirement("MapClearTester");
addBiRequirement("MapComputeIfAbsentTester");
addBiRequirement("MapComputeIfPresentTester");
addBiRequirement("MapComputeTester");
addBiRequirement("MapComputeIfAbsentNonDefaultTester");
addBiRequirement("MapComputeIfPresentNonDefaultTester");
addBiRequirement("MapComputeNonDefaultTester");
addBiRequirement("MapCopyTester");
addBiRequirement("MapContainsTester");
addBiRequirement("MapContainsKeyTester");
addBiRequirement("MapContainsValueTester");
addBiRequirement("MapCreatorTester");
addBiRequirement("MapEntrySetTester");
addBiRequirement("MapEqualsTester");
addBiRequirement("MapForEachTester");
addBiRequirement("MapGetOrDefaultTester");
addBiRequirement("MapGetTester");
addBiRequirement("MapHashCodeTester");
addBiRequirement("MapIsEmptyTester");
addBiRequirement("MapMergeTester");
addBiRequirement("MapMergeBulkTester");
addBiRequirement("MapPutAllArrayTester");
addBiRequirement("MapPutAllTester");
addBiRequirement("MapPutIfAbsentTester");
addBiRequirement("MapPutTester");
addBiRequirement("MapRemoveEntryTester");
addBiRequirement("MapRemoveOrDefaultTester");
addBiRequirement("MapRemoveTester");
addBiRequirement("MapReplaceAllTester");
addBiRequirement("MapReplaceEntryTester");
addBiRequirement("MapReplaceTester");
addBiRequirement("MapSizeTester");
addBiRequirement("MapSupplyIfAbsentTester");
addBiRequirement("MapSupplyIfAbsentNonDefaultTester");
addBiRequirement("MapToStringTester");
addBiRequirement("NavigableMapNavigationTester");
addBiRequirement("SortedMapNavigationTester");
addBiRequirement("OrderedMapNavigationTester");
addBiRequirement("OrderedMapMoveTester");
addBiRequirement("MapConstructorTester");
addRemapper("TestMapGenerator", "Test%sMapGenerator");
addRemapper("TestSortedMapGenerator", "Test%sSortedMapGenerator");
addRemapper("TestOrderedMapGenerator", "Test%sOrderedMapGenerator");
addRemapper("SimpleMapTestGenerator", "Simple%sMapTestGenerator");
addRemapper("DerivedMapGenerators", "Derived%sMapGenerators");
addRemapper("AbstractMapTester", "Abstract%sMapTester");
addRemapper("TestMap", "Test%sMap");
}
@Override
protected void loadFunctions()
{
addFunctionValueMapper("BULK_MERGE", "mergeAll");
addFunctionValueMappers("COMPUTE_IF_ABSENT", "compute%sIfAbsent");
addFunctionValueMappers("COMPUTE_IF_PRESENT", "compute%sIfPresent");
addFunctionValueMapper("COMPUTE", "compute");
addFunctionMapper("DEQUEUE_LAST", "dequeueLast");
addFunctionMapper("DEQUEUE", "dequeue");
addSimpleMapper("ENTRY_SET", keyType.getFileType().toLowerCase()+"2"+valueType.getFileType()+"EntrySet");
addFunctionMappers("FIRST_ENTRY_KEY", "first%sKey");
addFunctionValueMappers("FIRST_ENTRY_VALUE", "first%sValue");
if(keyType.isObject()) addFunctionValueMapper("GET_VALUE", valueType.isObject() ? "getObject" : "get");
else addSimpleMapper("GET_VALUE", "get");
addFunctionMappers("LAST_ENTRY_KEY", "last%sKey");
addFunctionValueMappers("LAST_ENTRY_VALUE", "last%sValue");
addFunctionValueMapper("MERGE", "merge");
addFunctionMappers("POLL_FIRST_ENTRY_KEY", "pollFirst%sKey");
addFunctionMappers("POLL_LAST_ENTRY_KEY", "pollLast%sKey");
if(keyType.isObject()) addFunctionMapper("REMOVE_VALUE", "rem");
else addSimpleMapper("REMOVE_VALUE", "remove");
addFunctionMapper("REMOVE", "remove");
addFunctionValueMappers("REPLACE_VALUES", valueType.isObject() ? "replaceObjects" : "replace%ss");
addFunctionValueMappers("SUPPLY_IF_ABSENT", "supply%sIfAbsent");
}
@Override
protected void loadClasses()
{
//Implementation Classes
addAbstractBiMapper("IMMUTABLE_HASH_MAP", "Immutable%sOpenHashMap", "2");
addBiClassMapper("LINKED_CUSTOM_HASH_MAP", "LinkedOpenCustomHashMap", "2");
addBiClassMapper("LINKED_HASH_MAP", "LinkedOpenHashMap", "2");
addBiClassMapper("CUSTOM_HASH_MAP", "OpenCustomHashMap", "2");
addBiClassMapper("CONCURRENT_HASH_MAP", "ConcurrentOpenHashMap", "2");
addBiClassMapper("AVL_TREE_MAP", "AVLTreeMap", "2");
addBiClassMapper("RB_TREE_MAP", "RBTreeMap", "2");
addFunctionValueMappers("LINKED_ENUM_MAP", valueType.isObject() ? "LinkedEnum2ObjectMap" : "LinkedEnum2%sMap");
addFunctionValueMappers("ENUM_MAP", valueType.isObject() ? "Enum2ObjectMap" : "Enum2%sMap");
addBiClassMapper("HASH_MAP", "OpenHashMap", "2");
addBiClassMapper("ARRAY_MAP", "ArrayMap", "2");
//Abstract Classes
addAbstractBiMapper("ABSTRACT_MAP", "Abstract%sMap", "2");
//Helper Classes
addBiClassMapper("MAPS", "Maps", "2");
//Interfaces
addBiClassMapper("NAVIGABLE_MAP", "NavigableMap", "2");
addBiClassMapper("ORDERED_MAP", "OrderedMap", "2");
addBiClassMapper("SORTED_MAP", "SortedMap", "2");
addBiClassMapper("CONCURRENT_MAP", "ConcurrentMap", "2");
addBiClassMapper("MAP", "Map", "2");
}
@Override
protected void loadTestClasses()
{
//Implementation Classes
addAbstractBiMapper("SIMPLE_TEST_MAP", "Test%sMap", "2");
addBiClassMapper("MAP_TESTS", "MapTests", "2");
addAbstractBiMapper("NAVIGABLE_MAP_TEST_BUILDER", "%sNavigableMapTestSuiteBuilder", "2");
addAbstractBiMapper("SORTED_MAP_TEST_BUILDER", "%sSortedMapTestSuiteBuilder", "2");
addAbstractBiMapper("ORDERED_MAP_TEST_BUILDER", "%sOrderedMapTestSuiteBuilder", "2");
addAbstractBiMapper("MAP_TEST_BUILDER", "%sMapTestSuiteBuilder", "2");
//Abstract Classes
addAbstractBiMapper("ABSTRACT_MAP_TESTER", "Abstract%sMapTester", "2");
//Helper Classes
addAbstractBiMapper("MAP_CONSTRUCTOR_TESTS", "%sMapConstructorTests", "2");
addAbstractBiMapper("SIMPLE_MAP_TEST_GENERATOR", "Simple%sMapTestGenerator", "2");
addAbstractBiMapper("DERIVED_MAP_GENERATORS", "Derived%sMapGenerators", "2");
addAbstractBiMapper("TEST_ORDERED_MAP_GENERATOR", "Test%sOrderedMapGenerator", "2");
addAbstractBiMapper("TEST_SORTED_MAP_GENERATOR", "Test%sSortedMapGenerator", "2");
addAbstractBiMapper("TEST_MAP_GENERATOR", "Test%sMapGenerator", "2");
}
}

View File

@ -0,0 +1,60 @@
package speiger.src.builder.modules;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc")
public class PairModule extends BaseModule
{
public static final BaseModule INSTANCE = new PairModule();
@Override
public String getModuleName() { return "Pair"; }
@Override
public boolean isBiModule() { return true; }
@Override
protected void loadVariables() {}
@Override
protected void loadFunctions() {}
@Override
protected void loadTestClasses() {}
@Override
public Set<String> getModuleKeys(ClassType keyType, ClassType valueType) { return new TreeSet<>(Arrays.asList("Mutable", "Immutable")); }
@Override
protected void loadFlags() {
if(isModuleEnabled()) addFlag("PAIR_MODULE");
if(isModuleEnabled("Mutable")) addFlag("MUTABLE_PAIR");
if(isModuleEnabled("Immutable")) addFlag("IMMUTABLE_PAIR");
}
@Override
protected void loadBlockades() {
if(!isModuleEnabled()) addBlockedFiles("Pair");
if(!isModuleEnabled("Mutable")) addBlockedFiles("MutablePair");
if(!isModuleEnabled("Immutable")) addBlockedFiles("ImmutablePair");
}
@Override
protected void loadRemappers() {
//Main Classes
addBiRequirement("Pair", "");
addBiRequirement("MutablePair", "");
addBiRequirement("ImmutablePair", "");
//Test Classes
addBiRequirement("PairTester", "");
}
@Override
protected void loadClasses() {
//Implementations
addBiClassMapper("IMMUTABLE_PAIR", "ImmutablePair", "");
addBiClassMapper("MUTABLE_PAIR", "MutablePair", "");
//Interfaces
addBiClassMapper("PAIR", "Pair", "");
}
}

View File

@ -0,0 +1,101 @@
package speiger.src.builder.modules;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc")
public class PrioQueueModule extends BaseModule
{
public static final BaseModule INSTANCE = new PrioQueueModule();
@Override
public String getModuleName() { return "PriorityQueue"; }
@Override
protected void loadVariables() {}
@Override
protected void loadFunctions() {}
@Override
public boolean areDependenciesLoaded() { return isDependencyLoaded(CollectionModule.INSTANCE); }
@Override
public Set<String> getModuleKeys(ClassType keyType, ClassType valueType) {
return new TreeSet<>(Arrays.asList("Wrappers", "Implementations", "Dequeue", "FiFoQueue", "HeapQueue", "ArrayPrioQueue"));
}
@Override
protected void loadFlags() {
if(isModuleEnabled()) addFlag("QUEUE_MODULE");
if(isModuleEnabled("Wrappers")) addKeyFlag("QUEUES_FEATURE");
boolean implementations = isModuleEnabled("Implementations");
if(isModuleEnabled("Dequeue")) {
addKeyFlag("DEQUEUE_FEATURE");
if(implementations && isModuleEnabled("FiFoQueue")) addKeyFlag("FIFO_QUEUE_FEATURE");
}
if(implementations && isModuleEnabled("HeapQueue")) addKeyFlag("HEAP_QUEUE_FEATURE");
if(implementations && isModuleEnabled("ArrayPrioQueue")) addKeyFlag("ARRAY_QUEUE_FEATURE");
}
@Override
protected void loadBlockades() {
if(!isModuleEnabled()) addBlockedFiles("PriorityQueue", "AbstractPriorityQueue");
if(!isModuleEnabled("Wrappers")) addBlockedFiles("PriorityQueues");
boolean implementations = !isModuleEnabled("Implementations");
boolean dequeue = !isModuleEnabled("Dequeue");
if(dequeue) addBlockedFiles("PriorityDequeue");
if(dequeue || implementations || !isModuleEnabled("FiFoQueue")) addBlockedFiles("ArrayFIFOQueue");
if(implementations || !isModuleEnabled("HeapQueue")) addBlockedFiles("HeapPriorityQueue");
if(implementations || !isModuleEnabled("ArrayPrioQueue")) addBlockedFiles("ArrayPriorityQueue");
if(keyType == ClassType.BOOLEAN) {
addBlockedFiles("QueueTests");
}
}
@Override
protected void loadRemappers() {
//Main Classes
addRemapper("AbstractPriorityQueue", "Abstract%sPriorityQueue");
//Test Classes
addRemapper("TestQueueGenerator", "Test%sQueueGenerator");
addRemapper("AbstractQueueTester", "Abstract%sQueueTester");
addRemapper("SimpleQueueTestGenerator", "Simple%sQueueTestGenerator");
}
@Override
protected void loadClasses() {
//Implementation Classes
addClassMapper("ARRAY_FIFO_QUEUE", "ArrayFIFOQueue");
addClassMapper("ARRAY_PRIORITY_QUEUE", "ArrayPriorityQueue");
addClassMapper("HEAP_PRIORITY_QUEUE", "HeapPriorityQueue");
//Abstract Classes
addAbstractMapper("ABSTRACT_PRIORITY_QUEUE", "Abstract%sPriorityQueue");
//Helper Classes
addClassMapper("PRIORITY_QUEUES", "PriorityQueues");
//Interfaces
addClassMapper("PRIORITY_QUEUE", "PriorityQueue");
addClassMapper("PRIORITY_DEQUEUE", "PriorityDequeue");
}
@Override
protected void loadTestClasses()
{
//Implementation Classes
addClassMapper("DEQUEUE_TEST_BUILDER", "DequeueTestSuiteBuilder");
addClassMapper("QUEUE_TEST_BUILDER", "QueueTestSuiteBuilder");
addClassMapper("QUEUE_TESTS", "QueueTests");
//Abstract Classes
addAbstractMapper("ABSTRACT_QUEUE_TESTER", "Abstract%sQueueTester");
//Helper Classes
addAbstractMapper("SIMPLE_QUEUE_TEST_GENERATOR", "Simple%sQueueTestGenerator");
addAbstractMapper("TEST_QUEUE_GENERATOR", "Test%sQueueGenerator");
}
}

View File

@ -0,0 +1,174 @@
package speiger.src.builder.modules;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import speiger.src.builder.ClassType;
@SuppressWarnings("javadoc")
public class SetModule extends BaseModule
{
public static final BaseModule INSTANCE = new SetModule();
@Override
public String getModuleName() { return "Set"; }
@Override
protected void loadVariables() {}
@Override
public boolean isModuleValid(ClassType keyType, ClassType valueType) { return keyType != ClassType.BOOLEAN; }
@Override
public boolean areDependenciesLoaded() { return isDependencyLoaded(CollectionModule.INSTANCE); }
@Override
public Set<String> getModuleKeys(ClassType keyType, ClassType valueType) {
Set<String> sets = new TreeSet<>();
sets.addAll(Arrays.asList("Wrappers", "Implementations"));
sets.addAll(Arrays.asList("OrderedSet", "SortedSet"));
sets.addAll(Arrays.asList("ArraySet", "ImmutableSet"));
sets.addAll(Arrays.asList("HashSet", "LinkedHashSet"));
sets.addAll(Arrays.asList("CustomHashSet", "LinkedCustomHashSet"));
sets.addAll(Arrays.asList("AVLTreeSet", "RBTreeSet"));
return sets;
}
@Override
protected void loadFlags()
{
if(isModuleEnabled()) addFlag("SET_MODULE");
if(isModuleEnabled("Wrappers")) addFlag("SETS_FEATURE");
boolean implementations = isModuleEnabled("Implementations");
boolean hashSet = implementations && isModuleEnabled("HashSet");
boolean customHashSet = implementations && isModuleEnabled("CustomHashSet");
if(isModuleEnabled("OrderedSet")) {
addFlag("ORDERED_SET_FEATURE");
if(implementations && isModuleEnabled("ArraySet")) addFlag("ARRAY_SET_FEATURE");
if(hashSet && isModuleEnabled("LinkedHashSet")) addFlag("LINKED_SET_FEATURE");
if(customHashSet && isModuleEnabled("LinkedCustomHashSet")) addFlag("LINKED_CUSTOM_SET_FEATURE");
}
if(isModuleEnabled("SortedSet")) {
addFlag("SORTED_SET_FEATURE");
if(implementations && isModuleEnabled("AVLTreeSet")) addFlag("AVL_TREE_SET_FEATURE");
if(implementations && isModuleEnabled("RBTreeSet")) addFlag("RB_TREE_SET_FEATURE");
}
if(implementations && isModuleEnabled("ImmutableSet")) addFlag("IMMUTABLE_SET_FEATURE");
if(hashSet) addFlag("HASH_SET_FEATURE");
if(customHashSet) addFlag("CUSTOM_HASH_SET_FEATURE");
}
@Override
protected void loadBlockades()
{
if(!isModuleEnabled()) addBlockedFiles("Set", "AbstractSet");
if(!isModuleEnabled("Wrappers")) addBlockedFiles("Sets");
boolean implementations = !isModuleEnabled("Implementations");
if(implementations || !isModuleEnabled("ImmutableSet")) addBlockedFiles("ImmutableOpenHashSet");
boolean ordered = !isModuleEnabled("OrderedSet");
if(ordered) addBlockedFiles("OrderedSet");
boolean hashSet = implementations || !isModuleEnabled("HashSet");
if(hashSet) addBlockedFiles("OpenHashSet");
if(hashSet || ordered || !isModuleEnabled("LinkedHashSet")) addBlockedFiles("LinkedOpenHashSet");
boolean customHashSet = implementations || !isModuleEnabled("CustomHashSet");
if(customHashSet) addBlockedFiles("OpenCustomHashSet");
if(customHashSet || ordered || !isModuleEnabled("LinkedCustomHashSet")) addBlockedFiles("LinkedOpenCustomHashSet");
if(implementations || ordered || !isModuleEnabled("ArraySet")) addBlockedFiles("ArraySet");
boolean sorted = !isModuleEnabled("SortedSet");
if(sorted) addBlockedFiles("SortedSet", "NavigableSet");
if(implementations || sorted || !isModuleEnabled("AVLTreeSet")) addBlockedFiles("AVLTreeSet");
if(implementations || sorted || !isModuleEnabled("RBTreeSet")) addBlockedFiles("RBTreeSet");
if(keyType == ClassType.BOOLEAN)
{
//Main Classes
addBlockedFiles("SortedSet", "NavigableSet", "AVLTreeSet", "RBTreeSet");
addBlockedFiles("OrderedSet", "ArraySet", "LinkedOpenHashSet", "LinkedOpenCustomHashSet");
addBlockedFiles("Set", "Sets", "AbstractSet", "OpenHashSet", "OpenCustomHashSet", "ImmutableOpenHashSet");
//Test Classes
addBlockedFiles("SetTests", "SetTestSuiteBuilder", "TestSetGenerator");
addBlockedFiles("OrderedSetTestSuiteBuilder", "TestOrderedSetGenerator", "OrderedSetMoveTester", "OrderedSetNavigationTester", "OrderedSetIterationTester");
addBlockedFiles("SortedSetTestSuiteBuilder", "TestSortedSetGenerator", "SortedSetNaviationTester", "SortedSetSubsetTestSetGenerator", "SortedSetIterationTester", "SortedSetNaviationTester");
addBlockedFiles("NavigableSetTestSuiteBuilder", "TestNavigableSetGenerator", "NavigableSetNavigationTester");
addBlockedFiles("MinimalSet", "AbstractSetTester", "SetAddAllTester", "SetAddTester", "SetCreationTester", "SetEqualsTester", "SetRemoveTester");
}
}
@Override
protected void loadRemappers()
{
//Main Classes
addRemapper("AbstractSet", "Abstract%sSet");
addRemapper("ImmutableOpenHashSet", "Immutable%sOpenHashSet");
//Test Classes
addRemapper("MinimalSet", "Minimal%sSet");
addRemapper("TestNavigableSetGenerator", "Test%sNavigableSetGenerator");
addRemapper("TestSortedSetGenerator", "Test%sSortedSetGenerator");
addRemapper("TestOrderedSetGenerator", "Test%sOrderedSetGenerator");
addRemapper("TestSetGenerator", "Test%sSetGenerator");
addRemapper("AbstractSetTester", "Abstract%sSetTester");
}
@Override
protected void loadFunctions()
{
addFunctionMapper("POLL_FIRST_KEY", "pollFirst");
addFunctionMapper("POLL_LAST_KEY", "pollLast");
addFunctionMapper("FIRST_KEY", "first");
addFunctionMapper("LAST_KEY", "last");
}
@Override
protected void loadTestClasses()
{
//Implementation Classes
addAbstractMapper("MINIMAL_SET", "Minimal%sSet");
addClassMapper("ORDERED_SET_TEST_BUILDER", "OrderedSetTestSuiteBuilder");
addClassMapper("SORTED_SET_TEST_BUILDER", "SortedSetTestSuiteBuilder");
addClassMapper("NAVIGABLE_SET_TEST_BUILDER", "NavigableSetTestSuiteBuilder");
addClassMapper("SET_TEST_BUILDER", "SetTestSuiteBuilder");
addClassMapper("SET_TESTS", "SetTests");
//Abstract Classes
addAbstractMapper("ABSTRACT_SET_TESTER", "Abstract%sSetTester");
//Helper Classes
addClassMapper("SUB_SORTED_SET_CLASS_GENERATOR", "SortedSetSubsetTestSetGenerator");
addClassMapper("SUB_NAVIGABLE_SET_CLASS_GENERATOR", "NavigableSetSubsetTestSetGenerator");
addAbstractMapper("TEST_NAVIGABLE_SET_GENERATOR", "Test%sNavigableSetGenerator");
addAbstractMapper("TEST_SORTED_SET_GENERATOR", "Test%sSortedSetGenerator");
addAbstractMapper("TEST_ORDERED_SET_GENERATOR", "Test%sOrderedSetGenerator");
addAbstractMapper("TEST_SET_GENERATOR", "Test%sSetGenerator");
}
@Override
protected void loadClasses()
{
//Implementation Classes
addClassMapper("LINKED_CUSTOM_HASH_SET", "LinkedOpenCustomHashSet");
addClassMapper("LINKED_HASH_SET", "LinkedOpenHashSet");
addAbstractMapper("IMMUTABLE_HASH_SET", "Immutable%sOpenHashSet");
addClassMapper("CUSTOM_HASH_SET", "OpenCustomHashSet");
addClassMapper("HASH_SET", "OpenHashSet");
addClassMapper("RB_TREE_SET", "RBTreeSet");
addClassMapper("AVL_TREE_SET", "AVLTreeSet");
addClassMapper("ARRAY_SET", "ArraySet");
//Abstract Classes
addAbstractMapper("ABSTRACT_SET", "Abstract%sSet");
//Helper Classes
addClassMapper("SETS", "Sets");
//Interfaces
addClassMapper("NAVIGABLE_SET", "NavigableSet");
addClassMapper("SORTED_SET", "SortedSet");
addClassMapper("ORDERED_SET", "OrderedSet");
addClassMapper("SET", "Set");
}
}

View File

@ -3,10 +3,14 @@ package speiger.src.collections.PACKAGE.collections;
import java.util.Collection;
import java.util.Objects;
import java.util.AbstractCollection;
#if TYPE_OBJECT
import java.util.function.Consumer;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#endif
/**
@ -35,6 +39,9 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
return modified;
}
@Override
public COLLECTION KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
@ -76,23 +83,30 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
@Override
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) {
Objects.requireNonNull(c);
if(c.isEmpty()) return true;
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();)
if(!contains(iter.NEXT()))
return false;
return true;
}
@Override
public boolean containsAll(Collection<?> c) {
Objects.requireNonNull(c);
return c instanceof COLLECTION ? containsAll((COLLECTION KEY_GENERIC_TYPE)c) : super.containsAll(c);
}
/**
* This implementation iterates over the elements of the collection and checks if they are stored in this collection
* @param c the elements that should be checked for
* @return true if any element is in this collection
* @deprecated if this is a primitive collection
* @throws java.lang.NullPointerException if the collection is null
*/
@Override
@Primitive
public boolean containsAny(Collection<?> c) {
Objects.requireNonNull(c);
if(c.isEmpty()) return false;
for(Object e : c)
if(contains(e))
return true;
@ -108,6 +122,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
@Override
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) {
Objects.requireNonNull(c);
if(c.isEmpty()) return false;
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();)
if(contains(iter.NEXT()))
return true;
@ -150,6 +165,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) {
Objects.requireNonNull(c);
if(c.isEmpty()) return false;
boolean modified = false;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
if(c.contains(iter.NEXT())) {
@ -160,6 +176,23 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
return modified;
}
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
Objects.requireNonNull(c);
if(c.isEmpty()) return false;
Objects.requireNonNull(r);
boolean modified = false;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
KEY_TYPE e = iter.NEXT();
if(c.contains(e)) {
r.accept(e);
iter.remove();
modified = true;
}
}
return modified;
}
/**
* A Type-Specific implementation of retainAll. This Implementation iterates over all elements and removes them as they were not found in the other collection.
* @param c the elements that should be kept
@ -184,6 +217,28 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
return modified;
}
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
Objects.requireNonNull(c);
Objects.requireNonNull(r);
if(c.isEmpty()) {
boolean modified = !isEmpty();
forEach(r);
clear();
return modified;
}
boolean modified = false;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
KEY_TYPE e = iter.NEXT();
if(!c.contains(e)) {
r.accept(e);
iter.remove();
modified = true;
}
}
return modified;
}
#if !TYPE_OBJECT
/**
* A Type-Specific implementation of toArray that links to {@link #TO_ARRAY(KEY_TYPE[])} with a newly created array.
@ -191,6 +246,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
*/
@Override
public KEY_TYPE[] TO_ARRAY() {
if(isEmpty()) return ARRAYS.EMPTY_ARRAY;
return TO_ARRAY(new KEY_TYPE[size()]);
}
@ -203,6 +259,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) a = new KEY_TYPE[size()];
ITERATORS.unwrap(a, iterator());
if (a.length > size()) a[size()] = EMPTY_KEY_VALUE;
return a;
}
#endif

View File

@ -57,7 +57,7 @@ public interface BI_ITERATOR KEY_GENERIC_TYPE extends ITERATOR KEY_GENERIC_TYPE
public default int back(int amount) {
if(amount < 0) throw new IllegalStateException("Can't go forward");
int i = 0;
for(;i<amount && hasPrevious();previous(),i++);
for(;i<amount && hasPrevious();PREVIOUS(),i++);
return i;
}
}

View File

@ -5,20 +5,29 @@ import java.util.Collection;
import java.util.Objects;
import java.util.function.JAVA_PREDICATE;
import java.util.function.Predicate;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
import java.util.stream.JAVA_STREAM;
import java.util.stream.StreamSupport;
#endif
#endif
#if TYPE_OBJECT
import java.util.function.Consumer;
import java.util.function.IntFunction;
#else
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
#if SPLIT_ITERATOR_FEATURE
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT
#endif
import speiger.src.collections.PACKAGE.utils.COLLECTIONS;
import speiger.src.collections.utils.ISizeProvider;
import speiger.src.collections.utils.SanityChecks;
#endif
/**
* A Type-Specific {@link Collection} that reduces (un)boxing
* @Type(T)
*/
public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITERABLE KEY_GENERIC_TYPE
public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITERABLE KEY_GENERIC_TYPE, ISizeProvider
{
#if !TYPE_OBJECT
/**
@ -36,6 +45,38 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
*/
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c);
/**
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
* @param e the elements that should be added
* @return if the collection was modified
*/
public default boolean addAll(KEY_TYPE... e) { return addAll(e, 0, e.length); }
/**
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
* @param e the elements that should be added
* @param length how many elements of the array should be added
* @return if the collection was modified
*/
public default boolean addAll(KEY_TYPE[] e, int length) { return addAll(e, 0, length); }
/**
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
* @param e the elements that should be added
* @param offset where to start within the array
* @param length how many elements of the array should be added
* @return if the collection was modified
*/
public default boolean addAll(KEY_TYPE[] e, int offset, int length) {
if(length <= 0) return false;
SanityChecks.checkArrayCapacity(e.length, offset, length);
boolean added = false;
for(int i = 0;i<length;i++) {
if(add(e[offset+i])) added = true;
}
return added;
}
#if !TYPE_OBJECT
/**
* A Type-Specific contains function to reduce (un)boxing
@ -86,6 +127,16 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
*/
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c);
/**
* A Type-Specific removeAll function that reduces (un)boxing.
* It also notifies the remover of which exact element is going to be removed.
* @param c the collection of elements that should be removed
* @param r elements that got removed
* @return true if any element was removed
* @see Collection#removeAll(Collection)
*/
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r);
/**
* A Type-Specific retainAll function that reduces (un)boxing.
* @param c the collection of elements that should be kept
@ -94,7 +145,49 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
*/
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c);
#if !TYPE_OBJECT
/**
* A Type-Specific retainAll function that reduces (un)boxing.
* It also notifies the remover of which exact element is going to be removed.
* @param c the collection of elements that should be kept
* @param r elements that got removed
* @return true if any element was removed
* @see Collection#retainAll(Collection)
*/
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r);
/**
* A Helper function to reduce the usage of Streams and allows to collect all elements
* @param collection that the elements should be inserted to
* @param <E> the collection type
* @return the input with the desired elements
*/
default <E extends COLLECTION KEY_GENERIC_TYPE> E pour(E collection) {
collection.addAll(this);
return collection;
}
/**
* A Function that does a shallow clone of the Collection itself.
* This function is more optimized then a copy constructor since the Collection does not have to be unsorted/resorted.
* It can be compared to Cloneable but with less exception risk
* @return a Shallow Copy of the collection
* @note Wrappers and view collections will not support this feature
*/
public COLLECTION KEY_GENERIC_TYPE copy();
#if TYPE_OBJECT
/**
* A Helper function that simplifies the process of creating a new Array.
* @param action the array creation function
* @param <E> the returning arrayType
* @return an array containing all of the elements in this collection
* @see Collection#toArray(Object[])
*/
default <E> E[] TO_ARRAY(IntFunction<E[]> action) {
return TO_ARRAY(action.apply(size()));
}
#else
/**
* A Type-Specific toArray function that delegates to {@link #TO_ARRAY(KEY_TYPE[])} with a newly created array.
* @return an array containing all of the elements in this collection
@ -181,6 +274,29 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
@Override
public ITERATOR KEY_GENERIC_TYPE iterator();
/**
* Creates a Wrapped Collection that is Synchronized
* @return a new Collection that is synchronized
* @see COLLECTIONS#synchronize
*/
public default COLLECTION KEY_GENERIC_TYPE synchronize() { return COLLECTIONS.synchronize(this); }
/**
* Creates a Wrapped Collection that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new Collection Wrapper that is synchronized
* @see COLLECTIONS#synchronize
*/
public default COLLECTION KEY_GENERIC_TYPE synchronize(Object mutex) { return COLLECTIONS.synchronize(this, mutex); }
/**
* Creates a Wrapped Collection that is unmodifiable
* @return a new Collection Wrapper that is unmodifiable
* @see COLLECTIONS#unmodifiable
*/
public default COLLECTION KEY_GENERIC_TYPE unmodifiable() { return COLLECTIONS.unmodifiable(this); }
#if SPLIT_ITERATOR_FEATURE
#if PRIMITIVES
/**
* Returns a Java-Type-Specific Stream to reduce boxing/unboxing.
@ -193,11 +309,15 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
* @return a Stream of the closest java type
*/
default JAVA_STREAM parallelPrimitiveStream() { return StreamSupport.NEW_STREAM(SPLIT_ITERATORS.createJavaSplititerator(this, 0), true); }
#endif
#if STREAM_FEATURE
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing
* @return type specific splititerator
*/
@Override
default SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createSplititerator(this, 0); }
#endif
#endif
}

View File

@ -1,18 +1,83 @@
package speiger.src.collections.PACKAGE.collections;
import java.util.Objects;
#if !TYPE_OBJECT
import java.util.function.Consumer;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.objects.collections.ObjectIterable;
#else
import java.util.function.BiFunction;
import java.util.function.IntFunction;
import java.util.Comparator;
#if BOOLEAN_COLLECTION_MODULE
import speiger.src.collections.booleans.collections.BooleanIterable;
#endif
#iterate
#argument OUTPUT_ITERABLE ByteIterable ShortIterable IntIterable LongIterable FloatIterable DoubleIterable
#argument MAPPER ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument PACKAGE bytes shorts ints longs floats doubles
#argument FILTER_TYPE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
import speiger.src.collections.objects.functions.function.MAPPER;
import speiger.src.collections.PACKAGE.collections.OUTPUT_ITERABLE;
#endif
#enditerate
#endif
import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION;
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if ARRAY_LIST_FEATURE || LINKED_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.LIST;
#if ARRAY_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
#else
import speiger.src.collections.PACKAGE.lists.LINKED_LIST;
#endif
#endif
#if SET_MODULE && !TYPE_BOOLEAN
#if LINKED_SET_FEATURE || LINKED_CUSTOM_SET_FEATURE || SET_FEATURE || CUSTOM_SET_FEATURE || RB_TREE_SET_FEATURE || AVL_TREE_SET_FEATURE || ARRAY_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.SET;
#if LINKED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.LINKED_HASH_SET;
#else if LINKED_CUSTOM_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.LINKED_CUSTOM_HASH_SET;
#else if SET_FEATURE
import speiger.src.collections.PACKAGE.sets.HASH_SET;
#else if CUSTOM_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.CUSTOM_HASH_SET;
#else if RB_TREE_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.RB_TREE_SET;
#else if AVL_TREE_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.AVL_TREE_SET;
#else if ARRAY_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.ARRAY_SET;
#endif
#endif
#endif
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#if ASYNC_MODULE
import speiger.src.collections.PACKAGE.utils.ASYNC_BUILDER;
#endif
#if SPLIT_ITERATOR_FEATURE
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
#endif
import speiger.src.collections.PACKAGE.utils.ITERABLES;
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#if !LINKED_HASH_SET_FEATURE && LINKED_CUSTOM_HASH_SET_FEATURE
import speiger.src.collections.PACKAGE.utils.STRATEGY;
#endif
import speiger.src.collections.utils.ISizeProvider;
/**
* A Type-Specific {@link Iterable} that reduces (un)boxing
@ -59,6 +124,17 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
}
#endif
/**
* A Indexed forEach implementation that allows you to keep track of how many elements were already iterated over.
* @param action The action to be performed for each element
* @throws java.lang.NullPointerException if the specified action is null
*/
public default void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
int index = 0;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();action.accept(index++, iter.NEXT()));
}
/**
* Helper function to reduce Lambda usage and allow for more method references, since these are faster/cleaner.
* @param input the object that should be included
@ -66,11 +142,12 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
* @param <E> the generic type of the Object
* @throws java.lang.NullPointerException if the specified action is null
*/
default <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
default <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
iterator().forEachRemaining(input, action);
}
#if SPLIT_ITERATOR_FEATURE
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing
* @return type specific splititerator
@ -78,6 +155,19 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
@Override
default SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createUnknownSplititerator(iterator(), 0); }
#endif
#if ASYNC_MODULE
/**
* Creates a Async Builder for moving work of the thread.
* It is not designed to split the work to multithreaded work, so using this keep it singlethreaded, but it allows to be moved to another thread.
* @see ASYNC_BUILDER
* @return a AsyncBuilder
*/
default ASYNC_BUILDER KEY_GENERIC_TYPE asAsync() {
return new ASYNC_BUILDERBRACES(this);
}
#endif
/**
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
* @param mapper the mapping function
@ -88,12 +178,32 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
return ITERABLES.map(this, mapper);
}
#if TYPE_OBJECT
#iterate
#argument OUTPUT_ITERABLE BooleanIterable ByteIterable ShortIterable IntIterable LongIterable FloatIterable DoubleIterable
#argument MAPPER Predicate ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument DATA_TYPE Boolean Byte Short Int Long Float Double
#argument FILTER_TYPE BOOLEAN_COLLECTION_MODULE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
/**
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
* @param mapper the mapping function
* @return a new Iterable that returns the desired result
*/
default OUTPUT_ITERABLE mapToDATA_TYPE(MAPPER<T> mapper) {
return ITERABLES.mapToDATA_TYPE(this, mapper);
}
#endif
#enditerate
#endif
/**
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
* @param mapper the flatMapping function
* @param <V> The return type supplier.
* @param <E> The return type.
* @return a new Iterable that returns the desired result
* @note does not support TO_ARRAY optimizations.
*/
default <E, V extends Iterable<E>> ObjectIterable<E> flatMap(TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<V> mapper) {
return ITERABLES.flatMap(this, mapper);
@ -104,6 +214,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
* @param mapper the flatMapping function
* @param <E> The return type.
* @return a new Iterable that returns the desired result
* @note does not support TO_ARRAY optimizations.
*/
default <E> ObjectIterable<E> arrayflatMap(TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E[]> mapper) {
return ITERABLES.arrayFlatMap(this, mapper);
@ -113,11 +224,146 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
* A Helper function to reduce the usage of Streams and allows to filter out unwanted elements
* @param filter the elements that should be kept.
* @return a Iterable that filtered out all unwanted elements
* @note does not support TO_ARRAY optimizations.
*/
default ITERABLE KEY_GENERIC_TYPE filter(PREDICATE KEY_GENERIC_TYPE filter) {
return ITERABLES.filter(this, filter);
}
/**
* A Helper function to reduce the usage of Streams and allows to filter out duplicated elements
* @return a Iterable that filtered out all duplicated elements
* @note does not support TO_ARRAY optimizations.
*/
default ITERABLE KEY_GENERIC_TYPE distinct() {
return ITERABLES.distinct(this);
}
/**
* A Helper function to reduce the usage of Streams and allows to repeat elements a desired amount of times
* @param repeats how many times the elements should be repeated
* @return a Iterable that is repeating multiple times
*/
default ITERABLE KEY_GENERIC_TYPE repeat(int repeats) {
return ITERABLES.repeat(this, repeats);
}
/**
* A Helper function to reduce the usage of Streams and allows to limit the amount of elements
* @param limit the amount of elements it should be limited to
* @return a Iterable that is limited in length
*/
default ITERABLE KEY_GENERIC_TYPE limit(long limit) {
return ITERABLES.limit(this, limit);
}
/**
* A Helper function to reduce the usage of Streams and allows to sort the elements
* @param sorter that sorts the elements.
* @return a Iterable that is sorted
*/
default ITERABLE KEY_GENERIC_TYPE sorted(COMPARATOR KEY_GENERIC_TYPE sorter) {
return ITERABLES.sorted(this, sorter);
}
/**
* A Helper function to reduce the usage of Streams and allows to preview elements before they are iterated through
* @param action the action that should be applied
* @return a Peeked Iterable
*/
default ITERABLE KEY_GENERIC_TYPE peek(CONSUMER KEY_GENERIC_TYPE action) {
return ITERABLES.peek(this, action);
}
/**
* A Helper function to reduce the usage of Streams and allows to collect all elements
* @param collection that the elements should be inserted to
* @param <E> the collection type
* @return the input with the desired elements
*/
default <E extends COLLECTION KEY_GENERIC_TYPE> E pour(E collection) {
ITERATORS.pour(iterator(), collection);
return collection;
}
#if ARRAY_LIST_FEATURE || LINKED_LIST_FEATURE
/**
* A Helper function that reduces the usage of streams and allows to collect all elements as a ArrayList
* @return a new ArrayList of all elements
*/
default LIST KEY_GENERIC_TYPE pourAsList() {
#if ARRAY_LIST_FEATURE
return pour(new ARRAY_LISTBRACES());
#else
return pour(new LINKED_LISTBRACES());
#endif
}
#endif
#if !TYPE_BOOLEAN && SET_MODULE
#if LINKED_SET_FEATURE || LINKED_CUSTOM_SET_FEATURE || SET_FEATURE || CUSTOM_SET_FEATURE || RB_TREE_SET_FEATURE || AVL_TREE_SET_FEATURE || ARRAY_SET_FEATURE
/**
* A Helper function that reduces the usage of streams and allows to collect all elements as a LinkedHashSet
* @return a new LinkedHashSet of all elements
*/
default SET KEY_GENERIC_TYPE pourAsSet() {
#if LINKED_SET_FEATURE
return pour(new LINKED_HASH_SETBRACES());
#else if LINKED_CUSTOM_SET_FEATURE
return pour(new LINKED_CUSTOM_HASH_SETBRACES(STRATEGY.normalStrategy()));
#else if SET_FEATURE
return pour(new HASH_SETBRACES());
#else if CUSTOM_SET_FEATURE
return pour(new CUSTOM_HASH_SETBRACES(STRATEGY.normalStrategy()));
#else if RB_TREE_SET_FEATURE
return pour(new RB_Tree_SETBRACES());
#else if AVL_TREE_SET_FEATURE
return pour(new AVL_Tree_SETBRACES());
#else if ARRAY_SET_FEATURE
return pour(new ARRAY_SETBRACES());
#endif
}
#endif
#endif
#if TYPE_OBJECT
/**
* A Helper function that reduces the usage of streams and allows to collect all elements as a Array
* @param action is the creator function of said Array to ensure type is kept.
* @param <E> the returning arrayType
* @return a new Array of all elements
*/
default <E> E[] TO_ARRAY(IntFunction<E[]> action) {
ISizeProvider prov = ISizeProvider.of(this);
if(prov != null) {
int size = prov.size();
if(size >= 0) {
E[] array = action.apply(size);
ITERATORS.unwrap(array, iterator());
return array;
}
}
return ARRAYS.pour(iterator(), action);
}
#else
/**
* A Helper function that reduces the usage of streams and allows to collect all elements as a Array
* @return a new Array of all elements
*/
default KEY_TYPE[] TO_ARRAY() {
ISizeProvider prov = ISizeProvider.of(this);
if(prov != null) {
int size = prov.size();
if(size >= 0) {
KEY_TYPE[] array = NEW_KEY_ARRAY(size);
ITERATORS.unwrap(array, iterator());
return array;
}
}
return ARRAYS.pour(iterator());
}
#endif
/**
* Helper function to reduce stream usage that allows to filter for any matches.
* @param filter that should be applied
@ -126,7 +372,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
default boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
if(filter.TEST_VALUE(iter.NEXT())) return true;
if(filter.test(iter.NEXT())) return true;
}
return false;
}
@ -139,7 +385,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
default boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
if(filter.TEST_VALUE(iter.NEXT())) return false;
if(filter.test(iter.NEXT())) return false;
}
return true;
}
@ -152,7 +398,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
default boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
if(!filter.TEST_VALUE(iter.NEXT())) return false;
if(!filter.test(iter.NEXT())) return false;
}
return true;
}
@ -166,8 +412,79 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
Objects.requireNonNull(filter);
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
KEY_TYPE entry = iter.NEXT();
if(filter.TEST_VALUE(entry)) return entry;
if(filter.test(entry)) return entry;
}
return EMPTY_VALUE;
}
#if !TYPE_OBJECT
/**
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
* elements of this Iterable
* @param operator the operation that should be applied
* @param identity the start value
* @return the reduction result, returns identity if nothing was found
*/
default KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
state = operator.APPLY_VALUE(state, iter.NEXT());
}
return state;
}
#else
/**
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
* elements of this Iterable
* @param operator the operation that should be applied
* @param identity the start value
* @Type(E)
* @return the reduction result, returns identity if nothing was found
*/
default <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
state = operator.APPLY_VALUE(state, iter.NEXT());
}
return state;
}
#endif
/**
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
* elements of this Iterable
* @param operator the operation that should be applied
* @return the reduction result, returns null value if nothing was found
*/
default KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
if(empty) {
empty = false;
state = iter.NEXT();
continue;
}
state = operator.APPLY_VALUE(state, iter.NEXT());
}
return state;
}
/**
* Helper function to reduce stream usage that allows to count the valid elements.
* @param filter that should be applied
* @return the amount of Valid Elements
*/
default int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
if(filter.test(iter.NEXT())) result++;
}
return result;
}
}

View File

@ -8,7 +8,7 @@ import java.util.function.Consumer;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
/**
* A Type-Specific {@link Iterator} that reduces (un)boxing
@ -74,9 +74,9 @@ public interface ITERATOR KEY_GENERIC_TYPE extends Iterator<CLASS_TYPE>
* @param <E> the generic type of the Object
* @throws java.lang.NullPointerException if the specified action is null
*/
default <E> void forEachRemaining(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
default <E> void forEachRemaining(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
while(hasNext()) { action.accept(NEXT(), input); }
while(hasNext()) { action.accept(input, NEXT()); }
}
/**

View File

@ -1,5 +1,7 @@
package speiger.src.collections.PACKAGE.collections;
import java.util.NoSuchElementException;
import speiger.src.collections.utils.Stack;
/**
@ -14,6 +16,14 @@ public interface STACK
*/
public void push(KEY_TYPE e);
/**
* Helper function that pushes the top element on top of the stack again.
* @throws NoSuchElementException if the stack is empty
*/
public default void pushTop() {
push(top());
}
/**
* Removes the Object on top of the stack.
* @return the element that is on top of the stack
@ -59,4 +69,19 @@ public interface STACK
public default boolean isEmpty() {
return size() == 0;
}
/**
* A method to drop the contents of the Stack without clearing the stack
* @Type(E)
* @return the contents of the stack into a seperate array.
*/
public default GENERIC_SPECIAL_KEY_BRACES<E> KEY_SPECIAL_TYPE[] TO_ARRAY() { return TO_ARRAY(NEW_SPECIAL_KEY_ARRAY(size())); }
/**
* A method to drop the contents of the Stack without clearing the stack
* @param input where the elements should be inserted to. If it does not fit then it creates a new appropiatly created array
* @Type(E)
* @return the contents of the stack into a seperate array.
* @note if the Type is generic then a Object Array is created instead of a Type Array
*/
public GENERIC_SPECIAL_KEY_BRACES<E> KEY_SPECIAL_TYPE[] TO_ARRAY(KEY_SPECIAL_TYPE[] input);
}

View File

@ -50,6 +50,10 @@ public interface COMPARATOR extends Comparator<CLASS_TYPE>
{
COMPARATOR original;
/**
* default constructor
* @param original that is going to be reversed
*/
public Reversed(COMPARATOR original) {
this.original = original;
}

View File

@ -0,0 +1,19 @@
package speiger.src.collections.PACKAGE.functions;
/**
* Type-Specific Supplier interface that reduces (un)boxing and allows to merge other consumer types into this interface
* @Type(T)
*/
#if TYPE_OBJECT
public interface SUPPLIER KEY_GENERIC_TYPE extends java.util.function.Supplier<KEY_TYPE>
#else if JDK_TYPE && !TYPE_BOOLEAN
public interface SUPPLIER KEY_GENERIC_TYPE extends JAVA_SUPPLIER
#else
public interface SUPPLIER KEY_GENERIC_TYPE
#endif
{
/**
* @return the supplied value
*/
public KEY_TYPE SUPPLY_GET();
}

View File

@ -0,0 +1,92 @@
package speiger.src.collections.PACKAGE.functions;
import java.util.concurrent.RunnableFuture;
#if !TYPE_OBJECT
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
#endif
/**
*
* A Type Specific Task interface that allows you to keep track of the task that is currently running.<br>
* It extends Runnable future and supports said functions but also provides quality of life functions like:<br>
*
* - isSuccesfull: which allows to detect if the task was completed properly and not interrupted or crashed.
* - pause/resume: which allows to pause/resume the task at any moment, making it easier to create thread-safe actions.
* @Type(T)
*/
public interface TASK KEY_GENERIC_TYPE extends RunnableFuture<CLASS_TYPE> {
/**
* Helper function to detect if the task is currently paused.
* @return true if paused
*/
public boolean isPaused();
/**
* Pauses the task, which lets the thread finish without completing the task.
* Tasks are written in the way where they can pause without any issues.
* This won't be instant, as this function is applied asynchronous and doesn't check if the thread paused.
* So make sure it had the time to pause.
*/
public void pause();
/**
* Pauses the task, which lets the thread finish without completing the task.
* Tasks are written in the way where they can pause without any issues.
* This won't be instant, as this function is applied asynchronous.
* It will await the pausing of the task.
*/
public void awaitPausing();
/**
* Continues the task if it wasn't already completed.
* This is done by resubmitting the task to the executor provided.
*/
public void resume();
/**
* Quality of life function that allows to detect if no cancellation/exception was applied to this task and it completed on its own.
* @return true if it was properly completed
*/
public boolean isSuccessful();
#if !TYPE_OBJECT
/**
* A Type Specific get method that allows to reduce (un)boxing of primtives.
*
* Waits if necessary for the computation to complete, and then
* retrieves its result.
*
* @return the computed result as primitive
* @throws CancellationException if the computation was cancelled
* @throws ExecutionException if the computation threw an exception
* @throws InterruptedException if the current thread was interrupted
* while waiting
*/
public KEY_TYPE GET_KEY() throws InterruptedException, ExecutionException;
/**
* Waits if necessary for at most the given time for the computation
* to complete, and then retrieves its result, if available.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return the computed result as primitive
* @throws CancellationException if the computation was cancelled
* @throws ExecutionException if the computation threw an exception
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws TimeoutException if the wait timed out
*/
public KEY_TYPE GET_KEY(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
@Override
@Deprecated
public default CLASS_TYPE get() throws InterruptedException, ExecutionException { return KEY_TO_OBJ(GET_KEY()); }
@Override
@Deprecated
public default CLASS_TYPE get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return KEY_TO_OBJ(GET_KEY(timeout, unit)); }
#endif
}

View File

@ -1,6 +1,6 @@
package speiger.src.collections.PACKAGE.functions.function;
#if JDK_FUNCTION && VALUE_BOOLEAN
#if VALUE_BOOLEAN || SAME_TYPE
import java.util.Objects;
#endif
@ -21,12 +21,67 @@ public interface FUNCTION KEY_VALUE_GENERIC_TYPE
* @param k the value that should be processed
* @return the result of the function
*/
public VALUE_TYPE GET_VALUE(KEY_TYPE k);
public VALUE_TYPE APPLY(KEY_TYPE k);
#if JDK_FUNCTION
#if SAME_TYPE
/**
* Creates a Default function that returns the input provided.
* @Type(T)
* @return a input returning function
*/
public static GENERIC_KEY_BRACES FUNCTION KEY_SAME_GENERIC_TYPE identity() {
return T -> T;
}
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @Type(I)
* @param before the function that should be used first
* @return a composed function with a different starting function.
*/
public default GENERIC_SPECIAL_VALUE_BRACES<I> FUNCTION SV_GENERIC_TYPE<I> compose(FUNCTION SK_GENERIC_TYPE<I> before) {
Objects.requireNonNull(before);
return T -> APPLY(before.APPLY(T));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @Type(I)
* @param after the function that should be used last
* @return a composed function with a different starting function.
*/
public default GENERIC_SPECIAL_VALUE_BRACES<I> FUNCTION KS_GENERIC_TYPE<I> andThen(FUNCTION VS_GENERIC_TYPE<I> after) {
Objects.requireNonNull(after);
return T -> after.APPLY(APPLY(T));
}
#endif
#if VALUE_BOOLEAN
@Override
public default VALUE_TYPE test(KEY_TYPE k) { return GET_VALUE(k); }
/**
* Creates a Always true function that may be useful if you don't need to process information or just want a default.
* @Type(T)
* @return a default returning function
*/
public static GENERIC_KEY_BRACES FUNCTION KEY_GENERIC_TYPE alwaysTrue() {
return T -> true;
}
/**
* Creates a Always false function that may be useful if you don't need to process information or just want a default.
* @Type(T)
* @return a default returning function
*/
public static GENERIC_KEY_BRACES FUNCTION KEY_GENERIC_TYPE alwaysFalse() {
return T -> false;
}
/**
* A Type specific and-function helper function that reduces boxing/unboxing
@ -35,19 +90,26 @@ public interface FUNCTION KEY_VALUE_GENERIC_TYPE
*/
public default FUNCTION KEY_VALUE_GENERIC_TYPE andType(FUNCTION KEY_VALUE_GENERIC_TYPE other) {
Objects.requireNonNull(other);
return T -> GET_VALUE(T) && other.GET_VALUE(T);
return T -> APPLY(T) && other.APPLY(T);
}
#if JDK_FUNCTION
@Override
@Deprecated
public default FUNCTION KEY_VALUE_GENERIC_TYPE and(JAVA_FUNCTION KEY_VALUE_SUPER_GENERIC_TYPE other) {
Objects.requireNonNull(other);
return T -> GET_VALUE(T) && other.test(T);
return T -> APPLY(T) && other.APPLY(T);
}
@Override
#else
/**
* A type specific inverter function
* @return the same function but inverts the result
*/
#endif
public default FUNCTION KEY_VALUE_GENERIC_TYPE negate() {
return T -> !GET_VALUE(T);
return T -> !APPLY(T);
}
/**
@ -57,23 +119,16 @@ public interface FUNCTION KEY_VALUE_GENERIC_TYPE
*/
public default FUNCTION KEY_VALUE_GENERIC_TYPE orType(FUNCTION KEY_VALUE_GENERIC_TYPE other) {
Objects.requireNonNull(other);
return T -> GET_VALUE(T) || other.GET_VALUE(T);
return T -> APPLY(T) || other.APPLY(T);
}
#if JDK_FUNCTION
@Override
@Deprecated
public default FUNCTION KEY_VALUE_GENERIC_TYPE or(JAVA_FUNCTION KEY_VALUE_SUPER_GENERIC_TYPE other) {
Objects.requireNonNull(other);
return T -> GET_VALUE(T) || other.test(T);
return T -> APPLY(T) || other.APPLY(T);
}
#else if VALUE_OBJECT
@Override
public default VALUE_TYPE apply(KEY_TYPE k) { return GET_VALUE(k); }
#else
@Override
public default VALUE_TYPE APPLY_VALUE(KEY_TYPE k) { return GET_VALUE(k); }
#endif
#endif
}

View File

@ -1,13 +1,22 @@
package speiger.src.collections.PACKAGE.lists;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.RandomAccess;
import speiger.src.collections.PACKAGE.collections.ABSTRACT_COLLECTION;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
#if INT_LIST_MODULE && !TYPE_INT
import speiger.src.collections.ints.lists.IntList;
#endif
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
import speiger.src.collections.utils.SanityChecks;
/**
* Abstract implementation of the {@link LIST} interface.
@ -15,9 +24,8 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
*/
public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE implements LIST KEY_GENERIC_TYPE
{
#if !TYPE_OBJECT
/**
* A Type-Specific implementation of add function that delegates to {@link #add(int, KEY_TYPE)}
* A Type-Specific implementation of add function that delegates to {@link List#add(int, Object)}
*/
@Override
public boolean add(KEY_TYPE e) {
@ -25,6 +33,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
return true;
}
#if !TYPE_OBJECT
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
@ -62,8 +71,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
*/
@Override
@Deprecated
public boolean addAll(Collection<? extends CLASS_TYPE> c)
{
public boolean addAll(Collection<? extends CLASS_TYPE> c) {
return c instanceof COLLECTION ? addAll((COLLECTION KEY_GENERIC_TYPE)c) : addAll(size(), c);
}
@ -71,7 +79,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
* The IndexOf implementation iterates over all elements and compares them to the search value.
* @param o the value that the index is searched for.
* @return index of the value that was searched for. -1 if not found
* @deprecated it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
* @note it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
*/
@Override
@Primitive
@ -99,7 +107,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
* The lastIndexOf implementation iterates over all elements and compares them to the search value.
* @param o the value that the index is searched for.
* @return the last index of the value that was searched for. -1 if not found
* @deprecated it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
* @note it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
*/
@Override
@Primitive
@ -155,6 +163,14 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
}
#endif
@Override
public boolean REMOVE_SWAP(KEY_TYPE e) {
int index = indexOf(e);
if(index == -1) return false;
swapRemove(index);
return true;
}
/**
* Compares if the list are the same.
*/
@ -205,7 +221,13 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
@Override
public LIST KEY_GENERIC_TYPE subList(int fromIndex, int toIndex) {
return new SUB_LIST(this, fromIndex, toIndex);
SanityChecks.checkArrayCapacity(size(), fromIndex, toIndex-fromIndex);
return new SubList(this, 0, fromIndex, toIndex);
}
@Override
public LIST KEY_GENERIC_TYPE reversed() {
return new ReversedList(this);
}
@Override
@ -220,125 +242,290 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator(int index) {
if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
return new LIST_ITER(index);
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(int...indecies) {
return new IndexedIterator(indecies);
}
#if INT_LIST_MODULE
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(IntList indecies) {
return new ListIndexedIterator(indecies);
}
#endif
@Override
public void size(int size) {
while(size > size()) add(EMPTY_KEY_VALUE);
while(size < size()) REMOVE(size() - 1);
}
private class SUB_LIST extends ABSTRACT_LIST KEY_GENERIC_TYPE {
ABSTRACT_LIST KEY_GENERIC_TYPE l;
int offset;
int size;
public ABSTRACT_LIST KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
SUB_LIST(ABSTRACT_LIST KEY_GENERIC_TYPE l, int from, int to) {
if (from < 0) throw new IndexOutOfBoundsException("fromIndex = " + from);
else if (to > l.size()) throw new IndexOutOfBoundsException("toIndex = " + to);
else if (from > to) throw new IllegalArgumentException("fromIndex(" + from + ") > toIndex(" + to + ")");
this.l = l;
offset = from;
size = to - from;
private class ReversedList extends ABSTRACT_LIST KEY_GENERIC_TYPE
{
final ABSTRACT_LIST KEY_GENERIC_TYPE list;
public ReversedList(ABSTRACT_LIST KEY_GENERIC_TYPE list) {
this.list = list;
}
@Override
public void add(int index, KEY_TYPE e) {
checkAddRange(index);
l.add(index+offset, e);
size++;
}
@Override
public boolean addAll(int index, Collection<? extends CLASS_TYPE> c) {
checkAddRange(index);
int size = c.size();
if(size == 0) return false;
l.addAll(index + offset, l);
offset += size;
return true;
list.add(list.size() - index - 1, e);
}
@Override
public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c) {
checkAddRange(index);
int size = c.size();
if(size == 0) return false;
l.addAll(index + offset, l);
offset += size;
return true;
return addCollection(index, c);
}
@Override
public boolean addAll(int index, LIST KEY_GENERIC_TYPE c) {
checkAddRange(index);
int size = c.size();
if(size == 0) return false;
l.addAll(index + offset, l);
offset += size;
if(c instanceof RandomAccess) {
for(int i = 0,m=c.size();i<m;i++) {
list.add(list.size() - index - i - 1, c.GET_KEY(i));
}
return true;
}
return addCollection(index, c);
}
private boolean addCollection(int index, COLLECTION KEY_GENERIC_TYPE c) {
int i = 0;
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();i++) {
list.add(list.size() - index - i - 1, iter.NEXT());
}
return true;
}
@Override
public boolean addAll(int index, Collection<? extends CLASS_TYPE> c) {
int i = 0;
for(Iterator<? extends CLASS_TYPE> iter = c.iterator();iter.hasNext();i++) {
list.add(list.size() - index - i - 1, iter.next());
}
return true;
}
@Override
public KEY_TYPE GET_KEY(int index) {
return list.GET_KEY(list.size() - index - 1);
}
@Override
public KEY_TYPE set(int index, KEY_TYPE e) {
return list.set(list.size() - index - 1, e);
}
@Override
public KEY_TYPE REMOVE(int index) {
return list.REMOVE(list.size() - index - 1);
}
@Override
public void addElements(int from, KEY_TYPE[] a, int offset, int length) {
checkRange(from);
l.addElements(from + this.offset, a, offset, length);
size += length;
for(int i = 0,m=length;i<m;i++) {
list.add(list.size() - from - i - 1, a[i+offset]);
}
}
@Override
public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) {
checkRange(from);
return l.getElements(from + this.offset, a, offset, length);
return reverse(list.getElements(list.size() - from - 1, a, offset, length));
}
@Override
public void removeElements(int from, int to) {
checkRange(from);
checkRange(to);
l.removeElements(from + offset, to + offset);
size -= to - from;
list.removeElements(list.size() - to - 1, list.size() - from - 1);
}
@Override
public KEY_TYPE swapRemove(int index) {
return list.swapRemove(list.size() - index - 1);
}
#if TYPE_OBJECT
@Override
public <K> K[] extractElements(int from, int to, Class<K> clz) {
checkRange(from);
checkRange(to);
K[] a = l.extractElements(from + offset, to + offset, clz);
size -= to - from;
return a;
public <K> K[] extractElements(int from, int to, Class<K> type) {
return reverse(list.extractElements(list.size() - to - 1, list.size() - from - 1, type));
}
#else
@Override
public KEY_TYPE[] extractElements(int from, int to) {
checkRange(from);
checkRange(to);
KEY_TYPE[] a = l.extractElements(from + offset, to + offset);
return reverse(list.extractElements(list.size() - to - 1, list.size() - from - 1));
}
#endif
@Override
public int size() {
return list.size();
}
@Override
public void clear() {
list.clear();
}
@Override
public LIST KEY_GENERIC_TYPE reversed() {
return list;
}
#if TYPE_OBJECT
private <K> K[] reverse(K[] data) {
for (int i = 0, mid = data.length >> 1, j = data.length - 1; i < mid; i++, j--) {
K t = data[i];
data[i] = data[j];
data[j] = t;
}
return data;
}
#else
private KEY_TYPE[] reverse(KEY_TYPE[] data) {
for (int i = 0, mid = data.length >> 1, j = data.length - 1; i < mid; i++, j--) {
KEY_TYPE t = data[i];
data[i] = data[j];
data[j] = t;
}
return data;
}
#endif
}
private class SubList extends ABSTRACT_LIST KEY_GENERIC_TYPE
{
final ABSTRACT_LIST KEY_GENERIC_TYPE list;
final int parentOffset;
final int offset;
int size;
public SubList(ABSTRACT_LIST KEY_GENERIC_TYPE list, int offset, int from, int to) {
this.list = list;
this.parentOffset = from;
this.offset = offset + from;
this.size = to - from;
}
@Override
public void add(int index, KEY_TYPE element) {
checkAddSubRange(index);
list.add(parentOffset+index, element);
size++;
}
@Override
public boolean addAll(int index, Collection<? extends CLASS_TYPE> c) {
checkAddSubRange(index);
int add = c.size();
if(add <= 0) return false;
list.addAll(parentOffset+index, c);
this.size += add;
return true;
}
@Override
public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c) {
checkAddSubRange(index);
int add = c.size();
if(add <= 0) return false;
list.addAll(parentOffset+index, c);
this.size += add;
return true;
}
@Override
public boolean addAll(int index, LIST KEY_GENERIC_TYPE c) {
checkAddSubRange(index);
int add = c.size();
if(add <= 0) return false;
list.addAll(parentOffset+index, c);
this.size += add;
return true;
}
@Override
public void addElements(int from, KEY_TYPE[] a, int offset, int length) {
checkAddSubRange(from);
if(length <= 0) return;
list.addElements(parentOffset+from, a, offset, length);
this.size += length;
}
@Override
public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) {
SanityChecks.checkArrayCapacity(size, from, length);
SanityChecks.checkArrayCapacity(a.length, offset, length);
return list.getElements(from+parentOffset, a, offset, length);
}
@Override
public void removeElements(int from, int to) {
if(to-from <= 0) return;
checkSubRange(from);
checkAddSubRange(to);
list.removeElements(from+parentOffset, to+parentOffset);
size -= to - from;
return a;
}
#if TYPE_OBJECT
@Override
public <K> K[] extractElements(int from, int to, Class<K> type) {
checkSubRange(from);
checkAddSubRange(to);
K[] result = list.extractElements(from+parentOffset, to+parentOffset, type);
size -= result.length;
return result;
}
#else
@Override
public KEY_TYPE[] extractElements(int from, int to) {
checkSubRange(from);
checkAddSubRange(to);
KEY_TYPE[] result = list.extractElements(from+parentOffset, to+parentOffset);
size -= result.length;
return result;
}
#endif
@Override
public KEY_TYPE GET_KEY(int index) {
checkRange(index);
return l.GET_KEY(index + offset);
checkSubRange(index);
return list.GET_KEY(parentOffset+index);
}
@Override
public KEY_TYPE set(int index, KEY_TYPE e) {
checkRange(index);
return l.set(index + offset, e);
public KEY_TYPE set(int index, KEY_TYPE element) {
checkSubRange(index);
return list.set(parentOffset+index, element);
}
@Override
public KEY_TYPE swapRemove(int index) {
checkSubRange(index);
if(index == size-1) {
KEY_TYPE result = list.REMOVE(parentOffset+size-1);
size--;
return result;
}
KEY_TYPE result = list.set(index+parentOffset, list.GET_KEY(parentOffset+size-1));
list.REMOVE(parentOffset+size-1);
size--;
return result;
}
@Override
public KEY_TYPE REMOVE(int index) {
checkRange(index);
checkSubRange(index);
KEY_TYPE result = list.REMOVE(index+parentOffset);
size--;
return l.REMOVE(index + offset);
return result;
}
@Override
@ -346,15 +533,263 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
return size;
}
private void checkRange(int index) {
@Override
public SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createSplititerator(this, 16464); }
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator(int index) {
if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
return new SubListIterator(this, index);
}
@Override
public LIST KEY_GENERIC_TYPE subList(int fromIndex, int toIndex) {
SanityChecks.checkArrayCapacity(size, fromIndex, toIndex-fromIndex);
return new SubList(this, offset, fromIndex, toIndex);
}
protected void checkSubRange(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
private void checkAddRange(int index) {
protected void checkAddSubRange(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
private class SubListIterator implements LIST_ITERATOR KEY_GENERIC_TYPE
{
ABSTRACT_LIST KEY_GENERIC_TYPE list;
int index;
int lastReturned = -1;
SubListIterator(ABSTRACT_LIST KEY_GENERIC_TYPE list, int index) {
this.list = list;
this.index = index;
}
@Override
public boolean hasNext() {
return index < list.size();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
int i = index++;
return list.GET_KEY((lastReturned = i));
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
index--;
return list.GET_KEY((lastReturned = index));
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index-1;
}
@Override
public void remove() {
if(lastReturned == -1) throw new IllegalStateException();
list.REMOVE(lastReturned);
index = lastReturned;
lastReturned = -1;
}
@Override
public void set(KEY_TYPE e) {
if(lastReturned == -1) throw new IllegalStateException();
list.set(lastReturned, e);
}
@Override
public void add(KEY_TYPE e) {
list.add(index, e);
index++;
lastReturned = -1;
}
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, size() - index);
index += steps;
if(steps > 0) lastReturned = Math.min(index-1, size()-1);
return steps;
}
@Override
public int back(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
if(steps > 0) lastReturned = Math.min(index, size()-1);
return steps;
}
}
}
#if INT_LIST_MODULE
private class ListIndexedIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
IntList indecies;
int index;
int lastReturned = -1;
ListIndexedIterator(IntList indecies) {
this.indecies = indecies;
}
@Override
public boolean hasNext() {
return index < indecies.size();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
int i = index++;
return GET_KEY((lastReturned = indecies.getInt(i)));
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
index--;
return GET_KEY((lastReturned = indecies.getInt(index)));
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index-1;
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void set(KEY_TYPE e) {
if(lastReturned == -1) throw new IllegalStateException();
ABSTRACT_LIST.this.set(lastReturned, e);
}
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, indecies.size() - index);
index += steps;
if(steps > 0) lastReturned = Math.min(index-1, indecies.size()-1);
return steps;
}
@Override
public int back(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
if(steps > 0) lastReturned = Math.max(index, 0);
return steps;
}
}
#endif
private class IndexedIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
int[] indecies;
int index;
int lastReturned = -1;
IndexedIterator(int[] indecies) {
this.indecies = indecies;
}
@Override
public boolean hasNext() {
return index < indecies.length;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
int i = index++;
return GET_KEY((lastReturned = indecies[i]));
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
index--;
return GET_KEY((lastReturned = indecies[index]));
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index-1;
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void set(KEY_TYPE e) {
if(lastReturned == -1) throw new IllegalStateException();
ABSTRACT_LIST.this.set(lastReturned, e);
}
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, indecies.length - index);
index += steps;
if(steps > 0) lastReturned = Math.min(index-1, indecies.length-1);
return steps;
}
@Override
public int back(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
if(steps > 0) lastReturned = Math.max(index, 0);
return steps;
}
}
private class LIST_ITER implements LIST_ITERATOR KEY_GENERIC_TYPE {
@ -372,8 +807,9 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
@Override
public KEY_TYPE NEXT() {
lastReturned = index;
return GET_KEY(index++);
if(!hasNext()) throw new NoSuchElementException();
int i = index++;
return GET_KEY((lastReturned = i));
}
@Override
@ -383,8 +819,9 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
@Override
public KEY_TYPE PREVIOUS() {
lastReturned = index;
return GET_KEY(index--);
if(!hasPrevious()) throw new NoSuchElementException();
index--;
return GET_KEY((lastReturned = index));
}
@Override
@ -399,34 +836,31 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
@Override
public void remove() {
if(lastReturned == -1)
throw new IllegalStateException();
if(lastReturned == -1) throw new IllegalStateException();
ABSTRACT_LIST.this.REMOVE(lastReturned);
if(lastReturned < index)
index--;
index = lastReturned;
lastReturned = -1;
}
@Override
public void set(KEY_TYPE e) {
if(lastReturned == -1)
throw new IllegalStateException();
if(lastReturned == -1) throw new IllegalStateException();
ABSTRACT_LIST.this.set(lastReturned, e);
}
@Override
public void add(KEY_TYPE e) {
if(lastReturned == -1)
throw new IllegalStateException();
ABSTRACT_LIST.this.add(index++, e);
ABSTRACT_LIST.this.add(index, e);
index++;
lastReturned = -1;
}
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, (size() - 1) - index);
int steps = Math.min(amount, size() - index);
index += steps;
if(steps > 0) lastReturned = Math.min(index-1, size()-1);
return steps;
}
@ -435,6 +869,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
if(steps > 0) lastReturned = Math.max(index, 0);
return steps;
}
}

View File

@ -8,13 +8,24 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
#if TYPE_OBJECT
#if IARRAY_FEATURE || TYPE_OBJECT
import java.util.function.Consumer;
#endif
import java.util.function.BiFunction;
import java.util.function.IntFunction;
#endif
#if !TYPE_OBJECT && JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
#if PRIMITIVES
#if !JDK_FUNCTION
import java.util.function.JAVA_PREDICATE;
#endif
import java.util.function.JAVA_UNARY_OPERATOR;
import java.nio.JAVA_BUFFER;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
@ -26,42 +37,51 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#endif
import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#if TYPE_OBJECT
import speiger.src.collections.utils.Stack;
#else
import speiger.src.collections.objects.utils.ObjectArrays;
#endif
#if PRIMITIVES
#if PRIMITIVES && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
import java.util.stream.JAVA_STREAM;
import java.util.stream.StreamSupport;
#endif
#if IARRAY_FEATURE
import speiger.src.collections.PACKAGE.utils.IARRAY;
#endif
#if SPLIT_ITERATOR_FEATURE
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
#endif
#if !IARRAY_FEATURE
import speiger.src.collections.utils.IArray;
#endif
import speiger.src.collections.utils.SanityChecks;
#if TYPE_OBJECT
/**
* A Type-Specific Array-based implementation of list that is written to reduce (un)boxing
*
* <p>This implementation is optimized to improve how data is processed with interfaces like {@link IARRAY}, {@link Stack}
#if IARRAY_FEATURE
* <p>This implementation is optimized to improve how data is processed with interfaces like {@link IARRAY}, {@link STACK}
#else
* <p>This implementation is optimized to improve how data is processed with interfaces like {@link IArray}, {@link STACK}
#endif
* and with optimized functions that use type-specific implementations for primitives and optimized logic for bulkactions.
*
* @Type(T)
*/
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IARRAY<KEY_TYPE>, Stack<KEY_TYPE>
#if IARRAY_FEATURE
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IARRAY KEY_GENERIC_TYPE, STACK KEY_GENERIC_TYPE
#else
/**
* A Type-Specific Array-based implementation of list that is written to reduce (un)boxing
*
* <p>This implementation is optimized to improve how data is processed with interfaces like {@link IARRAY}, {@link STACK}
* and with optimized functions that use type-specific implementations for primitives and optimized logic for bulkactions.
*/
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IARRAY, STACK
public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements IArray, STACK KEY_GENERIC_TYPE
#endif
{
static final int DEFAULT_ARRAY_SIZE = 10;
@ -83,6 +103,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
* @param size the minimum initial size of the Backing array
*/
public ARRAY_LIST(int size) {
if(size < 0) throw new IllegalStateException("Size has to be 0 or greater");
data = NEW_KEY_ARRAY(size);
}
@ -181,7 +202,20 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
*/
public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE of(Class<KEY_TYPE> c) {
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
list.data = (KEY_TYPE[])ObjectArrays.newArray(c.getClass().getComponentType(), 0);
list.data = (KEY_TYPE[])ObjectArrays.newArray(c, 0);
return list;
}
/**
* Creates a new ArrayList with a EmptyObject array of the Type requested
* @param c the type of the array
* @param size the initial size of the backing array
* @Type(T)
* @return a typed List
*/
public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE of(Class<KEY_TYPE> c, int size) {
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
list.data = (KEY_TYPE[])ObjectArrays.newArray(c, size);
return list;
}
@ -190,7 +224,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
* @return true (as specified by {@link Collection#add})
*/
@Override
public boolean add(KEY_TYPE e) {
@ -229,6 +263,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
* @param index the index where to append the elements to
* @param c the elements to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range
* @throws NullPointerException if collection contains a null element
*/
@Override
@Primitive
@ -236,6 +271,9 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
if(c instanceof COLLECTION) return addAll(index, (COLLECTION KEY_GENERIC_TYPE)c);
int add = c.size();
if(add <= 0) return false;
#if !TYPE_OBJECT
if(c.contains(null)) throw new NullPointerException();
#endif
grow(size + add);
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
size+=add;
@ -282,6 +320,16 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return true;
}
@Override
public boolean addAll(KEY_TYPE[] e, int offset, int length) {
if(length <= 0) return false;
SanityChecks.checkArrayCapacity(e.length, offset, length);
grow(size + length);
System.arraycopy(e, offset, data, size, length);
size+=length;
return true;
}
/**
* Appends the specified array elements to the index of the list.
* @param from the index where to append the elements to
@ -294,8 +342,9 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public void addElements(int from, KEY_TYPE[] a, int offset, int length) {
if(length <= 0) return;
checkAddRange(from);
SanityChecks.checkArrayCapacity(a.length, offset, length);
grow(size + length);
if(from != size) System.arraycopy(data, from, data, from+length, size - length);
if(from != size) System.arraycopy(data, from, data, from+length, size - from);
size+=length;
System.arraycopy(a, offset, data, from, length);
}
@ -331,11 +380,11 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
int length = to - from;
if(length <= 0) return;
if(to != size) System.arraycopy(data, to, data, from, size - to);
size -= length;
#if TYPE_OBJECT
for(int i = 0;i<length;i++)
data[i+to] = null;
data[i+size] = null;
#endif
size -= length;
}
#if TYPE_OBJECT
@ -351,13 +400,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
checkRange(from);
checkAddRange(to);
int length = to - from;
if(length <= 0) return ARRAYS.newArray(type, 0);
K[] a = ARRAYS.newArray(type, length);
if(length <= 0) return a;
System.arraycopy(data, from, a, 0, length);
if(to != size) System.arraycopy(data, to, data, from, size - to);
for(int i = 0;i<length;i++)
data[i+to] = null;
size -= length;
for(int i = 0;i<length;i++)
data[i+size] = null;
return a;
}
@ -380,7 +429,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
}
#endif
#if PRIMITIVES
@Override
public void fillBuffer(JAVA_BUFFER buffer) {
buffer.put(data, 0, size);
}
#endif
/**
* A function to find if the Element is present in this list.
* @param o the element that is searched for
@ -549,6 +604,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return data[(size() - 1) - index];
}
#if IARRAY_FEATURE
/**
* Provides the Underlying Array in the Implementation
* @return underlying Array
@ -568,6 +624,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return data.getClass() != Object[].class;
}
#endif
#endif
/**
* A Type Specific foreach function that reduces (un)boxing
@ -591,17 +648,17 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
for(int i = 0;i<size;i++)
action.accept(data[i], input);
action.accept(input, data[i]);
}
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(data[i])) return true;
if(filter.test(data[i])) return true;
}
return false;
}
@ -610,7 +667,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(data[i])) return false;
if(filter.test(data[i])) return false;
}
return true;
}
@ -619,7 +676,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(!filter.TEST_VALUE(data[i])) return false;
if(!filter.test(data[i])) return false;
}
return true;
}
@ -628,11 +685,60 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(data[i])) return data[i];
if(filter.test(data[i])) return data[i];
}
return EMPTY_VALUE;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(int i = 0;i<size;i++) {
if(empty) {
empty = false;
state = data[i];
continue;
}
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(int i = 0;i<size;i++) {
if(filter.test(data[i])) result++;
}
return result;
}
/**
* A Type-Specific set function to reduce (un)boxing
* @param index the index of the element to set
@ -706,6 +812,17 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return old;
}
public KEY_TYPE swapRemove(int index) {
checkRange(index);
KEY_TYPE old = data[index];
size--;
data[index] = data[size];
#if TYPE_OBJECT
data[size] = null;
#endif
return old;
}
#if !TYPE_OBJECT
/**
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
@ -837,6 +954,22 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return modified;
}
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
if(c.isEmpty()) return false;
int j = 0;
for(int i = 0;i<size;i++) {
if(!c.contains(data[i])) data[j++] = data[i];
else r.accept(data[i]);
}
boolean modified = j != size;
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return modified;
}
/**
* A function to retain all elements that were provided in the other collection
* This function might delegate to a more appropriate function if necessary
@ -864,6 +997,27 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return modified;
}
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
if(c.isEmpty()) {
boolean modifed = size > 0;
forEach(r);
clear();
return modifed;
}
int j = 0;
for(int i = 0;i<size;i++) {
if(c.contains(data[i])) data[j++] = data[i];
else r.accept(data[i]);
}
boolean modified = j != size;
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return modified;
}
#if PRIMITIVES
/**
* A optimized List#removeIf(Predicate) that more quickly removes elements from the list then the ArrayList implementation
@ -891,6 +1045,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
@Primitive
public Object[] toArray() {
if(size == 0) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size];
for(int i = 0;i<size;i++)
obj[i] = KEY_TO_OBJ(data[i]);
@ -907,8 +1062,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size];
else if(a.length < size) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size);
#if TYPE_OBJECT
System.arraycopy(data, 0, a, 0, size);
#else
for(int i = 0;i<size;i++)
a[i] = (E)KEY_TO_OBJ(data[i]);
#endif
if (a.length > size) a[size] = null;
return a;
}
@ -917,9 +1077,16 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a.length < size) a = new KEY_TYPE[size];
System.arraycopy(data, 0, a, 0, size);
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#else
@Override
public <E> E[] toArray(IntFunction<E[]> action) {
return super.toArray(action);
}
#endif
/**
* A function to return the size of the list
@ -995,8 +1162,16 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
grow(size);
}
@Override
public ARRAY_LIST KEY_GENERIC_TYPE copy() {
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
list.data = Arrays.copyOf(data, data.length);
list.size = size;
return list;
}
protected void grow(int capacity) {
if(capacity < data.length) return;
if(capacity <= data.length) return;
data = Arrays.copyOf(data, data == ARRAYS.EMPTY_ARRAY ? Math.max(DEFAULT_ARRAY_SIZE, capacity) : (int)Math.max(Math.min((long)data.length + (data.length >> 1), SanityChecks.MAX_ARRAY_SIZE), capacity));
}
@ -1010,7 +1185,8 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
#if PRIMITIVES
#if SPLIT_ITERATOR_FEATURE
#if PRIMITIVES && STREAM_FEATURE
/**
* Returns a Java-Type-Specific Stream to reduce boxing/unboxing.
* @return a Stream of the closest java type
@ -1027,4 +1203,5 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
*/
@Override
public SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createArraySplititerator(data, size, 16464); }
#endif
}

View File

@ -5,14 +5,21 @@ import java.util.Arrays;
import java.util.Comparator;
#endif
import java.util.Collection;
import java.util.NoSuchElementException;
import java.util.Objects;
#if TYPE_OBJECT
import java.util.function.BiFunction;
import java.util.function.Consumer;
#endif
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
#if !TYPE_OBJECT && JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
#if PRIMITIVES
#if !JDK_FUNCTION
import java.util.function.JAVA_PREDICATE;
#endif
import java.util.function.JAVA_UNARY_OPERATOR;
#endif
@ -22,16 +29,21 @@ import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#if PRIMITIVES
#if PRIMITIVES && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
import java.util.stream.JAVA_STREAM;
import java.util.stream.StreamSupport;
#endif
#if SPLIT_ITERATOR_FEATURE
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
#endif
import speiger.src.collections.utils.SanityChecks;
#if TYPE_OBJECT
@ -120,11 +132,14 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
@Override
public boolean addAll(int index, LIST KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override
public boolean addAll(KEY_TYPE[] e, int offset, int length) { throw new UnsupportedOperationException(); }
@Override
public void addElements(int from, KEY_TYPE[] a, int offset, int length) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) {
SanityChecks.checkArrayCapacity(data.length, offset, length);
SanityChecks.checkArrayCapacity(a.length, offset, length);
SanityChecks.checkArrayCapacity(data.length, from, length);
System.arraycopy(data, from, a, offset, length);
return a;
}
@ -259,6 +274,11 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
return data[index];
}
@Override
public IMMUTABLE_LIST KEY_GENERIC_TYPE copy() {
return new IMMUTABLE_LISTBRACES(Arrays.copyOf(data, data.length));
}
/**
* A Type Specific foreach function that reduces (un)boxing
*
@ -281,17 +301,17 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
for(int i = 0,m=data.length;i<m;i++)
action.accept(data[i], input);
action.accept(input, data[i]);
}
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0,m=data.length;i<m;i++) {
if(filter.TEST_VALUE(data[i])) return true;
if(filter.test(data[i])) return true;
}
return false;
}
@ -300,7 +320,7 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0,m=data.length;i<m;i++) {
if(filter.TEST_VALUE(data[i])) return false;
if(filter.test(data[i])) return false;
}
return true;
}
@ -309,7 +329,7 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0,m=data.length;i<m;i++) {
if(!filter.TEST_VALUE(data[i])) return false;
if(!filter.test(data[i])) return false;
}
return true;
}
@ -318,11 +338,66 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0,m=data.length;i<m;i++) {
if(filter.TEST_VALUE(data[i])) return data[i];
if(filter.test(data[i])) return data[i];
}
return EMPTY_VALUE;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(int i = 0,m=data.length;i<m;i++) {
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(int i = 0,m=data.length;i<m;i++) {
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(int i = 0,m=data.length;i<m;i++) {
if(empty) {
empty = false;
state = data[i];
continue;
}
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(int i = 0,m=data.length;i<m;i++) {
if(filter.test(data[i])) result++;
}
return result;
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator(int index) {
if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
return new LIST_ITER(index);
}
@Override
public KEY_TYPE set(int index, KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
@ -334,7 +409,8 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
#endif
@Override
public KEY_TYPE REMOVE(int index) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE swapRemove(int index) { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
public boolean REMOVE_KEY(KEY_TYPE type) { throw new UnsupportedOperationException(); }
@ -353,6 +429,10 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); }
#if PRIMITIVES
@Override
@ -366,6 +446,7 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
@Override
@Primitive
public Object[] toArray() {
if(data.length == 0) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[data.length];
for(int i = 0,m=data.length;i<m;i++)
obj[i] = KEY_TO_OBJ(data[i]);
@ -384,6 +465,7 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
else if(a.length < data.length) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), data.length);
for(int i = 0,m=data.length;i<m;i++)
a[i] = (E)KEY_TO_OBJ(data[i]);
if (a.length > data.length) a[data.length] = null;
return a;
}
@ -392,6 +474,7 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a.length < data.length) a = new KEY_TYPE[data.length];
System.arraycopy(data, 0, a, 0, data.length);
if (a.length > data.length) a[data.length] = EMPTY_KEY_VALUE;
return a;
}
@ -415,7 +498,8 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + data.length);
}
#if PRIMITIVES
#if SPLIT_ITERATOR_FEATURE
#if PRIMITIVES && STREAM_FEATURE
/**
* Returns a Java-Type-Specific Stream to reduce boxing/unboxing.
* @return a Stream of the closest java type
@ -432,4 +516,70 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
*/
@Override
public SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createArraySplititerator(data, data.length, 16464); }
#endif
private class LIST_ITER implements LIST_ITERATOR KEY_GENERIC_TYPE {
int index;
LIST_ITER(int index) {
this.index = index;
}
@Override
public boolean hasNext() {
return index < size();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return GET_KEY(index++);
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
return GET_KEY(--index);
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index-1;
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
@Override
public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, size() - index);
index += steps;
return steps;
}
@Override
public int back(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
return steps;
}
}
}

View File

@ -1,39 +1,64 @@
package speiger.src.collections.PACKAGE.lists;
#if TYPE_OBJECT
#if DEQUEUE_FEATURE
import java.util.Comparator;
#endif
import java.util.function.BiFunction;
#else if PRIMITIVES
import java.nio.JAVA_BUFFER;
#endif
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.NoSuchElementException;
import java.util.Spliterator;
#if PRIMITIVES
import java.util.Spliterator.JAVA_SPLIT_ITERATOR;
#endif
import java.util.function.Consumer;
import java.util.function.Predicate;
#if TYPE_OBJECT
import java.util.function.IntFunction;
#endif
#if !TYPE_OBJECT && JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import java.util.function.UnaryOperator;
#if PRIMITIVES
#if !JDK_FUNCTION
import java.util.function.JAVA_PREDICATE;
#endif
import java.util.function.JAVA_UNARY_OPERATOR;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.collections.STACK;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE;
#endif
#if !TYPE_OBJECT
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#endif
import speiger.src.collections.objects.utils.ObjectArrays;
#if TYPE_OBJECT
import speiger.src.collections.utils.Stack;
#else
import speiger.src.collections.objects.utils.ObjectArrays;
#endif
#if PRIMITIVES
import java.util.stream.JAVA_STREAM;
@ -55,10 +80,10 @@ import speiger.src.collections.utils.SanityChecks;
*
* @Type(T)
*/
#if TYPE_OBJECT
public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, Stack KEY_GENERIC_TYPE
#else
#if DEQUEUE_FEATURE
public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, STACK KEY_GENERIC_TYPE
#else
public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE implements STACK KEY_GENERIC_TYPE
#endif
{
Entry KEY_GENERIC_TYPE first;
@ -145,10 +170,16 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
int length = c.size();
if(length == 0) return false;
checkAddRange(index);
Entry KEY_GENERIC_TYPE next = null;
Entry KEY_GENERIC_TYPE prev = null;
if(index == size) prev = last;
else if(index == 0) next = first;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE prev;
if(index == size) {
prev = last;
next = null;
}
else if(index == 0) {
next = first;
prev = null;
}
else {
next = getNode(index);
prev = next.prev;
@ -170,30 +201,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public boolean addAll(int index, LIST KEY_GENERIC_TYPE c) {
int length = c.size();
if(length == 0) return false;
checkAddRange(index);
Entry KEY_GENERIC_TYPE next = null;
Entry KEY_GENERIC_TYPE prev = null;
if(index == size) prev = last;
else if(index == 0) next = first;
else {
next = getNode(index);
prev = next.prev;
}
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();) {
Entry KEY_GENERIC_TYPE entry = new EntryBRACES(iter.NEXT(), prev, null);
if(prev == null) first = entry;
else prev.next = entry;
prev = entry;
}
if(next == null) last = prev;
else {
prev.next = next;
next.prev = prev;
}
size += length;
return true;
return addAll(index, (COLLECTION KEY_GENERIC_TYPE)c); //
}
@Override
@ -203,10 +211,16 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
int length = c.size();
if(length == 0) return false;
checkAddRange(index);
Entry KEY_GENERIC_TYPE next = null;
Entry KEY_GENERIC_TYPE prev = null;
if(index == size) prev = last;
else if(index == 0) next = first;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE prev;
if(index == size) {
prev = last;
next = null;
}
else if(index == 0) {
next = first;
prev = null;
}
else {
next = getNode(index);
prev = next.prev;
@ -226,6 +240,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return true;
}
#if DEQUEUE_FEATURE
@Override
public void enqueue(KEY_TYPE e) {
add(e);
@ -236,19 +251,35 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
add(0, e);
}
#endif
@Override
public void push(KEY_TYPE e) {
add(e);
}
@Override
public boolean addAll(KEY_TYPE[] e, int offset, int length) {
if(length <= 0) return false;
SanityChecks.checkArrayCapacity(e.length, offset, length);
for(int i = 0;i<length;i++) linkLast(e[offset+i]);
return true;
}
@Override
public void addElements(int from, KEY_TYPE[] a, int offset, int length) {
if(length <= 0) return;
SanityChecks.checkArrayCapacity(a.length, offset, length);
checkAddRange(from);
Entry KEY_GENERIC_TYPE next = null;
Entry KEY_GENERIC_TYPE prev = null;
if(from == size) prev = last;
else if(from == 0) next = first;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE prev;
if(from == size) {
prev = last;
next = null;
}
else if(from == 0) {
next = first;
prev = null;
}
else {
next = getNode(from);
prev = next.prev;
@ -280,18 +311,41 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return a;
}
#if DEQUEUE_FEATURE
@Override
public KEY_TYPE first() {
if(first == null) throw new IllegalStateException();
return first.value;
return GET_FIRST_KEY();
}
@Override
public KEY_TYPE last() {
if(last == null) throw new IllegalStateException();
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 GET_LAST_KEY() {
if(last == null) throw new NoSuchElementException();
return last.value;
}
@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);
@ -312,9 +366,8 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
@Primitive
public int indexOf(Object o) {
if(o == null) return -1;
Entry KEY_GENERIC_TYPE entry = first;
for(int i = 0;entry != null;entry = entry.next) {
for(int i = 0;entry != null;entry = entry.next,i++) {
if(Objects.equals(KEY_TO_OBJ(entry.value), o)) return i;
}
return -1;
@ -323,9 +376,8 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
@Primitive
public int lastIndexOf(Object o) {
if(o == null) return -1;
Entry KEY_GENERIC_TYPE entry = last;
for(int i = size-1;entry != null;entry = entry.prev) {
for(int i = size-1;entry != null;entry = entry.prev,i--) {
if(Objects.equals(KEY_TO_OBJ(entry.value), o)) return i;
}
return -1;
@ -340,7 +392,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public int indexOf(KEY_TYPE e) {
Entry entry = first;
for(int i = 0;entry != null;entry = entry.next) {
for(int i = 0;entry != null;entry = entry.next,i++) {
if(KEY_EQUALS(entry.value, e)) return i;
}
return -1;
@ -349,26 +401,17 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public int lastIndexOf(KEY_TYPE e) {
Entry entry = last;
for(int i = size-1;entry != null;entry = entry.prev) {
for(int i = size-1;entry != null;entry = entry.prev,i--) {
if(KEY_EQUALS(entry.value, e)) return i;
}
return -1;
}
#endif
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return new ListIter(first, 0);
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator() {
return new ListIter(first, 0);
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator(int index) {
if(index == size-1) return new ListIter(last, index);
if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
if(index == size) return new ListIter(null, index);
if(index == 0) return new ListIter(first, index);
return new ListIter(getNode(index), index);
}
@ -402,17 +445,25 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
int index = 0;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next)
action.accept(index++, entry.value);
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next)
action.accept(entry.value, input);
action.accept(input, entry.value);
}
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(filter.TEST_VALUE(entry.value)) return true;
if(filter.test(entry.value)) return true;
}
return false;
}
@ -421,7 +472,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(filter.TEST_VALUE(entry.value)) return false;
if(filter.test(entry.value)) return false;
}
return true;
}
@ -430,7 +481,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(!filter.TEST_VALUE(entry.value)) return false;
if(!filter.test(entry.value)) return false;
}
return true;
}
@ -439,11 +490,60 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(filter.TEST_VALUE(entry.value)) return entry.value;
if(filter.test(entry.value)) return entry.value;
}
return EMPTY_VALUE;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
state = operator.APPLY_VALUE(state, entry.value);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
state = operator.APPLY_VALUE(state, entry.value);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(empty) {
empty = false;
state = entry.value;
continue;
}
state = operator.APPLY_VALUE(state, entry.value);
}
return state;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(filter.test(entry.value)) result++;
}
return result;
}
@Override
public KEY_TYPE set(int index, KEY_TYPE e) {
checkRange(index);
@ -476,6 +576,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
}
#endif
#if DEQUEUE_FEATURE
@Override
public void onChanged() {}
@Override
@ -483,25 +584,36 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public KEY_TYPE dequeue() {
if(first == null) throw new IllegalStateException();
if(first == null) throw new NoSuchElementException();
return unlinkFirst(first);
}
@Override
public KEY_TYPE dequeueLast() {
if(last == null) throw new IllegalStateException();
return pop();
}
#endif
@Override
public KEY_TYPE pop() {
if(last == null) throw new NoSuchElementException();
return unlinkLast(last);
}
#if DEQUEUE_FEATURE
@Override
public KEY_TYPE pop() {
return dequeueLast();
public boolean removeFirst(KEY_TYPE e) {
#if TYPE_OBJECT
return remove(e);
#else
return REMOVE_KEY(e);
#endif
}
@Override
public boolean removeFirst(KEY_TYPE e) {
public boolean removeLast(KEY_TYPE e) {
if(size == 0) return false;
for(Entry KEY_GENERIC_TYPE entry = first;entry.next != null;entry = entry.next) {
for(Entry KEY_GENERIC_TYPE entry = last;entry != null;entry = entry.prev) {
if(KEY_EQUALS(entry.value, e)) {
unlink(entry);
return true;
@ -510,12 +622,67 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return false;
}
#endif
@Override
public boolean removeLast(KEY_TYPE e) {
public KEY_TYPE swapRemove(int index) {
checkRange(index);
Entry KEY_GENERIC_TYPE entry = getNode(index);
if(entry == null) return EMPTY_KEY_VALUE;
if(entry.next == null) return unlinkLast(entry);
Entry KEY_GENERIC_TYPE before = entry.prev;
KEY_TYPE result = unlink(entry);
if(before == null) {
Entry KEY_GENERIC_TYPE temp = last;
last = temp.prev;
last.next = null;
temp.next = first;
temp.prev = null;
first.prev = temp;
first = temp;
return result;
}
else if(before.next != last) {
Entry KEY_GENERIC_TYPE temp = last;
last = temp.prev;
last.next = null;
temp.next = before.next;
temp.prev = before;
before.next = temp;
temp.next.prev = temp;
}
return result;
}
@Override
public boolean REMOVE_SWAP(KEY_TYPE e) {
if(size == 0) return false;
for(Entry KEY_GENERIC_TYPE entry = last;entry.prev != null;entry = entry.prev) {
for(Entry KEY_GENERIC_TYPE entry = last;entry != null;entry = entry.prev) {
if(KEY_EQUALS(entry.value, e)) {
if(entry.next == null) {
unlinkLast(entry);
return true;
}
Entry KEY_GENERIC_TYPE before = entry.prev;
unlink(entry);
if(before == null) {
Entry KEY_GENERIC_TYPE temp = last;
last = temp.prev;
last.next = null;
temp.next = first;
temp.prev = null;
first.prev = temp;
first = temp;
return true;
}
else if(before.next != last) {
Entry KEY_GENERIC_TYPE temp = last;
last = temp.prev;
last.next = null;
temp.next = before.next;
temp.prev = before;
before.next = temp;
temp.next.prev = temp;
}
return true;
}
}
@ -526,7 +693,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public boolean remove(Object e) {
if(size <= 0) return false;
for(Entry KEY_GENERIC_TYPE entry = first;entry.next != null;entry = entry.next) {
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(KEY_EQUALS(entry.value, e)) {
unlink(entry);
return true;
@ -538,14 +705,22 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#else
@Override
public boolean REMOVE_KEY(KEY_TYPE e) {
return removeFirst(e);
if(size == 0) return false;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(KEY_EQUALS(entry.value, e)) {
unlink(entry);
return true;
}
}
return false;
}
#endif
@Override
public KEY_TYPE REMOVE(int index) {
checkRange(index);
return unlink(getNode(index));
Entry KEY_GENERIC_TYPE entry = getNode(index);
return entry == null ? EMPTY_KEY_VALUE : unlink(entry);
}
@Override
@ -557,16 +732,18 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
if(from < size - to) {
Entry KEY_GENERIC_TYPE entry = getNode(from);
while(length > 0) {
entry = entry.next;
unlink(entry.prev);
Entry KEY_GENERIC_TYPE next = entry.next;
unlink(entry);
entry = next;
length--;
}
return;
}
Entry KEY_GENERIC_TYPE entry = getNode(to);
Entry KEY_GENERIC_TYPE entry = getNode(to-1);
while(length > 0) {
entry = entry.prev;
unlink(entry.next);
Entry KEY_GENERIC_TYPE prev = entry.prev;
unlink(entry);
entry = prev;
length--;
}
}
@ -577,20 +754,22 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
checkRange(from);
checkAddRange(to);
int length = to - from;
if(length <= 0) return ARRAYS.newArray(type, 0);
K[] a = ARRAYS.newArray(type, length);
if(length <= 0) return a;
if(from < size - to) {
Entry KEY_GENERIC_TYPE entry = getNode(from);
for(int i = 0;length > 0;i++, length--) {
entry = entry.next;
a[i] = (K)unlink(entry.prev);
Entry KEY_GENERIC_TYPE next = entry.next;
a[i] = (K)unlink(entry);
entry = next;
}
return a;
}
Entry KEY_GENERIC_TYPE entry = getNode(to);
for(int i = length-1;length > 0;i--) {
entry = entry.prev;
a[i] = (K)unlink(entry.next);
Entry KEY_GENERIC_TYPE entry = getNode(to-1);
for(int i = length-1;length > 0;i--, length--) {
Entry KEY_GENERIC_TYPE prev = entry.prev;
a[i] = (K)unlink(entry);
entry = prev;
}
return a;
}
@ -606,19 +785,29 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
if(from < size - to) {
Entry KEY_GENERIC_TYPE entry = getNode(from);
for(int i = 0;length > 0;i++, length--) {
entry = entry.next;
d[i] = unlink(entry.prev);
Entry KEY_GENERIC_TYPE next = entry.next;
d[i] = unlink(entry);
entry = next;
}
return d;
}
Entry KEY_GENERIC_TYPE entry = getNode(to);
for(int i = length-1;length > 0;i--) {
entry = entry.prev;
d[i] = unlink(entry.next);
Entry KEY_GENERIC_TYPE entry = getNode(to-1);
for(int i = length-1;length > 0;i--, length--) {
Entry KEY_GENERIC_TYPE prev = entry.prev;
d[i] = unlink(entry);
entry = prev;
}
return d;
}
#endif
#if PRIMITIVES
@Override
public void fillBuffer(JAVA_BUFFER buffer) {
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next)
buffer.put(entry.value);
}
#endif
@Override
@Primitive
@ -686,6 +875,27 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return modified;
}
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
if(c.isEmpty()) return false;
boolean modified = false;
int j = 0;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;) {
if(c.contains(entry.value)) {
r.accept(entry.value);
Entry KEY_GENERIC_TYPE next = entry.next;
unlink(entry);
entry = next;
modified = true;
continue;
}
else j++;
entry = entry.next;
}
size = j;
return modified;
}
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) {
if(c.isEmpty()) {
@ -710,6 +920,32 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return modified;
}
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
if(c.isEmpty()) {
boolean changed = size > 0;
forEach(r);
clear();
return changed;
}
boolean modified = false;
int j = 0;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;) {
if(!c.contains(entry.value)) {
r.accept(entry.value);
Entry KEY_GENERIC_TYPE next = entry.next;
unlink(entry);
entry = next;
modified = true;
continue;
}
else j++;
entry = entry.next;
}
size = j;
return modified;
}
@Override
@Primitive
public boolean removeIf(Predicate<? super CLASS_TYPE> filter) {
@ -754,6 +990,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#endif
@Override
public Object[] toArray() {
if(size == 0) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size];
int i = 0;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
@ -770,6 +1007,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
a[i++] = (E)KEY_TO_OBJ(entry.value);
}
if (a.length > size) a[size] = null;
return a;
}
@ -779,11 +1017,18 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
if(a.length < size) a = new KEY_TYPE[size];
int i = 0;
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
a[i++] = KEY_TO_OBJ(entry.value);
a[i++] = entry.value;
}
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#else
@Override
public <E> E[] toArray(IntFunction<E[]> action) {
return super.toArray(action);
}
#endif
@Override
public int size() {
@ -802,6 +1047,23 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
size = 0;
}
@Override
public LINKED_LIST KEY_GENERIC_TYPE copy() {
LINKED_LIST KEY_GENERIC_TYPE list = new LINKED_LISTBRACES();
list.size = size;
if(first != null) {
list.first = new EntryBRACES(first.value, null, null);
Entry KEY_GENERIC_TYPE lastReturned = list.first;
for(Entry KEY_GENERIC_TYPE entry = first.next;entry != null;entry = entry.next) {
Entry KEY_GENERIC_TYPE next = new EntryBRACES(entry.value, lastReturned, null);
lastReturned.next = next;
lastReturned = next;
}
list.last = lastReturned;
}
return list;
}
protected Entry KEY_GENERIC_TYPE getNode(int index) {
if(index < size >> 2) {
Entry KEY_GENERIC_TYPE x = first;
@ -906,33 +1168,37 @@ 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;
}
@Override
public String toString() {
return KEY_TO_STRING(value);
}
}
private class ListIter implements LIST_ITERATOR KEY_GENERIC_TYPE
{
Entry KEY_GENERIC_TYPE node;
Entry KEY_GENERIC_TYPE next;
Entry KEY_GENERIC_TYPE lastReturned;
int index;
ListIter(Entry KEY_GENERIC_TYPE node, int index) {
this.node = node;
ListIter(Entry KEY_GENERIC_TYPE next, int index) {
this.next = next;
this.index = index;
}
@Override
public boolean hasNext() {
return node != null;
return index < size;
}
@Override
public boolean hasPrevious() {
return node != null;
return index > 0;
}
@Override
@ -948,23 +1214,26 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
if(lastReturned.next == node) index--;
Entry KEY_GENERIC_TYPE lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned) next = lastNext;
else index--;
lastReturned = null;
}
@Override
public KEY_TYPE PREVIOUS() {
lastReturned = node;
node = node.prev;
if(!hasPrevious()) throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
index--;
return lastReturned.value;
}
@Override
public KEY_TYPE NEXT() {
lastReturned = node;
node = node.next;
if(!hasNext()) throw new NoSuchElementException();
lastReturned = next;
next = next.next;
index++;
return lastReturned.value;
}
@ -977,10 +1246,9 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public void add(KEY_TYPE e) {
if(lastReturned == null) throw new IllegalStateException();
if(node.next == null) linkLast(e);
else linkBefore(e, node);
lastReturned = null;
if (next == null) linkLast(e);
else linkBefore(e, next);
index++;
}
}
@ -1043,7 +1311,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public long estimateSize() {
return list.size - index;
return (long)list.size - (long)index;
}
@Override
@ -1109,7 +1377,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public long estimateSize() {
return list.size - index;
return (long)list.size - (long)index;
}
@Override

View File

@ -1,22 +1,32 @@
package speiger.src.collections.PACKAGE.lists;
#if PRIMITIVES
import java.nio.JAVA_BUFFER;
#endif
import java.util.List;
#if !TYPE_OBJECT && !TYPE_BOOLEAN
import java.util.Objects;
import java.util.function.JAVA_UNARY_OPERATOR;
import java.util.function.UnaryOperator;
#else if TYPE_OBJECT
#endif
import java.util.Objects;
import java.util.Comparator;
#if !TYPE_BOOLEAN
import java.util.function.UnaryOperator;
#endif
import java.util.Comparator;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#if LISTS_FEATURE
import speiger.src.collections.PACKAGE.utils.LISTS;
#endif
#if INT_LIST_MODULE && !TYPE_INT
import speiger.src.collections.ints.lists.IntList;
#endif
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT
import speiger.src.collections.utils.SanityChecks;
@ -47,6 +57,26 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
public void add(int index, KEY_TYPE e);
#endif
/**
* A Helper function that will only add elements if it is not present.
* @param e the element to add
* @return true if the list was modified
*/
public default boolean addIfAbsent(KEY_TYPE e) {
if(indexOf(e) == -1) return add(e);
return false;
}
/**
* A Helper function that will only add elements if it is present.
* @param e the element to add
* @return true if the list was modified
*/
public default boolean addIfPresent(KEY_TYPE e) {
if(indexOf(e) != -1) return add(e);
return false;
}
/**
* A Type-Specific addAll Function to reduce (un)boxing
* @param c the elements that need to be added
@ -71,6 +101,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
@ -169,7 +239,6 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
* @param offset the start index of the array should be read from
* @param length how many elements should be read from
* @throws IndexOutOfBoundsException if from is outside of the lists range
* @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
*/
public void addElements(int from, KEY_TYPE[] a, int offset, int length);
@ -204,6 +273,22 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
*/
public void removeElements(int from, int to);
/**
* A Highly Optimized remove function that removes the desired element.
* But instead of shifting the elements to the left it moves the last element to the removed space.
* @param index the index of the element to be removed
* @return the element previously at the specified position
*/
public KEY_TYPE swapRemove(int index);
/**
* A Highly Optimized remove function that removes the desired element.
* But instead of shifting the elements to the left it moves the last element to the removed space.
* @param e the element that should be removed
* @return true if the element was removed
*/
public boolean REMOVE_SWAP(KEY_TYPE e);
#if TYPE_OBJECT
/**
* A function to fast extract elements out of the list, this removes the elements that were fetched.
@ -256,6 +341,14 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
*/
public KEY_TYPE[] extractElements(int from, int to);
#if PRIMITIVES
/**
* Helper function that allows to fastFill a buffer reducing the duplication requirement
* @param buffer where the data should be stored in.
*/
public default void fillBuffer(JAVA_BUFFER buffer) { buffer.put(TO_ARRAY()); }
#endif
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
@ -311,6 +404,16 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
}
#endif
/**
* A Indexed forEach implementation that allows you to keep track of how many elements were already iterated over.
* @param action The action to be performed for each element
* @throws java.lang.NullPointerException if the specified action is null
*/
@Override
public default void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(int i = 0,m=size();i<m;action.accept(i, GET_KEY(i++)));
}
/**
* A Type-Specific Iterator of listIterator
* @see java.util.List#listIterator
@ -325,6 +428,26 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator(int index);
/**
* Creates a Iterator that follows the indecies provided.<br>
* For example if the Lists Contents is:<br> -1, 0 1 <br>and the indecies are: <br>0, 1, 2, 2, 1, 0<br>
* then the iterator will return the following values: <br>-1, 0, 1, 1, 0, -1
* @param indecies that should be used for the iteration.
* @return a custom indexed iterator
*/
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(int...indecies);
#if INT_LIST_MODULE
/**
* Creates a Iterator that follows the indecies provided.<br>
* For example if the Lists Contents is:<br> -1, 0 1 <br>and the indecies are: <br>0, 1, 2, 2, 1, 0<br>
* then the iterator will return the following values: <br>-1, 0, 1, 1, 0, -1
* @param indecies that should be used for the iteration.
* @return a custom indexed iterator
*/
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(IntList indecies);
#endif
/**
* A Type-Specific List of subList
* @see java.util.List#subList(int, int)
@ -332,6 +455,36 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
@Override
public LIST KEY_GENERIC_TYPE subList(int from, int to);
/**
* A Type-Specific List Helper that shows all elements in reverse.
* @return a list wrapper that has all elements reversed!
*/
public LIST KEY_GENERIC_TYPE reversed();
#if LISTS_FEATURE
/**
* Creates a Wrapped List that is Synchronized
* @return a new List that is synchronized
* @see LISTS#synchronize
*/
public default LIST KEY_GENERIC_TYPE synchronize() { return LISTS.synchronize(this); }
/**
* Creates a Wrapped List that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new List Wrapper that is synchronized
* @see LISTS#synchronize
*/
public default LIST KEY_GENERIC_TYPE synchronize(Object mutex) { return LISTS.synchronize(this, mutex); }
/**
* Creates a Wrapped List that is unmodifiable
* @return a new List Wrapper that is unmodifiable
* @see LISTS#unmodifiable
*/
public default LIST KEY_GENERIC_TYPE unmodifiable() { return LISTS.unmodifiable(this); }
#endif
/**
* A function to ensure the elements are within the requested size.
* If smaller then the stored elements they get removed as needed.
@ -340,6 +493,8 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
*/
public void size(int size);
@Override
public LIST KEY_GENERIC_TYPE copy();
#if !TYPE_OBJECT
/** {@inheritDoc}

View File

@ -3,21 +3,30 @@ package speiger.src.collections.PACKAGE.maps.abstracts;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;
#if VALUE_BOOLEAN && JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#if !VALUE_BOOLEAN || !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.FUNCTION;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SET;
#if MAPS_FEATURE
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
#endif
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR;
#endif
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER;
import speiger.src.collections.objects.collections.ObjectIterable;
#if !TYPE_OBJECT && !VALUE_OBJECT
import speiger.src.collections.objects.collections.ObjectIterator;
#endif
@ -46,6 +55,27 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
return this;
}
protected ObjectIterable<MAP.Entry KEY_VALUE_GENERIC_TYPE> getFastIterable(MAP KEY_VALUE_GENERIC_TYPE map) {
#if MAPS_FEATURE
return MAPS.fastIterable(map);
#else
return map.ENTRY_SET();
#endif
}
protected ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> getFastIterator(MAP KEY_VALUE_GENERIC_TYPE map) {
#if MAPS_FEATURE
return MAPS.fastIterator(map);
#else
return map.ENTRY_SET().iterator();
#endif
}
@Override
public MAP KEY_VALUE_GENERIC_TYPE copy() {
throw new UnsupportedOperationException();
}
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
@Deprecated
@ -57,14 +87,14 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
#if VALUE_PRIMITIVES
@Override
public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m))
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : getFastIterable(m))
addTo(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
#endif
@Override
public void putAll(MAP KEY_VALUE_GENERIC_TYPE m) {
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(m);iter.hasNext();) {
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = getFastIterator(m);iter.hasNext();) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = iter.next();
put(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
@ -84,9 +114,18 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
for(int i = 0;i<size;i++) put(keys[i], values[i]);
}
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public void putAll(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, int offset, int size) {
SanityChecks.checkArrayCapacity(keys.length, offset, size);
SanityChecks.checkArrayCapacity(values.length, offset, size);
for(int i = 0;i<size;i++) put(keys[i], values[i]);
}
#endif
@Override
public void putAllIfAbsent(MAP KEY_VALUE_GENERIC_TYPE m) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m))
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : getFastIterable(m))
putIfAbsent(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
@ -146,14 +185,14 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override
public void REPLACE_VALUES(MAP KEY_VALUE_GENERIC_TYPE m) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m))
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : getFastIterable(m))
replace(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
@Override
public void REPLACE_VALUES(UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(this);iter.hasNext();) {
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = getFastIterator(this);iter.hasNext();) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = iter.next();
entry.setValue(mappingFunction.APPLY_VALUE(entry.ENTRY_KEY(), entry.ENTRY_VALUE()));
}
@ -162,6 +201,96 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
#if !VALUE_OBJECT
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, GET_VALUE(key));
#else
VALUE_TYPE value = GET_VALUE(key);
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, value);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
if(VALUE_EQUALS_NOT(value, getDefaultReturnValue()) || containsKey(key)) {
remove(key);
return getDefaultReturnValue();
}
return getDefaultReturnValue();
}
#endif
put(key, newValue);
return newValue;
}
@Override
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
#if VALUE_OBJECT
VALUE_TYPE value;
if((value = GET_VALUE(key)) == getDefaultReturnValue() || !containsKey(key)) {
VALUE_TYPE newValue = mappingFunction.APPLY(key);
if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) {
put(key, newValue);
return newValue;
}
}
return value;
#else
if(!containsKey(key)) {
VALUE_TYPE newValue = mappingFunction.APPLY(key);
put(key, newValue);
return newValue;
}
return get(key);
#endif
}
@Override
public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) {
Objects.requireNonNull(valueProvider);
#if VALUE_OBJECT
VALUE_TYPE value;
if((value = GET_VALUE(key)) == getDefaultReturnValue() || !containsKey(key)) {
VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET();
if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) {
put(key, newValue);
return newValue;
}
}
return value;
#else
if(!containsKey(key)) {
VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET();
put(key, newValue);
return newValue;
}
return get(key);
#endif
}
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
#if VALUE_OBJECT
VALUE_TYPE value;
if(VALUE_EQUALS_NOT((value = GET_VALUE(key)), getDefaultReturnValue()) || containsKey(key)) {
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, value);
if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) {
put(key, newValue);
return newValue;
}
remove(key);
}
#else
if(containsKey(key)) {
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, GET_VALUE(key));
put(key, newValue);
return newValue;
}
#endif
return getDefaultReturnValue();
}
#if !VALUE_OBJECT
@Override
public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
VALUE_TYPE value = GET_VALUE(key);
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, value);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
@ -176,11 +305,11 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
}
@Override
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
VALUE_TYPE value;
if((value = GET_VALUE(key)) == getDefaultReturnValue() || !containsKey(key)) {
VALUE_TYPE newValue = mappingFunction.GET_VALUE(key);
VALUE_TYPE newValue = mappingFunction.APPLY(key);
if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) {
put(key, newValue);
return newValue;
@ -190,7 +319,21 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
}
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) {
Objects.requireNonNull(valueProvider);
VALUE_TYPE value;
if((value = GET_VALUE(key)) == getDefaultReturnValue() || !containsKey(key)) {
VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET();
if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) {
put(key, newValue);
return newValue;
}
}
return value;
}
@Override
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
VALUE_TYPE value;
if(VALUE_EQUALS_NOT((value = GET_VALUE(key)), getDefaultReturnValue()) || containsKey(key)) {
@ -204,6 +347,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
return getDefaultReturnValue();
}
#endif
@Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
@ -217,7 +361,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : getFastIterable(m)) {
KEY_TYPE key = entry.ENTRY_KEY();
VALUE_TYPE oldValue = GET_VALUE(key);
VALUE_TYPE newValue = VALUE_EQUALS(oldValue, getDefaultReturnValue()) ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(oldValue, entry.ENTRY_VALUE());
@ -256,10 +400,20 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
}
#endif
@Override
public CLASS_VALUE_TYPE remove(Object key) {
#if TYPE_OBJECT
return VALUE_TO_OBJ(REMOVE_VALUE((CLASS_TYPE)key));
#else
return key instanceof CLASS_TYPE ? VALUE_TO_OBJ(REMOVE_VALUE(CLASS_TO_KEY(key))) : VALUE_TO_OBJ(getDefaultReturnValue());
#endif
}
@Override
public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(this);iter.hasNext();) {
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = getFastIterator(this);iter.hasNext();) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = iter.next();
action.accept(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
@ -277,7 +431,11 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
#else
@Override
public boolean remove(Object o) {
return VALUE_EQUALS_NOT(ABSTRACT_MAP.this.remove(o), getDefaultReturnValue());
if(ABSTRACT_MAP.this.containsKey(o)) {
ABSTRACT_MAP.this.remove(o);
return true;
}
return false;
}
#endif
@ -289,7 +447,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return new ITERATOR KEY_GENERIC_TYPE() {
ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(ABSTRACT_MAP.this);
ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = getFastIterator(ABSTRACT_MAP.this);
@Override
public boolean hasNext() {
return iter.hasNext();
@ -340,7 +498,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override
public VALUE_ITERATOR VALUE_GENERIC_TYPE iterator() {
return new VALUE_ITERATOR VALUE_GENERIC_TYPE() {
ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(ABSTRACT_MAP.this);
ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = getFastIterator(ABSTRACT_MAP.this);
@Override
public boolean hasNext() {
return iter.hasNext();
@ -380,7 +538,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override
public int hashCode() {
int hash = 0;
ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(this);
ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = getFastIterator(this);
while(iter.hasNext()) hash += iter.next().hashCode();
return hash;
}
@ -475,7 +633,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override
public String toString() {
return KEY_TO_STRING(key) + "->" + VALUE_TO_STRING(value);
return KEY_TO_STRING(key) + "=" + VALUE_TO_STRING(value);
}
}
}

View File

@ -1,33 +1,50 @@
package speiger.src.collections.PACKAGE.maps.impl.customHash;
import java.util.Comparator;
import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.BiFunction;
import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE;
#endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
#if !TYPE_OBJECT && !VALUE_OBJECT
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
#endif
#if !TYPE_OBJECT || VALUE_BOOLEAN
#if !SAME_TYPE && !TYPE_INT
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
#endif
#if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
#if !TYPE_INT || !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
import speiger.src.collections.PACKAGE.utils.STRATEGY;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR;
#endif
#if !VALUE_OBJECT && !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER;
import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
@ -35,21 +52,24 @@ import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
#if !TYPE_OBJECT && !VALUE_OBJECT || !VALUE_OBJECT
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
#endif
#if !TYPE_OBJECT || !VALUE_BOOLEAN
#if !VALUE_OBJECT || SAME_TYPE
import speiger.src.collections.objects.functions.function.Object2BooleanFunction;
#endif
#endif
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.consumer.VALUE_BI_OBJECT_CONSUMER;
#if !TYPE_OBJECT
import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_CONSUMER;
#endif
#if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
#endif
#endif
#if !TYPE_OBJECT
#if !VALUE_OBJECT
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
#endif
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSortedSet;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
#endif
import speiger.src.collections.utils.HashUtil;
@ -60,7 +80,7 @@ import speiger.src.collections.utils.HashUtil;
* @Type(T)
* @ValueType(V)
*/
public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE
public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE implements ORDERED_MAP KEY_VALUE_GENERIC_TYPE
{
/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
protected transient long[] links;
@ -277,7 +297,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public boolean moveToFirst(KEY_TYPE key) {
if(strategy.equals(FIRST_ENTRY_KEY(), key)) return false;
if(isEmpty() || strategy.equals(FIRST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToFirstIndex(nullIndex);
@ -299,7 +319,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public boolean moveToLast(KEY_TYPE key) {
if(strategy.equals(LAST_ENTRY_KEY(), key)) return false;
if(isEmpty() || strategy.equals(LAST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToLastIndex(nullIndex);
@ -336,19 +356,22 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
public LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE map = new LINKED_CUSTOM_HASH_MAPKV_BRACES(0, loadFactor, strategy);
map.minCapacity = minCapacity;
map.mask = mask;
map.maxFill = maxFill;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
map.links = Arrays.copyOf(links, links.length);
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
@ -359,8 +382,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
public KEY_TYPE POLL_FIRST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = firstIndex;
firstIndex = (int)links[pos];
if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(strategy.equals(result, EMPTY_KEY_VALUE)) {
@ -383,8 +405,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
public KEY_TYPE POLL_LAST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = lastIndex;
lastIndex = (int)(links[pos] >>> 32);
if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(strategy.equals(result, EMPTY_KEY_VALUE)) {
@ -410,15 +431,15 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
return (ORDERED_SET KEY_GENERIC_TYPE)keySet;
}
@Override
@ -445,7 +466,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= size) {
if(request >= nullIndex) {
clear();
return;
}
@ -592,7 +613,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
values = newValues;
}
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE {
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
@ -652,99 +673,171 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
return new FastEntryIterator(fromElement);
}
@Override
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
while(index != -1) {
action.accept(new BasicEntryKV_BRACES(keys[index], values[index]));
action.accept(new ValueMapEntry(index));
index = (int)links[index];
}
}
@Override
public void fastForEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
entry.set(index);
action.accept(entry);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, ObjectObjectConsumer<MAP.Entry KEY_VALUE_GENERIC_TYPE, E> action) {
public void forEachIndexed(IntObjectConsumer<MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1) {
action.accept(new BasicEntryKV_BRACES(keys[index], values[index]), input);
action.accept(count++, new ValueMapEntry(index));
index = (int)links[index];
}
}
@Override
public boolean matchesAny(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return false;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
public <E> void forEach(E input, ObjectObjectConsumer<E, MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return true;
action.accept(input, new ValueMapEntry(index));
index = (int)links[index];
}
}
@Override
public boolean matchesAny(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return false;
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(index);
if(filter.test(entry)) return true;
index = (int)links[index];
}
return false;
}
@Override
public boolean matchesNone(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public boolean matchesNone(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return false;
entry.set(index);
if(filter.test(entry)) return false;
index = (int)links[index];
}
return true;
}
@Override
public boolean matchesAll(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public boolean matchesAll(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(!filter.getBoolean(entry)) return false;
entry.set(index);
if(!filter.test(entry)) return false;
index = (int)links[index];
}
return true;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public <E> E reduce(E identity, BiFunction<E, MAP.Entry KEY_VALUE_GENERIC_TYPE, E> operator) {
Objects.requireNonNull(operator);
E state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index];
}
return state;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = new ValueMapEntry(index);
index = (int)links[index];
continue;
}
state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index];
}
return state;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return null;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return entry;
entry.set(index);
if(filter.test(entry)) return entry;
index = (int)links[index];
}
return null;
}
@Override
public int count(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
MapEntry entry = new MapEntry();
int result = 0;
int index = firstIndex;
while(index != -1) {
entry.set(index);
if(filter.test(entry)) result++;
index = (int)links[index];
}
return result;
}
@Override
@Deprecated
public boolean contains(Object o) {
if(o instanceof Map.Entry) {
if(o instanceof MAP.Entry) return LINKED_CUSTOM_HASH_MAP.this.containsKey(((MAP.Entry KEY_VALUE_GENERIC_TYPE)o).ENTRY_KEY());
return LINKED_CUSTOM_HASH_MAP.this.containsKey(((Map.Entry<?, ?>)o).getKey());
if(o instanceof MAP.Entry) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o;
int index = LINKED_CUSTOM_HASH_MAP.this.findIndex(entry.ENTRY_KEY());
if(index >= 0) return VALUE_EQUALS(entry.ENTRY_VALUE(), LINKED_CUSTOM_HASH_MAP.this.values[index]);
}
else {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
#if !TYPE_OBJECT
if(!(entry.getKey() instanceof CLASS_TYPE)) return false;
#endif
int index = LINKED_CUSTOM_HASH_MAP.this.findIndex((CLASS_TYPE)entry.getKey());
if(index >= 0) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(LINKED_CUSTOM_HASH_MAP.this.values[index]));
}
}
return false;
}
@ -772,20 +865,9 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
public void clear() {
LINKED_CUSTOM_HASH_MAP.this.clear();
}
@Override
public Comparator<MAP.Entry KEY_VALUE_GENERIC_TYPE> comparator() {
return null;
}
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> subSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement, MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> headSet(MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> tailSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { throw new UnsupportedOperationException(); }
}
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE {
#if TYPE_OBJECT
@Override
@Deprecated
@ -796,7 +878,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public boolean remove(Object o) {
int oldSize = size;
remove(o);
LINKED_CUSTOM_HASH_MAP.this.remove(o);
return size != oldSize;
}
@ -809,7 +891,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public boolean remove(KEY_TYPE o) {
int oldSize = size;
remove(o);
LINKED_CUSTOM_HASH_MAP.this.remove(o);
return size != oldSize;
}
@ -845,6 +927,9 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
return new KeyIterator(fromElement);
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return LINKED_CUSTOM_HASH_MAP.this.size();
@ -885,12 +970,24 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1){
action.accept(count++, keys[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1){
action.accept(keys[index], input);
action.accept(input, keys[index]);
index = (int)links[index];
}
}
@ -901,7 +998,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(size() <= 0) return false;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return true;
if(filter.test(keys[index])) return true;
index = (int)links[index];
}
return false;
@ -913,7 +1010,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return false;
if(filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
@ -925,35 +1022,82 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(!filter.TEST_VALUE(keys[index])) return false;
if(!filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, keys[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = keys[index];
index = (int)links[index];
continue;
}
state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return keys[index];
if(filter.test(keys[index])) return keys[index];
index = (int)links[index];
}
return EMPTY_KEY_VALUE;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
int index = firstIndex;
while(index != -1){
if(filter.test(keys[index])) result++;
index = (int)links[index];
}
return result;
}
}
private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE {
@ -1001,12 +1145,24 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
}
@Override
public <E> void forEach(E input, VALUE_BI_OBJECT_CONSUMER VVS_GENERIC_TYPE<E> action) {
public void forEachIndexed(VALUE_BI_FROM_INT_CONSUMER VALUE_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1){
action.accept(count++, values[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1){
action.accept(values[index], input);
action.accept(input, values[index]);
index = (int)links[index];
}
}
@ -1017,7 +1173,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(size() <= 0) return false;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return true;
if(filter.test(values[index])) return true;
index = (int)links[index];
}
return false;
@ -1029,7 +1185,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return false;
if(filter.test(values[index])) return false;
index = (int)links[index];
}
return true;
@ -1041,23 +1197,82 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(!filter.VALUE_TEST_VALUE(values[index])) return false;
if(!filter.test(values[index])) return false;
index = (int)links[index];
}
return true;
}
#if !VALUE_OBJECT
@Override
public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
VALUE_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <VALUE_SPECIAL_TYPE> VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction<VALUE_SPECIAL_TYPE, VALUE_TYPE, VALUE_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
VALUE_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, values[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = values[index];
index = (int)links[index];
continue;
}
state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index];
}
return state;
}
@Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return values[index];
if(filter.test(values[index])) return values[index];
index = (int)links[index];
}
return EMPTY_VALUE;
}
@Override
public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
int index = firstIndex;
while(index != -1){
if(filter.test(values[index])) result++;
index = (int)links[index];
}
return result;
}
}
private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
@ -1097,12 +1312,12 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
return entry = new MapEntry(nextEntry());
return entry = new ValueMapEntry(nextEntry());
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
return entry = new MapEntry(previousEntry());
return entry = new ValueMapEntry(previousEntry());
}
@Override

View File

@ -1,32 +1,49 @@
package speiger.src.collections.PACKAGE.maps.impl.hash;
import java.util.Comparator;
import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.BiFunction;
import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE;
#endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
#if !TYPE_OBJECT && !VALUE_OBJECT
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
#endif
#if !TYPE_OBJECT || VALUE_BOOLEAN
#if !SAME_TYPE && !TYPE_INT
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
#endif
#if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
#if !TYPE_INT || !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR;
#endif
#if !VALUE_OBJECT && !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER;
import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
@ -34,21 +51,24 @@ import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
#if !TYPE_OBJECT && !VALUE_OBJECT || !VALUE_OBJECT
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
#endif
#if !TYPE_OBJECT || !VALUE_BOOLEAN
#if !VALUE_OBJECT || SAME_TYPE
import speiger.src.collections.objects.functions.function.Object2BooleanFunction;
#endif
#endif
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.consumer.VALUE_BI_OBJECT_CONSUMER;
#if !TYPE_OBJECT
import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_CONSUMER;
#endif
#if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
#endif
#endif
#if !TYPE_OBJECT
#if !VALUE_OBJECT
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
#endif
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSortedSet;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
#endif
import speiger.src.collections.utils.HashUtil;
@ -59,7 +79,7 @@ import speiger.src.collections.utils.HashUtil;
* @Type(T)
* @ValueType(V)
*/
public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE
public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_GENERIC_TYPE implements ORDERED_MAP KEY_VALUE_GENERIC_TYPE
{
/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
protected transient long[] links;
@ -254,7 +274,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public boolean moveToFirst(KEY_TYPE key) {
if(KEY_EQUALS(FIRST_ENTRY_KEY(), key)) return false;
if(isEmpty() || KEY_EQUALS(FIRST_ENTRY_KEY(), key)) return false;
if(KEY_EQUALS_NULL(key)) {
if(containsNull) {
moveToFirstIndex(nullIndex);
@ -276,7 +296,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public boolean moveToLast(KEY_TYPE key) {
if(KEY_EQUALS(LAST_ENTRY_KEY(), key)) return false;
if(isEmpty() || KEY_EQUALS(LAST_ENTRY_KEY(), key)) return false;
if(KEY_EQUALS_NULL(key)) {
if(containsNull) {
moveToLastIndex(nullIndex);
@ -312,19 +332,49 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
return values[index];
}
#if !VALUE_OBJECT
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
public boolean containsValue(VALUE_TYPE value) {
int index = firstIndex;
while(index != -1) {
if(VALUE_EQUALS(values[index], value)) return true;
index = (int)links[index];
}
return false;
}
#endif
@Override
@ValuePrimitive
public boolean containsValue(Object value) {
int index = firstIndex;
while(index != -1) {
#if VALUE_OBJECT
if(VALUE_EQUALS(values[index], value)) return true;
#else
if((value == null && values[index] == getDefaultReturnValue()) || EQUALS_VALUE_TYPE(values[index], value)) return true;
#endif
index = (int)links[index];
}
return false;
}
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { throw new UnsupportedOperationException(); }
public LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE map = new LINKED_HASH_MAPKV_BRACES(0, loadFactor);
map.minCapacity = minCapacity;
map.mask = mask;
map.maxFill = maxFill;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
map.links = Arrays.copyOf(links, links.length);
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public KEY_TYPE FIRST_ENTRY_KEY() {
@ -336,8 +386,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
public KEY_TYPE POLL_FIRST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = firstIndex;
firstIndex = (int)links[pos];
if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(KEY_EQUALS_NULL(result)) {
@ -360,8 +409,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
public KEY_TYPE POLL_LAST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = lastIndex;
lastIndex = (int)(links[pos] >>> 32);
if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(KEY_EQUALS_NULL(result)) {
@ -387,15 +435,15 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
return (ORDERED_SET KEY_GENERIC_TYPE)keySet;
}
@Override
@ -422,7 +470,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= size) {
if(request >= nullIndex) {
clear();
return;
}
@ -569,7 +617,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
values = newValues;
}
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE {
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
@ -629,99 +677,168 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
return new FastEntryIterator(fromElement);
}
@Override
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
while(index != -1){
action.accept(new BasicEntryKV_BRACES(keys[index], values[index]));
action.accept(new ValueMapEntry(index));
index = (int)links[index];
}
}
@Override
public void fastForEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1){
entry.set(keys[index], values[index]);
entry.set(index);
action.accept(entry);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, ObjectObjectConsumer<MAP.Entry KEY_VALUE_GENERIC_TYPE, E> action) {
public void forEachIndexed(IntObjectConsumer<MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1) {
action.accept(new BasicEntryKV_BRACES(keys[index], values[index]), input);
action.accept(count++, new ValueMapEntry(index));
index = (int)links[index];
}
}
@Override
public boolean matchesAny(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return false;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
public <E> void forEach(E input, ObjectObjectConsumer<E, MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return true;
action.accept(input, new ValueMapEntry(index));
index = (int)links[index];
}
}
@Override
public boolean matchesAny(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return false;
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(index);
if(filter.test(entry)) return true;
index = (int)links[index];
}
return false;
}
@Override
public boolean matchesNone(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public boolean matchesNone(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return false;
entry.set(index);
if(filter.test(entry)) return false;
index = (int)links[index];
}
return true;
}
@Override
public boolean matchesAll(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public boolean matchesAll(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(!filter.getBoolean(entry)) return false;
entry.set(index);
if(!filter.test(entry)) return false;
index = (int)links[index];
}
return true;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public <E> E reduce(E identity, BiFunction<E, MAP.Entry KEY_VALUE_GENERIC_TYPE, E> operator) {
Objects.requireNonNull(operator);
E state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index];
}
return state;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = new ValueMapEntry(index);
index = (int)links[index];
continue;
}
state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index];
}
return state;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return null;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return entry;
entry.set(index);
if(filter.test(entry)) return entry;
index = (int)links[index];
}
return null;
}
@Override
public int count(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1) {
entry.set(index);
if(filter.test(entry)) result++;
index = (int)links[index];
}
return result;
}
@Override
@Deprecated
public boolean contains(Object o) {
if(o instanceof Map.Entry) {
if(o instanceof MAP.Entry) return LINKED_HASH_MAP.this.containsKey(((MAP.Entry KEY_VALUE_GENERIC_TYPE)o).ENTRY_KEY());
return LINKED_HASH_MAP.this.containsKey(((Map.Entry<?, ?>)o).getKey());
if(o instanceof MAP.Entry) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o;
int index = LINKED_HASH_MAP.this.findIndex(entry.ENTRY_KEY());
if(index >= 0) return VALUE_EQUALS(entry.ENTRY_VALUE(), LINKED_HASH_MAP.this.values[index]);
}
else {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
int index = LINKED_HASH_MAP.this.findIndex(entry.getKey());
if(index >= 0) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(LINKED_HASH_MAP.this.values[index]));
}
}
return false;
}
@ -749,20 +866,9 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
public void clear() {
LINKED_HASH_MAP.this.clear();
}
@Override
public Comparator<MAP.Entry KEY_VALUE_GENERIC_TYPE> comparator() {
return null;
}
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> subSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement, MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> headSet(MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> tailSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { throw new UnsupportedOperationException(); }
}
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE {
#if TYPE_OBJECT
@Override
@Deprecated
@ -773,7 +879,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public boolean remove(Object o) {
int oldSize = size;
remove(o);
LINKED_HASH_MAP.this.remove(o);
return size != oldSize;
}
@ -786,15 +892,13 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public boolean remove(KEY_TYPE o) {
int oldSize = size;
remove(o);
LINKED_HASH_MAP.this.remove(o);
return size != oldSize;
}
#endif
@Override
public boolean add(KEY_TYPE o) {
throw new UnsupportedOperationException();
}
public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@ -822,6 +926,9 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
return new KeyIterator(fromElement);
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return LINKED_HASH_MAP.this.size();
@ -862,12 +969,24 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1){
action.accept(count++, keys[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1){
action.accept(keys[index], input);
action.accept(input, keys[index]);
index = (int)links[index];
}
}
@ -878,7 +997,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(size() <= 0) return false;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return true;
if(filter.test(keys[index])) return true;
index = (int)links[index];
}
return false;
@ -890,7 +1009,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return false;
if(filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
@ -901,36 +1020,83 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
Objects.requireNonNull(filter);
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(!filter.TEST_VALUE(keys[index])) return false;
while(index != -1) {
if(!filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, keys[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = keys[index];
index = (int)links[index];
continue;
}
state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return keys[index];
if(filter.test(keys[index])) return keys[index];
index = (int)links[index];
}
return EMPTY_KEY_VALUE;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
int index = firstIndex;
while(index != -1){
if(filter.test(keys[index])) result++;
index = (int)links[index];
}
return result;
}
}
private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE {
@ -979,12 +1145,24 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
}
@Override
public <E> void forEach(E input, VALUE_BI_OBJECT_CONSUMER VVS_GENERIC_TYPE<E> action) {
public void forEachIndexed(VALUE_BI_FROM_INT_CONSUMER VALUE_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1){
action.accept(count++, values[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1){
action.accept(values[index], input);
action.accept(input, values[index]);
index = (int)links[index];
}
}
@ -995,7 +1173,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(size() <= 0) return false;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return true;
if(filter.test(values[index])) return true;
index = (int)links[index];
}
return false;
@ -1006,8 +1184,8 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
Objects.requireNonNull(filter);
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return false;
while(index != -1) {
if(filter.test(values[index])) return false;
index = (int)links[index];
}
return true;
@ -1018,24 +1196,83 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
Objects.requireNonNull(filter);
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(!filter.VALUE_TEST_VALUE(values[index])) return false;
while(index != -1) {
if(!filter.test(values[index])) return false;
index = (int)links[index];
}
return true;
}
#if !VALUE_OBJECT
@Override
public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
VALUE_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <VALUE_SPECIAL_TYPE> VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction<VALUE_SPECIAL_TYPE, VALUE_TYPE, VALUE_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
VALUE_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, values[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = values[index];
index = (int)links[index];
continue;
}
state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index];
}
return state;
}
@Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return values[index];
if(filter.test(values[index])) return values[index];
index = (int)links[index];
}
return EMPTY_VALUE;
}
@Override
public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
int index = firstIndex;
while(index != -1){
if(filter.test(values[index])) result++;
index = (int)links[index];
}
return result;
}
}
private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
@ -1075,12 +1312,12 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
return entry = new MapEntry(nextEntry());
return entry = new ValueMapEntry(nextEntry());
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
return entry = new MapEntry(previousEntry());
return entry = new ValueMapEntry(previousEntry());
}
@Override
@ -1137,7 +1374,6 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); }
}
private class MapIterator {
@ -1207,7 +1443,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(previous == -1) firstIndex = next;
else links[previous] ^= ((links[previous] ^ (next & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
if (next == -1) lastIndex = previous;
if(next == -1) lastIndex = previous;
else links[next] ^= ((links[next] ^ ((previous & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
if(current == nullIndex) {
current = -1;

View File

@ -1,53 +1,73 @@
package speiger.src.collections.PACKAGE.maps.impl.immutable;
import java.util.Arrays;
import java.util.Map;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.BiFunction;
import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE;
#endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
#if !TYPE_OBJECT && !VALUE_OBJECT
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
#endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN
#if !SAME_TYPE && !TYPE_INT
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
#endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
#if !TYPE_INT || !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#endif
#if !VALUE_BOOLEAN || !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.function.FUNCTION;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
#endif
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#endif
#if !TYPE_OBJECT && !VALUE_OBJECT || !VALUE_OBJECT
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
#endif
#if !TYPE_OBJECT || !VALUE_BOOLEAN
#if !VALUE_OBJECT || SAME_TYPE
import speiger.src.collections.objects.functions.function.Object2BooleanFunction;
#endif
#endif
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.consumer.VALUE_BI_OBJECT_CONSUMER;
#if !TYPE_OBJECT
import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_CONSUMER;
#endif
#if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
#endif
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SET;
#endif
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER;
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR;
#if !VALUE_OBJECT
@ -58,11 +78,14 @@ import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_ARRAYS;
#endif
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
#if !TYPE_OBJECT
#if !VALUE_OBJECT
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
#endif
import speiger.src.collections.objects.lists.ObjectListIterator;
import speiger.src.collections.objects.sets.ObjectSortedSet;
#endif
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.SanityChecks;
@ -73,7 +96,7 @@ import speiger.src.collections.utils.SanityChecks;
* @Type(T)
* @ValueType(V)
*/
public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE
public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements ORDERED_MAP KEY_VALUE_GENERIC_TYPE
{
/** The Backing keys array */
protected transient KEY_TYPE[] keys;
@ -92,15 +115,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
/** The Last Index in the Map */
protected int lastIndex = -1;
/** EntrySet cache */
protected transient FastEntrySet KEY_VALUE_GENERIC_TYPE entrySet;
protected transient FastOrderedSet KEY_VALUE_GENERIC_TYPE entrySet;
/** KeySet cache */
protected transient SET KEY_GENERIC_TYPE keySet;
protected transient ORDERED_SET KEY_GENERIC_TYPE keySet;
/** Values cache */
protected transient VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC;
/** Amount of Elements stored in the HashMap */
protected int size;
/**
* Helper constructor for copying the Map
*/
protected IMMUTABLE_HASH_MAP() {}
#if !TYPE_OBJECT || !VALUE_OBJECT
/**
@ -202,7 +229,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
KEY_TYPE[] keys = NEW_KEY_ARRAY(map.size());
VALUE_TYPE[] values = NEW_VALUE_ARRAY(keys.length);
int index = 0;
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(map)) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : getFastIterable(map)) {
keys[index] = entry.ENTRY_KEY();
values[index] = entry.ENTRY_VALUE();
index++;
@ -222,18 +249,34 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
{
KEY_TYPE o = a[i];
if(KEY_EQUALS_NULL(o)) {
if(!containsNull) size++;
if(!containsNull) {
size++;
if(prev != -1) {
newLinks[prev] ^= ((newLinks[prev] ^ (newSize & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
newLinks[newSize] ^= ((newLinks[newSize] ^ ((prev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
prev = newSize;
}
else {
prev = firstIndex = newSize;
newLinks[newSize] = -1L;
}
}
containsNull = true;
newValues[newSize] = b[i];
continue;
}
boolean found = true;
int pos = HashUtil.mix(KEY_TO_HASH(o)) & newMask;
KEY_TYPE current = newKeys[pos];
if(KEY_EQUALS_NOT_NULL(current)) {
if(KEY_EQUALS(current, o)) continue;
while(KEY_EQUALS_NOT_NULL((current = newKeys[pos = (++pos & mask)]))) {
if(KEY_EQUALS(current, o)) {
newValues[pos] = b[i];
continue;
}
while(KEY_EQUALS_NOT_NULL((current = newKeys[pos = (++pos & newMask)]))) {
if(KEY_EQUALS(current, o)) {
found = false;
newValues[pos] = b[i];
break;
}
}
@ -241,7 +284,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
if(found) {
size++;
newKeys[pos] = o;
values[pos] = b[i];
newValues[pos] = b[i];
if(prev != -1) {
newLinks[prev] ^= ((newLinks[prev] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
newLinks[pos] ^= ((newLinks[pos] ^ ((prev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
@ -253,7 +296,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
}
}
nullIndex = size;
nullIndex = newSize;
mask = newMask;
keys = newKeys;
values = newValues;
@ -266,11 +309,11 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#if VALUE_PRIMITIVES
@Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@ -301,9 +344,11 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
#if !VALUE_OBJECT
@Override
public boolean containsValue(VALUE_TYPE value) {
if(VALUE_EQUALS(value, values[nullIndex])) return true;
for(int i = nullIndex-1;i >= 0;i--)
if(KEY_EQUALS_NOT_NULL(keys[i]) && VALUE_EQUALS(values[i], value)) return true;
int index = firstIndex;
while(index != -1) {
if(VALUE_EQUALS(values[index], value)) return true;
index = (int)links[index];
}
return false;
}
@ -311,9 +356,15 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
@Override
@ValuePrimitive
public boolean containsValue(Object value) {
if((value == null && VALUE_EQUALS(values[nullIndex], getDefaultReturnValue())) || EQUALS_VALUE_TYPE(values[nullIndex], value)) return true;
for(int i = nullIndex-1;i >= 0;i--)
if(KEY_EQUALS_NOT_NULL(keys[i]) && ((value == null && values[i] == getDefaultReturnValue()) || EQUALS_VALUE_TYPE(values[i], value))) return true;
int index = firstIndex;
while(index != -1) {
#if VALUE_OBJECT
if(VALUE_EQUALS(values[index], value)) return true;
#else
if((value == null && values[index] == getDefaultReturnValue()) || EQUALS_VALUE_TYPE(values[index], value)) return true;
#endif
index = (int)links[index];
}
return false;
}
@ -393,13 +444,13 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
}
@ -411,13 +462,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { throw new UnsupportedOperationException(); }
public IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE map = new IMMUTABLE_HASH_MAPKV_BRACES();
map.mask = mask;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
map.links = Arrays.copyOf(links, links.length);
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) {
@ -436,13 +493,22 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
@Override
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
#if !VALUE_OBJECT
@Override
public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@ -471,6 +537,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
#endif
protected int findIndex(Object key) {
if(key == null) return containsNull ? nullIndex : -(nullIndex + 1);
#if !TYPE_OBJECT
if(KEY_EQUALS_NULL(CLASS_TO_KEY(key))) return containsNull ? nullIndex : -(nullIndex + 1);
#endif
int pos = HashUtil.mix(key.hashCode()) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NOT_NULL(current)) {
@ -511,6 +580,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
Object key = entry.getKey();
#if !TYPE_OBJECT
if(!(key instanceof CLASS_TYPE)) return false;
#endif
Object value = entry.getValue();
#if TYPE_OBJECT && VALUE_OBJECT
return KEY_EQUALS(keys[index], key) && VALUE_EQUALS(values[index], value);
@ -532,11 +604,11 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
@Override
public String toString() {
return KEY_TO_STRING(keys[index]) + "->" + VALUE_TO_STRING(values[index]);
return KEY_TO_STRING(keys[index]) + "=" + VALUE_TO_STRING(values[index]);
}
}
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE {
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
@ -584,6 +656,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
return new FastEntryIterator(fromElement);
}
@Override
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
@ -605,74 +680,144 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
@Override
public <E> void forEach(E input, ObjectObjectConsumer<MAP.Entry KEY_VALUE_GENERIC_TYPE, E> action) {
public void forEachIndexed(IntObjectConsumer<MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1) {
action.accept(new BasicEntryKV_BRACES(keys[index], values[index]), input);
action.accept(count++, new BasicEntryKV_BRACES(keys[index], values[index]));
index = (int)links[index];
}
}
@Override
public boolean matchesAny(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public <E> void forEach(E input, ObjectObjectConsumer<E, MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1) {
action.accept(input, new BasicEntryKV_BRACES(keys[index], values[index]));
index = (int)links[index];
}
}
@Override
public boolean matchesAny(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return false;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return true;
if(filter.test(entry)) return true;
index = (int)links[index];
}
return false;
}
@Override
public boolean matchesNone(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public boolean matchesNone(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return false;
if(filter.test(entry)) return false;
index = (int)links[index];
}
return true;
}
@Override
public boolean matchesAll(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public boolean matchesAll(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(!filter.getBoolean(entry)) return false;
if(!filter.test(entry)) return false;
index = (int)links[index];
}
return true;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
public <E> E reduce(E identity, BiFunction<E, MAP.Entry KEY_VALUE_GENERIC_TYPE, E> operator) {
Objects.requireNonNull(operator);
E state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index]));
index = (int)links[index];
}
return state;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = new BasicEntryKV_BRACES(keys[index], values[index]);
index = (int)links[index];
continue;
}
state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index]));
index = (int)links[index];
}
return state;
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return null;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.getBoolean(entry)) return entry;
if(filter.test(entry)) return entry;
index = (int)links[index];
}
return null;
}
@Override
public int count(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
int result = 0;
int index = firstIndex;
while(index != -1) {
entry.set(keys[index], values[index]);
if(filter.test(entry)) result++;
index = (int)links[index];
}
return result;
}
@Override
@Deprecated
public boolean contains(Object o) {
if(o instanceof Map.Entry) {
if(o instanceof MAP.Entry) return IMMUTABLE_HASH_MAP.this.containsKey(((MAP.Entry KEY_VALUE_GENERIC_TYPE)o).ENTRY_KEY());
return IMMUTABLE_HASH_MAP.this.containsKey(((Map.Entry<?, ?>)o).getKey());
if(o instanceof MAP.Entry) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o;
int index = IMMUTABLE_HASH_MAP.this.findIndex(entry.ENTRY_KEY());
if(index >= 0) return VALUE_EQUALS(entry.ENTRY_VALUE(), IMMUTABLE_HASH_MAP.this.values[index]);
}
else {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
int index = IMMUTABLE_HASH_MAP.this.findIndex(entry.getKey());
if(index >= 0) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(IMMUTABLE_HASH_MAP.this.values[index]));
}
}
return false;
}
@ -688,20 +833,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
@Override
public void clear() { throw new UnsupportedOperationException(); }
@Override
public Comparator<MAP.Entry KEY_VALUE_GENERIC_TYPE> comparator() {
return null;
}
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> subSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement, MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> headSet(MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> tailSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { throw new UnsupportedOperationException(); }
}
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE {
#if TYPE_OBJECT
@Override
@Deprecated
@ -749,6 +883,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
return new KeyIterator(fromElement);
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return IMMUTABLE_HASH_MAP.this.size();
@ -783,12 +920,24 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1){
action.accept(count++, keys[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1){
action.accept(keys[index], input);
action.accept(input, keys[index]);
index = (int)links[index];
}
}
@ -799,7 +948,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
if(size() <= 0) return false;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return true;
if(filter.test(keys[index])) return true;
index = (int)links[index];
}
return false;
@ -811,7 +960,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return false;
if(filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
@ -823,35 +972,82 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(!filter.TEST_VALUE(keys[index])) return false;
if(!filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, keys[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = keys[index];
index = (int)links[index];
continue;
}
state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE;
int index = firstIndex;
while(index != -1){
if(filter.TEST_VALUE(keys[index])) return keys[index];
if(filter.test(keys[index])) return keys[index];
index = (int)links[index];
}
return EMPTY_KEY_VALUE;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int index = firstIndex;
int result = 0;
while(index != -1){
if(filter.test(keys[index])) result++;
index = (int)links[index];
}
return result;
}
}
private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE {
@ -897,12 +1093,24 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
@Override
public <E> void forEach(E input, VALUE_BI_OBJECT_CONSUMER VVS_GENERIC_TYPE<E> action) {
public void forEachIndexed(VALUE_BI_FROM_INT_CONSUMER VALUE_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int count = 0;
int index = firstIndex;
while(index != -1){
action.accept(count++, values[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
int index = firstIndex;
while(index != -1){
action.accept(values[index], input);
action.accept(input, values[index]);
index = (int)links[index];
}
}
@ -913,7 +1121,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
if(size() <= 0) return false;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return true;
if(filter.test(values[index])) return true;
index = (int)links[index];
}
return false;
@ -925,7 +1133,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return false;
if(filter.test(values[index])) return false;
index = (int)links[index];
}
return true;
@ -937,23 +1145,82 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
if(size() <= 0) return true;
int index = firstIndex;
while(index != -1){
if(!filter.VALUE_TEST_VALUE(values[index])) return false;
if(!filter.test(values[index])) return false;
index = (int)links[index];
}
return true;
}
#if !VALUE_OBJECT
@Override
public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
VALUE_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <VALUE_SPECIAL_TYPE> VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction<VALUE_SPECIAL_TYPE, VALUE_TYPE, VALUE_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
VALUE_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.apply(state, values[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
empty = false;
state = values[index];
index = (int)links[index];
continue;
}
state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index];
}
return state;
}
@Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE;
int index = firstIndex;
while(index != -1){
if(filter.VALUE_TEST_VALUE(values[index])) return values[index];
if(filter.test(values[index])) return values[index];
index = (int)links[index];
}
return EMPTY_VALUE;
}
@Override
public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
int index = firstIndex;
while(index != -1){
if(filter.test(values[index])) result++;
index = (int)links[index];
}
return result;
}
}
private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {

View File

@ -3,16 +3,28 @@ package speiger.src.collections.PACKAGE.maps.impl.misc;
import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;
#if VALUE_OBJECT
import java.util.Objects;
import java.util.function.Consumer;
#if VALUE_BOOLEAN
import java.util.function.Predicate;
#endif
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
#if !VALUE_OBJECT
import speiger.src.collections.objects.collections.ObjectIterator;
#endif
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER;
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#if !VALUE_OBJECT
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#endif
#if !VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.FUNCTION;
#endif
import speiger.src.collections.objects.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.objects.maps.interfaces.MAP;
import speiger.src.collections.objects.sets.AbstractObjectSet;
@ -28,13 +40,13 @@ import speiger.src.collections.objects.sets.ObjectSet;
public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE
{
/** Enum Type that is being used */
protected final Class<T> keyType;
protected Class<T> keyType;
/** The Backing keys array. */
protected transient final T[] keys;
protected transient T[] keys;
/** The Backing values array */
protected transient final VALUE_TYPE[] values;
protected transient VALUE_TYPE[] values;
/** The Backing array that indicates which index is present or not */
protected transient final long[] present;
protected transient long[] present;
/** Amount of Elements stored in the ArrayMap */
protected int size = 0;
/** EntrySet cache */
@ -44,6 +56,9 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
/** Values cache */
protected transient VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC;
protected ENUM_MAP() {
}
/**
* Default Constructor
* @param keyType the type of Enum that should be used
@ -55,6 +70,86 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
present = new long[((keys.length - 1) >> 6) + 1];
}
#if !VALUE_OBJECT
/**
* Helper constructor that allow to create a EnumMap from boxed values (it will unbox them)
* @param keys the keys that should be put into the EnumMap
* @param values the values that should be put into the EnumMap.
* @throws IllegalStateException if the keys and values do not match in lenght
*/
public ENUM_MAP(T[] keys, CLASS_VALUE_TYPE[] values) {
if(keys.length <= 0) throw new IllegalArgumentException("Empty Array are not allowed");
if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size");
keyType = keys[0].getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(this.keys.length);
present = new long[((this.keys.length - 1) >> 6) + 1];
putAll(keys, values);
}
#endif
/**
* Helper constructor that allow to create a EnumMap from unboxed values
* @param keys the keys that should be put into the map
* @param values the values that should be put into the map.
* @throws IllegalStateException if the keys and values do not match in lenght
*/
public ENUM_MAP(T[] keys, VALUE_TYPE[] values) {
if(keys.length <= 0) throw new IllegalArgumentException("Empty Array are not allowed");
if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size");
keyType = keys[0].getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(this.keys.length);
present = new long[((this.keys.length - 1) >> 6) + 1];
putAll(keys, values);
}
/**
* A Helper constructor that allows to create a EnumMap with exactly the same values as the provided map.
* @param map the values that should be present in the map
*/
public ENUM_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map) {
if(map instanceof ENUM_MAP) {
ENUM_MAP KEY_VALUE_GENERIC_TYPE enumMap = (ENUM_MAP KEY_VALUE_GENERIC_TYPE)map;
keyType = enumMap.keyType;
keys = enumMap.keys;
values = enumMap.values.clone();
present = enumMap.present.clone();
size = enumMap.size;
}
else if(map.isEmpty()) throw new IllegalArgumentException("Empty Maps are not allowed");
else {
keyType = map.keySet().iterator().next().getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(keys.length);
present = new long[((keys.length - 1) >> 6) + 1];
putAll(map);
}
}
/**
* A Type Specific Helper function that allows to create a new EnumMap with exactly the same values as the provided map.
* @param map the values that should be present in the map
*/
public ENUM_MAP(MAP KEY_VALUE_GENERIC_TYPE map) {
if(map instanceof ENUM_MAP) {
ENUM_MAP KEY_VALUE_GENERIC_TYPE enumMap = (ENUM_MAP KEY_VALUE_GENERIC_TYPE)map;
keyType = enumMap.keyType;
keys = enumMap.keys;
values = enumMap.values.clone();
present = enumMap.present.clone();
size = enumMap.size;
}
else if(map.isEmpty()) throw new IllegalArgumentException("Empty Maps are not allowed");
else {
keyType = map.keySet().iterator().next().getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(keys.length);
present = new long[((keys.length - 1) >> 6) + 1];
putAll(map);
}
}
@Override
public VALUE_TYPE put(T key, VALUE_TYPE value) {
int index = key.ordinal();
@ -71,7 +166,14 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
@Override
public VALUE_TYPE putIfAbsent(T key, VALUE_TYPE value) {
int index = key.ordinal();
if(isSet(index)) return values[index];
if(isSet(index)) {
if(VALUE_EQUALS(values[index], getDefaultReturnValue())) {
VALUE_TYPE oldValue = values[index];
values[index] = value;
return oldValue;
}
return values[index];
}
set(index);
values[index] = value;
return getDefaultReturnValue();
@ -91,9 +193,22 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
return getDefaultReturnValue();
}
@Override
public VALUE_TYPE subFrom(T key, VALUE_TYPE value) {
int index = key.ordinal();
if(!isSet(index)) return getDefaultReturnValue();
VALUE_TYPE oldValue = values[index];
values[index] -= value;
if(value < 0 ? (values[index] >= getDefaultReturnValue()) : (values[index] <= getDefaultReturnValue())) {
clear(index);
values[index] = EMPTY_VALUE;
}
return oldValue;
}
#endif
@Override
public boolean containsKey(Object key) {
if(!keyType.isInstance(key)) return false;
return isSet(((T)key).ordinal());
}
@ -101,7 +216,7 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
@Override
public boolean containsValue(Object value) {
for(int i = 0;i<values.length;i++)
if(VALUE_EQUALS(value, values[i])) return true;
if(isSet(i) && VALUE_EQUALS(value, values[i])) return true;
return false;
}
@ -109,13 +224,25 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
@Override
public boolean containsValue(VALUE_TYPE value) {
for(int i = 0;i<values.length;i++)
if(VALUE_EQUALS(value, values[i])) return true;
if(isSet(i) && VALUE_EQUALS(value, values[i])) return true;
return false;
}
#endif
@Override
public CLASS_VALUE_TYPE remove(Object key) {
if(!keyType.isInstance(key)) return getDefaultReturnValue();
int index = ((T)key).ordinal();
if(!isSet(index)) return getDefaultReturnValue();
clear(index);
VALUE_TYPE result = values[index];
values[index] = EMPTY_VALUE;
return result;
}
@Override
public VALUE_TYPE rem(T key) {
if(!keyType.isInstance(key)) return getDefaultReturnValue();
int index = key.ordinal();
if(!isSet(index)) return getDefaultReturnValue();
clear(index);
@ -157,6 +284,7 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
#endif
@Override
public VALUE_TYPE GET_VALUE(T key) {
if(!keyType.isInstance(key)) return getDefaultReturnValue();
int index = key.ordinal();
return isSet(index) ? values[index] : getDefaultReturnValue();
}
@ -164,6 +292,7 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
#if VALUE_OBJECT
@Override
public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) {
if(!keyType.isInstance(key)) return defaultValue;
int index = ((T)key).ordinal();
return isSet(index) ? values[index] : defaultValue;
}
@ -171,11 +300,21 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
#else
@Override
public VALUE_TYPE getOrDefault(T key, VALUE_TYPE defaultValue) {
if(!keyType.isInstance(key)) return defaultValue;
int index = key.ordinal();
return isSet(index) ? values[index] : defaultValue;
}
#endif
@Override
public ENUM_MAP KEY_VALUE_GENERIC_TYPE copy() {
ENUM_MAP KEY_VALUE_GENERIC_TYPE map = new ENUM_MAPKV_BRACES(keyType);
map.size = size;
System.arraycopy(present, 0, map.present, 0, Math.min(present.length, map.present.length));
System.arraycopy(values, 0, map.values, 0, Math.min(values.length, map.values.length));
return map;
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new EntrySet();
@ -194,6 +333,236 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
return valuesC;
}
@Override
public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) {
if(size() <= 0) return;
for(int i = 0,m=keys.length;i<m;i++) {
if(isSet(i)) action.accept(keys[i], values[i]);
}
}
@Override
public boolean replace(T key, VALUE_TYPE oldValue, VALUE_TYPE newValue) {
int index = key.ordinal();
if(!isSet(index) || values[index] != oldValue) return false;
values[index] = newValue;
return true;
}
@Override
public VALUE_TYPE replace(T key, VALUE_TYPE value) {
int index = key.ordinal();
if(!isSet(index)) return getDefaultReturnValue();
VALUE_TYPE oldValue = values[index];
values[index] = value;
return oldValue;
}
@Override
public VALUE_TYPE COMPUTE(T key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
int index = key.ordinal();
if(!isSet(index)) {
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue());
#if VALUE_OBJECT
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
#endif
set(index);
values[index] = newValue;
return newValue;
}
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
#if VALUE_OBJECT
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
clear(index);
values[index] = EMPTY_VALUE;
return newValue;
}
#endif
values[index] = newValue;
return newValue;
}
@Override
public VALUE_TYPE COMPUTE_IF_ABSENT(T key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
int index = key.ordinal();
if(!isSet(index)) {
VALUE_TYPE newValue = mappingFunction.APPLY(key);
#if VALUE_OBJECT
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
#endif
set(index);
values[index] = newValue;
return newValue;
}
VALUE_TYPE newValue = values[index];
#if VALUE_OBJECT
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
newValue = mappingFunction.APPLY(key);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
values[index] = newValue;
}
#endif
return newValue;
}
@Override
public VALUE_TYPE SUPPLY_IF_ABSENT(T key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) {
int index = key.ordinal();
if(!isSet(index)) {
VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET();
#if VALUE_OBJECT
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
#endif
set(index);
values[index] = newValue;
return newValue;
}
VALUE_TYPE newValue = values[index];
#if VALUE_OBJECT
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
newValue = valueProvider.VALUE_SUPPLY_GET();
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
values[index] = newValue;
}
#endif
return newValue;
}
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(T key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
int index = key.ordinal();
#if !VALUE_OBJECT
if(!isSet(index)) return getDefaultReturnValue();
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
#else
if(!isSet(index) || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue();
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
clear(index);
values[index] = EMPTY_VALUE;
return newValue;
}
#endif
values[index] = newValue;
return newValue;
}
#if !VALUE_OBJECT
@Override
public VALUE_TYPE COMPUTENonDefault(T key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
int index = key.ordinal();
if(!isSet(index)) {
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue());
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
set(index);
values[index] = newValue;
return newValue;
}
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
clear(index);
values[index] = EMPTY_VALUE;
return newValue;
}
values[index] = newValue;
return newValue;
}
@Override
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(T key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
int index = key.ordinal();
if(!isSet(index)) {
VALUE_TYPE newValue = mappingFunction.APPLY(key);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
set(index);
values[index] = newValue;
return newValue;
}
VALUE_TYPE newValue = values[index];
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
newValue = mappingFunction.APPLY(key);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
values[index] = newValue;
}
return newValue;
}
@Override
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(T key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) {
int index = key.ordinal();
if(!isSet(index)) {
VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET();
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
set(index);
values[index] = newValue;
return newValue;
}
VALUE_TYPE newValue = values[index];
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
newValue = valueProvider.VALUE_SUPPLY_GET();
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
values[index] = newValue;
}
return newValue;
}
@Override
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(T key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
int index = key.ordinal();
if(!isSet(index) || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue();
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
clear(index);
values[index] = EMPTY_VALUE;
return newValue;
}
values[index] = newValue;
return newValue;
}
#endif
@Override
public VALUE_TYPE MERGE(T key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) {
int index = key.ordinal();
#if VALUE_OBJECT
Objects.requireNonNull(value);
#endif
VALUE_TYPE newValue = !isSet(index) || VALUE_EQUALS(values[index], getDefaultReturnValue()) ? value : mappingFunction.APPLY_VALUE(values[index], value);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
if(isSet(index)) {
clear(index);
values[index] = EMPTY_VALUE;
}
}
else if(!isSet(index)) {
set(index);
values[index] = newValue;
}
else values[index] = newValue;
return newValue;
}
@Override
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : getFastIterable(m)) {
T key = entry.ENTRY_KEY();
int index = key.ordinal();
VALUE_TYPE newValue = !isSet(index) || VALUE_EQUALS(values[index], getDefaultReturnValue()) ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(values[index], entry.ENTRY_VALUE());
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
if(isSet(index)) {
clear(index);
values[index] = EMPTY_VALUE;
}
}
else if(!isSet(index)) {
set(index);
values[index] = newValue;
}
else values[index] = newValue;
}
}
@Override
public void clear() {
if(size == 0) return;
@ -202,6 +571,11 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
Arrays.fill(values, EMPTY_VALUE);
}
@Override
public int size() {
return size;
}
protected void onNodeAdded(int index) {
}
@ -211,16 +585,18 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
}
protected void set(int index) {
present[index >> 6] |= (1L << index);
onNodeAdded(index);
present[index >> 6] |= (1L << index);
size++;
}
protected void clear(int index) {
size--;
present[index >> 6] &= ~(1L << index);
onNodeRemoved(index);
}
protected boolean isSet(int index) { return (present[index >> 6] & (1L << index)) != 0; }
private static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) {
protected static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) {
return keyType.getEnumConstants();
}
@ -228,7 +604,20 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
@Override
public boolean contains(Object o) {
if(o instanceof Map.Entry) return containsKey(((Map.Entry<?, ?>)o).getKey());
if(o instanceof Map.Entry) {
if(o instanceof MAP.Entry) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o;
if(!keyType.isInstance(entry.ENTRY_KEY())) return false;
int index = ((T)entry.ENTRY_KEY()).ordinal();
if(index >= 0 && ENUM_MAP.this.isSet(index)) return VALUE_EQUALS(entry.ENTRY_VALUE(), ENUM_MAP.this.values[index]);
}
else {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
if(!keyType.isInstance(entry.getKey())) return false;
int index = ((T)entry.getKey()).ordinal();
if(index >= 0 && ENUM_MAP.this.isSet(index)) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(ENUM_MAP.this.values[index]));
}
}
return false;
}
@ -245,6 +634,14 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
return false;
}
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
if(size() <= 0) return;
for(int i = 0,m=keys.length;i<m;i++) {
if(isSet(i)) action.accept(new ValueMapEntry(i));
}
}
@Override
public ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iterator() {
return new EntryIterator();
@ -324,8 +721,7 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
class EntryIterator extends MapIterator implements ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
int index = nextEntry();
return new BasicEntry<>(keys[index], values[index]);
return new ValueMapEntry(nextEntry());
}
}
@ -360,13 +756,105 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
public int nextEntry() {
if(!hasNext()) throw new NoSuchElementException();
lastReturnValue = nextIndex;
return nextIndex;
nextIndex = -1;
return lastReturnValue;
}
public void remove() {
if(lastReturnValue == -1) throw new IllegalStateException();
clear(lastReturnValue);
values[lastReturnValue] = EMPTY_VALUE;
lastReturnValue = -1;
}
}
protected class ValueMapEntry extends MapEntry {
protected KEY_TYPE key;
protected VALUE_TYPE value;
public ValueMapEntry(int index) {
super(index);
key = keys[index];
value = values[index];
}
@Override
public KEY_TYPE ENTRY_KEY() {
return key;
}
@Override
public VALUE_TYPE ENTRY_VALUE() {
return value;
}
@Override
public VALUE_TYPE setValue(VALUE_TYPE value) {
this.value = value;
return super.setValue(value);
}
}
protected class MapEntry implements MAP.Entry KEY_VALUE_GENERIC_TYPE, Map.Entry<CLASS_TYPE, CLASS_VALUE_TYPE> {
public int index = -1;
public MapEntry() {}
public MapEntry(int index) {
this.index = index;
}
void set(int index) {
this.index = index;
}
@Override
public KEY_TYPE ENTRY_KEY() {
return keys[index];
}
@Override
public VALUE_TYPE ENTRY_VALUE() {
return values[index];
}
@Override
public VALUE_TYPE setValue(VALUE_TYPE value) {
VALUE_TYPE oldValue = values[index];
values[index] = value;
return oldValue;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Map.Entry) {
if(obj instanceof MAP.Entry) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)obj;
return KEY_EQUALS(ENTRY_KEY(), entry.ENTRY_KEY()) && VALUE_EQUALS(ENTRY_VALUE(), entry.ENTRY_VALUE());
}
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
Object key = entry.getKey();
Object value = entry.getValue();
#if TYPE_OBJECT && VALUE_OBJECT
return KEY_EQUALS(ENTRY_KEY(), key) && VALUE_EQUALS(ENTRY_VALUE(), value);
#else if TYPE_OBJECT
return value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(ENTRY_KEY(), key) && VALUE_EQUALS(ENTRY_VALUE(), CLASS_TO_VALUE(value));
#else if VALUE_OBJECT
return key instanceof CLASS_TYPE && KEY_EQUALS(ENTRY_KEY(), CLASS_TO_KEY(key)) && VALUE_EQUALS(ENTRY_VALUE(), value);
#else
return key instanceof CLASS_TYPE && value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(ENTRY_KEY(), CLASS_TO_KEY(key)) && VALUE_EQUALS(ENTRY_VALUE(), CLASS_TO_VALUE(value));
#endif
}
return false;
}
@Override
public int hashCode() {
return KEY_TO_HASH(ENTRY_KEY()) ^ VALUE_TO_HASH(ENTRY_VALUE());
}
@Override
public String toString() {
return KEY_TO_STRING(ENTRY_KEY()) + "=" + VALUE_TO_STRING(ENTRY_VALUE());
}
}
}

View File

@ -1,6 +1,5 @@
package speiger.src.collections.PACKAGE.maps.impl.misc;
import java.util.Comparator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
@ -10,16 +9,14 @@ import java.util.Objects;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
@ -31,8 +28,6 @@ import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSortedSet;
import speiger.src.collections.objects.sets.ObjectSet;
#endif
/**
@ -42,7 +37,7 @@ import speiger.src.collections.objects.sets.ObjectSet;
* @Type(T)
* @ValueType(V)
*/
public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE
public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VALUE_GENERIC_TYPE implements ORDERED_MAP KEY_VALUE_GENERIC_TYPE
{
/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
protected long[] links;
@ -59,6 +54,136 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
links = new long[keys.length];
}
#if !VALUE_OBJECT
/**
* Helper constructor that allow to create a EnumMap from boxed values (it will unbox them)
* @param keys the keys that should be put into the EnumMap
* @param values the values that should be put into the EnumMap.
* @throws IllegalStateException if the keys and values do not match in lenght
*/
public LINKED_ENUM_MAP(T[] keys, CLASS_VALUE_TYPE[] values) {
if(keys.length <= 0) throw new IllegalArgumentException("Empty Array are not allowed");
if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size");
keyType = keys[0].getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(this.keys.length);
present = new long[((this.keys.length - 1) >> 6) + 1];
links = new long[this.keys.length];
putAll(keys, values);
}
#endif
/**
* Helper constructor that allow to create a EnumMap from unboxed values
* @param keys the keys that should be put into the map
* @param values the values that should be put into the map.
* @throws IllegalStateException if the keys and values do not match in lenght
*/
public LINKED_ENUM_MAP(T[] keys, VALUE_TYPE[] values) {
if(keys.length <= 0) throw new IllegalArgumentException("Empty Array are not allowed");
if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size");
keyType = keys[0].getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(this.keys.length);
present = new long[((this.keys.length - 1) >> 6) + 1];
links = new long[this.keys.length];
putAll(keys, values);
}
/**
* A Helper constructor that allows to create a EnumMap with exactly the same values as the provided map.
* @param map the values that should be present in the map
*/
public LINKED_ENUM_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map) {
if(map instanceof LINKED_ENUM_MAP) {
LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE enumMap = (LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE)map;
keyType = enumMap.keyType;
keys = enumMap.keys;
values = enumMap.values.clone();
present = enumMap.present.clone();
links = enumMap.links.clone();
size = enumMap.size;
}
else if(map instanceof ENUM_MAP) {
ENUM_MAP KEY_VALUE_GENERIC_TYPE enumMap = (ENUM_MAP KEY_VALUE_GENERIC_TYPE)map;
keyType = enumMap.keyType;
keys = enumMap.keys;
values = enumMap.values.clone();
present = enumMap.present.clone();
links = new long[keys.length];
for(int i = 0,m=keys.length;i<m;i++) {
if(isSet(i)) {
if(size == 0) {
firstIndex = lastIndex = i;
links[i] = -1L;
}
else {
links[lastIndex] ^= ((links[lastIndex] ^ (i & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[i] = ((lastIndex & 0xFFFFFFFFL) << 32) | 0xFFFFFFFFL;
lastIndex = i;
}
size++;
}
}
}
else if(map.isEmpty()) throw new IllegalArgumentException("Empty Maps are not allowed");
else {
keyType = map.keySet().iterator().next().getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(keys.length);
present = new long[((keys.length - 1) >> 6) + 1];
links = new long[keys.length];
putAll(map);
}
}
/**
* A Type Specific Helper function that allows to create a new EnumMap with exactly the same values as the provided map.
* @param map the values that should be present in the map
*/
public LINKED_ENUM_MAP(MAP KEY_VALUE_GENERIC_TYPE map) {
if(map instanceof LINKED_ENUM_MAP) {
LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE enumMap = (LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE)map;
keyType = enumMap.keyType;
keys = enumMap.keys;
values = enumMap.values.clone();
present = enumMap.present.clone();
links = enumMap.links.clone();
size = enumMap.size;
}
else if(map instanceof ENUM_MAP) {
ENUM_MAP KEY_VALUE_GENERIC_TYPE enumMap = (ENUM_MAP KEY_VALUE_GENERIC_TYPE)map;
keyType = enumMap.keyType;
keys = enumMap.keys;
values = enumMap.values.clone();
present = enumMap.present.clone();
links = new long[keys.length];
for(int i = 0,m=keys.length;i<m;i++) {
if(isSet(i)) {
if(size == 0) {
firstIndex = lastIndex = i;
links[i] = -1L;
}
else {
links[lastIndex] ^= ((links[lastIndex] ^ (i & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[i] = ((lastIndex & 0xFFFFFFFFL) << 32) | 0xFFFFFFFFL;
lastIndex = i;
}
size++;
}
}
}
else if(map.isEmpty()) throw new IllegalArgumentException("Empty Maps are not allowed");
else {
keyType = map.keySet().iterator().next().getDeclaringClass();
this.keys = getKeyUniverse(keyType);
this.values = NEW_VALUE_ARRAY(keys.length);
present = new long[((keys.length - 1) >> 6) + 1];
links = new long[keys.length];
putAll(map);
}
}
@Override
public VALUE_TYPE putAndMoveToFirst(T key, VALUE_TYPE value) {
int index = key.ordinal();
@ -114,7 +239,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
@Override
public VALUE_TYPE getAndMoveToFirst(T key) {
int index = key.ordinal();
if(index < 0) return getDefaultReturnValue();
if(!isSet(index)) return getDefaultReturnValue();
moveToFirstIndex(index);
return values[index];
}
@ -122,25 +247,23 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
@Override
public VALUE_TYPE getAndMoveToLast(T key) {
int index = key.ordinal();
if(index < 0) return getDefaultReturnValue();
if(!isSet(index)) return getDefaultReturnValue();
moveToLastIndex(index);
return values[index];
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
public LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE copy() {
LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE map = new LINKED_ENUM_MAPKV_BRACES(keyType);
map.size = size;
System.arraycopy(present, 0, map.present, 0, Math.min(present.length, map.present.length));
System.arraycopy(values, 0, map.values, 0, Math.min(values.length, map.values.length));
System.arraycopy(links, 0, map.links, 0, Math.min(links.length, map.links.length));
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(T fromKey, T toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(T toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(T fromKey) { throw new UnsupportedOperationException(); }
@Override
public T FIRST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
@ -190,15 +313,15 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
return (ORDERED_SET KEY_GENERIC_TYPE)keySet;
}
@Override
@ -291,7 +414,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
}
}
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE {
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
@ -351,21 +474,23 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
return new FastEntryIterator(fromElement);
}
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
while(index != -1){
action.accept(new BasicEntryKV_BRACES(keys[index], values[index]));
action.accept(new ValueMapEntry(index));
index = (int)links[index];
}
}
@Override
public void fastForEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
MapEntry entry = new MapEntry();
int index = firstIndex;
while(index != -1){
entry.set(keys[index], values[index]);
entry.set(index);
action.accept(entry);
index = (int)links[index];
}
@ -375,8 +500,18 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
@Deprecated
public boolean contains(Object o) {
if(o instanceof Map.Entry) {
if(o instanceof MAP.Entry) return LINKED_ENUM_MAP.this.containsKey(((MAP.Entry KEY_VALUE_GENERIC_TYPE)o).ENTRY_KEY());
return LINKED_ENUM_MAP.this.containsKey(((Map.Entry<?, ?>)o).getKey());
if(o instanceof MAP.Entry) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o;
if(!keyType.isInstance(entry.ENTRY_KEY())) return false;
int index = ((T)entry.ENTRY_KEY()).ordinal();
if(index >= 0 && LINKED_ENUM_MAP.this.isSet(index)) return VALUE_EQUALS(entry.ENTRY_VALUE(), LINKED_ENUM_MAP.this.values[index]);
}
else {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
if(!keyType.isInstance(entry.getKey())) return false;
int index = ((T)entry.getKey()).ordinal();
if(index >= 0 && LINKED_ENUM_MAP.this.isSet(index)) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(LINKED_ENUM_MAP.this.values[index]));
}
}
return false;
}
@ -404,20 +539,9 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
public void clear() {
LINKED_ENUM_MAP.this.clear();
}
@Override
public Comparator<MAP.Entry KEY_VALUE_GENERIC_TYPE> comparator() {
return null;
}
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> subSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement, MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> headSet(MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> tailSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { throw new UnsupportedOperationException(); }
}
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE {
#if TYPE_OBJECT
@Override
@Deprecated
@ -428,7 +552,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
@Override
public boolean remove(Object o) {
int oldSize = size;
remove(o);
LINKED_ENUM_MAP.this.remove(o);
return size != oldSize;
}
@ -441,7 +565,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
@Override
public boolean remove(T o) {
int oldSize = size;
remove(o);
LINKED_ENUM_MAP.this.remove(o);
return size != oldSize;
}
@ -477,6 +601,8 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
return new KeyIterator(fromElement);
}
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return LINKED_ENUM_MAP.this.size();
@ -528,17 +654,6 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
}
#endif
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(T fromElement, T toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(T toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(T fromElement) { throw new UnsupportedOperationException(); }
}
private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE {
@ -634,12 +749,12 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
return entry = new MapEntry(nextEntry());
return entry = new ValueMapEntry(nextEntry());
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
return entry = new MapEntry(previousEntry());
return entry = new ValueMapEntry(previousEntry());
}
@Override
@ -713,6 +828,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
previous = from.ordinal() - 1;
index = from.ordinal();
next = from.ordinal();
if(!isSet(index)) throw new NoSuchElementException();
}
public boolean hasNext() {
@ -741,6 +857,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
previous = (int)(links[current] >>> 32);
}
else next = (int)links[current];
size--;
if(previous == -1) firstIndex = next;
else links[previous] ^= ((links[previous] ^ (next & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
@ -748,6 +865,8 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
if (next == -1) lastIndex = previous;
else links[next] ^= ((links[next] ^ ((previous & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
values[current] = EMPTY_VALUE;
present[current >> 6] &= ~(1L << current);
current = -1;
}
public int previousEntry() {
@ -784,6 +903,33 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
}
}
protected class ValueMapEntry extends MapEntry {
protected KEY_TYPE key;
protected VALUE_TYPE value;
public ValueMapEntry(int index) {
super(index);
key = keys[index];
value = values[index];
}
@Override
public KEY_TYPE ENTRY_KEY() {
return key;
}
@Override
public VALUE_TYPE ENTRY_VALUE() {
return value;
}
@Override
public VALUE_TYPE setValue(VALUE_TYPE value) {
this.value = value;
return super.setValue(value);
}
}
protected class MapEntry implements MAP.Entry KEY_VALUE_GENERIC_TYPE, Map.Entry<CLASS_TYPE, CLASS_VALUE_TYPE> {
public int index = -1;
@ -792,6 +938,10 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
this.index = index;
}
void set(int index) {
this.index = index;
}
@Override
public KEY_TYPE ENTRY_KEY() {
return keys[index];
@ -814,19 +964,19 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
if(obj instanceof Map.Entry) {
if(obj instanceof MAP.Entry) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)obj;
return KEY_EQUALS(keys[index], entry.ENTRY_KEY()) && VALUE_EQUALS(values[index], entry.ENTRY_VALUE());
return KEY_EQUALS(ENTRY_KEY(), entry.ENTRY_KEY()) && VALUE_EQUALS(ENTRY_VALUE(), entry.ENTRY_VALUE());
}
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
Object key = entry.getKey();
Object value = entry.getValue();
#if TYPE_OBJECT && VALUE_OBJECT
return KEY_EQUALS(keys[index], key) && VALUE_EQUALS(values[index], value);
return KEY_EQUALS(ENTRY_KEY(), key) && VALUE_EQUALS(ENTRY_VALUE(), value);
#else if TYPE_OBJECT
return value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(keys[index], key) && VALUE_EQUALS(values[index], CLASS_TO_VALUE(value));
return value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(ENTRY_KEY(), key) && VALUE_EQUALS(ENTRY_VALUE(), CLASS_TO_VALUE(value));
#else if VALUE_OBJECT
return key instanceof CLASS_TYPE && KEY_EQUALS(keys[index], CLASS_TO_KEY(key)) && VALUE_EQUALS(values[index], value);
return key instanceof CLASS_TYPE && KEY_EQUALS(ENTRY_KEY(), CLASS_TO_KEY(key)) && VALUE_EQUALS(ENTRY_VALUE(), value);
#else
return key instanceof CLASS_TYPE && value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(keys[index], CLASS_TO_KEY(key)) && VALUE_EQUALS(values[index], CLASS_TO_VALUE(value));
return key instanceof CLASS_TYPE && value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(ENTRY_KEY(), CLASS_TO_KEY(key)) && VALUE_EQUALS(ENTRY_VALUE(), CLASS_TO_VALUE(value));
#endif
}
return false;
@ -834,12 +984,12 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
@Override
public int hashCode() {
return KEY_TO_HASH(keys[index]) ^ VALUE_TO_HASH(values[index]);
return KEY_TO_HASH(ENTRY_KEY()) ^ VALUE_TO_HASH(ENTRY_VALUE());
}
@Override
public String toString() {
return KEY_TO_STRING(keys[index]) + "->" + VALUE_TO_STRING(values[index]);
return KEY_TO_STRING(ENTRY_KEY()) + "=" + VALUE_TO_STRING(ENTRY_VALUE());
}
}
}

View File

@ -0,0 +1,95 @@
package speiger.src.collections.PACKAGE.maps.interfaces;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* A type specific ConcurrentMap interface that reduces boxing/unboxing.
* Since the interface adds nothing new. It is there just for completion sake.
* @Type(T)
* @ValueType(V)
*/
public interface CONCURRENT_MAP KEY_VALUE_GENERIC_TYPE extends ConcurrentMap<CLASS_TYPE, CLASS_VALUE_TYPE>, MAP KEY_VALUE_GENERIC_TYPE
{
@Override
@Primitive
public default CLASS_VALUE_TYPE compute(CLASS_TYPE key, BiFunction<? super CLASS_TYPE, ? super CLASS_VALUE_TYPE, ? extends CLASS_VALUE_TYPE> mappingFunction) {
return MAP.super.compute(key, mappingFunction);
}
@Override
@Primitive
public default CLASS_VALUE_TYPE computeIfAbsent(CLASS_TYPE key, Function<? super CLASS_TYPE, ? extends CLASS_VALUE_TYPE> mappingFunction) {
return MAP.super.computeIfAbsent(key, mappingFunction);
}
@Override
@Primitive
public default CLASS_VALUE_TYPE computeIfPresent(CLASS_TYPE key, BiFunction<? super CLASS_TYPE, ? super CLASS_VALUE_TYPE, ? extends CLASS_VALUE_TYPE> mappingFunction) {
return MAP.super.computeIfPresent(key, mappingFunction);
}
@Override
@Primitive
public default void forEach(BiConsumer<? super CLASS_TYPE, ? super CLASS_VALUE_TYPE> action) {
MAP.super.forEach(action);
}
@Override
@Primitive
public default CLASS_VALUE_TYPE merge(CLASS_TYPE key, CLASS_VALUE_TYPE value, BiFunction<? super CLASS_VALUE_TYPE, ? super CLASS_VALUE_TYPE, ? extends CLASS_VALUE_TYPE> mappingFunction) {
return MAP.super.merge(key, value, mappingFunction);
}
#if TYPE_OBJECT && VALUE_OBJECT
@Override
public CLASS_VALUE_TYPE getOrDefault(Object key, CLASS_VALUE_TYPE defaultValue);
@Override
public CLASS_VALUE_TYPE putIfAbsent(CLASS_TYPE key, CLASS_VALUE_TYPE value);
@Override
public boolean remove(Object key, Object value);
@Override
public boolean replace(CLASS_TYPE key, CLASS_VALUE_TYPE oldValue, CLASS_VALUE_TYPE newValue);
@Override
public CLASS_VALUE_TYPE replace(CLASS_TYPE key, CLASS_VALUE_TYPE value);
#else
@Primitive
@Override
public default CLASS_VALUE_TYPE getOrDefault(Object key, CLASS_VALUE_TYPE defaultValue) {
return MAP.super.getOrDefault(key, defaultValue);
}
@Override
@Primitive
public default CLASS_VALUE_TYPE putIfAbsent(CLASS_TYPE key, CLASS_VALUE_TYPE value) {
return MAP.super.putIfAbsent(key, value);
}
@Override
@Deprecated
public default boolean remove(Object key, Object value) {
return MAP.super.remove(key, value);
}
@Override
@Deprecated
public default boolean replace(CLASS_TYPE key, CLASS_VALUE_TYPE oldValue, CLASS_VALUE_TYPE newValue) {
return MAP.super.replace(key, oldValue, newValue);
}
@Override
@Deprecated
public default CLASS_VALUE_TYPE replace(CLASS_TYPE key, CLASS_VALUE_TYPE value) {
return MAP.super.replace(key, value);
}
#endif
@Override
@Deprecated
public default void replaceAll(BiFunction<? super CLASS_TYPE, ? super CLASS_VALUE_TYPE, ? extends CLASS_VALUE_TYPE> mappingFunction) {
MAP.super.replaceAll(mappingFunction);
}
}

View File

@ -2,6 +2,9 @@ package speiger.src.collections.PACKAGE.maps.interfaces;
import java.util.NavigableMap;
import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET;
#if MAPS_FEATURE
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
#endif
/**
* A Type Specific Navigable Map interface with a couple helper methods
@ -10,6 +13,8 @@ import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET;
*/
public interface NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE extends SORTED_MAP KEY_VALUE_GENERIC_TYPE, NavigableMap<CLASS_TYPE, CLASS_VALUE_TYPE>
{
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy();
/** @return a Type Specific desendingMap */
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap();
@ -31,7 +36,34 @@ public interface NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE extends SORTED_MAP KEY_VAL
/** @return a Type Specific pollLastEntry */
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry();
/** @return a Type Specific Navigable Key Set */
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE keySet();
#if MAPS_FEATURE
/**
* Creates a Wrapped NavigableMap that is Synchronized
* @return a new NavigableMap that is synchronized
* @see MAPS#synchronize
*/
public default NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE synchronize() { return MAPS.synchronize(this); }
/**
* Creates a Wrapped NavigableMap that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new NavigableMap Wrapper that is synchronized
* @see MAPS#synchronize
*/
public default NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE synchronize(Object mutex) { return MAPS.synchronize(this, mutex); }
/**
* Creates a Wrapped NavigableMap that is unmodifiable
* @return a new NavigableMap Wrapper that is unmodifiable
* @see MAPS#unmodifiable
*/
public default NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE unmodifiable() { return MAPS.unmodifiable(this); }
#endif
#if !TYPE_OBJECT
/**
* A Type Specific SubMap method to reduce boxing/unboxing
@ -182,13 +214,13 @@ public interface NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE extends SORTED_MAP KEY_VAL
default NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { return tailMap(OBJ_TO_KEY(fromKey), true); }
#else
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(CLASS_TYPE key) { return lowerEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(CLASS_TYPE key);
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(CLASS_TYPE key) { return floorEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(CLASS_TYPE key);
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(CLASS_TYPE key) { return ceilingEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(CLASS_TYPE key);
@Override
default MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(CLASS_TYPE key) { return higherEntry(OBJ_TO_KEY(key)); }
MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(CLASS_TYPE key);
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, boolean fromInclusive, CLASS_TYPE toKey, boolean toInclusive);

View File

@ -0,0 +1,149 @@
package speiger.src.collections.PACKAGE.maps.interfaces;
#if MAPS_FEATURE
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
#endif
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
/**
* A Special Map Interface giving Access to some really usefull functions
* The Idea behind this interface is to allow access to functions that give control to the Order of elements.
* Since Linked implementations as examples can be reordered outside of the Insertion Order.
* This interface provides basic access to such functions while also providing some Sorted/NaivgableMap implementations that still fit into here.
*
* @Type(T)
* @ValueType(V)
*/
public interface ORDERED_MAP KEY_VALUE_GENERIC_TYPE extends MAP KEY_VALUE_GENERIC_TYPE
{
/**
* A customized put method that allows you to insert into the first index.
* @param key the key that should be inserted
* @param value the value that should be inserted
* @return the previous present or default return value
* @see java.util.Map#put(Object, Object)
*/
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value);
/**
* A customized put method that allows you to insert into the last index. (This may be nessesary depending on the implementation)
* @param key the key that should be inserted
* @param value the value that should be inserted
* @return the previous present or default return value
* @see java.util.Map#put(Object, Object)
*/
public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value);
/**
* A specific move method to move a given key/value to the first index.
* @param key that should be moved to the first index
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
*/
public boolean moveToFirst(KEY_TYPE key);
/**
* A specific move method to move a given key/value to the last index.
* @param key that should be moved to the first last
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
*/
public boolean moveToLast(KEY_TYPE key);
/**
* A Specific get method that allows to move teh given key/value int the first index.
* @param key that is searched for
* @return the given value for the requested key or default return value
*/
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key);
/**
* A Specific get method that allows to move teh given key/value int the last index.
* @param key that is searched for
* @return the given value for the requested key or default return value
*/
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key);
/**
* A method to get the first Key of a Map.
* @return the first key in the map
*/
public KEY_TYPE FIRST_ENTRY_KEY();
/**
* A method to get and remove the first Key of a Map.
* @return the first key in the map
*/
public KEY_TYPE POLL_FIRST_ENTRY_KEY();
/**
* A method to get the last Key of a Map.
* @return the last key in the map
*/
public KEY_TYPE LAST_ENTRY_KEY();
/**
* A method to get and remove the last Key of a Map.
* @return the last key in the map
*/
public KEY_TYPE POLL_LAST_ENTRY_KEY();
/**
* A method to get the first Value of a Map.
* @return the first key in the map
*/
public VALUE_TYPE FIRST_ENTRY_VALUE();
/**
* A method to get the last Value of a Map.
* @return the last key in the map
*/
public VALUE_TYPE LAST_ENTRY_VALUE();
@Override
public ORDERED_MAP KEY_VALUE_GENERIC_TYPE copy();
@Override
public ORDERED_SET KEY_GENERIC_TYPE keySet();
@Override
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET();
#if MAPS_FEATURE
/**
* Creates a Wrapped SortedMap that is Synchronized
* @return a new SortedMap that is synchronized
* @see MAPS#synchronize
*/
@Override
public default ORDERED_MAP KEY_VALUE_GENERIC_TYPE synchronize() { return MAPS.synchronize(this); }
/**
* Creates a Wrapped SortedMap that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new SortedMap Wrapper that is synchronized
* @see MAPS#synchronize
*/
@Override
public default ORDERED_MAP KEY_VALUE_GENERIC_TYPE synchronize(Object mutex) { return MAPS.synchronize(this, mutex); }
/**
* Creates a Wrapped SortedMap that is unmodifiable
* @return a new SortedMap Wrapper that is unmodifiable
* @see MAPS#unmodifiable
*/
@Override
public default ORDERED_MAP KEY_VALUE_GENERIC_TYPE unmodifiable() { return MAPS.unmodifiable(this); }
#endif
/**
* Fast Ordered Entry Set that allows for a faster Entry Iterator by recycling the Entry Object and just exchanging 1 internal value
* @Type(T)
* @ValueType(V)
*/
interface FastOrderedSet KEY_VALUE_GENERIC_TYPE extends MAP.FastEntrySet KEY_VALUE_GENERIC_TYPE, ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
@Override
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> fastIterator();
/**
* Fast iterator that recycles the given Entry object to improve speed and reduce object allocation
* @param fromElement that is going to be started from.
* @return a improved iterator that starts from the desired element
*/
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> fastIterator(KEY_TYPE fromElement);
}
}

View File

@ -9,8 +9,13 @@ import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
#if MAPS_FEATURE
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.objects.sets.ObjectSortedSet;
#endif
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
/**
@ -18,68 +23,46 @@ import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
*
* @Type(T)
* @ValueType(V)
* @note ORDERED_MAP is only extended until 0.6.0 for Compat reasons.
* The supported classes already implement ORDERED_MAP directly and will remove SORTED_MAP implementations in favor of ORDERED_MAP instead
*/
public interface SORTED_MAP KEY_VALUE_GENERIC_TYPE extends SortedMap<CLASS_TYPE, CLASS_VALUE_TYPE>, MAP KEY_VALUE_GENERIC_TYPE
{
/**
* A customized put method that allows you to insert into the first index.
* @param key the key that should be inserted
* @param value the value that should be inserted
* @return the previous present or default return value
* @see java.util.Map#put(Object, Object)
* @note some implementations do not support this method
*/
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value);
/**
* A customized put method that allows you to insert into the last index. (This may be nessesary depending on the implementation)
* @param key the key that should be inserted
* @param value the value that should be inserted
* @return the previous present or default return value
* @see java.util.Map#put(Object, Object)
* @note some implementations do not support this method
*/
public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value);
/**
* A specific move method to move a given key/value to the first index.
* @param key that should be moved to the first index
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
* @note some implementations do not support this method
*/
public boolean moveToFirst(KEY_TYPE key);
/**
* A specific move method to move a given key/value to the last index.
* @param key that should be moved to the first last
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
* @note some implementations do not support this method
*/
public boolean moveToLast(KEY_TYPE key);
/**
* A Specific get method that allows to move teh given key/value int the first index.
* @param key that is searched for
* @return the given value for the requested key or default return value
* @note some implementations do not support this method
*/
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key);
/**
* A Specific get method that allows to move teh given key/value int the last index.
* @param key that is searched for
* @return the given value for the requested key or default return value
* @note some implementations do not support this method
*/
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key);
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator();
@Override
public SET KEY_GENERIC_TYPE keySet();
public SORTED_MAP KEY_VALUE_GENERIC_TYPE copy();
@Override
public SORTED_SET KEY_GENERIC_TYPE keySet();
@Override
public VALUE_COLLECTION VALUE_GENERIC_TYPE values();
#if MAPS_FEATURE
/**
* Creates a Wrapped SortedMap that is Synchronized
* @return a new SortedMap that is synchronized
* @see MAPS#synchronize
*/
public default SORTED_MAP KEY_VALUE_GENERIC_TYPE synchronize() { return MAPS.synchronize(this); }
/**
* Creates a Wrapped SortedMap that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new SortedMap Wrapper that is synchronized
* @see MAPS#synchronize
*/
public default SORTED_MAP KEY_VALUE_GENERIC_TYPE synchronize(Object mutex) { return MAPS.synchronize(this, mutex); }
/**
* Creates a Wrapped SortedMap that is unmodifiable
* @return a new SortedMap Wrapper that is unmodifiable
* @see MAPS#unmodifiable
*/
public default SORTED_MAP KEY_VALUE_GENERIC_TYPE unmodifiable() { return MAPS.unmodifiable(this); }
#endif
#if !TYPE_OBJECT
/**
* A Type Specific SubMap method to reduce boxing/unboxing
@ -178,11 +161,11 @@ public interface SORTED_MAP KEY_VALUE_GENERIC_TYPE extends SortedMap<CLASS_TYPE,
public VALUE_TYPE LAST_ENTRY_VALUE();
@Override
public default SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey) { return subMap(OBJ_TO_KEY(fromKey), OBJ_TO_KEY(toKey)); }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey);
@Override
public default SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey) { return headMap(OBJ_TO_KEY(toKey)); }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey);
@Override
public default SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { return tailMap(OBJ_TO_KEY(fromKey)); }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey);
#endif
/**
@ -193,6 +176,11 @@ public interface SORTED_MAP KEY_VALUE_GENERIC_TYPE extends SortedMap<CLASS_TYPE,
interface FastSortedSet KEY_VALUE_GENERIC_TYPE extends MAP.FastEntrySet KEY_VALUE_GENERIC_TYPE, ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
@Override
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> fastIterator();
/**
* Fast iterator that recycles the given Entry object to improve speed and reduce object allocation
* @param fromElement that is going to be started from.
* @return a improved iterator that starts from the desired element
*/
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> fastIterator(KEY_TYPE fromElement);
}
}

View File

@ -1,7 +1,12 @@
package speiger.src.collections.PACKAGE.misc.pairs;
#if IMMUTABLE_PAIR
import speiger.src.collections.PACKAGE.misc.pairs.impl.IMMUTABLE_PAIR;
#endif
#if MUTABLE_PAIR
import speiger.src.collections.PACKAGE.misc.pairs.impl.MUTABLE_PAIR;
#endif
/**
* Key Value Pair Interface that allows to reduce boxing/unboxing.
* @Type(T)
@ -9,6 +14,7 @@ import speiger.src.collections.PACKAGE.misc.pairs.impl.MUTABLE_PAIR;
*/
public interface PAIR KEY_VALUE_GENERIC_TYPE
{
#if IMMUTABLE_PAIR
/**
* Empty Reference for Immutable Pairs
*/
@ -68,6 +74,8 @@ public interface PAIR KEY_VALUE_GENERIC_TYPE
return new IMMUTABLE_PAIRKV_BRACES(pair.ENTRY_KEY(), pair.ENTRY_VALUE());
}
#endif
#if MUTABLE_PAIR
/**
* @Type(T)
* @ValueType(V)
@ -118,6 +126,7 @@ public interface PAIR KEY_VALUE_GENERIC_TYPE
return new MUTABLE_PAIRKV_BRACES(pair.ENTRY_KEY(), pair.ENTRY_VALUE());
}
#endif
/**
* Sets the Key of the Pair.
* @param key the key that should be set.

View File

@ -0,0 +1,46 @@
package speiger.src.collections.PACKAGE.queues;
#if TYPE_OBJECT
import java.util.Objects;
#endif
import java.util.StringJoiner;
/**
* Helper class that implements all the essential methods for the PriorityQueues
* @Type(T)
*/
public abstract class ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
{
@Override
public boolean equals(Object obj) {
if(obj instanceof PRIORITY_QUEUE) {
PRIORITY_QUEUE KEY_GENERIC_TYPE queue = (PRIORITY_QUEUE KEY_GENERIC_TYPE)obj;
if(queue.size() != size()) return false;
for(int i = 0,m=size();i<m;i++) {
if(KEY_EQUALS_NOT(queue.peek(i), peek(i))) return false;
}
return true;
}
return false;
}
@Override
public int hashCode() {
int result = 1;
for (int i = 0,m=size();i<m;i++) {
result = 31 * result + KEY_TO_HASH(peek(i));
}
return result;
}
@Override
public String toString()
{
if(isEmpty()) return "[]";
StringJoiner joiner = new StringJoiner(", ", "[", "]");
for (int i = 0,m=size();i<m;i++) {
joiner.add(KEY_TO_STRING(peek(i)));
}
return joiner.toString();
}
}

View File

@ -1,20 +1,28 @@
package speiger.src.collections.PACKAGE.queues;
#if TYPE_OBJECT
import java.util.Arrays;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
import java.util.Objects;
import java.util.NoSuchElementException;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.utils.ITrimmable;
/**
@ -22,7 +30,7 @@ import speiger.src.collections.utils.ITrimmable;
* Its specific implementation uses a backing array that grows and shrinks as it is needed.
* @Type(T)
*/
public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, ITrimmable
public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, ITrimmable
{
/** Max Possible ArraySize without the JVM Crashing */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
@ -34,6 +42,8 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
protected int first;
/** The Last Index pointer */
protected int last;
/** The Minimum Capacity of the Queue **/
protected int minCapacity;
/**
* Constructor using a initial array
@ -62,7 +72,9 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
*/
public ARRAY_FIFO_QUEUE(KEY_TYPE[] values, int offset, int size) {
if (values.length < size) throw new IllegalArgumentException("Initial array (" + values.length + ") is smaller then the expected size (" + size + ")");
if(values.length <= 0) values = NEW_KEY_ARRAY(1);
if(values.length <= 0) values = NEW_KEY_ARRAY(MIN_CAPACITY);
else if(values.length < MIN_CAPACITY) values = Arrays.copyOf(values, MIN_CAPACITY);
minCapacity = MIN_CAPACITY;
array = values;
first = offset;
last = (offset + size) % array.length;
@ -76,7 +88,8 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
*/
public ARRAY_FIFO_QUEUE(int capacity) {
if (capacity < 0) throw new IllegalArgumentException("Initial capacity (" + capacity + ") is negative");
array = NEW_KEY_ARRAY(Math.max(1, capacity));
array = NEW_KEY_ARRAY(Math.max(MIN_CAPACITY, capacity+1));
minCapacity = array.length;
}
/**
@ -99,16 +112,21 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
@Override
public void clear() {
if(first != last) {
#if TYPE_OBJECT
Arrays.fill(array, null);
#endif
first = last = 0;
}
else if(first != 0) {
first = last = 0;
}
}
@Override
public void enqueue(KEY_TYPE e) {
array[last] = e;
last = ++last % array.length;
array[last++] = e;
if(last == array.length) last = 0;
if(last == first) expand();
}
@ -126,7 +144,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
#if TYPE_OBJECT
array[first] = null;
#endif
first = ++first % array.length;
if(++first == array.length) first = 0;
reduce();
return data;
}
@ -145,8 +163,18 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
@Override
public KEY_TYPE peek(int index) {
if(first == last || index < 0 || index > size()) throw new NoSuchElementException();
return array[(first + index) % array.length];
if(first == last || index < 0 || index >= size()) throw new NoSuchElementException();
index += first;
return index >= array.length ? array[index-array.length] : array[index];
}
@Override
public boolean contains(KEY_TYPE e) {
if(first == last) return false;
for(int i = 0,m=size();i<m;i++) {
if(e == array[(first + i) % array.length]) return true;
}
return false;
}
@Override
@ -222,6 +250,16 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
@Override
public void onChanged() {}
@Override
public ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE copy() {
ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE queue = new ARRAY_FIFO_QUEUEBRACES();
queue.first = first;
queue.last = last;
queue.minCapacity = minCapacity;
queue.array = Arrays.copyOf(array, array.length);
return queue;
}
@Override
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() { return null; }
@ -235,11 +273,20 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(first == last) return;
for(int i = 0,m=size();i<m;i++)
action.accept(array[(first + i) % array.length], input);
action.accept(i, array[(first + i) % array.length]);
clearAndTrim(0);
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(first == last) return;
for(int i = 0,m=size();i<m;i++)
action.accept(input, array[(first + i) % array.length]);
clearAndTrim(0);
}
@ -247,7 +294,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0,m=size();i<m;i++) {
if(filter.TEST_VALUE(array[(first + i) % array.length])) return true;
if(filter.test(array[(first + i) % array.length])) return true;
}
return false;
}
@ -256,7 +303,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0,m=size();i<m;i++) {
if(filter.TEST_VALUE(array[(first + i) % array.length])) return false;
if(filter.test(array[(first + i) % array.length])) return false;
}
return true;
}
@ -265,7 +312,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0,m=size();i<m;i++) {
if(!filter.TEST_VALUE(array[(first + i) % array.length])) return false;
if(!filter.test(array[(first + i) % array.length])) return false;
}
return true;
}
@ -275,7 +322,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
Objects.requireNonNull(filter);
for(int i = 0,m=size();i<m;i++) {
int index = (first + i) % array.length;
if(filter.TEST_VALUE(array[index])) {
if(filter.test(array[index])) {
KEY_TYPE data = array[index];
removeIndex(index);
return data;
@ -284,9 +331,58 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
return EMPTY_VALUE;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(int i = 0,m=size();i<m;i++) {
state = operator.APPLY_VALUE(state, array[(first + i) % array.length]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(int i = 0,m=size();i<m;i++) {
state = operator.APPLY_VALUE(state, array[(first + i) % array.length]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(int i = 0,m=size();i<m;i++) {
if(empty) {
empty = false;
state = array[(first + i) % array.length];
continue;
}
state = operator.APPLY_VALUE(state, array[(first + i) % array.length]);
}
return state;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(int i = 0,m=size();i<m;i++) {
if(filter.test(array[(first + i) % array.length])) result++;
}
return result;
}
@Override
public boolean trim(int size) {
int newSize = Math.max(size, size());
int newSize = Math.max(Math.max(size, size()), minCapacity);
if(newSize >= array.length) return false;
KEY_TYPE[] newArray = NEW_KEY_ARRAY(newSize);
if(first <= last) System.arraycopy(array, first, newArray, 0, last - first);
@ -307,7 +403,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
*/
@Override
public void clearAndTrim(int size) {
int newSize = Math.max(MIN_CAPACITY, size);
int newSize = Math.max(minCapacity, size);
if(array.length <= newSize) {
clear();
return;
@ -329,7 +425,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
protected void reduce() {
final int size = size();
if (array.length > MIN_CAPACITY && size <= array.length / 4) resize(size, array.length / 2);
if (array.length > minCapacity && size <= array.length / 4) resize(size, Math.max(array.length / 2, minCapacity));
}
protected void expand() {
@ -362,6 +458,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE value = array[index];
removeIndex(index);
index = ++index % array.length;

View File

@ -5,8 +5,12 @@ import java.util.NoSuchElementException;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
import java.util.Objects;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
@ -14,9 +18,14 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
import speiger.src.collections.utils.SanityChecks;
/**
* A Array Priority Queue, this is a very unoptimized implementation of the PriorityQueue for very specific usecases.
@ -24,7 +33,7 @@ import speiger.src.collections.PACKAGE.utils.ARRAYS;
* It is highly suggested to use HeapPriorityQueue otherwise, unless you know why you need this specific implementation
* @Type(T)
*/
public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE
{
/** The Backing Array */
protected transient KEY_TYPE[] array = EMPTY_KEY_ARRAY;
@ -68,7 +77,6 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
public ARRAY_PRIORITY_QUEUE(int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
if(size < 0) throw new IllegalAccessError("Size has to be 0 or positive");
if(size > 0) array = NEW_KEY_ARRAY(size);
this.size = size;
comparator = comp;
}
@ -185,7 +193,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
@Override
public void enqueue(KEY_TYPE e) {
if(size == array.length) array = Arrays.copyOf(array, size+1);
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
if(firstIndex != -1){
int compare = comparator == null ? COMPAREABLE_TO_KEY(e, array[firstIndex]) : comparator.compare(e, array[firstIndex]);
if(compare < 0) firstIndex = size;
@ -207,12 +215,26 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
return value;
}
@Override
public KEY_TYPE first() {
if(isEmpty()) throw new NoSuchElementException();
if(firstIndex == -1) findFirstIndex();
return array[firstIndex];
}
@Override
public KEY_TYPE peek(int index) {
if(index < 0 || index >= size) throw new NoSuchElementException();
return array[index];
}
@Override
public boolean contains(KEY_TYPE e) {
for(int i = 0;i<size;i++)
if(KEY_EQUALS(e, array[i])) return true;
return false;
}
@Override
public boolean removeFirst(KEY_TYPE e) {
for(int i = 0;i<size;i++)
@ -262,16 +284,22 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(int i = 0,m=size;i<m;i++) action.accept(dequeue(), input);
for(int i = 0,m=size;i<m;i++) action.accept(i, dequeue());
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
for(int i = 0,m=size;i<m;i++) action.accept(input, dequeue());
}
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(array[i])) return true;
if(filter.test(array[i])) return true;
}
return false;
}
@ -280,7 +308,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(array[i])) return false;
if(filter.test(array[i])) return false;
}
return true;
}
@ -289,16 +317,55 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(!filter.TEST_VALUE(array[i])) return false;
if(!filter.test(array[i])) return false;
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, array[i]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, array[i]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(int i = 0;i<size;i++) {
if(empty) {
empty = false;
state = array[i];
continue;
}
state = operator.APPLY_VALUE(state, array[i]);
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(array[i])) {
if(filter.test(array[i])) {
KEY_TYPE data = array[i];
removeIndex(i);
return data;
@ -307,11 +374,31 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
return EMPTY_VALUE;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(int i = 0;i<size;i++) {
if(filter.test(array[i])) result++;
}
return result;
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return new Iter();
}
@Override
public ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE copy() {
ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE queue = new ARRAY_PRIORITY_QUEUEBRACES();
queue.firstIndex = firstIndex;
queue.size = size;
queue.comparator = comparator;
queue.array = Arrays.copyOf(array, array.length);
return queue;
}
@Override
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() {
return comparator;
@ -353,6 +440,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return dequeue();
}
}

View File

@ -5,8 +5,12 @@ import java.util.NoSuchElementException;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
import java.util.Objects;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
@ -14,16 +18,21 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
import speiger.src.collections.utils.SanityChecks;
/**
* A Simple Heap base Priority Queue implementation
* It is a ArrayBased Alternative to TreeSets that has less object allocations
* @Type(T)
*/
public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE
{
/** The Backing Array */
protected transient KEY_TYPE[] array = EMPTY_KEY_ARRAY;
@ -64,7 +73,6 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
*/
public HEAP_PRIORITY_QUEUE(int size, COMPARATOR KEY_SUPER_GENERIC_TYPE comp) {
if(size > 0) array = NEW_KEY_ARRAY(size);
this.size = size;
comparator = comp;
}
@ -205,7 +213,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
@Override
public void enqueue(KEY_TYPE e) {
if(size == array.length) array = Arrays.copyOf(array, size + 1);
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
array[size++] = e;
ARRAYS.shiftUp(array, size-1, comparator);
}
@ -228,6 +236,13 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
return array[index];
}
@Override
public boolean contains(KEY_TYPE e) {
for(int i = 0;i<size;i++)
if(KEY_EQUALS(e, array[i])) return true;
return false;
}
@Override
public boolean removeFirst(KEY_TYPE e) {
for(int i = 0;i<size;i++)
@ -249,16 +264,22 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(int i = 0,m=size;i<m;i++) action.accept(dequeue(), input);
for(int i = 0,m=size;i<m;i++) action.accept(i, dequeue());
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
for(int i = 0,m=size;i<m;i++) action.accept(input, dequeue());
}
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(array[i])) return true;
if(filter.test(array[i])) return true;
}
return false;
}
@ -267,7 +288,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(array[i])) return false;
if(filter.test(array[i])) return false;
}
return true;
}
@ -276,16 +297,55 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(!filter.TEST_VALUE(array[i])) return false;
if(!filter.test(array[i])) return false;
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, array[i]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, array[i]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(int i = 0;i<size;i++) {
if(empty) {
empty = false;
state = array[i];
continue;
}
state = operator.APPLY_VALUE(state, array[i]);
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(array[i])) {
if(filter.test(array[i])) {
KEY_TYPE data = array[i];
removeIndex(i);
return data;
@ -294,6 +354,16 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
return EMPTY_VALUE;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(int i = 0;i<size;i++) {
if(filter.test(array[i])) result++;
}
return result;
}
protected boolean removeIndex(int index) {
array[index] = array[--size];
#if TYPE_OBJECT
@ -309,6 +379,15 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
ARRAYS.shiftDown(array, size, 0, comparator);
}
@Override
public HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE copy() {
HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE queue = new HEAP_PRIORITY_QUEUEBRACES();
queue.size = size;
queue.comparator = comparator;
queue.array = Arrays.copyOf(array, array.length);
return queue;
}
@Override
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() {
return comparator;
@ -329,6 +408,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return dequeue();
}
}

View File

@ -1,5 +1,15 @@
package speiger.src.collections.PACKAGE.queues;
#if TYPE_OBJECT
import java.util.Collection;
import java.util.Iterator;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if QUEUES_FEATURE
import speiger.src.collections.PACKAGE.utils.PRIORITY_QUEUES;
#endif
/**
* A Type Speciifc PriorityDeque or Dequeue interface to allow implementations like FIFO queues.
* @Type(T)
@ -11,6 +21,56 @@ public interface PRIORITY_DEQUEUE KEY_GENERIC_TYPE extends PRIORITY_QUEUE KEY_GE
* @param e the element that should be inserted into the first place
*/
public void enqueueFirst(KEY_TYPE e);
/**
* Method to mass insert a elements into the first Index of the PriorityDequeue.
* @param e the elements that should be inserted
*/
public default void enqueueAllFirst(KEY_TYPE... e) {
enqueueAllFirst(e, 0, e.length);
}
/**
* Method to mass insert a elements into the first Index of the PriorityDequeue.
* @param e the elements that should be inserted
* @param length the amount of elements that should be inserted
*/
public default void enqueueAllFirst(KEY_TYPE[] e, int length) {
enqueueAllFirst(e, 0, length);
}
/**
* Method to mass insert a elements into the first Index of the PriorityDequeue.
* @param e the elements that should be inserted
* @param offset the offset where in the array should be started
* @param length the amount of elements that should be inserted
*/
public default void enqueueAllFirst(KEY_TYPE[] e, int offset, int length) {
for(int i = 0;i<length;i++)
enqueueFirst(e[i+offset]);
}
/**
* Method to mass insert elements into first Index of the PriorityDequeue.
* @param c the elements that should be inserted from the Collection
*/
public default void enqueueAllFirst(COLLECTION KEY_GENERIC_TYPE c) {
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();)
enqueueFirst(iter.NEXT());
}
#if TYPE_OBJECT
/**
* Method to mass insert elements into first Index of the PriorityDequeue.
* This method exists to add support for Java Collections to make it more useable
* @param c the elements that should be inserted from the Collection
*/
public default void enqueueAllFirst(Collection<? extends CLASS_TYPE> c) {
for(Iterator<? extends CLASS_TYPE> iter = c.iterator();iter.hasNext();)
enqueueFirst(iter.next());
}
#endif
/**
* A Method to remove a element from the last place instead of the first
* @return the last element inserted
@ -22,4 +82,24 @@ public interface PRIORITY_DEQUEUE KEY_GENERIC_TYPE extends PRIORITY_QUEUE KEY_GE
* @return the Last Element within the dequeue without deleting it
*/
public default KEY_TYPE last() { return peek(size()-1); }
#if QUEUES_FEATURE
/**
* Creates a Wrapped PriorityDequeue that is Synchronized
* @return a new PriorityDequeue that is synchronized
* @see PRIORITY_QUEUES#synchronize
*/
public default PRIORITY_DEQUEUE KEY_GENERIC_TYPE synchronizeQueue() { return PRIORITY_QUEUES.synchronize(this); }
/**
* Creates a Wrapped PriorityDequeue that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new PriorityDequeue Wrapper that is synchronized
* @see PRIORITY_QUEUES#synchronize
*/
public default PRIORITY_DEQUEUE KEY_GENERIC_TYPE synchronizeQueue(Object mutex) { return PRIORITY_QUEUES.synchronize(this, mutex); }
#endif
@Override
public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy();
}

View File

@ -2,11 +2,19 @@ package speiger.src.collections.PACKAGE.queues;
#if TYPE_OBJECT
import java.util.Comparator;
import speiger.src.collections.objects.collections.ObjectIterator;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.IntFunction;
#else
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERABLE;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if QUEUES_FEATURE
import speiger.src.collections.PACKAGE.utils.PRIORITY_QUEUES;
#endif
/**
* A Simple PriorityQueue (or Queue) interface that provides with the nessesary functions to interact with it, without cluttering with the Collection interface.
@ -33,6 +41,57 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TY
* @param e the element that should be inserted
*/
public void enqueue(KEY_TYPE e);
/**
* Method to mass insert elements into the PriorityQueue
* @param e the elements that should be inserted
*/
public default void enqueueAll(KEY_TYPE... e) {
enqueueAll(e, 0, e.length);
}
/**
* Method to mass insert elements into the PriorityQueue
* @param e the elements that should be inserted
* @param length the amount of elements that should be inserted
*/
public default void enqueueAll(KEY_TYPE[] e, int length) {
enqueueAll(e, 0, length);
}
/**
* Method to mass insert elements into the PriorityQueue
* @param e the elements that should be inserted
* @param offset the offset where in the array should be started
* @param length the amount of elements that should be inserted
*/
public default void enqueueAll(KEY_TYPE[] e, int offset, int length) {
for(int i = 0;i<length;i++)
enqueue(e[i+offset]);
}
/**
* Method to mass insert elements into the PriorityQueue
* @param c the elements that should be inserted from the Collection
*/
public default void enqueueAll(COLLECTION KEY_GENERIC_TYPE c) {
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();)
enqueue(iter.NEXT());
}
#if TYPE_OBJECT
/**
* Method to mass insert elements into the PriorityQueue
* This method exists to add support for Java Collections to make it more useable
* @param c the elements that should be inserted from the Collection
*/
public default void enqueueAll(Collection<? extends CLASS_TYPE> c) {
for(Iterator<? extends CLASS_TYPE> iter = c.iterator();iter.hasNext();)
enqueue(iter.next());
}
#endif
/**
* Method to extract a element from the PriorityQueue
* @return a element from the Queue
@ -52,6 +111,13 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TY
*/
public default KEY_TYPE first() { return peek(0); }
/**
* Method to find out if a element is part of the queue
* @param e the element that is searched for
* @return true if the element is in the queue
*/
public boolean contains(KEY_TYPE e);
/**
* Removes the first found element in the queue
* @param e the element that should be removed
@ -70,6 +136,15 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TY
*/
public void onChanged();
/**
* A Function that does a shallow clone of the PriorityQueue itself.
* This function is more optimized then a copy constructor since the PriorityQueue does not have to be unsorted/resorted.
* It can be compared to Cloneable but with less exception risk
* @return a Shallow Copy of the PriorityQueue
* @note Wrappers and view PriorityQueues will not support this feature
*/
public PRIORITY_QUEUE KEY_GENERIC_TYPE copy();
/**
* @return the sorter of the Queue, can be null
*/
@ -80,6 +155,24 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TY
* @return draining iterator of the PriorityQueue
*/
public ITERATOR KEY_GENERIC_TYPE iterator();
#endif
#if QUEUES_FEATURE
/**
* Creates a Wrapped PriorityQueue that is Synchronized
* @return a new PriorityQueue that is synchronized
* @see PRIORITY_QUEUES#synchronize
*/
public default PRIORITY_QUEUE KEY_GENERIC_TYPE synchronizeQueue() { return PRIORITY_QUEUES.synchronize(this); }
/**
* Creates a Wrapped PriorityQueue that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new PriorityQueue Wrapper that is synchronized
* @see PRIORITY_QUEUES#synchronize
*/
public default PRIORITY_QUEUE KEY_GENERIC_TYPE synchronizeQueue(Object mutex) { return PRIORITY_QUEUES.synchronize(this, mutex); }
#endif
/**
* A method to drop the contents of the Queue without clearing the queue
@ -95,4 +188,16 @@ public interface PRIORITY_QUEUE KEY_GENERIC_TYPE extends ITERABLE KEY_GENERIC_TY
* @note if the Type is generic then a Object Array is created instead of a Type Array
*/
public GENERIC_SPECIAL_KEY_BRACES<E> KEY_SPECIAL_TYPE[] TO_ARRAY(KEY_SPECIAL_TYPE[] input);
#if TYPE_OBJECT
/**
* A Helper function that simplifies the process of creating a new Array.
* @param action the array creation function
* @param <E> the returning arrayType
* @return an array containing all of the elements in this collection
* @see Collection#toArray(Object[])
*/
default <E> E[] TO_ARRAY(IntFunction<E[]> action) {
return TO_ARRAY(action.apply(size()));
}
#endif
}

View File

@ -1,7 +1,8 @@
package speiger.src.collections.PACKAGE.sets;
import java.util.Iterator;
#if TYPE_OBJECT
import java.util.Objects;
#endif
import java.util.Set;
import speiger.src.collections.PACKAGE.collections.ABSTRACT_COLLECTION;
@ -13,15 +14,22 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
*/
public abstract class ABSTRACT_SET KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE
{
#if TYPE_OBJECT
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
@Override
public abstract ITERATOR KEY_GENERIC_TYPE iterator();
@Override
public ABSTRACT_SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public int hashCode() {
int hashCode = 1;
int hashCode = 0;
ITERATOR KEY_GENERIC_TYPE i = iterator();
while(i.hasNext())
hashCode = 31 * hashCode + KEY_TO_HASH(i.NEXT());
hashCode += KEY_TO_HASH(i.NEXT());
return hashCode;
}
@ -33,24 +41,10 @@ public abstract class ABSTRACT_SET KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
return false;
Set<?> l = (Set<?>)o;
if(l.size() != size()) return false;
#if !TYPE_OBJECT
if(l instanceof SET)
{
ITERATOR e1 = iterator();
ITERATOR e2 = ((SET)l).iterator();
while (e1.hasNext() && e2.hasNext()) {
if(!(KEY_EQUALS(e1.NEXT(), e2.NEXT())))
try {
return containsAll(l);
} catch (ClassCastException | NullPointerException unused) {
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
#endif
Iterator<CLASS_TYPE> e1 = iterator();
Iterator<?> e2 = l.iterator();
while (e1.hasNext() && e2.hasNext()) {
if(!Objects.equals(e1.next(), e2.next()))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
}

View File

@ -3,23 +3,29 @@ package speiger.src.collections.PACKAGE.sets;
import java.util.Arrays;
import java.util.Collection;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
#if PRIMITIVES
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
#if PRIMITIVES && !JDK_FUNCTION
import java.util.function.JAVA_PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.objects.utils.ObjectArrays;
#endif
@ -34,7 +40,7 @@ import speiger.src.collections.PACKAGE.utils.ARRAYS;
* This implementation does not shrink the backing array
* @Type(T)
*/
public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE
{
/** The Backing Array */
protected transient KEY_TYPE[] data;
@ -54,6 +60,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
* @throws NegativeArraySizeException if the capacity is negative
*/
public ARRAY_SET(int capacity) {
if(capacity < 0) throw new IllegalStateException("Size has to be 0 or greater");
data = NEW_KEY_ARRAY(capacity);
}
@ -72,8 +79,8 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
* @throws NegativeArraySizeException if the length is negative
*/
public ARRAY_SET(KEY_TYPE[] array, int length) {
data = Arrays.copyOf(array, length);
size = length;
this(length);
addAll(array, length);
}
/**
@ -130,6 +137,17 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
return false;
}
#if TYPE_OBJECT
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) {
int index = findIndex(o);
if(index != -1) return data[index];
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
data[size++] = o;
return o;
}
#endif
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) {
int index = findIndex(o);
@ -157,7 +175,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
}
else if(index != size - 1) {
o = data[index];
System.arraycopy(data, index+1, data, index, size - index);
System.arraycopy(data, index+1, data, index, size - index - 1);
data[size-1] = o;
}
return false;
@ -227,6 +245,21 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
return result;
}
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
int j = 0;
for(int i = 0;i<size;i++) {
if(!c.contains(data[i])) data[j++] = data[i];
else r.accept(data[i]);
}
boolean result = j != size;
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return result;
}
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) {
int j = 0;
@ -242,6 +275,21 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
return result;
}
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) {
int j = 0;
for(int i = 0;i<size;i++) {
if(c.contains(data[i])) data[j++] = data[i];
else r.accept(data[i]);
}
boolean result = j != size;
#if TYPE_OBJECT
Arrays.fill(data, j, size, null);
#endif
size = j;
return result;
}
@Override
@Primitive
public boolean removeAll(Collection<?> c) {
@ -350,17 +398,23 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(int i = 0;i<size;action.accept(i, data[i++]));
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
for(int i = 0;i<size;i++)
action.accept(data[i], input);
action.accept(input, data[i]);
}
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(data[i])) return true;
if(filter.test(data[i])) return true;
}
return false;
}
@ -369,7 +423,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(data[i])) return false;
if(filter.test(data[i])) return false;
}
return true;
}
@ -378,7 +432,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(!filter.TEST_VALUE(data[i])) return false;
if(!filter.test(data[i])) return false;
}
return true;
}
@ -387,11 +441,60 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) {
if(filter.TEST_VALUE(data[i])) return data[i];
if(filter.test(data[i])) return data[i];
}
return EMPTY_VALUE;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
for(int i = 0;i<size;i++) {
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
for(int i = 0;i<size;i++) {
if(empty) {
empty = false;
state = data[i];
continue;
}
state = operator.APPLY_VALUE(state, data[i]);
}
return state;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
for(int i = 0;i<size;i++) {
if(filter.test(data[i])) result++;
}
return result;
}
#if !TYPE_OBJECT
protected int findIndex(KEY_TYPE o) {
for(int i = size-1;i>=0;i--)
@ -418,31 +521,11 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
throw new NoSuchElementException();
}
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) {
int fromIndex = findIndex(fromElement);
int toIndex = findIndex(toElement);
if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, toIndex - fromIndex + 1);
}
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) {
int toIndex = findIndex(toElement);
if(toIndex == -1) throw new NoSuchElementException();
return new SubSet(0, toIndex+1);
}
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) {
int fromIndex = findIndex(fromElement);
if(fromIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, size - fromIndex);
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
public ARRAY_SET KEY_GENERIC_TYPE copy() {
ARRAY_SET KEY_GENERIC_TYPE set = new ARRAY_SETBRACES();
set.data = Arrays.copyOf(data, data.length);
set.size = size;
return set;
}
@Override
@ -460,6 +543,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) return Arrays.copyOf(data, size());
System.arraycopy(data, 0, a, 0, size());
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
@ -467,6 +551,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
@Override
@Deprecated
public Object[] toArray() {
if(isEmpty()) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size()];
for(int i = 0;i<size();i++)
obj[i] = KEY_TO_OBJ(data[i]);
@ -480,261 +565,10 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
for(int i = 0;i<size();i++)
a[i] = (E)KEY_TO_OBJ(data[i]);
if (a.length > size) a[size] = null;
return a;
}
private class SubSet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
int offset;
int length;
SubSet(int offset, int length) {
this.offset = offset;
this.length = length;
}
int end() { return offset+length; }
@Override
public boolean add(KEY_TYPE o) {
int index = findIndex(o);
if(index == -1) {
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
if(end() != size) System.arraycopy(data, end(), data, end()+1, size-(offset+length));
data[end()] = o;
size++;
length++;
return true;
}
return false;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) {
int index = findIndex(o);
if(index == -1) {
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
System.arraycopy(data, offset, data, offset+1, size-offset);
data[offset] = o;
size++;
length++;
return true;
}
else if(index != 0) {
o = data[index];
System.arraycopy(data, offset, data, offset+1, index-offset);
data[offset] = o;
}
return false;
}
@Override
public boolean addAndMoveToLast(KEY_TYPE o) {
int index = findIndex(o);
if(index == -1) {
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
System.arraycopy(data, end()+1, data, end(), size-end());
data[end()] = o;
size++;
length++;
return true;
}
else if(index != 0) {
o = data[index];
System.arraycopy(data, offset+1, data, offset, index-offset);
data[offset+length] = o;
}
return false;
}
@Override
public boolean moveToFirst(KEY_TYPE o) {
int index = findIndex(o);
if(index > offset) {
o = data[index];
System.arraycopy(data, offset, data, offset+1, index-offset);
data[offset] = o;
return true;
}
return false;
}
@Override
public boolean moveToLast(KEY_TYPE o) {
int index = findIndex(o);
if(index != -1 && index < end()-1) {
o = data[index];
System.arraycopy(data, index+1, data, index, end()-index-1);
data[end()-1] = o;
return true;
}
return false;
}
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE e) {
return findIndex(e) != -1;
}
#endif
@Override
public boolean contains(Object e) {
return findIndex(e) != -1;
}
@Override
public KEY_TYPE FIRST_KEY() {
if(length == 0) throw new NoSuchElementException();
return data[offset];
}
@Override
public KEY_TYPE LAST_KEY() {
if(length == 0) throw new NoSuchElementException();
return data[end()-1];
}
#if !TYPE_OBJECT
@Override
public boolean remove(KEY_TYPE o) {
int index = findIndex(o);
if(index != -1) {
size--;
length--;
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
return true;
}
return false;
}
#endif
@Override
public boolean remove(Object o) {
int index = findIndex(o);
if(index != -1) {
size--;
length--;
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
#if TYPE_OBJECT
data[size] = EMPTY_KEY_VALUE;
#endif
return true;
}
return false;
}
@Override
public KEY_TYPE POLL_FIRST_KEY() {
if(length == 0) throw new NoSuchElementException();
size--;
length--;
KEY_TYPE result = data[offset];
System.arraycopy(data, offset+1, data, offset, size-offset);
#if TYPE_OBJECT
data[size] = EMPTY_KEY_VALUE;
#endif
return result;
}
@Override
public KEY_TYPE POLL_LAST_KEY() {
if(length == 0) throw new NoSuchElementException();
KEY_TYPE result = data[offset+length];
size--;
length--;
System.arraycopy(data, end()+1, data, end(), size-end());
#if TYPE_OBJECT
data[size] = EMPTY_KEY_VALUE;
#endif
return result;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() {
return new SetIterator(offset);
}
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) {
int index = findIndex(fromElement);
if(index != -1) return new SetIterator(index);
throw new NoSuchElementException();
}
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) {
int fromIndex = findIndex(fromElement);
int toIndex = findIndex(toElement);
if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, toIndex - fromIndex + 1);
}
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) {
int toIndex = findIndex(toElement);
if(toIndex == -1) throw new NoSuchElementException();
return new SubSet(0, toIndex+1);
}
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) {
int fromIndex = findIndex(fromElement);
if(fromIndex == -1) throw new NoSuchElementException();
return new SubSet(fromIndex, size - fromIndex);
}
@Override
public int size() {
return length;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) return Arrays.copyOfRange(data, offset, end());
System.arraycopy(data, offset, a, 0, size());
return a;
}
#endif
@Override
@Deprecated
public Object[] toArray() {
Object[] obj = new Object[size()];
for(int i = 0;i<size();i++)
obj[i] = KEY_TO_OBJ(data[offset+i]);
return obj;
}
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size()];
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
for(int i = 0;i<size();i++)
a[i] = (E)KEY_TO_OBJ(data[offset+i]);
return a;
}
#if !TYPE_OBJECT
protected int findIndex(KEY_TYPE o) {
for(int i = length-1;i>=0;i--)
if(KEY_EQUALS(data[offset+i], o)) return i + offset;
return -1;
}
#endif
protected int findIndex(Object o) {
for(int i = length-1;i>=0;i--)
if(EQUALS_KEY_TYPE(data[offset+i], o)) return i + offset;
return -1;
}
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
int index;
int lastReturned = -1;
@ -750,6 +584,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
lastReturned = index;
return data[index++];
}
@ -761,8 +596,9 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
@Override
public KEY_TYPE PREVIOUS() {
lastReturned = index;
return data[index--];
if(!hasPrevious()) throw new NoSuchElementException();
--index;
return data[(lastReturned = index)];
}
@Override
@ -777,94 +613,9 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
@Override
public void remove() {
if(lastReturned == -1)
throw new IllegalStateException();
SubSet.this.remove(data[lastReturned]);
if(lastReturned < index)
index--;
lastReturned = -1;
}
#if TYPE_OBJECT
@Override
public void set(Object e) { throw new UnsupportedOperationException(); }
@Override
public void add(Object e) { throw new UnsupportedOperationException(); }
#else
@Override
public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
#endif
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, (size() - 1) - index);
index += steps;
return steps;
}
@Override
public int back(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
return steps;
}
}
}
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
int index;
int lastReturned = -1;
public SetIterator(int index) {
this.index = index;
}
@Override
public boolean hasNext() {
return index < size();
}
@Override
public KEY_TYPE NEXT() {
lastReturned = index;
return data[index++];
}
@Override
public boolean hasPrevious() {
return index > 0;
}
@Override
public KEY_TYPE PREVIOUS() {
lastReturned = index;
return data[index--];
}
@Override
public int nextIndex() {
return index;
}
@Override
public int previousIndex() {
return index-1;
}
@Override
public void remove() {
if(lastReturned == -1)
throw new IllegalStateException();
if(lastReturned == -1) throw new IllegalStateException();
ARRAY_SET.this.remove(data[lastReturned]);
if(lastReturned < index)
index--;
if(lastReturned < index) index--;
lastReturned = -1;
}
@ -886,8 +637,9 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, (size() - 1) - index);
int steps = Math.min(amount, size() - index);
index += steps;
if(steps > 0) lastReturned = Math.min(index-1, size()-1);
return steps;
}
@ -896,6 +648,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
if(steps > 0) lastReturned = Math.min(index, size()-1);
return steps;
}
}

View File

@ -1,25 +1,35 @@
package speiger.src.collections.PACKAGE.sets;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
#if !TYPE_OBJECT
import speiger.src.collections.objects.utils.ObjectArrays;
#endif
import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.utils.HashUtil;
@ -31,7 +41,7 @@ import speiger.src.collections.utils.SanityChecks;
* Extra to that there is a couple quality of life functions provided
* @Type(T)
*/
public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE
{
/** The Backing keys array */
protected transient KEY_TYPE[] keys;
@ -50,6 +60,12 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
/** The Last Index in the Map */
protected int lastIndex = -1;
/**
* Helper constructor to optimize the copying
* Only accessible through implementations
*/
protected IMMUTABLE_HASH_SET() {}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
@ -181,7 +197,18 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
{
KEY_TYPE o = a[i];
if(KEY_EQUALS_NULL(o)) {
if(!containsNull) size++;
if(!containsNull) {
size++;
if(prev != -1) {
newLinks[prev] ^= ((newLinks[prev] ^ (newSize & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
newLinks[newSize] ^= ((newLinks[newSize] ^ ((prev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
prev = newSize;
}
else {
prev = firstIndex = newSize;
newLinks[newSize] = -1L;
}
}
containsNull = true;
continue;
}
@ -190,7 +217,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
KEY_TYPE current = newKeys[pos];
if(KEY_EQUALS_NOT_NULL(current)) {
if(KEY_EQUALS(current, o)) continue;
while(KEY_EQUALS_NOT_NULL((current = newKeys[pos = (++pos & mask)]))) {
while(KEY_EQUALS_NOT_NULL((current = newKeys[pos = (++pos & newMask)]))) {
if(KEY_EQUALS(current, o)) {
found = false;
break;
@ -211,7 +238,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
}
}
}
nullIndex = size;
nullIndex = newSize;
mask = newMask;
keys = newKeys;
links = newLinks;
@ -221,6 +248,10 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
@Override
public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#if TYPE_OBJECT
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
@Override
@Primitive
public boolean addAll(Collection<? extends CLASS_TYPE> c) { throw new UnsupportedOperationException(); }
@ -237,7 +268,12 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
@Override
public boolean contains(Object o) {
#if TYPE_OBJECT
if(o == null) return containsNull;
#else
if(o == null) return false;
if(o instanceof CLASS_TYPE && KEY_EQUALS(CLASS_TO_KEY(o), EMPTY_KEY_VALUE)) return containsNull;
#endif
int pos = HashUtil.mix(o.hashCode()) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NULL(current)) return false;
@ -297,11 +333,22 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
int count = 0;
int index = firstIndex;
while(index != -1) {
action.accept(count++, keys[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
int index = firstIndex;
while(index != -1) {
action.accept(keys[index], input);
action.accept(input, keys[index]);
index = (int)links[index];
}
}
@ -311,7 +358,8 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return true;
if(filter.test(keys[index])) return true;
index = (int)links[index];
}
return false;
}
@ -321,7 +369,8 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return false;
if(filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
@ -331,21 +380,79 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(!filter.TEST_VALUE(keys[index])) return false;
if(!filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
state = keys[index];
empty = false;
}
else state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return keys[index];
if(filter.test(keys[index])) return keys[index];
index = (int)links[index];
}
return EMPTY_VALUE;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
int index = firstIndex;
while(index != -1) {
if(filter.test(keys[index])) result++;
index = (int)links[index];
}
return result;
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE iterator() {
return new SetIterator();
@ -356,17 +463,54 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
return new SetIterator(fromElement);
}
#if !TYPE_OBJECT
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) a = new KEY_TYPE[size()];
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
a[i] = keys[index];
}
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#endif
@Override
@Deprecated
public Object[] toArray() {
if(isEmpty()) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size()];
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
obj[i] = KEY_TO_OBJ(keys[index]);
}
return obj;
}
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size()];
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
a[i] = (E)KEY_TO_OBJ(keys[index]);
}
if (a.length > size) a[size] = null;
return a;
}
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
public IMMUTABLE_HASH_SET KEY_GENERIC_TYPE copy() {
IMMUTABLE_HASH_SET KEY_GENERIC_TYPE set = new IMMUTABLE_HASH_SETBRACES();
set.containsNull = containsNull;
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
set.size = size;
set.mask = mask;
set.nullIndex = nullIndex;
set.keys = Arrays.copyOf(keys, keys.length);
set.links = Arrays.copyOf(links, links.length);
return set;
}
@Override
public void clear() { throw new UnsupportedOperationException(); }
@ -413,6 +557,30 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
}
}
@Override
public int skip(int amount) {
int result = 0;
while(next != -1 && result != amount) {
current = previous = next;
next = (int)(links[current]);
result++;
}
if(index >= 0) index+=result;
return result;
}
@Override
public int back(int amount) {
int result = 0;
while(previous != -1 && result != amount) {
current = next = previous;
previous = (int)(links[current] >> 32);
result++;
}
if(index >= 0) index-=result;
return result;
}
@Override
public boolean hasNext() {
return next != -1;
@ -441,9 +609,8 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
current = previous;
current = next = previous;
previous = (int)(links[current] >> 32);
next = current;
if(index >= 0) index--;
return keys[current];
}
@ -451,9 +618,8 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
current = next;
current = previous = next;
next = (int)(links[current]);
previous = current;
if(index >= 0) index++;
return keys[current];
}

View File

@ -1,29 +1,35 @@
package speiger.src.collections.PACKAGE.sets;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
import java.util.Arrays;
import java.util.Objects;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.utils.STRATEGY;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.SanityChecks;
@ -34,7 +40,7 @@ import speiger.src.collections.utils.SanityChecks;
* This implementation of SortedSet does not support SubSet of any kind. It implements the interface due to sortability and first/last access
* @Type(T)
*/
public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE
{
/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
protected transient long[] links;
@ -124,7 +130,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public LINKED_CUSTOM_HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(length < 0 ? 0 : length, strategy);
this(length, strategy);
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
@ -238,6 +244,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
}
containsNull = true;
onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex);
}
else {
int pos = HashUtil.mix(strategy.hashCode(o)) & mask;
@ -250,6 +257,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
}
keys[pos] = o;
onNodeAdded(pos);
moveToFirstIndex(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return true;
@ -283,7 +291,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
@Override
public boolean moveToFirst(KEY_TYPE o) {
if(strategy.equals(FIRST_KEY(), o)) return false;
if(isEmpty() || strategy.equals(FIRST_KEY(), o)) return false;
if(strategy.equals(o, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToFirstIndex(nullIndex);
@ -305,7 +313,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
@Override
public boolean moveToLast(KEY_TYPE o) {
if(strategy.equals(LAST_KEY(), o)) return false;
if(isEmpty() || strategy.equals(LAST_KEY(), o)) return false;
if(strategy.equals(o, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToLastIndex(nullIndex);
@ -371,8 +379,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
public KEY_TYPE POLL_FIRST_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = firstIndex;
firstIndex = (int)links[pos];
if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(strategy.equals(result, EMPTY_KEY_VALUE)) {
@ -394,8 +401,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
public KEY_TYPE POLL_LAST_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = lastIndex;
lastIndex = (int)(links[pos] >>> 32);
if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(strategy.equals(result, EMPTY_KEY_VALUE)) {
@ -509,7 +515,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= size) {
if(request >= nullIndex) {
clear();
return;
}
@ -523,6 +529,41 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
containsNull = false;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) a = new KEY_TYPE[size()];
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
a[i] = keys[index];
}
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#endif
@Override
@Deprecated
public Object[] toArray() {
if(isEmpty()) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size()];
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
obj[i] = KEY_TO_OBJ(keys[index]);
}
return obj;
}
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size()];
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
a[i] = (E)KEY_TO_OBJ(keys[index]);
}
if (a.length > size) a[size] = null;
return a;
}
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
@ -534,11 +575,22 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
int count = 0;
int index = firstIndex;
while(index != -1) {
action.accept(count++, keys[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
int index = firstIndex;
while(index != -1) {
action.accept(keys[index], input);
action.accept(input, keys[index]);
index = (int)links[index];
}
}
@ -548,7 +600,8 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return true;
if(filter.test(keys[index])) return true;
index = (int)links[index];
}
return false;
}
@ -558,7 +611,8 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return false;
if(filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
@ -568,21 +622,79 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(!filter.TEST_VALUE(keys[index])) return false;
if(!filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
state = keys[index];
empty = false;
}
else state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return keys[index];
if(filter.test(keys[index])) return keys[index];
index = (int)links[index];
}
return EMPTY_VALUE;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
int index = firstIndex;
while(index != -1) {
if(filter.test(keys[index])) result++;
index = (int)links[index];
}
return result;
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE iterator() {
return new SetIterator();
@ -594,16 +706,20 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
public LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE copy() {
LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE set = new LINKED_CUSTOM_HASH_SETBRACES(0, loadFactor, strategy);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
set.links = Arrays.copyOf(links, links.length);
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
return set;
}
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
int previous = -1;
@ -652,6 +768,30 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
return previous != -1;
}
@Override
public int skip(int amount) {
int result = 0;
while(next != -1 && result != amount) {
current = previous = next;
next = (int)(links[current]);
result++;
}
if(index >= 0) index+=result;
return result;
}
@Override
public int back(int amount) {
int result = 0;
while(previous != -1 && result != amount) {
current = next = previous;
previous = (int)(links[current] >> 32);
result++;
}
if(index >= 0) index-=result;
return result;
}
@Override
public int nextIndex() {
ensureIndexKnown();
@ -710,9 +850,8 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
current = previous;
current = next = previous;
previous = (int)(links[current] >> 32);
next = current;
if(index >= 0) index--;
return keys[current];
}
@ -720,9 +859,8 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
current = next;
current = previous = next;
next = (int)(links[current]);
previous = current;
if(index >= 0) index++;
return keys[current];
}

View File

@ -1,14 +1,16 @@
package speiger.src.collections.PACKAGE.sets;
#if TYPE_OBJECT
import java.util.Comparator;
#endif
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
#if TYPE_OBJECT
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
@ -17,15 +19,19 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
#endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#endif
import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.SanityChecks;
@ -35,7 +41,7 @@ import speiger.src.collections.utils.SanityChecks;
* This implementation of SortedSet does not support SubSet of any kind. It implements the interface due to sortability and first/last access
* @Type(T)
*/
public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE
{
/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
protected transient long[] links;
@ -111,7 +117,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public LINKED_HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor) {
this(length < 0 ? 0 : length);
this(length);
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
@ -209,6 +215,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
}
containsNull = true;
onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex);
}
else {
int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
@ -221,6 +228,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
}
keys[pos] = o;
onNodeAdded(pos);
moveToFirstIndex(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return true;
@ -254,7 +262,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
@Override
public boolean moveToFirst(KEY_TYPE o) {
if(KEY_EQUALS(FIRST_KEY(), o)) return false;
if(isEmpty() || KEY_EQUALS(FIRST_KEY(), o)) return false;
if(KEY_EQUALS_NULL(o)) {
if(containsNull) {
moveToFirstIndex(nullIndex);
@ -276,7 +284,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
@Override
public boolean moveToLast(KEY_TYPE o) {
if(KEY_EQUALS(LAST_KEY(), o)) return false;
if(isEmpty() || KEY_EQUALS(LAST_KEY(), o)) return false;
if(KEY_EQUALS_NULL(o)) {
if(containsNull) {
moveToLastIndex(nullIndex);
@ -342,8 +350,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
public KEY_TYPE POLL_FIRST_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = firstIndex;
firstIndex = (int)links[pos];
if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(KEY_EQUALS_NULL(result)) {
@ -365,8 +372,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
public KEY_TYPE POLL_LAST_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = lastIndex;
lastIndex = (int)(links[pos] >>> 32);
if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL;
onNodeRemoved(pos);
KEY_TYPE result = keys[pos];
size--;
if(KEY_EQUALS_NULL(result)) {
@ -378,6 +384,41 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
return result;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) a = new KEY_TYPE[size()];
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
a[i] = keys[index];
}
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#endif
@Override
@Deprecated
public Object[] toArray() {
if(isEmpty()) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size()];
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
obj[i] = KEY_TO_OBJ(keys[index]);
}
return obj;
}
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size()];
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
for(int i = 0, index = firstIndex;index != -1;i++,index = (int)links[index]) {
a[i] = (E)KEY_TO_OBJ(keys[index]);
}
if (a.length > size) a[size] = null;
return a;
}
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
@ -389,11 +430,22 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
int count = 0;
int index = firstIndex;
while(index != -1) {
action.accept(count++, keys[index]);
index = (int)links[index];
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
int index = firstIndex;
while(index != -1) {
action.accept(keys[index], input);
action.accept(input, keys[index]);
index = (int)links[index];
}
}
@ -403,7 +455,8 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return true;
if(filter.test(keys[index])) return true;
index = (int)links[index];
}
return false;
}
@ -413,7 +466,8 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return false;
if(filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
@ -423,21 +477,79 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(!filter.TEST_VALUE(keys[index])) return false;
if(!filter.test(keys[index])) return false;
index = (int)links[index];
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
int index = firstIndex;
while(index != -1) {
state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
int index = firstIndex;
while(index != -1) {
if(empty) {
state = keys[index];
empty = false;
}
else state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index];
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int index = firstIndex;
while(index != -1) {
if(filter.TEST_VALUE(keys[index])) return keys[index];
if(filter.test(keys[index])) return keys[index];
index = (int)links[index];
}
return EMPTY_VALUE;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
int result = 0;
int index = firstIndex;
while(index != -1) {
if(filter.test(keys[index])) result++;
index = (int)links[index];
}
return result;
}
@Override
protected void onNodeAdded(int pos) {
if(size == 0) {
@ -540,7 +652,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= size) {
if(request >= nullIndex) {
clear();
return;
}
@ -565,16 +677,20 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
public LINKED_HASH_SET KEY_GENERIC_TYPE copy() {
LINKED_HASH_SET KEY_GENERIC_TYPE set = new LINKED_HASH_SETBRACES(0, loadFactor);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
set.links = Arrays.copyOf(links, links.length);
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
return set;
}
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
int previous = -1;
@ -613,6 +729,30 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
}
}
@Override
public int skip(int amount) {
int result = 0;
while(next != -1 && result != amount) {
current = previous = next;
next = (int)(links[current]);
result++;
}
if(index >= 0) index+=result;
return result;
}
@Override
public int back(int amount) {
int result = 0;
while(previous != -1 && result != amount) {
current = next = previous;
previous = (int)(links[current] >> 32);
result++;
}
if(index >= 0) index-=result;
return result;
}
@Override
public boolean hasNext() {
return next != -1;
@ -681,9 +821,8 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
current = previous;
current = next = previous;
previous = (int)(links[current] >> 32);
next = current;
if(index >= 0) index--;
return keys[current];
}
@ -691,9 +830,8 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
current = next;
current = previous = next;
next = (int)(links[current]);
previous = current;
if(index >= 0) index++;
return keys[current];
}

View File

@ -4,6 +4,9 @@ import java.util.NavigableSet;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
#if SETS_FEATURE
import speiger.src.collections.PACKAGE.utils.SETS;
#endif
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
/**
@ -115,7 +118,33 @@ public interface NAVIGABLE_SET KEY_GENERIC_TYPE extends NavigableSet<CLASS_TYPE>
/** @return a Type Specific desendingSet */
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet();
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE copy();
#if SETS_FEATURE
/**
* Creates a Wrapped NavigableSet that is Synchronized
* @return a new NavigableSet that is synchronized
* @see SETS#synchronize
*/
public default NAVIGABLE_SET KEY_GENERIC_TYPE synchronize() { return SETS.synchronize(this); }
/**
* Creates a Wrapped NavigableSet that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new NavigableSet Wrapper that is synchronized
* @see SETS#synchronize
*/
public default NAVIGABLE_SET KEY_GENERIC_TYPE synchronize(Object mutex) { return SETS.synchronize(this, mutex); }
/**
* Creates a Wrapped NavigableSet that is unmodifiable
* @return a new NavigableSet Wrapper that is unmodifiable
* @see SETS#unmodifiable
*/
public default NAVIGABLE_SET KEY_GENERIC_TYPE unmodifiable() { return SETS.unmodifiable(this); }
#endif
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing
* @return type specific splititerator
@ -144,10 +173,10 @@ public interface NAVIGABLE_SET KEY_GENERIC_TYPE extends NavigableSet<CLASS_TYPE>
default CLASS_TYPE last() { return SORTED_SET.super.last(); }
@Override
@Deprecated
public default CLASS_TYPE pollFirst() { return KEY_TO_OBJ(POLL_FIRST_KEY()); }
public default CLASS_TYPE pollFirst() { return isEmpty() ? null : KEY_TO_OBJ(POLL_FIRST_KEY()); }
@Override
@Deprecated
public default CLASS_TYPE pollLast() { return KEY_TO_OBJ(POLL_LAST_KEY()); }
public default CLASS_TYPE pollLast() { return isEmpty() ? null : KEY_TO_OBJ(POLL_LAST_KEY()); }
@Override
@Deprecated

View File

@ -2,24 +2,32 @@ package speiger.src.collections.PACKAGE.sets;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
#if TYPE_OBJECT
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
import speiger.src.collections.PACKAGE.lists.LIST;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.utils.STRATEGY;
import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.ITrimmable;
@ -274,6 +282,30 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
return true;
}
#if TYPE_OBJECT
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) {
if(strategy.equals(o, EMPTY_KEY_VALUE)) {
if(containsNull) return EMPTY_KEY_VALUE;
containsNull = true;
onNodeAdded(nullIndex);
}
else {
int pos = HashUtil.mix(strategy.hashCode(o)) & mask;
KEY_TYPE current = keys[pos];
if(!strategy.equals(current, EMPTY_KEY_VALUE)) {
if(strategy.equals(current, o)) return current;
while(!strategy.equals((current = keys[pos = (++pos & mask)]), EMPTY_KEY_VALUE))
if(strategy.equals(current, o)) return current;
}
keys[pos] = o;
onNodeAdded(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return o;
}
#endif
@Override
@Primitive
public boolean addAll(Collection<? extends CLASS_TYPE> c) {
@ -346,10 +378,10 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
#endif
@Override
public boolean trim(int size) {
int newSize = HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor));
if(newSize >= nullIndex || size >= Math.min((int)Math.ceil(newSize * loadFactor), newSize - 1)) return false;
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= nullIndex || this.size >= Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false;
try {
rehash(newSize);
rehash(request);
}
catch(OutOfMemoryError e) { return false; }
return true;
@ -358,7 +390,7 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= size) {
if(request >= nullIndex) {
clear();
return;
}
@ -429,7 +461,10 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
int newMask = newSize - 1;
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
for(int i = nullIndex, pos = 0, j = (size - (containsNull ? 1 : 0));j-- != 0;) {
while(strategy.equals(keys[--i], EMPTY_KEY_VALUE));
while(true) {
if(--i < 0) throw new ConcurrentModificationException("Set was modified during rehash");
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) break;
}
if(!strategy.equals(newKeys[pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask], EMPTY_KEY_VALUE))
while(!strategy.equals(newKeys[pos = (++pos & newMask)], EMPTY_KEY_VALUE));
newKeys[pos] = keys[i];
@ -445,6 +480,57 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
return new SetIterator();
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) a = new KEY_TYPE[size()];
if(containsNull) a[0] = EMPTY_KEY_VALUE;
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) a[index++] = keys[i];
}
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#endif
@Override
@Deprecated
public Object[] toArray() {
if(isEmpty()) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size()];
if(containsNull) obj[0] = KEY_TO_OBJ(EMPTY_KEY_VALUE);
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) obj[index++] = KEY_TO_OBJ(keys[i]);
}
return obj;
}
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size()];
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
if(containsNull) a[0] = (E)KEY_TO_OBJ(EMPTY_KEY_VALUE);
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) a[index++] = (E)KEY_TO_OBJ(keys[i]);
}
if (a.length > size) a[size] = null;
return a;
}
@Override
public CUSTOM_HASH_SET KEY_GENERIC_TYPE copy() {
CUSTOM_HASH_SET KEY_GENERIC_TYPE set = new CUSTOM_HASH_SETBRACES(0, loadFactor, strategy);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
return set;
}
@Override
public void clear() {
if(size == 0) return;
@ -460,6 +546,7 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
if(containsNull) action.accept(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
@ -468,12 +555,22 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
if(containsNull) action.accept(keys[nullIndex], input);
if(containsNull) action.accept(0, keys[nullIndex]);
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) action.accept(index++, keys[i]);
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
if(containsNull) action.accept(input, keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) action.accept(keys[i], input);
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) action.accept(input, keys[i]);
}
}
@ -481,9 +578,9 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return false;
if(containsNull && filter.TEST_VALUE(keys[nullIndex])) return true;
if(containsNull && filter.test(keys[nullIndex])) return true;
for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.TEST_VALUE(keys[i])) return true;
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return true;
}
return false;
}
@ -492,9 +589,9 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
if(containsNull && filter.TEST_VALUE(keys[nullIndex])) return false;
if(containsNull && filter.test(keys[nullIndex])) return false;
for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.TEST_VALUE(keys[i])) return false;
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return false;
}
return true;
}
@ -503,30 +600,92 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
if(containsNull && !filter.TEST_VALUE(keys[nullIndex])) return false;
if(containsNull && !filter.test(keys[nullIndex])) return false;
for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && !filter.TEST_VALUE(keys[i])) return false;
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && !filter.test(keys[i])) return false;
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
if(containsNull) state = operator.APPLY_VALUE(state, keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue;
state = operator.APPLY_VALUE(state, keys[i]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
if(containsNull) state = operator.APPLY_VALUE(state, keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue;
state = operator.APPLY_VALUE(state, keys[i]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
if(containsNull) {
state = keys[nullIndex];
empty = false;
}
for(int i = nullIndex-1;i>=0;i--) {
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue;
if(empty) {
empty = false;
state = keys[i];
continue;
}
state = operator.APPLY_VALUE(state, keys[i]);
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE;
if(containsNull && filter.TEST_VALUE(keys[nullIndex])) return keys[nullIndex];
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex];
for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.TEST_VALUE(keys[i])) return keys[i];
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return keys[i];
}
return EMPTY_VALUE;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
if(containsNull && filter.test(keys[nullIndex])) result++;
for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) result++;
}
return result;
}
private class SetIterator implements ITERATOR KEY_GENERIC_TYPE {
int pos = nullIndex;
int returnedPos = -1;
int lastReturned = -1;
int nextIndex = Integer.MIN_VALUE;
boolean returnNull = containsNull;
LIST KEY_GENERIC_TYPE wrapped = null;
KEY_TYPE[] wrapped = null;
int wrappedIndex = 0;
@Override
public boolean hasNext() {
@ -538,7 +697,7 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
else {
while(true) {
if(--pos < 0) {
if(wrapped == null || wrapped.size() <= -pos - 1) break;
if(wrapped == null || wrappedIndex <= -pos - 1) break;
nextIndex = -pos - 1;
break;
}
@ -555,9 +714,10 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
returnedPos = pos;
if(nextIndex < 0){
lastReturned = Integer.MAX_VALUE;
KEY_TYPE value = wrapped.GET_KEY(nextIndex);
KEY_TYPE value = wrapped[nextIndex];
nextIndex = Integer.MIN_VALUE;
return value;
}
@ -573,9 +733,10 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
containsNull = false;
keys[nullIndex] = EMPTY_KEY_VALUE;
}
else if(pos >= 0) shiftKeys(pos);
else if(returnedPos >= 0) shiftKeys(returnedPos);
else {
CUSTOM_HASH_SET.this.remove(wrapped.GET_KEY(-pos - 1));
CUSTOM_HASH_SET.this.remove(wrapped[-returnedPos - 1]);
lastReturned = -1;
return;
}
size--;
@ -596,12 +757,19 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
if(last <= startPos ? (last >= slot || slot > startPos) : (last >= slot && slot > startPos)) break;
startPos = ++startPos & mask;
}
if(startPos < last) {
if(wrapped == null) wrapped = new ARRAY_LISTBRACES(2);
wrapped.add(keys[startPos]);
}
if(startPos < last) addWrapper(keys[startPos]);
keys[last] = current;
}
}
private void addWrapper(KEY_TYPE value) {
if(wrapped == null) wrapped = NEW_KEY_ARRAY(2);
else if(wrappedIndex >= wrapped.length) {
KEY_TYPE[] newArray = NEW_KEY_ARRAY(wrapped.length * 2);
System.arraycopy(wrapped, 0, newArray, 0, wrapped.length);
wrapped = newArray;
}
wrapped[wrappedIndex++] = value;
}
}
}

View File

@ -2,23 +2,32 @@ package speiger.src.collections.PACKAGE.sets;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
#if TYPE_OBJECT
import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
import speiger.src.collections.PACKAGE.lists.LIST;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_OBJECT_CONSUMER;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.ITrimmable;
import speiger.src.collections.utils.SanityChecks;
@ -232,6 +241,30 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
return true;
}
#if TYPE_OBJECT
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) {
if(KEY_EQUALS_NULL(o)) {
if(containsNull) return null;
containsNull = true;
onNodeAdded(nullIndex);
}
else {
int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NOT_NULL(current)) {
if(KEY_EQUALS(current, o)) return current;
while(KEY_EQUALS_NOT_NULL((current = keys[pos = (++pos & mask)])))
if(KEY_EQUALS(current, o)) return current;
}
keys[pos] = o;
onNodeAdded(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return o;
}
#endif
@Override
@Primitive
public boolean addAll(Collection<? extends CLASS_TYPE> c) {
@ -249,7 +282,12 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
@Override
public boolean contains(Object o) {
#if TYPE_OBJECT
if(o == null) return containsNull;
#else
if(o == null) return false;
if(o instanceof CLASS_TYPE && KEY_EQUALS(CLASS_TO_KEY(o), EMPTY_KEY_VALUE)) return containsNull;
#endif
int pos = HashUtil.mix(o.hashCode()) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NULL(current)) return false;
@ -262,7 +300,12 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
@Override
public boolean remove(Object o) {
#if TYPE_OBJECT
if(o == null) return (containsNull ? removeNullIndex() : false);
#else
if(o == null) return false;
if(o instanceof CLASS_TYPE && KEY_EQUALS(CLASS_TO_KEY(o), EMPTY_KEY_VALUE)) return (containsNull ? removeNullIndex() : false);
#endif
int pos = HashUtil.mix(o.hashCode()) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NULL(current)) return false;
@ -303,10 +346,10 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
#endif
@Override
public boolean trim(int size) {
int newSize = HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor));
if(newSize >= nullIndex || size >= Math.min((int)Math.ceil(newSize * loadFactor), newSize - 1)) return false;
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= nullIndex || this.size >= Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false;
try {
rehash(newSize);
rehash(request);
}
catch(OutOfMemoryError e) { return false; }
return true;
@ -315,7 +358,7 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
@Override
public void clearAndTrim(int size) {
int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)));
if(request >= size) {
if(request >= nullIndex) {
clear();
return;
}
@ -327,6 +370,44 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
containsNull = false;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) a = new KEY_TYPE[size()];
if(containsNull) a[0] = EMPTY_KEY_VALUE;
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i])) a[index++] = keys[i];
}
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#endif
@Override
@Deprecated
public Object[] toArray() {
if(isEmpty()) return ObjectArrays.EMPTY_ARRAY;
Object[] obj = new Object[size()];
if(containsNull) obj[0] = KEY_TO_OBJ(EMPTY_KEY_VALUE);
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i])) obj[index++] = KEY_TO_OBJ(keys[i]);
}
return obj;
}
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size()];
else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size());
if(containsNull) a[0] = (E)KEY_TO_OBJ(EMPTY_KEY_VALUE);
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i])) a[index++] = (E)KEY_TO_OBJ(keys[i]);
}
if (a.length > size) a[size] = null;
return a;
}
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
if(size() <= 0) return;
@ -337,12 +418,22 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
}
@Override
public <E> void forEach(E input, BI_OBJECT_CONSUMER KKS_GENERIC_TYPE<E> action) {
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
if(containsNull) action.accept(keys[nullIndex], input);
if(containsNull) action.accept(0, keys[nullIndex]);
for(int i = nullIndex-1, index = containsNull ? 1 : 0;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i])) action.accept(index++, keys[i]);
}
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
if(size() <= 0) return;
if(containsNull) action.accept(input, keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i])) action.accept(keys[i], input);
if(KEY_EQUALS_NOT_NULL(keys[i])) action.accept(input, keys[i]);
}
}
@ -350,9 +441,9 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return false;
if(containsNull && filter.TEST_VALUE(keys[nullIndex])) return true;
if(containsNull && filter.test(keys[nullIndex])) return true;
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.TEST_VALUE(keys[i])) return true;
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return true;
}
return false;
}
@ -361,9 +452,9 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
if(containsNull && filter.TEST_VALUE(keys[nullIndex])) return false;
if(containsNull && filter.test(keys[nullIndex])) return false;
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.TEST_VALUE(keys[i])) return false;
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return false;
}
return true;
}
@ -372,24 +463,84 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return true;
if(containsNull && !filter.TEST_VALUE(keys[nullIndex])) return false;
if(containsNull && !filter.test(keys[nullIndex])) return false;
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && !filter.TEST_VALUE(keys[i])) return false;
if(KEY_EQUALS_NOT_NULL(keys[i]) && !filter.test(keys[i])) return false;
}
return true;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = identity;
if(containsNull) state = operator.APPLY_VALUE(state, keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NULL(keys[i])) continue;
state = operator.APPLY_VALUE(state, keys[i]);
}
return state;
}
#else
@Override
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
Objects.requireNonNull(operator);
KEY_SPECIAL_TYPE state = identity;
if(containsNull) state = operator.APPLY_VALUE(state, keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NULL(keys[i])) continue;
state = operator.APPLY_VALUE(state, keys[i]);
}
return state;
}
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE;
boolean empty = true;
if(containsNull) {
state = keys[nullIndex];
empty = false;
}
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NULL(keys[i])) continue;
if(empty) {
empty = false;
state = keys[i];
continue;
}
state = operator.APPLY_VALUE(state, keys[i]);
}
return state;
}
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE;
if(containsNull && filter.TEST_VALUE(keys[nullIndex])) return keys[nullIndex];
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex];
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.TEST_VALUE(keys[i])) return keys[i];
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return keys[i];
}
return EMPTY_VALUE;
}
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter);
if(size() <= 0) return 0;
int result = 0;
if(containsNull && filter.test(keys[nullIndex])) result++;
for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) result++;
}
return result;
}
private void ensureCapacity(int newCapacity) {
int size = HashUtil.arraySize(newCapacity, loadFactor);
if(size > nullIndex) rehash(size);
@ -449,7 +600,10 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
int newMask = newSize - 1;
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
for(int i = nullIndex, pos = 0, j = (size - (containsNull ? 1 : 0));j-- != 0;) {
while(KEY_EQUALS_NULL(keys[--i]));
while(true) {
if(--i < 0) throw new ConcurrentModificationException("Set was modified during rehash");
if(KEY_EQUALS_NOT_NULL(keys[i])) break;
}
if(KEY_EQUALS_NOT_NULL(newKeys[pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask]))
while(KEY_EQUALS_NOT_NULL(newKeys[pos = (++pos & newMask)]));
newKeys[pos] = keys[i];
@ -465,6 +619,19 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
return new SetIterator();
}
@Override
public HASH_SET KEY_GENERIC_TYPE copy() {
HASH_SET KEY_GENERIC_TYPE set = new HASH_SETBRACES(0, loadFactor);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
return set;
}
@Override
public void clear() {
if(size == 0) return;
@ -480,10 +647,12 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
private class SetIterator implements ITERATOR KEY_GENERIC_TYPE {
int pos = nullIndex;
int returnedPos = -1;
int lastReturned = -1;
int nextIndex = Integer.MIN_VALUE;
boolean returnNull = containsNull;
LIST KEY_GENERIC_TYPE wrapped = null;
KEY_TYPE[] wrapped = null;
int wrappedIndex = 0;
@Override
public boolean hasNext() {
@ -496,7 +665,7 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
{
while(true) {
if(--pos < 0) {
if(wrapped == null || wrapped.size() <= -pos - 1) break;
if(wrapped == null || wrappedIndex <= -pos - 1) break;
nextIndex = -pos - 1;
break;
}
@ -513,9 +682,10 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
returnedPos = pos;
if(nextIndex < 0){
lastReturned = Integer.MAX_VALUE;
KEY_TYPE value = wrapped.GET_KEY(nextIndex);
KEY_TYPE value = wrapped[nextIndex];
nextIndex = Integer.MIN_VALUE;
return value;
}
@ -531,9 +701,10 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
containsNull = false;
keys[nullIndex] = EMPTY_KEY_VALUE;
}
else if(pos >= 0) shiftKeys(pos);
else if(returnedPos >= 0) shiftKeys(returnedPos);
else {
HASH_SET.this.remove(wrapped.GET_KEY(-pos - 1));
HASH_SET.this.remove(wrapped[-returnedPos - 1]);
lastReturned = -1;
return;
}
size--;
@ -554,12 +725,19 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
if(last <= startPos ? (last >= slot || slot > startPos) : (last >= slot && slot > startPos)) break;
startPos = ++startPos & mask;
}
if(startPos < last) {
if(wrapped == null) wrapped = new ARRAY_LISTBRACES(2);
wrapped.add(keys[startPos]);
}
if(startPos < last) addWrapper(keys[startPos]);
keys[last] = current;
}
}
private void addWrapper(KEY_TYPE value) {
if(wrapped == null) wrapped = NEW_KEY_ARRAY(2);
else if(wrappedIndex >= wrapped.length) {
KEY_TYPE[] newArray = NEW_KEY_ARRAY(wrapped.length * 2);
System.arraycopy(wrapped, 0, newArray, 0, wrapped.length);
wrapped = newArray;
}
wrapped[wrappedIndex++] = value;
}
}
}

View File

@ -0,0 +1,116 @@
package speiger.src.collections.PACKAGE.sets;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
#if SETS_FEATURE
import speiger.src.collections.PACKAGE.utils.SETS;
#endif
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
/**
* A Special Set Interface giving Access to some really usefull functions
* The Idea behind this interface is to allow access to functions that give control to the Order of elements.
* Since Linked implementations as examples can be reordered outside of the Insertion Order.
* This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here.
*
* @Type(T)
*/
public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE
{
/**
* A customized add method that allows you to insert into the first index.
* @param o the element that should be inserted
* @return true if it was added
* @see java.util.Set#add(Object)
*/
public boolean addAndMoveToFirst(KEY_TYPE o);
/**
* A customized add method that allows you to insert into the last index.
* @param o the element that should be inserted
* @return true if it was added
* @see java.util.Set#add(Object)
*/
public boolean addAndMoveToLast(KEY_TYPE o);
/**
* A specific move method to move a given key to the first index.
* @param o that should be moved to the first index
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
*/
public boolean moveToFirst(KEY_TYPE o);
/**
* A specific move method to move a given key to the last index.
* @param o that should be moved to the first last
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
*/
public boolean moveToLast(KEY_TYPE o);
@Override
public ORDERED_SET KEY_GENERIC_TYPE copy();
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator();
/**
* A type Specific Iterator starting from a given key
* @param fromElement the element the iterator should start from
* @return a iterator starting from the given element
* @throws java.util.NoSuchElementException if fromElement isn't found
*/
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement);
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing
* @return type specific splititerator
*/
@Override
default SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createSplititerator(this, 0); }
/**
* A method to get the first element in the set
* @return first element in the set
*/
public KEY_TYPE FIRST_KEY();
/**
* A method to get and remove the first element in the set
* @return first element in the set
*/
public KEY_TYPE POLL_FIRST_KEY();
/**
* A method to get the last element in the set
* @return last element in the set
*/
public KEY_TYPE LAST_KEY();
/**
* A method to get and remove the last element in the set
* @return last element in the set
*/
public KEY_TYPE POLL_LAST_KEY();
#if SETS_FEATURE
/**
* Creates a Wrapped OrderedSet that is Synchronized
* @return a new OrderedSet that is synchronized
* @see SETS#synchronize
*/
public default ORDERED_SET KEY_GENERIC_TYPE synchronize() { return SETS.synchronize(this); }
/**
* Creates a Wrapped OrderedSet that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new OrderedSet Wrapper that is synchronized
* @see SETS#synchronize
*/
public default ORDERED_SET KEY_GENERIC_TYPE synchronize(Object mutex) { return SETS.synchronize(this, mutex); }
/**
* Creates a Wrapped OrderedSet that is unmodifiable
* @return a new OrderedSet Wrapper that is unmodifiable
* @see SETS#unmodifiable
*/
public default ORDERED_SET KEY_GENERIC_TYPE unmodifiable() { return SETS.unmodifiable(this); }
#endif
}

View File

@ -5,8 +5,12 @@ import java.util.Set;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
#if SETS_FEATURE
import speiger.src.collections.PACKAGE.utils.SETS;
#endif
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
/**
* A Type Specific Set class to reduce boxing/unboxing
* @Type(T)
@ -16,6 +20,9 @@ public interface SET KEY_GENERIC_TYPE extends Set<CLASS_TYPE>, COLLECTION KEY_GE
@Override
public ITERATOR KEY_GENERIC_TYPE iterator();
@Override
public SET KEY_GENERIC_TYPE copy();
#if !TYPE_OBJECT
/**
* A Type Specific remove function to reduce boxing/unboxing
@ -46,6 +53,40 @@ public interface SET KEY_GENERIC_TYPE extends Set<CLASS_TYPE>, COLLECTION KEY_GE
public default boolean remove(Object o) {
return COLLECTION.super.remove(o);
}
#else
/**
* A Helper method that allows to add a element or getting the already present implement.
* Allowing to make unique references reuseable.
* @param o the element to add
* @return either the inserted element or the present element.
*/
public KEY_TYPE addOrGet(KEY_TYPE o);
#endif
#if SETS_FEATURE
/**
* Creates a Wrapped Set that is Synchronized
* @return a new Set that is synchronized
* @see SETS#synchronize
*/
public default SET KEY_GENERIC_TYPE synchronize() { return SETS.synchronize(this); }
/**
* Creates a Wrapped Set that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new Set Wrapper that is synchronized
* @see SETS#synchronize
*/
public default SET KEY_GENERIC_TYPE synchronize(Object mutex) { return SETS.synchronize(this, mutex); }
/**
* Creates a Wrapped Set that is unmodifiable
* @return a new Set Wrapper that is unmodifiable
* @see SETS#unmodifiable
*/
public default SET KEY_GENERIC_TYPE unmodifiable() { return SETS.unmodifiable(this); }
#endif
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing

View File

@ -9,50 +9,20 @@ import java.util.Comparator;
#else
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
#if SETS_FEATURE
import speiger.src.collections.PACKAGE.utils.SETS;
#endif
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
/**
* A Type Specific SortedSet implementation to reduce boxing/unboxing
* with a couple extra methods that allow greater control over sets.
* @Type(T)
* @note ORDERED_SET is only extended until 0.6.0 for Compat reasons.
* The supported classes already implement ORDERED_SET directly and will remove SORTED_SET implementations in favor of ORDERED_SET instead
*/
public interface SORTED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, SortedSet<CLASS_TYPE>
{
/**
* A customized add method that allows you to insert into the first index.
* @param o the element that should be inserted
* @return true if it was added
* @see java.util.Set#add(Object)
* @note some implementations do not support this method
*/
public boolean addAndMoveToFirst(KEY_TYPE o);
/**
* A customized add method that allows you to insert into the last index.
* @param o the element that should be inserted
* @return true if it was added
* @see java.util.Set#add(Object)
* @note some implementations do not support this method
*/
public boolean addAndMoveToLast(KEY_TYPE o);
/**
* A specific move method to move a given key to the first index.
* @param o that should be moved to the first index
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
* @note some implementations do not support this method
*/
public boolean moveToFirst(KEY_TYPE o);
/**
* A specific move method to move a given key to the last index.
* @param o that should be moved to the first last
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
* @note some implementations do not support this method
*/
public boolean moveToLast(KEY_TYPE o);
/**
* A Type Specific Comparator method
* @return the type specific comparator
@ -60,6 +30,9 @@ public interface SORTED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, Sorte
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator();
@Override
public SORTED_SET KEY_GENERIC_TYPE copy();
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator();
/**
@ -70,6 +43,30 @@ public interface SORTED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, Sorte
*/
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement);
#if SETS_FEATURE
/**
* Creates a Wrapped SortedSet that is Synchronized
* @return a new SortedSet that is synchronized
* @see SETS#synchronize
*/
public default SORTED_SET KEY_GENERIC_TYPE synchronize() { return SETS.synchronize(this); }
/**
* Creates a Wrapped SortedSet that is Synchronized
* @param mutex is the controller of the synchronization block
* @return a new SortedSet Wrapper that is synchronized
* @see SETS#synchronize
*/
public default SORTED_SET KEY_GENERIC_TYPE synchronize(Object mutex) { return SETS.synchronize(this, mutex); }
/**
* Creates a Wrapped SortedSet that is unmodifiable
* @return a new SortedSet Wrapper that is unmodifiable
* @see SETS#unmodifiable
*/
public default SORTED_SET KEY_GENERIC_TYPE unmodifiable() { return SETS.unmodifiable(this); }
#endif
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing
* @return type specific splititerator

View File

@ -1,18 +1,21 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Arrays;
import java.util.Random;
#if JAVA_VERSION>=17
import java.util.random.RANDOM;
#else
import java.util.RANDOM;
#endif
import java.util.concurrent.RecursiveAction;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#else
import java.util.Comparator;
import java.util.function.IntFunction;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.utils.SanityChecks;
/**
@ -132,10 +135,41 @@ public class ARRAYS
* @return array with all requested elements of the iterator
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] pour(ITERATOR KEY_GENERIC_TYPE iter, int max) {
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
COLLECTIONS.CollectionWrapper KEY_GENERIC_TYPE list = COLLECTIONS.wrapper();
ITERATORS.pour(iter, list, max);
return list.TO_ARRAY(NEW_KEY_ARRAY(list.size()));
}
#if TYPE_OBJECT
/**
* A Helper function that pours all elements of a iterator into a Array
* @param iter the elements that should be gathered.
* @ArrayType(T)
* @ArrayType(E)
* @param action that is creating the Array to be poured into
* @return array with all elements of the iterator
*/
public static <T, E> E[] pour(ITERATOR KEY_GENERIC_TYPE iter, IntFunction<E[]> action) {
return pour(iter, Integer.MAX_VALUE, action);
}
/**
* A Helper function that pours all elements of a iterator into a Array
* @param iter the elements that should be gathered.
* @param max how many elements should be added
* @param action that is creating the Array to be poured into
* @ArrayType(T)
* @ArrayType(E)
* @return array with all requested elements of the iterator
*/
public static <T, E> E[] pour(ITERATOR KEY_GENERIC_TYPE iter, int max, IntFunction<E[]> action) {
COLLECTIONS.CollectionWrapper KEY_GENERIC_TYPE list = COLLECTIONS.wrapper();
ITERATORS.pour(iter, list, max);
return list.TO_ARRAY(action.apply(list.size()));
}
#endif
/**
* Method to validate if the current value is the lowest value in the heap
* @param data the current heap.
@ -262,7 +296,7 @@ public class ARRAYS
* @ArrayType(T)
* @return the provided sorted array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, Random random) {
public static GENERIC_KEY_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, RANDOM random) {
for(int i = array.length-1; i>=0;i--) {
int p = random.nextInt(i + 1);
KEY_TYPE t = array[i];
@ -280,7 +314,7 @@ public class ARRAYS
* @ArrayType(T)
* @return the provided sorted array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, int length, Random random) {
public static GENERIC_KEY_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, int length, RANDOM random) {
return shuffle(array, 0, length, random);
}
@ -293,7 +327,7 @@ public class ARRAYS
* @ArrayType(T)
* @return the provided sorted array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, int offset, int length, Random random) {
public static GENERIC_KEY_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, int offset, int length, RANDOM random) {
for(int i = length-1; i>=0;i--) {
int p = offset + random.nextInt(i + 1);
KEY_TYPE t = array[offset+i];
@ -348,9 +382,11 @@ public class ARRAYS
* @param array the array that needs to be sorted
* @ArrayType(T)
* @param comp the Comparator that decides the sorting order
* @return input array.
*/
public static GENERIC_KEY_BRACES void stableSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
public static GENERIC_KEY_BRACES KEY_TYPE[] stableSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
stableSort(array, 0, array.length, comp);
return array;
}
/**
@ -386,9 +422,11 @@ public class ARRAYS
* Stable sort referres to Mergesort or Insertionsort
* @param array the array that needs to be sorted
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void stableSort(KEY_TYPE[] array) {
public static GENERIC_KEY_BRACES KEY_TYPE[] stableSort(KEY_TYPE[] array) {
stableSort(array, 0, array.length);
return array;
}
/**
@ -423,9 +461,11 @@ public class ARRAYS
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void unstableSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
public static GENERIC_KEY_BRACES KEY_TYPE[] unstableSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
unstableSort(array, 0, array.length, comp);
return array;
}
/**
@ -461,9 +501,11 @@ public class ARRAYS
* Unstable sort referres to QuickSort or SelectionSort
* @param array the array that needs to be sorted
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void unstableSort(KEY_TYPE[] array) {
public static GENERIC_KEY_BRACES KEY_TYPE[] unstableSort(KEY_TYPE[] array) {
unstableSort(array, 0, array.length);
return array;
}
/**
@ -496,9 +538,11 @@ public class ARRAYS
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void insertionSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
public static GENERIC_KEY_BRACES KEY_TYPE[] insertionSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
insertionSort(array, 0, array.length, comp);
return array;
}
/**
@ -535,9 +579,11 @@ public class ARRAYS
* Sorts an array according to the natural ascending order using InsertionSort,
* @param array the array that needs to be sorted
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void insertionSort(KEY_TYPE[] array) {
public static GENERIC_KEY_BRACES KEY_TYPE[] insertionSort(KEY_TYPE[] array) {
insertionSort(array, 0, array.length);
return array;
}
/**
@ -573,9 +619,11 @@ public class ARRAYS
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void selectionSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
public static GENERIC_KEY_BRACES KEY_TYPE[] selectionSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
selectionSort(array, 0, array.length, comp);
return array;
}
/**
@ -617,9 +665,11 @@ public class ARRAYS
* Sorts an array according to the natural ascending order using Selection Sort,
* @param array the array that needs to be sorted
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void selectionSort(KEY_TYPE[] array) {
public static GENERIC_KEY_BRACES KEY_TYPE[] selectionSort(KEY_TYPE[] array) {
selectionSort(array, 0, array.length);
return array;
}
/**
@ -640,7 +690,7 @@ public class ARRAYS
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void selectionSort(KEY_TYPE[] array, int from, int to) {
for (int i = from; i < to; i++) {
for (int i = from,m=to-1; i < m; i++) {
KEY_TYPE min = array[i];
int minId = i;
for(int j = i+1; j < to; j++) {
@ -661,9 +711,11 @@ public class ARRAYS
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void mergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
public static GENERIC_KEY_BRACES KEY_TYPE[] mergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
mergeSort(array, null, 0, array.length, comp);
return array;
}
/**
@ -713,9 +765,11 @@ public class ARRAYS
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void mergeSort(KEY_TYPE[] array) {
public static GENERIC_KEY_BRACES KEY_TYPE[] mergeSort(KEY_TYPE[] array) {
mergeSort(array, null, 0, array.length);
return array;
}
/**
@ -922,9 +976,11 @@ public class ARRAYS
* @author Speiger
* @param array the array that needs to be sorted
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void memFreeMergeSort(KEY_TYPE[] array) {
public static GENERIC_KEY_BRACES KEY_TYPE[] memFreeMergeSort(KEY_TYPE[] array) {
memFreeMergeSort(array, 0, array.length);
return array;
}
/**
@ -1099,9 +1155,11 @@ public class ARRAYS
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void quickSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
public static GENERIC_KEY_BRACES KEY_TYPE[] quickSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
quickSort(array, 0, array.length, comp);
return array;
}
/**
@ -1157,9 +1215,11 @@ public class ARRAYS
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES void quickSort(KEY_TYPE[] array) {
public static GENERIC_KEY_BRACES KEY_TYPE[] quickSort(KEY_TYPE[] array) {
quickSort(array, 0, array.length);
return array;
}
/**

View File

@ -1,8 +1,22 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Arrays;
import java.util.Collection;
#if !TYPE_BOOLEAN
import java.util.ConcurrentModificationException;
#endif
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
#if TYPE_OBJECT
import java.util.Comparator;
import java.util.function.BiFunction;
#endif
import java.util.function.Predicate;
#if PRIMITIVES
#if JDK_FUNCTION && !TYPE_OBJECT
import java.util.function.PREDICATE;
#endif
#if PRIMITIVES && !JDK_FUNCTION
import java.util.function.JAVA_PREDICATE;
#endif
import java.util.function.Consumer;
@ -10,11 +24,24 @@ import java.util.function.Consumer;
import speiger.src.collections.PACKAGE.collections.ABSTRACT_COLLECTION;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.objects.utils.ObjectArrays;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !TYPE_BOOLEAN
import speiger.src.collections.utils.HashUtil;
#endif
import speiger.src.collections.utils.ITrimmable;
import speiger.src.collections.utils.SanityChecks;
/**
* A Helper class for Collections
@ -70,6 +97,578 @@ public class COLLECTIONS
return c instanceof SynchronizedCollection ? c : new SynchronizedCollectionBRACES(c, mutex);
}
/**
* Creates a Singleton Collection of a given element
* @param element the element that should be converted into a singleton collection
* @Type(T)
* @return a singletoncollection of the given element
*/
public static GENERIC_KEY_BRACES COLLECTION KEY_GENERIC_TYPE singleton(KEY_TYPE element) {
return new SingletonCollectionBRACES(element);
}
protected static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper() {
return new CollectionWrapperBRACES();
}
protected static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper(int size) {
return new CollectionWrapperBRACES(size);
}
#if !TYPE_BOOLEAN
protected static GENERIC_KEY_BRACES DistinctCollectionWrapper KEY_GENERIC_TYPE distinctWrapper() {
return new DistinctCollectionWrapperBRACES();
}
protected static GENERIC_KEY_BRACES DistinctCollectionWrapper KEY_GENERIC_TYPE distinctWrapper(int size) {
return new DistinctCollectionWrapperBRACES(size);
}
#endif
protected static class CollectionWrapper KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE implements ITrimmable {
KEY_TYPE[] elements;
int size = 0;
public CollectionWrapper() {
this(10);
}
public CollectionWrapper(int size) {
if(size < 0) throw new IllegalStateException("Size has to be 0 or greater");
elements = NEW_KEY_ARRAY(size);
}
@Override
public boolean add(KEY_TYPE o) {
if(size >= elements.length) elements = Arrays.copyOf(elements, (int)Math.min((long)elements.length + (elements.length >> 1), SanityChecks.MAX_ARRAY_SIZE));
elements[size++] = o;
return true;
}
public KEY_TYPE GET_KEY(int index) {
if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
return elements[index];
}
#if TYPE_OBJECT
@Override
public boolean remove(Object e) {
for(int i = 0;i<size;i++) {
if(KEY_EQUALS(elements[i], e)) {
removeIndex(i);
return true;
}
}
return false;
}
#else
@Override
public boolean REMOVE_KEY(KEY_TYPE e) {
for(int i = 0;i<size;i++) {
if(KEY_EQUALS(elements[i], e)) {
removeIndex(i);
return true;
}
}
return false;
}
#endif
private void removeIndex(int index) {
if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
size--;
if(index != size) System.arraycopy(elements, index+1, elements, index, size - index);
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return new ITERATOR KEY_GENERIC_TYPE() {
int index = 0;
int lastReturned = -1;
@Override
public boolean hasNext() {
return index < size;
}
@Override
public KEY_TYPE NEXT() {
int i = index++;
return elements[(lastReturned = i)];
}
@Override
public void remove() {
if(lastReturned == -1) throw new IllegalStateException();
removeIndex(lastReturned);
index = lastReturned;
lastReturned = -1;
}
};
}
@Override
public int size() {
return size;
}
@Override
public void clear() {
#if TYPE_OBJECT
for(int i = 0;i<size;elements[i] = null,i++);
#endif
size = 0;
}
#if TYPE_OBJECT
public void sort(Comparator<? super CLASS_TYPE> c) {
if(c != null) ARRAYS.stableSort(elements, size, c);
else ARRAYS.stableSort(elements, size);
}
public void unstableSort(Comparator<? super CLASS_TYPE> c) {
if(c != null) ARRAYS.unstableSort(elements, size, c);
else ARRAYS.unstableSort(elements, size);
}
#else
public void sort(COMPARATOR c) {
if(c != null) ARRAYS.stableSort(elements, size, c);
else ARRAYS.stableSort(elements, size);
}
public void unstableSort(COMPARATOR c) {
if(c != null) ARRAYS.unstableSort(elements, size, c);
else ARRAYS.unstableSort(elements, size);
}
#endif
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
for(int i = 0;i<size;i++)
action.accept(elements[i]);
}
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
Objects.requireNonNull(action);
for(int i = 0;i<size;i++)
action.accept(input, elements[i]);
}
@Override
public boolean trim(int size) {
if(size > size() || size() == elements.length) return false;
int value = Math.max(size, size());
elements = value == 0 ? EMPTY_KEY_ARRAY : Arrays.copyOf(elements, value);
return true;
}
@Override
public void clearAndTrim(int size) {
if(elements.length <= size) {
clear();
return;
}
elements = size == 0 ? EMPTY_KEY_ARRAY : NEW_KEY_ARRAY(size);
this.size = size;
}
@Override
@Primitive
public Object[] toArray() {
Object[] obj = new Object[size];
for(int i = 0;i<size;i++)
obj[i] = KEY_TO_OBJ(elements[i]);
return obj;
}
@Override
@Primitive
public <E> E[] toArray(E[] a) {
if(a == null) a = (E[])new Object[size];
else if(a.length < size) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size);
#if TYPE_OBJECT
System.arraycopy(elements, 0, a, 0, size);
#else
for(int i = 0;i<size;i++)
a[i] = (E)KEY_TO_OBJ(elements[i]);
#endif
if (a.length > size) a[size] = null;
return a;
}
#if !TYPE_OBJECT
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a.length < size) a = new KEY_TYPE[size];
System.arraycopy(elements, 0, a, 0, size);
if (a.length > size) a[size] = EMPTY_KEY_VALUE;
return a;
}
#endif
}
#if !TYPE_BOOLEAN
protected static class DistinctCollectionWrapper KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE {
KEY_TYPE[] keys;
boolean containsNull;
int minCapacity;
int nullIndex;
int maxFill;
int mask;
int size;
public DistinctCollectionWrapper() {
this(HashUtil.DEFAULT_MIN_CAPACITY);
}
public DistinctCollectionWrapper(int size) {
if(minCapacity < 0) throw new IllegalStateException("Minimum Capacity is negative. This is not allowed");
minCapacity = nullIndex = HashUtil.arraySize(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR);
mask = nullIndex - 1;
maxFill = Math.min((int)Math.ceil(nullIndex * HashUtil.DEFAULT_LOAD_FACTOR), nullIndex - 1);
keys = NEW_KEY_ARRAY(nullIndex + 1);
}
@Override
public boolean add(KEY_TYPE o) {
if(KEY_EQUALS_NULL(o)) {
if(containsNull) return false;
containsNull = true;
}
else {
int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NOT_NULL(current)) {
if(KEY_EQUALS(current, o)) return false;
while(KEY_EQUALS_NOT_NULL((current = keys[pos = (++pos & mask)])))
if(KEY_EQUALS(current, o)) return false;
}
keys[pos] = o;
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, HashUtil.DEFAULT_LOAD_FACTOR));
return true;
}
@Override
public boolean contains(Object o) {
#if TYPE_OBJECT
if(o == null) return containsNull;
#else
if(o == null) return false;
if(o instanceof CLASS_TYPE && KEY_EQUALS(CLASS_TO_KEY(o), EMPTY_KEY_VALUE)) return containsNull;
#endif
int pos = HashUtil.mix(o.hashCode()) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NULL(current)) return false;
if(EQUALS_KEY_TYPE(current, o)) return true;
while(true) {
if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
else if(EQUALS_KEY_TYPE(current, o)) return true;
}
}
@Override
public boolean remove(Object o) {
#if TYPE_OBJECT
if(o == null) return (containsNull ? removeNullIndex() : false);
#else
if(o == null) return false;
if(o instanceof CLASS_TYPE && KEY_EQUALS(CLASS_TO_KEY(o), EMPTY_KEY_VALUE)) return (containsNull ? removeNullIndex() : false);
#endif
int pos = HashUtil.mix(o.hashCode()) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NULL(current)) return false;
if(EQUALS_KEY_TYPE(current, o)) return removeIndex(pos);
while(true) {
if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
else if(EQUALS_KEY_TYPE(current, o)) return removeIndex(pos);
}
}
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) {
if(KEY_EQUALS_NULL(o)) return containsNull;
int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NULL(current)) return false;
if(KEY_EQUALS(current, o)) return true;
while(true) {
if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
else if(KEY_EQUALS(current, o)) return true;
}
}
@Override
public boolean REMOVE_KEY(KEY_TYPE o) {
if(KEY_EQUALS_NULL(o)) return containsNull ? removeNullIndex() : false;
int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
KEY_TYPE current = keys[pos];
if(KEY_EQUALS_NULL(current)) return false;
if(KEY_EQUALS(current, o)) return removeIndex(pos);
while(true) {
if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
else if(KEY_EQUALS(current, o)) return removeIndex(pos);
}
}
#endif
protected boolean removeIndex(int pos) {
if(pos == nullIndex) return containsNull ? removeNullIndex() : false;
keys[pos] = EMPTY_KEY_VALUE;
size--;
shiftKeys(pos);
if(nullIndex > minCapacity && size < maxFill / 4 && nullIndex > HashUtil.DEFAULT_MIN_CAPACITY) rehash(nullIndex / 2);
return true;
}
protected boolean removeNullIndex() {
containsNull = false;
keys[nullIndex] = EMPTY_KEY_VALUE;
size--;
if(nullIndex > minCapacity && size < maxFill / 4 && nullIndex > HashUtil.DEFAULT_MIN_CAPACITY) rehash(nullIndex / 2);
return true;
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return new SetIterator();
}
@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]);
}
}
@Override
public DistinctCollectionWrapper KEY_GENERIC_TYPE copy() {
DistinctCollectionWrapper KEY_GENERIC_TYPE set = new DistinctCollectionWrapperBRACES(0);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
return set;
}
protected void shiftKeys(int startPos) {
int slot, last;
KEY_TYPE current;
while(true) {
startPos = ((last = startPos) + 1) & mask;
while(true){
if(KEY_EQUALS_NULL((current = keys[startPos]))) {
keys[last] = EMPTY_KEY_VALUE;
return;
}
slot = HashUtil.mix(KEY_TO_HASH(current)) & mask;
if(last <= startPos ? (last >= slot || slot > startPos) : (last >= slot && slot > startPos)) break;
startPos = ++startPos & mask;
}
keys[last] = current;
}
}
protected void rehash(int newSize) {
int newMask = newSize - 1;
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
for(int i = nullIndex, pos = 0, j = (size - (containsNull ? 1 : 0));j-- != 0;) {
while(true) {
if(--i < 0) throw new ConcurrentModificationException("Set was modified during rehash");
if(KEY_EQUALS_NOT_NULL(keys[i])) break;
}
if(KEY_EQUALS_NOT_NULL(newKeys[pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask]))
while(KEY_EQUALS_NOT_NULL(newKeys[pos = (++pos & newMask)]));
newKeys[pos] = keys[i];
}
nullIndex = newSize;
mask = newMask;
maxFill = Math.min((int)Math.ceil(nullIndex * HashUtil.DEFAULT_LOAD_FACTOR), nullIndex - 1);
keys = newKeys;
}
@Override
public void clear() {
if(size == 0) return;
size = 0;
containsNull = false;
Arrays.fill(keys, EMPTY_KEY_VALUE);
}
@Override
public int size() {
return size;
}
private class SetIterator implements ITERATOR KEY_GENERIC_TYPE {
int pos = nullIndex;
int returnedPos = -1;
int lastReturned = -1;
int nextIndex = Integer.MIN_VALUE;
boolean returnNull = containsNull;
KEY_TYPE[] wrapped = null;
int wrappedIndex = 0;
@Override
public boolean hasNext() {
if(nextIndex == Integer.MIN_VALUE) {
if(returnNull) {
returnNull = false;
nextIndex = nullIndex;
}
else
{
while(true) {
if(--pos < 0) {
if(wrapped == null || wrappedIndex <= -pos - 1) break;
nextIndex = -pos - 1;
break;
}
if(KEY_EQUALS_NOT_NULL(keys[pos])){
nextIndex = pos;
break;
}
}
}
}
return nextIndex != Integer.MIN_VALUE;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
returnedPos = pos;
if(nextIndex < 0){
lastReturned = Integer.MAX_VALUE;
KEY_TYPE value = wrapped[nextIndex];
nextIndex = Integer.MIN_VALUE;
return value;
}
KEY_TYPE value = keys[(lastReturned = nextIndex)];
nextIndex = Integer.MIN_VALUE;
return value;
}
@Override
public void remove() {
if(lastReturned == -1) throw new IllegalStateException();
if(lastReturned == nullIndex) {
containsNull = false;
keys[nullIndex] = EMPTY_KEY_VALUE;
}
else if(returnedPos >= 0) shiftKeys(returnedPos);
else {
#if TYPE_OBJECT
DistinctCollectionWrapper.this.remove(wrapped[-returnedPos - 1]);
#else
DistinctCollectionWrapper.this.REMOVE_KEY(wrapped[-returnedPos - 1]);
#endif
lastReturned = -1;
return;
}
size--;
lastReturned = -1;
}
private void shiftKeys(int startPos) {
int slot, last;
KEY_TYPE current;
while(true) {
startPos = ((last = startPos) + 1) & mask;
while(true){
if(KEY_EQUALS_NULL((current = keys[startPos]))) {
keys[last] = EMPTY_KEY_VALUE;
return;
}
slot = HashUtil.mix(KEY_TO_HASH(current)) & mask;
if(last <= startPos ? (last >= slot || slot > startPos) : (last >= slot && slot > startPos)) break;
startPos = ++startPos & mask;
}
if(startPos < last) addWrapper(keys[startPos]);
keys[last] = current;
}
}
private void addWrapper(KEY_TYPE value) {
if(wrapped == null) wrapped = NEW_KEY_ARRAY(2);
else if(wrappedIndex >= wrapped.length) {
KEY_TYPE[] newArray = NEW_KEY_ARRAY(wrapped.length * 2);
System.arraycopy(wrapped, 0, newArray, 0, wrapped.length);
wrapped = newArray;
}
wrapped[wrappedIndex++] = value;
}
}
}
#endif
private static class SingletonCollection KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE
{
KEY_TYPE element;
SingletonCollection(KEY_TYPE element) {
this.element = element;
}
#if !TYPE_OBJECT
@Override
public boolean REMOVE_KEY(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
@Override
public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public ITERATOR KEY_GENERIC_TYPE iterator()
{
return new ITERATOR KEY_GENERIC_TYPE() {
boolean next = true;
@Override
public boolean hasNext() { return next; }
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
next = false;
return element;
}
};
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Collection))
return false;
Collection<?> l = (Collection<?>)o;
if(l.size() != size()) return false;
Iterator<?> iter = l.iterator();
if (iter.hasNext() && !Objects.equals(element, iter.next())) {
return false;
}
return !iter.hasNext();
}
@Override
public int hashCode() {
return KEY_TO_HASH(element);
}
@Override
public int size() { return 1; }
@Override
public SingletonCollection KEY_GENERIC_TYPE copy() { return new SingletonCollectionBRACES(element); }
}
/**
* Synchronized Collection Wrapper for the synchronizedCollection function
* @Type(T)
@ -92,18 +691,16 @@ public class COLLECTIONS
public boolean add(KEY_TYPE o) { synchronized(mutex) { return c.add(o); } }
@Override
public boolean addAll(Collection<? extends CLASS_TYPE> c) { synchronized(mutex) { return this.c.addAll(c); } }
@Override
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.addAll(c); } }
@Override
public boolean addAll(KEY_TYPE[] e, int offset, int length) { synchronized(mutex) { return c.addAll(e, offset, length); } }
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) { synchronized(mutex) { return c.contains(o); } }
#else
@Override
public boolean contains(Object o) { synchronized(mutex) { return c.contains(o); } }
#endif
@Override
@Primitive
@ -130,6 +727,9 @@ public class COLLECTIONS
return c.iterator();
}
@Override
public COLLECTION KEY_GENERIC_TYPE copy() { synchronized(mutex) { return c.copy(); } }
@Override
@Primitive
public boolean remove(Object o) { synchronized(mutex) { return c.remove(o); } }
@ -146,7 +746,11 @@ public class COLLECTIONS
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.removeAll(c); } }
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { synchronized(mutex) { return this.c.removeAll(c, r); } }
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.retainAll(c); } }
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { synchronized(mutex) { return this.c.retainAll(c, r); } }
#if PRIMITIVES
@Override
public boolean remIf(JAVA_PREDICATE filter){ synchronized(mutex) { return c.remIf(filter); } }
@ -173,12 +777,37 @@ public class COLLECTIONS
@Override
public void forEach(Consumer<? super CLASS_TYPE> action) { synchronized(mutex) { c.forEach(action); } }
#endif
@Override
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) { synchronized(mutex) { c.forEachIndexed(action); } }
@Override
public int hashCode() { synchronized(mutex) { return c.hashCode(); } }
@Override
public boolean equals(Object obj) { synchronized(mutex) { return c.equals(obj); } }
public boolean equals(Object obj) {
if(obj == this) return true;
synchronized(mutex) { return c.equals(obj); }
}
@Override
public String toString() { synchronized(mutex) { return c.toString(); } }
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) { synchronized(mutex) { c.forEach(input, action); } }
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesAny(filter); } }
@Override
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesNone(filter); } }
@Override
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesAll(filter); } }
#if TYPE_OBJECT
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) { synchronized(mutex) { return c.reduce(identity, operator); } }
#else
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(identity, operator); } }
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(operator); } }
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } }
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.count(filter); } }
}
/**
@ -198,6 +827,8 @@ public class COLLECTIONS
public boolean addAll(Collection<? extends CLASS_TYPE> c) { throw new UnsupportedOperationException(); }
@Override
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override
public boolean addAll(KEY_TYPE[] e, int offset, int length) { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) { return c.contains(o); }
@ -222,6 +853,8 @@ public class COLLECTIONS
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.unmodifiable(c.iterator()); }
@Override
public COLLECTION KEY_GENERIC_TYPE copy() { return c.copy(); }
@Override
@Deprecated
public boolean remove(Object o) { throw new UnsupportedOperationException(); }
@Override
@ -240,7 +873,11 @@ public class COLLECTIONS
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); }
#if PRIMITIVES
@Override
public boolean remIf(JAVA_PREDICATE filter){ throw new UnsupportedOperationException(); }
@ -267,12 +904,34 @@ public class COLLECTIONS
@Override
public void forEach(Consumer<? super CLASS_TYPE> action) { c.forEach(action); }
#endif
@Override
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) { c.forEachIndexed(action); }
@Override
public int hashCode() { return c.hashCode(); }
@Override
public boolean equals(Object obj) { return c.equals(obj); }
public boolean equals(Object obj) { return obj == this || c.equals(obj); }
@Override
public String toString() { return c.toString(); }
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) { c.forEach(input, action); }
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesAny(filter); }
@Override
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesNone(filter); }
@Override
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesAll(filter); }
#if TYPE_OBJECT
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) { return c.reduce(identity, operator); }
#else
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(identity, operator); }
#endif
@Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(operator); }
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); }
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) { return c.count(filter); }
}
/**
@ -285,12 +944,14 @@ public class COLLECTIONS
@Override
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override
public boolean addAll(KEY_TYPE[] e, int offset, int length) { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) { return false; }
@Override
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { return false; }
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { return c.isEmpty(); }
@Override
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { return false; }
#else
@ -302,7 +963,7 @@ public class COLLECTIONS
public boolean containsAny(Collection<?> c) { return false; }
@Override
@Primitive
public boolean containsAll(Collection<?> c) { return false; }
public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
@Override
public int hashCode() { return 0; }
@ -341,14 +1002,28 @@ public class COLLECTIONS
public Object[] toArray() { return ObjectArrays.EMPTY_ARRAY; }
#if !TYPE_OBJECT
@Override
public <T> T[] toArray(T[] a) { return a; }
public <T> T[] toArray(T[] a) {
if(a != null && a.length > 0)
a[0] = null;
return a;
}
@Override
public KEY_TYPE[] TO_ARRAY() { return ARRAYS.EMPTY_ARRAY; }
@Override
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { return a; }
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a != null && a.length > 0)
a[0] = EMPTY_KEY_VALUE;
return a;
}
#else
@Override
public <E> E[] toArray(E[] a) { return a; }
public <E> E[] toArray(E[] a) {
if(a != null && a.length > 0)
a[0] = EMPTY_KEY_VALUE;
return a;
}
#endif
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.empty(); }
@ -356,5 +1031,7 @@ public class COLLECTIONS
public void clear() {}
@Override
public int size() { return 0; }
@Override
public EmptyCollection KEY_GENERIC_TYPE copy() { return this; }
}
}

View File

@ -7,7 +7,9 @@ import speiger.src.collections.utils.IArray;
/**
* Type-Specific Helper class to get the underlying array of array implementations.
#if ARRAY_LIST_FEATURE
* @see speiger.src.collections.PACKAGE.lists.ARRAY_LIST
#endif
* @Type(T)
*/
public interface IARRAY KEY_GENERIC_TYPE extends IArray

View File

@ -1,19 +1,69 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Objects;
#if TYPE_BOOLEAN
import java.util.concurrent.atomic.AtomicInteger;
#else if TYPE_OBJECT
import java.util.Comparator;
#endif
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.ITERABLE;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.objects.collections.ObjectIterable;
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#else
#if BOOLEAN_COLLECTION_MODULE
import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.booleans.collections.BooleanIterable;
import speiger.src.collections.booleans.collections.BooleanIterator;
#endif
#iterate
#argument FILTER_TYPE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#argument CONSUMER ByteConsumer ShortConsumer IntConsumer LongConsumer FloatConsumer DoubleConsumer
#argument OUTPUT_ITERABLE ByteIterable ShortIterable IntIterable LongIterable FloatIterable DoubleIterable
#argument OUTPUT_ITERATOR ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument MAPPER ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument PACKAGE bytes shorts ints longs floats doubles
#if FILTER_TYPE
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.objects.functions.function.MAPPER;
import speiger.src.collections.PACKAGE.collections.OUTPUT_ITERABLE;
import speiger.src.collections.PACKAGE.collections.OUTPUT_ITERATOR;
#endif
#enditerate
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.utils.ISizeProvider;
/**
* A Helper class for Iterables
*/
public class ITERABLES
{
/**
* A Helper function that maps a Java-Iterable into a new Type.
* @param iterable the iterable that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterable that is mapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterable<E> map(Iterable<? extends CLASS_TYPE> iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E> mapper) {
return new MappedIterable<>(wrap(iterable), mapper);
}
/**
* A Helper function that maps a Iterable into a new Type.
* @param iterable the iterable that should be mapped
@ -26,6 +76,52 @@ public class ITERABLES
return new MappedIterable<>(iterable, mapper);
}
#if TYPE_OBJECT
#iterate
#argument MAPPED_TYPE MappedBoolean MappedByte MappedShort MappedInt MappedLong MappedFloat MappedDouble
#argument OUTPUT_ITERABLE BooleanIterable ByteIterable ShortIterable IntIterable LongIterable FloatIterable DoubleIterable
#argument MAPPER Predicate ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument DATA_TYPE Boolean Byte Short Int Long Float Double
#argument FILTER_TYPE BOOLEAN_COLLECTION_MODULE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
/**
* A Helper function that maps a Java-Iterable into a new Type.
* @param iterable the iterable that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @return a iterable that is mapped to a new result
*/
public static <T> OUTPUT_ITERABLE mapToDATA_TYPE(Iterable<? extends CLASS_TYPE> iterable, MAPPER<T> mapper) {
return new MAPPED_TYPEIterable<>(wrap(iterable), mapper);
}
/**
* A Helper function that maps a Iterable into a new Type.
* @param iterable the iterable that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @return a iterable that is mapped to a new result
*/
public static <T> OUTPUT_ITERABLE mapToDATA_TYPE(ITERABLE KEY_GENERIC_TYPE iterable, MAPPER<T> mapper) {
return new MAPPED_TYPEIterable<>(iterable, mapper);
}
#endif
#enditerate
#endif
/**
* A Helper function that flatMaps a Java-Iterable into a new Type.
* @param iterable the iterable that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <V> The return type supplier.
* @param <E> The return type.
* @return a iterable that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E, V extends Iterable<E>> ObjectIterable<E> flatMap(Iterable<? extends CLASS_TYPE> iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<V> mapper) {
return new FlatMappedIterable<>(wrap(iterable), mapper);
}
/**
* A Helper function that flatMaps a Iterable into a new Type.
* @param iterable the iterable that should be flatMapped
@ -39,6 +135,18 @@ public class ITERABLES
return new FlatMappedIterable<>(iterable, mapper);
}
/**
* A Helper function that flatMaps a Java-Iterable into a new Type.
* @param iterable the iterable that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterable that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterable<E> arrayFlatMap(Iterable<? extends CLASS_TYPE> iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E[]> mapper) {
return new FlatMappedArrayIterable<>(wrap(iterable), mapper);
}
/**
* A Helper function that flatMaps a Iterable into a new Type.
* @param iterable the iterable that should be flatMapped
@ -51,6 +159,17 @@ public class ITERABLES
return new FlatMappedArrayIterable<>(iterable, mapper);
}
/**
* A Helper function that filters out all desired elements from a Java-Iterable
* @param iterable that should be filtered.
* @param filter the filter that decides that should be let through
* @Type(T)
* @return a filtered iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE filter(Iterable<? extends CLASS_TYPE> iterable, PREDICATE KEY_GENERIC_TYPE filter) {
return new FilteredIterableBRACES(wrap(iterable), filter);
}
/**
* A Helper function that filters out all desired elements
* @param iterable that should be filtered.
@ -62,7 +181,200 @@ public class ITERABLES
return new FilteredIterableBRACES(iterable, filter);
}
private static class MappedIterable KSS_GENERIC_TYPE<E, T> implements ObjectIterable<T>
/**
* A Helper function that filters out all duplicated elements.
* @param iterable that should be distinct
* @Type(T)
* @return a distinct iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE distinct(ITERABLE KEY_GENERIC_TYPE iterable) {
return new DistinctIterableBRACES(iterable);
}
/**
* A Helper function that filters out all duplicated elements from a Java Iterable.
* @param iterable that should be distinct
* @Type(T)
* @return a distinct iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE distinct(Iterable<? extends CLASS_TYPE> iterable) {
return new DistinctIterableBRACES(wrap(iterable));
}
/**
* A Helper function that repeats the Iterable a specific amount of times
* @param iterable that should be repeated
* @param repeats the amount of times the iterable should be repeated
* @Type(T)
* @return a repeating iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE repeat(ITERABLE KEY_GENERIC_TYPE iterable, int repeats) {
return new RepeatingIterableBRACES(iterable, repeats);
}
/**
* A Helper function that repeats the Iterable a specific amount of times from a Java Iterable
* @param iterable that should be repeated
* @param repeats the amount of times the iterable should be repeated
* @Type(T)
* @return a repeating iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE repeat(Iterable<? extends CLASS_TYPE> iterable, int repeats) {
return new RepeatingIterableBRACES(wrap(iterable), repeats);
}
/**
* A Helper function that hard limits the Iterable to a specific size
* @param iterable that should be limited
* @param limit the amount of elements it should be limited to
* @Type(T)
* @return a limited iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE limit(ITERABLE KEY_GENERIC_TYPE iterable, long limit) {
return new LimitedIterableBRACES(iterable, limit);
}
/**
* A Helper function that hard limits the Iterable to a specific size from a Java Iterable
* @param iterable that should be limited
* @param limit the amount of elements it should be limited to
* @Type(T)
* @return a limited iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE limit(Iterable<? extends CLASS_TYPE> iterable, long limit) {
return new LimitedIterableBRACES(wrap(iterable), limit);
}
/**
* A Helper function that sorts the Iterable.
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
* @param iterable that should be sorted
* @param sorter that sorts the iterable. Can be null.
* @Type(T)
* @return a sorted iterable.
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE sorted(ITERABLE KEY_GENERIC_TYPE iterable, COMPARATOR KEY_GENERIC_TYPE sorter) {
return new SortedIterableBRACES(iterable, sorter);
}
/**
* A Helper function that sorts the Iterable from a Java Iterable
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
* @param iterable that should be sorted
* @param sorter that sorts the iterable. Can be null.
* @Type(T)
* @return a sorted iterable.
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE sorted(Iterable<? extends CLASS_TYPE> iterable, COMPARATOR KEY_GENERIC_TYPE sorter) {
return new SortedIterableBRACES(wrap(iterable), sorter);
}
/**
* A Helper function that allows to preview the result of a Iterable.
* @param iterable that should be peeked at
* @param action callback that receives the value before the iterable returns it
* @Type(T)
* @return a peeked iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE peek(ITERABLE KEY_GENERIC_TYPE iterable, CONSUMER KEY_GENERIC_TYPE action) {
return new PeekIterableBRACES(iterable, action);
}
/**
* A Helper function that allows to preview the result of a Iterable from a Java Iterable
* @param iterable that should be peeked at
* @param action callback that receives the value before the iterable returns it
* @Type(T)
* @return a peeked iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE peek(Iterable<? extends CLASS_TYPE> iterable, CONSUMER KEY_GENERIC_TYPE action) {
return new PeekIterableBRACES(wrap(iterable), action);
}
/**
* A Wrapper function that wraps a Java-Iterable into a Type Specific Iterable
* @param iterable that should be wrapped
* @Type(T)
* @return a type specific iterable
*/
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE wrap(Iterable<? extends CLASS_TYPE> iterable) {
return new WrappedIterableBRACES(iterable);
}
private static class WrappedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
{
Iterable<? extends CLASS_TYPE> iterable;
public WrappedIterable(Iterable<? extends CLASS_TYPE> iterable) {
this.iterable = iterable;
}
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.wrap(iterable.iterator());
}
@Override
public int size() {
ISizeProvider prov = ISizeProvider.of(iterable);
return prov == null ? -1 : prov.size();
}
#if !TYPE_OBJECT
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
iterable.forEach(action);
}
#else
public void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
iterable.forEach(action);
}
#endif
}
#if TYPE_OBJECT
#iterate
#argument CONSUMER BooleanConsumer ByteConsumer ShortConsumer IntConsumer LongConsumer FloatConsumer DoubleConsumer
#argument MAPPED_TYPE MappedBoolean MappedByte MappedShort MappedInt MappedLong MappedFloat MappedDouble
#argument OUTPUT_ITERABLE BooleanIterable ByteIterable ShortIterable IntIterable LongIterable FloatIterable DoubleIterable
#argument OUTPUT_ITERATOR BooleanIterator ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument MAPPER Predicate ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument APPLY test applyAsByte applyAsShort applyAsInt applyAsLong applyAsFloat applyAsDouble
#argument DATA_TYPE Boolean Byte Short Int Long Float Double
#argument FILTER_TYPE BOOLEAN_COLLECTION_MODULE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
private static class MAPPED_TYPEIterable<E> implements OUTPUT_ITERABLE, ISizeProvider
{
ITERABLE KEY_SPECIAL_GENERIC_TYPE<E> iterable;
MAPPER<E> mapper;
MAPPED_TYPEIterable(ITERABLE<E> iterable, MAPPER<E> mapper) {
this.iterable = iterable;
this.mapper = mapper;
}
public OUTPUT_ITERATOR iterator() {
return ITERATORS.mapToDATA_TYPE(iterable.iterator(), mapper);
}
@Override
public int size() {
ISizeProvider prov = ISizeProvider.of(this);
return prov == null ? -1 : prov.size();
}
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
iterable.forEach(E -> action.accept(mapper.APPLY(E)));
}
}
#endif
#enditerate
#endif
private static class MappedIterable KSS_GENERIC_TYPE<E, T> implements ObjectIterable<T>, ISizeProvider
{
ITERABLE KEY_SPECIAL_GENERIC_TYPE<E> iterable;
TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, T> mapper;
@ -75,6 +387,18 @@ public class ITERABLES
public ObjectIterator<T> iterator() {
return ITERATORS.map(iterable.iterator(), mapper);
}
@Override
public int size() {
ISizeProvider prov = ISizeProvider.of(this);
return prov == null ? -1 : prov.size();
}
@Override
public void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
iterable.forEach(E -> action.accept(mapper.apply(E)));
}
}
private static class FlatMappedIterable KSS_GENERIC_TYPE<E, T,[SPACE]V extends Iterable<T>> implements ObjectIterable<T>
@ -91,6 +415,13 @@ public class ITERABLES
public ObjectIterator<T> iterator() {
return ITERATORS.flatMap(iterable.iterator(), mapper);
}
@Override
public void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
iterable.forEach(E -> mapper.apply(E).forEach(action));
}
}
private static class FlatMappedArrayIterable KSS_GENERIC_TYPE<E, T> implements ObjectIterable<T>
@ -107,6 +438,57 @@ public class ITERABLES
public ObjectIterator<T> iterator() {
return ITERATORS.arrayFlatMap(iterable.iterator(), mapper);
}
@Override
public void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
iterable.forEach(E -> {
T[] array = mapper.apply(E);
for(int i = 0,m=array.length;i<m;action.accept(array[i++]));
});
}
}
private static class RepeatingIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
{
ITERABLE KEY_GENERIC_TYPE iterable;
int repeats;
public RepeatingIterable(ITERABLE KEY_GENERIC_TYPE iterable, int repeats) {
this.iterable = iterable;
this.repeats = repeats;
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.repeat(iterable.iterator(), repeats);
}
@Override
public int size() {
ISizeProvider prov = ISizeProvider.of(iterable);
return prov == null ? -1 : prov.size() * (repeats+1);
}
#if !TYPE_OBJECT
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
COLLECTION KEY_GENERIC_TYPE repeater = COLLECTIONS.wrapper();
iterable.forEach(action.andThen(repeater::add));
for(int i = 0;i<repeats;i++)
repeater.forEach(action);
}
#else
@Override
public void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
COLLECTION KEY_GENERIC_TYPE repeater = COLLECTIONS.wrapper();
iterable.forEach(T -> {action.accept(T); repeater.add(T);});
for(int i = 0;i<repeats;i++)
repeater.forEach(action);
}
#endif
}
private static class FilteredIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
@ -123,5 +505,178 @@ public class ITERABLES
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.filter(iterable.iterator(), filter);
}
#if !TYPE_OBJECT
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
iterable.forEach(T -> { if(!filter.test(T)) action.accept(T); } );
}
#else
public void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
iterable.forEach(T -> { if(!filter.test(T)) action.accept(T); } );
}
#endif
}
private static class LimitedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
{
ITERABLE KEY_GENERIC_TYPE iterable;
long limit;
public LimitedIterable(ITERABLE KEY_GENERIC_TYPE iterable, long limit) {
this.iterable = iterable;
this.limit = limit;
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.limit(iterable.iterator(), limit);
}
@Override
public int size() {
ISizeProvider prov = ISizeProvider.of(iterable);
return prov == null ? -1 : (int)Math.min(prov.size(), limit);
}
#if !TYPE_OBJECT
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
AtomicLong counter = new AtomicLong();
iterable.forEach(T -> {
if(counter.get() >= limit) return;
counter.incrementAndGet();
action.accept(T);
});
}
#else
public void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
AtomicLong counter = new AtomicLong();
iterable.forEach(T -> {
if(counter.get() >= limit) return;
counter.incrementAndGet();
action.accept(T);
});
}
#endif
}
private static class SortedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
{
ITERABLE KEY_GENERIC_TYPE iterable;
COMPARATOR KEY_GENERIC_TYPE sorter;
public SortedIterable(ITERABLE KEY_GENERIC_TYPE iterable, COMPARATOR KEY_GENERIC_TYPE sorter) {
this.iterable = iterable;
this.sorter = sorter;
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.sorted(iterable.iterator(), sorter);
}
@Override
public int size() {
ISizeProvider prov = ISizeProvider.of(iterable);
return prov == null ? -1 : prov.size();
}
#if !TYPE_OBJECT
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
COLLECTIONS.CollectionWrapper KEY_GENERIC_TYPE wrapper = COLLECTIONS.wrapper();
iterable.forEach(wrapper::add);
wrapper.unstableSort(sorter);
wrapper.forEach(action);
}
#else
@Override
public void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
COLLECTIONS.CollectionWrapper KEY_GENERIC_TYPE wrapper = COLLECTIONS.wrapper();
iterable.forEach(wrapper::add);
wrapper.unstableSort(sorter);
wrapper.forEach(action);
}
#endif
}
private static class DistinctIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
{
ITERABLE KEY_GENERIC_TYPE iterable;
public DistinctIterable(ITERABLE KEY_GENERIC_TYPE iterable) {
this.iterable = iterable;
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.distinct(iterable.iterator());
}
#if !TYPE_OBJECT
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
#if TYPE_BOOLEAN
AtomicInteger result = new AtomicInteger();
iterable.forEach(T -> {
if(((result.get() & (T ? 2 : 1)) != 0)) return;
result.getAndAdd(T ? 2 : 1);
action.accept(T);
});
#else
COLLECTION KEY_GENERIC_TYPE filtered = COLLECTIONS.distinctWrapper();
iterable.forEach(T -> { if(filtered.add(T)) action.accept(T); });
#endif
}
#else
public void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
COLLECTION KEY_GENERIC_TYPE filtered = COLLECTIONS.distinctWrapper();
iterable.forEach(T -> { if(filtered.add(T)) action.accept(T); });
}
#endif
}
private static class PeekIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
{
ITERABLE KEY_GENERIC_TYPE iterable;
CONSUMER KEY_GENERIC_TYPE action;
public PeekIterable(ITERABLE KEY_GENERIC_TYPE iterable, CONSUMER KEY_GENERIC_TYPE action) {
this.iterable = iterable;
this.action = action;
}
@Override
public ITERATOR KEY_GENERIC_TYPE iterator() {
return ITERATORS.peek(iterable.iterator(), action);
}
@Override
public int size() {
ISizeProvider prov = ISizeProvider.of(iterable);
return prov == null ? -1 : prov.size();
}
#if !TYPE_OBJECT
@Override
public void forEach(CONSUMER action) {
Objects.requireNonNull(action);
iterable.forEach(this.action.andThen(action));
}
#else
public void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
iterable.forEach(this.action.andThen(action));
}
#endif
}
}

View File

@ -2,19 +2,53 @@ package speiger.src.collections.PACKAGE.utils;
import java.util.Iterator;
import java.util.NoSuchElementException;
#if TYPE_OBJECT
import java.util.Comparator;
#endif
import java.util.function.Consumer;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.utils.ObjectIterators;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#else
#if BOOLEAN_COLLECTION_MODULE
import speiger.src.collections.booleans.collections.BooleanIterator;
#endif
#iterate
#argument ITERATOR ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument PACKAGE bytes shorts ints longs floats doubles
#argument MAPPER ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument FILTER_TYPE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if BOOLEAN_COLLECTION_MODULE
import speiger.src.collections.objects.functions.function.MAPPER;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#endif
#enditerate
#endif
import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
#endif
#if ARRAY_LIST_FEATURE || LINKED_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.LIST;
#if ARRAY_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
#else if LINKED_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.LINKED_LIST;
#endif
#endif
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.utils.COLLECTIONS.CollectionWrapper;
/**
* A Helper class for Iterators
@ -24,7 +58,7 @@ public class ITERATORS
/**
* Empty Iterator Reference
*/
public static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES();
private static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES();
/**
* Returns a Immutable EmptyIterator instance that is automatically casted.
@ -89,6 +123,19 @@ public class ITERATORS
return iterator instanceof UnmodifiableListIterator ? iterator : new UnmodifiableListIteratorBRACES(iterator);
}
#if OBJECT_COLLECTION_MODULE
/**
* A Helper function that maps a Java-Iterator into a new Type.
* @param iterator that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterator that is mapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterator<E> map(Iterator<? extends CLASS_TYPE> iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E> mapper) {
return new MappedIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that maps a Iterator into a new Type.
* @param iterator that should be mapped
@ -101,6 +148,53 @@ public class ITERATORS
return new MappedIterator<>(iterator, mapper);
}
#endif
#if TYPE_OBJECT
#iterate
#argument MAPPED_TYPE MappedBoolean MappedByte MappedShort MappedInt MappedLong MappedFloat MappedDouble
#argument OUTPUT_ITERATOR BooleanIterator ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument MAPPER Predicate ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument DATA_TYPE Boolean Byte Short Int Long Float Double
#argument FILTER_TYPE BOOLEAN_COLLECTION_MODULE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
/**
* A Helper function that maps a Java-Iterator into a new Type.
* @param iterator that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @return a iterator that is mapped to a new result
*/
public static <T> OUTPUT_ITERATOR mapToDATA_TYPE(Iterator<? extends CLASS_TYPE> iterator, MAPPER<T> mapper) {
return new MAPPED_TYPEIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that maps a Iterator into a new Type.
* @param iterator that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @return a iterator that is mapped to a new result
*/
public static <T> OUTPUT_ITERATOR mapToDATA_TYPE(ITERATOR KEY_GENERIC_TYPE iterator, MAPPER<T> mapper) {
return new MAPPED_TYPEIterator<>(iterator, mapper);
}
#endif
#enditerate
#endif
/**
* A Helper function that flatMaps a Java-Iterator into a new Type.
* @param iterator that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <V> The return type supplier.
* @param <E> The return type.
* @return a iterator that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E, V extends Iterable<E>> ObjectIterator<E> flatMap(Iterator<? extends CLASS_TYPE> iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<V> mapper) {
return new FlatMappedIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that flatMaps a Iterator into a new Type.
* @param iterator that should be flatMapped
@ -114,6 +208,18 @@ public class ITERATORS
return new FlatMappedIterator<>(iterator, mapper);
}
/**
* A Helper function that flatMaps a Java-Iterator into a new Type.
* @param iterator that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterator that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterator<E> arrayFlatMap(Iterator<? extends CLASS_TYPE> iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E[]> mapper) {
return new FlatMappedArrayIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that flatMaps a Iterator into a new Type.
* @param iterator that should be flatMapped
@ -126,6 +232,17 @@ public class ITERATORS
return new FlatMappedArrayIterator<>(iterator, mapper);
}
/**
* A Helper function that filters out all desired elements from a Java-Iterator
* @param iterator that should be filtered.
* @param filter the filter that decides that should be let through
* @Type(T)
* @return a filtered iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE filter(Iterator<? extends CLASS_TYPE> iterator, PREDICATE KEY_GENERIC_TYPE filter) {
return new FilteredIteratorBRACES(wrap(iterator), filter);
}
/**
* A Helper function that filters out all desired elements
* @param iterator that should be filtered.
@ -137,6 +254,136 @@ public class ITERATORS
return new FilteredIteratorBRACES(iterator, filter);
}
/**
* A Helper function that filters out all duplicated elements.
* @param iterator that should be distinct
* @Type(T)
* @return a distinct iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE distinct(ITERATOR KEY_GENERIC_TYPE iterator) {
return new DistinctIteratorBRACES(iterator);
}
/**
* A Helper function that filters out all duplicated elements from a Java Iterator.
* @param iterator that should be distinct
* @Type(T)
* @return a distinct iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE distinct(Iterator<? extends CLASS_TYPE> iterator) {
return new DistinctIteratorBRACES(wrap(iterator));
}
/**
* A Helper function that repeats the Iterator a specific amount of times
* @param iterator that should be repeated
* @param repeats the amount of times the iterator should be repeated
* @Type(T)
* @return a repeating iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE repeat(ITERATOR KEY_GENERIC_TYPE iterator, int repeats) {
return new RepeatingIteratorBRACES(iterator, repeats);
}
/**
* A Helper function that repeats the Iterator a specific amount of times from a Java Iterator
* @param iterator that should be repeated
* @param repeats the amount of times the iterator should be repeated
* @Type(T)
* @return a repeating iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE repeat(Iterator<? extends CLASS_TYPE> iterator, int repeats) {
return new RepeatingIteratorBRACES(wrap(iterator), repeats);
}
/**
* A Helper function that creates a infinitely looping iterator
* @param iterator that should be looping infinitely
* @Type(T)
* @return a infinitely looping iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE infinite(ITERATOR KEY_GENERIC_TYPE iterator) {
return new InfiniteIteratorBRACES(iterator);
}
/**
* A Helper function that creates a infinitely looping iterator from a Java Iterator
* @param iterator that should be looping infinitely
* @Type(T)
* @return a infinitely looping iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE infinite(Iterator<? extends CLASS_TYPE> iterator) {
return new InfiniteIteratorBRACES(wrap(iterator));
}
/**
* A Helper function that hard limits the Iterator to a specific size
* @param iterator that should be limited
* @param limit the amount of elements it should be limited to
* @Type(T)
* @return a limited iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE limit(ITERATOR KEY_GENERIC_TYPE iterator, long limit) {
return new LimitedIteratorBRACES(iterator, limit);
}
/**
* A Helper function that hard limits the Iterator to a specific size from a Java Iterator
* @param iterator that should be limited
* @param limit the amount of elements it should be limited to
* @Type(T)
* @return a limited iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE limit(Iterator<? extends CLASS_TYPE> iterator, long limit) {
return new LimitedIteratorBRACES(wrap(iterator), limit);
}
/**
* A Helper function that sorts the Iterator beforehand.
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
* @param iterator that should be sorted.
* @param sorter the sorter of the iterator. Can be null.
* @Type(T)
* @return a new sorted iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE sorted(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE sorter) {
return new SortedIteratorBRACES(iterator, sorter);
}
/**
* A Helper function that sorts the Iterator beforehand from a Java Iterator.
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
* @param iterator that should be sorted.
* @param sorter the sorter of the iterator. Can be null.
* @Type(T)
* @return a new sorted iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE sorted(Iterator<? extends CLASS_TYPE> iterator, COMPARATOR KEY_GENERIC_TYPE sorter) {
return new SortedIteratorBRACES(wrap(iterator), sorter);
}
/**
* A Helper function that allows to preview the result of a Iterator.
* @param iterator that should be peeked at
* @param action callback that receives the value before the iterator returns it
* @Type(T)
* @return a peeked iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE peek(ITERATOR KEY_GENERIC_TYPE iterator, CONSUMER KEY_GENERIC_TYPE action) {
return new PeekIteratorBRACES(iterator, action);
}
/**
* A Helper function that allows to preview the result of a Iterator from a Java Iterator
* @param iterator that should be peeked at
* @param action callback that receives the value before the iterator returns it
* @Type(T)
* @return a peeked iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE peek(Iterator<? extends CLASS_TYPE> iterator, CONSUMER KEY_GENERIC_TYPE action) {
return new PeekIteratorBRACES(wrap(iterator), action);
}
/**
* Helper function to convert a Object Iterator into a Primitive Iterator
* @param iterator that should be converted to a unboxing iterator
@ -153,7 +400,7 @@ public class ITERATORS
* @ArrayType(T)
* @return a Iterator that is wrapping a array.
*/
public static GENERIC_KEY_BRACES ArrayIterator KEY_GENERIC_TYPE wrap(KEY_TYPE[] a) {
public static GENERIC_KEY_BRACES ArrayIterator KEY_GENERIC_TYPE wrap(KEY_TYPE... a) {
return wrap(a, 0, a.length);
}
@ -300,6 +547,8 @@ public class ITERATORS
}
#endif
#if LIST_MODULE
#if ARRAY_LIST_FEATURE || LINKED_LIST_FEATURE
/**
* A Helper function to pours all elements of a Iterator into a List
* @param iter the elements that should be poured into list.
@ -318,12 +567,20 @@ public class ITERATORS
* @return A list of all requested elements of the Iterator
*/
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE pour(ITERATOR KEY_GENERIC_TYPE iter, int max) {
#if ARRAY_LIST_FEATURE
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
#else
LINKED_LIST KEY_GENERIC_TYPE list = new LINKED_LISTBRACES();
#endif
pour(iter, list, max);
#if ARRAY_LIST_FEATURE
list.trim();
#endif
return list;
}
#endif
#endif
/**
* A Helper function to pours all elements of a Iterator into a Collection
* @param iter the elements that should be poured into list.
@ -583,41 +840,21 @@ public class ITERATORS
private static class EmptyIterator KEY_GENERIC_TYPE implements LIST_ITERATOR KEY_GENERIC_TYPE
{
@Override
public boolean hasNext() {
return false;
}
public boolean hasNext() { return false; }
@Override
public KEY_TYPE NEXT() {
return EMPTY_KEY_VALUE;
}
public KEY_TYPE NEXT() { throw new NoSuchElementException(); }
@Override
public boolean hasPrevious() {
return false;
}
public boolean hasPrevious() { return false; }
@Override
public KEY_TYPE PREVIOUS() {
return EMPTY_KEY_VALUE;
}
public KEY_TYPE PREVIOUS() { throw new NoSuchElementException(); }
@Override
public int nextIndex() {
return 0;
}
public int nextIndex() { return 0; }
@Override
public int previousIndex() {
return 0;
}
public int previousIndex() { return -1; }
@Override
public void remove() { throw new UnsupportedOperationException(); }
@Override
public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
}
@ -641,6 +878,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return a[from++];
}
@ -670,7 +908,7 @@ public class ITERATORS
@Override
public T next() {
return mapper.GET_VALUE(iterator.NEXT());
return mapper.apply(iterator.NEXT());
}
@Override
@ -679,6 +917,45 @@ public class ITERATORS
}
}
#if TYPE_OBJECT
#iterate
#argument MAPPED_TYPE MappedBoolean MappedByte MappedShort MappedInt MappedLong MappedFloat MappedDouble
#argument NEXT_TYPE nextBoolean nextByte nextShort nextInt nextLong nextFloat nextDouble
#argument OUTPUT_ITERATOR BooleanIterator ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument MAPPER Predicate ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument APPLY test applyAsByte applyAsShort applyAsInt applyAsLong applyAsFloat applyAsDouble
#argument DATA_TYPE boolean byte short int long float double
#argument FILTER_TYPE BOOLEAN_COLLECTION_MODULE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
private static class MAPPED_TYPEIterator<E> implements OUTPUT_ITERATOR
{
ITERATOR<E> iterator;
MAPPER<E> mapper;
MAPPED_TYPEIterator(ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator, MAPPER<E> mapper) {
this.iterator = iterator;
this.mapper = mapper;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public DATA_TYPE NEXT_TYPE() {
return mapper.APPLY(iterator.NEXT());
}
@Override
public int skip(int amount) {
return iterator.skip(amount);
}
}
#endif
#enditerate
#endif
private static class FlatMappedIterator KSS_GENERIC_TYPE<E, T,[SPACE]V extends Iterable<T>> implements ObjectIterator<T>
{
ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator;
@ -696,7 +973,7 @@ public class ITERATORS
foundNext = true;
while(iterator.hasNext()) {
if(last != null && last.hasNext()) return;
last = mapper.GET_VALUE(iterator.NEXT()).iterator();
last = mapper.apply(iterator.NEXT()).iterator();
}
}
@ -708,7 +985,7 @@ public class ITERATORS
@Override
public T next() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
T result = last.next();
foundNext = false;
return result;
@ -732,7 +1009,7 @@ public class ITERATORS
foundNext = true;
while(iterator.hasNext()) {
if(last != null && last.hasNext()) return;
last = ObjectIterators.wrap(mapper.GET_VALUE(iterator.NEXT()));
last = ObjectIterators.wrap(mapper.apply(iterator.NEXT()));
}
}
@ -744,13 +1021,165 @@ public class ITERATORS
@Override
public T next() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
T result = last.next();
foundNext = false;
return result;
}
}
private static class InfiniteIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iter;
CollectionWrapper KEY_GENERIC_TYPE looper = COLLECTIONS.wrapper();
int index = 0;
public InfiniteIterator(ITERATOR KEY_GENERIC_TYPE iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
return true;
}
@Override
public KEY_TYPE NEXT() {
if(iter != null) {
if(iter.hasNext()) {
KEY_TYPE value = iter.NEXT();
looper.add(value);
return value;
}
else iter = null;
}
return looper.GET_KEY((index++) % looper.size());
}
#if !TYPE_OBJECT
@Override
public void forEachRemaining(CONSUMER action) { throw new UnsupportedOperationException("This is a instant deadlock, so unsupported"); }
#endif
public void forEachRemaining(Consumer<? super CLASS_TYPE> action) { throw new UnsupportedOperationException("This is a instant deadlock, so unsupported"); }
public <E> void forEachRemaining(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) { throw new UnsupportedOperationException("This is a instant deadlock, so unsupported"); }
}
private static class RepeatingIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
final int repeats;
int index = 0;
ITERATOR KEY_GENERIC_TYPE iter;
COLLECTION KEY_GENERIC_TYPE repeater = COLLECTIONS.wrapper();
public RepeatingIterator(ITERATOR KEY_GENERIC_TYPE iter, int repeat) {
this.iter = iter;
this.repeats = repeat;
}
@Override
public boolean hasNext() {
if(iter.hasNext()) return true;
if(index < repeats) {
index++;
iter = repeater.iterator();
return iter.hasNext();
}
return false;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE value = iter.NEXT();
if(index == 0) repeater.add(value);
return value;
}
}
private static class SortedIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
COMPARATOR KEY_GENERIC_TYPE sorter;
COLLECTIONS.CollectionWrapper KEY_GENERIC_TYPE sortedElements = null;
int index = 0;
public SortedIterator(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE sorter) {
this.iterator = iterator;
this.sorter = sorter;
}
@Override
public boolean hasNext() {
if(sortedElements == null) {
boolean hasNext = iterator.hasNext();
if(hasNext) {
sortedElements = COLLECTIONS.wrapper();
pour(iterator, sortedElements);
}
else sortedElements = COLLECTIONS.wrapper();
if(hasNext) sortedElements.unstableSort(sorter);
}
return index < sortedElements.size();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return sortedElements.GET_KEY(index++);
}
}
private static class DistinctIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
#if TYPE_BOOLEAN
int filtered;
#else
COLLECTION KEY_GENERIC_TYPE filtered = COLLECTIONS.distinctWrapper();
#endif
KEY_TYPE lastFound;
boolean foundNext = false;
public DistinctIterator(ITERATOR KEY_GENERIC_TYPE iterator) {
this.iterator = iterator;
}
void compute() {
if(foundNext) return;
#if TYPE_BOOLEAN
if(filtered == 3) return;
#endif
while(iterator.hasNext()) {
lastFound = iterator.NEXT();
#if TYPE_BOOLEAN
if((filtered & (lastFound ? 1 : 2)) == 0) {
filtered |= (lastFound ? 1 : 2);
foundNext = true;
break;
}
#else
if(filtered.add(lastFound)) {
foundNext = true;
break;
}
#endif
}
}
@Override
public boolean hasNext() {
compute();
return foundNext;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
foundNext = false;
return lastFound;
}
}
private static class FilteredIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
@ -767,7 +1196,7 @@ public class ITERATORS
if(foundNext) return;
while(iterator.hasNext()) {
lastFound = iterator.NEXT();
if(filter.TEST_VALUE(lastFound)) {
if(filter.test(lastFound)) {
foundNext = true;
break;
}
@ -782,9 +1211,56 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
foundNext = false;
return lastFound;
}
}
private static class LimitedIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
long limit;
public LimitedIterator(ITERATOR KEY_GENERIC_TYPE iterator, long limit) {
this.iterator = iterator;
this.limit = limit;
}
@Override
public boolean hasNext() {
return limit > 0 && iterator.hasNext();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
limit--;
return iterator.NEXT();
}
}
private static class PeekIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
CONSUMER KEY_GENERIC_TYPE action;
public PeekIterator(ITERATOR KEY_GENERIC_TYPE iterator, CONSUMER KEY_GENERIC_TYPE action) {
this.iterator = iterator;
this.action = action;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE result = iterator.NEXT();
action.accept(result);
return result;
}
}
}

View File

@ -1,10 +1,22 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Collection;
import java.util.List;
#if IARRAY_FEATURE
import java.util.Objects;
import java.util.Random;
#endif
#if JAVA_VERSION>=17
import java.util.random.RANDOM;
#else
import java.util.RANDOM;
#endif
import java.util.RandomAccess;
#if IARRAY_FEATURE || TYPE_OBJECT
import java.util.function.Consumer;
#endif
#if PRIMITIVES
import java.nio.JAVA_BUFFER;
#endif
import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
@ -12,8 +24,10 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.lists.ABSTRACT_LIST;
import speiger.src.collections.PACKAGE.lists.LIST;
#if INT_LIST_MODULE && !TYPE_INT
import speiger.src.collections.ints.lists.IntList;
#endif
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
import speiger.src.collections.utils.SanityChecks;
/**
@ -24,7 +38,7 @@ public class LISTS
/**
* Empty List reference
*/
public static final EmptyList NO_GENERIC_TYPE EMPTY = new EmptyListBRACES();
private static final EmptyList NO_GENERIC_TYPE EMPTY = new EmptyListBRACES();
/**
* Returns a Immutable EmptyList instance that is automatically casted.
@ -56,7 +70,11 @@ public class LISTS
* @return a synchronized list wrapper. If the list is implementing RandomAccess or IARRAY that is also transferred. If the List already a synchronized wrapper then it just returns itself.
*/
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE synchronize(LIST KEY_GENERIC_TYPE l) {
#if IARRAY_FEATURE
return l instanceof SynchronizedList ? l : (l instanceof IARRAY ? new SynchronizedArrayListBRACES(l) : (l instanceof RandomAccess ? new SynchronizedRandomAccessListBRACES(l) : new SynchronizedListBRACES(l)));
#else
return l instanceof SynchronizedList ? l : (l instanceof RandomAccess ? new SynchronizedRandomAccessListBRACES(l) : new SynchronizedListBRACES(l));
#endif
}
/**
@ -67,7 +85,11 @@ public class LISTS
* @return a synchronized list wrapper. If the list is implementing RandomAccess or IARRAY that is also transferred. If the List already a synchronized wrapper then it just returns itself.
*/
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE synchronize(LIST KEY_GENERIC_TYPE l, Object mutex) {
#if IARRAY_FEATURE
return l instanceof SynchronizedList ? l : (l instanceof IARRAY ? new SynchronizedArrayListBRACES(l, mutex) : (l instanceof RandomAccess ? new SynchronizedRandomAccessListBRACES(l, mutex) : new SynchronizedListBRACES(l, mutex)));
#else
return l instanceof SynchronizedList ? l : (l instanceof RandomAccess ? new SynchronizedRandomAccessListBRACES(l, mutex) : new SynchronizedListBRACES(l, mutex));
#endif
}
/**
@ -88,6 +110,7 @@ public class LISTS
*/
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE reverse(LIST KEY_GENERIC_TYPE list) {
int size = list.size();
#if IARRAY_FEATURE
if(list instanceof IARRAY) {
IARRAY KEY_GENERIC_TYPE array = (IARRAY KEY_GENERIC_TYPE)list;
#if TYPE_OBJECT
@ -102,6 +125,7 @@ public class LISTS
return list;
#endif
}
#endif
if(list instanceof RandomAccess) {
for (int i = 0, mid = size >> 1, j = size - 1; i < mid; i++, j--) {
KEY_TYPE t = list.GET_KEY(i);
@ -137,8 +161,9 @@ public class LISTS
* @Type(T)
* @return the input list
*/
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE shuffle(LIST KEY_GENERIC_TYPE list, Random random) {
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE shuffle(LIST KEY_GENERIC_TYPE list, RANDOM random) {
int size = list.size();
#if IARRAY_FEATURE
if(list instanceof IARRAY) {
IARRAY KEY_GENERIC_TYPE array = (IARRAY KEY_GENERIC_TYPE)list;
#if TYPE_OBJECT
@ -160,7 +185,8 @@ public class LISTS
#endif
return list;
}
for(int i = list.size(); i-- != 0;) {
#endif
for(int i = size; i-- != 0;) {
int p = random.nextInt(i + 1);
KEY_TYPE t = list.GET_KEY(i);
list.set(i, list.GET_KEY(p));
@ -197,6 +223,8 @@ public class LISTS
@Override
public KEY_TYPE REMOVE(int index) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE swapRemove(int index) { throw new UnsupportedOperationException(); }
@Override
public void addElements(int from, KEY_TYPE[] a, int offset, int length) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) {
@ -204,6 +232,10 @@ public class LISTS
a[offset] = element;
return a;
}
#if PRIMITIVES
@Override
public void fillBuffer(JAVA_BUFFER buffer) { buffer.put(element); }
#endif
@Override
public void removeElements(int from, int to) { throw new UnsupportedOperationException(); }
@ -216,8 +248,12 @@ public class LISTS
#endif
@Override
public int size() { return 1; }
@Override
public SingletonList KEY_GENERIC_TYPE copy() { return new SingletonListBRACES(element); }
}
#if IARRAY_FEATURE
private static class SynchronizedArrayList KEY_GENERIC_TYPE extends SynchronizedList KEY_GENERIC_TYPE implements IARRAY KEY_GENERIC_TYPE
{
IARRAY KEY_GENERIC_TYPE l;
@ -258,6 +294,7 @@ public class LISTS
}
}
#endif
private static class SynchronizedRandomAccessList KEY_GENERIC_TYPE extends SynchronizedList KEY_GENERIC_TYPE implements RandomAccess
{
SynchronizedRandomAccessList(LIST KEY_GENERIC_TYPE l) {
@ -321,6 +358,12 @@ public class LISTS
@Override
public KEY_TYPE REMOVE(int index) { synchronized(mutex) { return l.REMOVE(index); } }
@Override
public KEY_TYPE swapRemove(int index) { synchronized(mutex) { return l.swapRemove(index); } }
@Override
public boolean REMOVE_SWAP(KEY_TYPE e) { synchronized(mutex) { return l.REMOVE_SWAP(e); } }
@Override
@Primitive
public int indexOf(Object e) { synchronized(mutex) { return l.indexOf(e); } }
@ -338,7 +381,7 @@ public class LISTS
#endif
@Override
public void addElements(int from, KEY_TYPE[] a, int offset, int length) { synchronized(mutex) { addElements(from, a, offset, length); } }
public void addElements(int from, KEY_TYPE[] a, int offset, int length) { synchronized(mutex) { l.addElements(from, a, offset, length); } }
@Override
public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) { synchronized(mutex) { return l.getElements(from, a, offset, length); } }
@ -354,6 +397,9 @@ public class LISTS
@Override
public <K> K[] extractElements(int from, int to, Class<K> clz) { synchronized(mutex) { return l.extractElements(from, to, clz); } }
#endif
#if PRIMITIVES
public void fillBuffer(JAVA_BUFFER buffer) { synchronized(mutex) { l.fillBuffer(buffer); } }
#endif
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator() {
@ -365,13 +411,33 @@ public class LISTS
return l.listIterator(index);
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(int...indecies) {
return l.indexedIterator(indecies);
}
#if INT_LIST_MODULE
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(IntList indecies) {
return l.indexedIterator(indecies);
}
#endif
@Override
public LIST KEY_GENERIC_TYPE subList(int from, int to) {
return synchronize(l.subList(from, to));
return LISTS.synchronize(l.subList(from, to));
}
@Override
public LIST KEY_GENERIC_TYPE reversed() {
return LISTS.synchronize(l.reversed());
}
@Override
public void size(int size) { synchronized(mutex) { l.size(size); } }
@Override
public LIST KEY_GENERIC_TYPE copy() { synchronized(mutex) { return l.copy(); } }
}
private static class UnmodifiableRandomList KEY_GENERIC_TYPE extends UnmodifiableList KEY_GENERIC_TYPE implements RandomAccess
@ -428,6 +494,12 @@ public class LISTS
@Override
public KEY_TYPE REMOVE(int index) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE swapRemove(int index) { throw new UnsupportedOperationException(); }
@Override
public boolean REMOVE_SWAP(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
@Primitive
public int indexOf(Object e) { return l.indexOf(e); }
@ -463,6 +535,10 @@ public class LISTS
@Override
public <K> K[] extractElements(int from, int to, Class<K> clz) { throw new UnsupportedOperationException(); }
#endif
#if PRIMITIVES
public void fillBuffer(JAVA_BUFFER buffer) { l.fillBuffer(buffer); }
#endif
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE listIterator() {
@ -474,13 +550,33 @@ public class LISTS
return ITERATORS.unmodifiable(l.listIterator(index));
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(int...indecies) {
return ITERATORS.unmodifiable(l.indexedIterator(indecies));
}
#if INT_LIST_MODULE
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(IntList indecies) {
return ITERATORS.unmodifiable(l.indexedIterator(indecies));
}
#endif
@Override
public LIST KEY_GENERIC_TYPE subList(int from, int to) {
return unmodifiable(l.subList(from, to));
return LISTS.unmodifiable(l.subList(from, to));
}
@Override
public LIST KEY_GENERIC_TYPE reversed() {
return LISTS.unmodifiable(l.reversed());
}
@Override
public void size(int size) { throw new UnsupportedOperationException(); }
@Override
public LIST KEY_GENERIC_TYPE copy() { return l.copy(); }
}
private static class EmptyList KEY_GENERIC_TYPE extends COLLECTIONS.EmptyCollection KEY_GENERIC_TYPE implements LIST KEY_GENERIC_TYPE
@ -514,6 +610,12 @@ public class LISTS
@Override
public KEY_TYPE REMOVE(int index) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE swapRemove(int index) { throw new UnsupportedOperationException(); }
@Override
public boolean REMOVE_SWAP(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public int indexOf(Object e) { return -1; }
@ -558,10 +660,38 @@ public class LISTS
return ITERATORS.empty();
}
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(int...indecies) {
return ITERATORS.empty();
}
#if INT_LIST_MODULE
@Override
public LIST_ITERATOR KEY_GENERIC_TYPE indexedIterator(IntList indecies) {
return ITERATORS.empty();
}
#endif
@Override
public int hashCode() { return 1; }
@Override
public boolean equals(Object o) {
if(o == this) return true;
if(!(o instanceof List)) return false;
return ((List<?>)o).isEmpty();
}
@Override
public LIST KEY_GENERIC_TYPE subList(int from, int to) { throw new UnsupportedOperationException(); }
@Override
public LIST KEY_GENERIC_TYPE reversed() { return this; }
@Override
public void size(int size) { throw new UnsupportedOperationException(); }
@Override
public EmptyList KEY_GENERIC_TYPE copy() { return this; }
}
}

View File

@ -1,15 +1,30 @@
package speiger.src.collections.PACKAGE.utils;
#if TYPE_OBJECT
import java.util.Collection;
import java.util.Comparator;
import java.util.function.Consumer;
#endif
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
#if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE;
#endif
import speiger.src.collections.PACKAGE.queues.PRIORITY_QUEUE;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
/**
* A Helper class for PriorityQueues
@ -38,6 +53,7 @@ public class PRIORITY_QUEUES
return queue instanceof SynchronizedPriorityQueue ? (SynchronizedPriorityQueue KEY_GENERIC_TYPE)queue : new SynchronizedPriorityQueueBRACES(queue, mutex);
}
#if DEQUEUE_FEATURE
/**
* Returns a synchronized PriorityDequeue instance based on the instance given.
* @param dequeue that should be synchronized
@ -59,6 +75,7 @@ public class PRIORITY_QUEUES
return dequeue instanceof SynchronizedPriorityDequeue ? (SynchronizedPriorityDequeue KEY_GENERIC_TYPE)dequeue : new SynchronizedPriorityDequeueBRACES(dequeue, mutex);
}
#endif
/**
* Wrapper class for synchronization
* @Type(T)
@ -86,11 +103,21 @@ public class PRIORITY_QUEUES
public void clear() { synchronized(mutex) { queue.clear(); } }
@Override
public void enqueue(KEY_TYPE e) { synchronized(mutex) { queue.enqueue(e); } }
@Override
public void enqueueAll(KEY_TYPE[] e, int offset, int length) { synchronized(mutex) { queue.enqueueAll(e, offset, length); } }
@Override
public void enqueueAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { queue.enqueueAll(c); } }
#if TYPE_OBJECT
@Override
public void enqueueAll(Collection<? extends CLASS_TYPE> c) { synchronized(mutex) { queue.enqueueAll(c); } }
#endif
@Override
public KEY_TYPE dequeue() { synchronized(mutex) { return queue.dequeue(); } }
@Override
public KEY_TYPE peek(int index) { synchronized(mutex) { return queue.peek(index); } }
@Override
public boolean contains(KEY_TYPE e) { synchronized(mutex) { return queue.contains(e); } }
@Override
public boolean removeFirst(KEY_TYPE e) { synchronized(mutex) { return queue.removeFirst(e); } }
@Override
public boolean removeLast(KEY_TYPE e) { synchronized(mutex) { return queue.removeLast(e); } }
@ -100,8 +127,25 @@ public class PRIORITY_QUEUES
public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() { synchronized(mutex) { return queue.comparator(); } }
@Override
public GENERIC_SPECIAL_KEY_BRACES<E> KEY_SPECIAL_TYPE[] TO_ARRAY(KEY_SPECIAL_TYPE[] input) { synchronized(mutex) { return queue.TO_ARRAY(input); } }
@Override
public PRIORITY_QUEUE KEY_GENERIC_TYPE copy() { synchronized(mutex) { return queue.copy(); } }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) { synchronized(mutex) { queue.forEach(action); } }
@Override
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) { synchronized(mutex) { queue.forEach(input, action); } }
@Override
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesAny(filter); } }
@Override
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesNone(filter); } }
@Override
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesAll(filter); } }
@Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.findFirst(filter); } }
@Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } }
}
#if DEQUEUE_FEATURE
/**
* Wrapper class for synchronization
* @Type(T)
@ -121,7 +165,18 @@ public class PRIORITY_QUEUES
@Override
public void enqueueFirst(KEY_TYPE e) { synchronized(mutex) { dequeue.enqueueFirst(e); } }
@Override
public void enqueueAllFirst(KEY_TYPE[] e, int offset, int length) { synchronized(mutex) { dequeue.enqueueAllFirst(e, offset, length); } }
@Override
public void enqueueAllFirst(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { dequeue.enqueueAllFirst(c); } }
#if TYPE_OBJECT
@Override
public void enqueueAllFirst(Collection<? extends CLASS_TYPE> c) { synchronized(mutex) { dequeue.enqueueAllFirst(c); } }
#endif
@Override
public KEY_TYPE dequeueLast() { synchronized(mutex) { return dequeue.dequeueLast(); } }
@Override
public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy() { synchronized(mutex) { return dequeue.copy(); } }
}
#endif
}

View File

@ -1,29 +1,31 @@
package speiger.src.collections.PACKAGE.utils;
#if TYPE_BOOLEAN
import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.sets.AbstractBooleanSet;
import speiger.src.collections.booleans.sets.BooleanSet;
import speiger.src.collections.booleans.utils.BooleanCollections.EmptyCollection;
#else
#if TYPE_OBJECT
import java.util.NoSuchElementException;
import java.util.Set;
#if TYPE_OBJECT && SORTED_SET_FEATURE
import java.util.Comparator;
#endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT
#if !TYPE_OBJECT && SORTED_SET_FEATURE
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if SORTED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET;
#endif
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SET;
#if ORDERED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
#endif
#if SORTED_SET_FEATURE
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
#endif
import speiger.src.collections.PACKAGE.utils.COLLECTIONS.EmptyCollection;
import speiger.src.collections.PACKAGE.utils.COLLECTIONS.SynchronizedCollection;
import speiger.src.collections.PACKAGE.utils.COLLECTIONS.UnmodifiableCollection;
import speiger.src.collections.utils.ITrimmable;
#endif
/**
* A Helper class for sets
@ -33,7 +35,7 @@ public class SETS
/**
* Empty Set Variable
*/
public static final SET NO_GENERIC_TYPE EMPTY = new EmptySetBRACES();
private static final SET NO_GENERIC_TYPE EMPTY = new EmptySetBRACES();
/**
* EmptySet getter
@ -48,7 +50,6 @@ public class SETS
#endif
}
#if !TYPE_BOOLEAN
/**
* Creates a Synchronized set while preserving the ITrimmable interface
* @param s the set that should be synchronized
@ -72,6 +73,7 @@ public class SETS
return s instanceof SynchronizedSet ? s : (s instanceof ITrimmable ? new SynchronizedTrimSetBRACES(s, mutex) : new SynchronizedSetBRACES(s, mutex));
}
#if SORTED_SET_FEATURE
/**
* Creates a Synchronized SortedSet while preserving the ITrimmable interface
* @param s the set that should be synchronized
@ -95,6 +97,33 @@ public class SETS
return s instanceof SynchronizedSortedSet ? s : (s instanceof ITrimmable ? new SynchronizedSortedTrimSetBRACES(s, mutex) : new SynchronizedSortedSetBRACES(s, mutex));
}
#endif
#if ORDERED_SET_FEATURE
/**
* Creates a Synchronized OrderedSet while preserving the ITrimmable interface
* @param s the set that should be synchronized
* @Type(T)
* @return a OrderedSet that is synchronized
* @note if the set is already synchronized then it will just self return it
*/
public static GENERIC_KEY_BRACES ORDERED_SET KEY_GENERIC_TYPE synchronize(ORDERED_SET KEY_GENERIC_TYPE s) {
return s instanceof SynchronizedOrderedSet ? s : (s instanceof ITrimmable ? new SynchronizedOrderedTrimSetBRACES(s) : new SynchronizedOrderedSetBRACES(s));
}
/**
* Creates a Synchronized OrderedSet while preserving the ITrimmable interface
* @param s the set that should be synchronized
* @param mutex controller for access
* @Type(T)
* @return a OrderedSet that is synchronized
* @note if the set is already synchronized then it will just self return it
*/
public static GENERIC_KEY_BRACES ORDERED_SET KEY_GENERIC_TYPE synchronize(ORDERED_SET KEY_GENERIC_TYPE s, Object mutex) {
return s instanceof SynchronizedOrderedSet ? s : (s instanceof ITrimmable ? new SynchronizedOrderedTrimSetBRACES(s, mutex) : new SynchronizedOrderedSetBRACES(s, mutex));
}
#endif
#if SORTED_SET_FEATURE
/**
* Creates a Synchronized NavigableSet while preserving the ITrimmable interface
* @param s the set that should be synchronized
@ -118,6 +147,7 @@ public class SETS
return s instanceof SynchronizedNavigableSet ? s : (s instanceof ITrimmable ? new SynchronizedNavigableTrimSetBRACES(s, mutex) : new SynchronizedNavigableSetBRACES(s, mutex));
}
#endif
/**
* Creates Unmodifyable Set wrapper
* @param s set that should be made unmodifiable
@ -128,6 +158,7 @@ public class SETS
return s instanceof UnmodifiableSet ? s : new UnmodifiableSetBRACES(s);
}
#if SORTED_SET_FEATURE
/**
* Creates Unmodifyable SortedSet wrapper
* @param s sortedSet that should be made unmodifiable
@ -138,6 +169,20 @@ public class SETS
return s instanceof UnmodifiableSortedSet ? s : new UnmodifiableSortedSetBRACES(s);
}
#endif
#if ORDERED_SET_FEATURE
/**
* Creates Unmodifyable OrderedSet wrapper
* @param s OrderedSet that should be made unmodifiable
* @Type(T)
* @return a UnmodifyableOrderedSet, if the set is already unmodifiable then it returns itself
*/
public static GENERIC_KEY_BRACES ORDERED_SET KEY_GENERIC_TYPE unmodifiable(ORDERED_SET KEY_GENERIC_TYPE s) {
return s instanceof UnmodifiableOrderedSet ? s : new UnmodifiableOrderedSetBRACES(s);
}
#endif
#if SORTED_SET_FEATURE
/**
* Creates Unmodifyable NavigableSet wrapper
* @param s navigableSet that should be made unmodifiable
@ -173,16 +218,19 @@ public class SETS
#endif
@Override
public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#if TYPE_OBJECT
public KEY_TYPE addOrGet(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
@Override
public ITERATOR KEY_GENERIC_TYPE iterator()
{
return new ITERATOR KEY_GENERIC_TYPE() {
boolean next = true;
@Override
public boolean hasNext() { return next = false; }
public boolean hasNext() { return next; }
@Override
public KEY_TYPE NEXT() {
if(!next) throw new IllegalStateException();
if(!hasNext()) throw new NoSuchElementException();
next = false;
return element;
}
@ -190,6 +238,9 @@ public class SETS
}
@Override
public int size() { return 1; }
@Override
public SingletonSet KEY_GENERIC_TYPE copy() { return new SingletonSetBRACES(element); }
}
private static class EmptySet KEY_GENERIC_TYPE extends EmptyCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE
@ -197,10 +248,22 @@ public class SETS
#if !TYPE_OBJECT
@Override
public boolean remove(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#else
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
@Override
public boolean equals(Object o) {
if(o == this) return true;
if(!(o instanceof Set)) return false;
return ((Set<?>)o).isEmpty();
}
#if !TYPE_BOOLEAN
@Override
public EmptySet KEY_GENERIC_TYPE copy() { return this; }
}
#if SORTED_SET_FEATURE
private static class UnmodifiableNavigableSet KEY_GENERIC_TYPE extends UnmodifiableSortedSet KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
{
NAVIGABLE_SET KEY_GENERIC_TYPE n;
@ -213,25 +276,38 @@ public class SETS
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) { return n.contains(o); }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE lower(CLASS_TYPE e) { return n.lower(e); }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE floor(CLASS_TYPE e) { return n.floor(e); }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE ceiling(CLASS_TYPE e) { return n.ceiling(e); }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE higher(CLASS_TYPE e) { return n.higher(e); }
#endif
@Override
@Deprecated
public boolean contains(Object o) { return n.contains(o); }
@Override
public KEY_TYPE lower(KEY_TYPE e) { return n.lower(e); }
@Override
public KEY_TYPE floor(KEY_TYPE e) { return n.floor(e); }
@Override
public KEY_TYPE ceiling(KEY_TYPE e) { return n.ceiling(e); }
@Override
public KEY_TYPE higher(KEY_TYPE e) { return n.higher(e); }
#if !TYPE_OBJECT
@Override
public CLASS_TYPE pollFirst() { throw new UnsupportedOperationException(); }
@Override
public CLASS_TYPE pollLast() { throw new UnsupportedOperationException(); }
@Override
public void setDefaultMaxValue(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@ -246,30 +322,70 @@ public class SETS
#endif
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { return unmodifiable(n.subSet(fromElement, fromInclusive, toElement, toInclusive)); }
public NAVIGABLE_SET KEY_GENERIC_TYPE copy() { return n.copy(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { return unmodifiable(n.headSet(toElement, inclusive)); }
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { return SETS.unmodifiable(n.subSet(fromElement, fromInclusive, toElement, toInclusive)); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { return unmodifiable(n.tailSet(fromElement, inclusive)); }
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { return SETS.unmodifiable(n.headSet(toElement, inclusive)); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { return SETS.unmodifiable(n.tailSet(fromElement, inclusive)); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return ITERATORS.unmodifiable(n.descendingIterator()); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return unmodifiable(n.descendingSet()); }
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return SETS.unmodifiable(n.descendingSet()); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return unmodifiable(n.subSet(fromElement, toElement)); }
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return SETS.unmodifiable(n.subSet(fromElement, toElement)); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return unmodifiable(n.headSet(toElement)); }
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return SETS.unmodifiable(n.headSet(toElement)); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return unmodifiable(n.tailSet(fromElement)); }
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return SETS.unmodifiable(n.tailSet(fromElement)); }
}
#endif
#if ORDERED_SET_FEATURE
private static class UnmodifiableOrderedSet KEY_GENERIC_TYPE extends UnmodifiableSet KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE
{
ORDERED_SET KEY_GENERIC_TYPE s;
UnmodifiableOrderedSet(ORDERED_SET KEY_GENERIC_TYPE c)
{
super(c);
s = c;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.unmodifiable(s.iterator()); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { return ITERATORS.unmodifiable(s.iterator(fromElement)); }
@Override
public ORDERED_SET KEY_GENERIC_TYPE copy() { return s.copy(); }
@Override
public KEY_TYPE FIRST_KEY() { return s.FIRST_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_KEY() { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE LAST_KEY() { return s.LAST_KEY(); }
@Override
public KEY_TYPE POLL_LAST_KEY() { throw new UnsupportedOperationException(); }
}
#endif
#if SORTED_SET_FEATURE
private static class UnmodifiableSortedSet KEY_GENERIC_TYPE extends UnmodifiableSet KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
{
SORTED_SET KEY_GENERIC_TYPE s;
@ -279,49 +395,31 @@ public class SETS
s = c;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return s.comparator(); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.unmodifiable(s.iterator()); }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { return ITERATORS.unmodifiable(s.iterator(fromElement)); }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return unmodifiable(s.subSet(fromElement, toElement)); }
public SORTED_SET KEY_GENERIC_TYPE copy() { return s.copy(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return unmodifiable(s.headSet(toElement)); }
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return SETS.unmodifiable(s.subSet(fromElement, toElement)); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return unmodifiable(s.tailSet(fromElement)); }
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return SETS.unmodifiable(s.headSet(toElement)); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return SETS.unmodifiable(s.tailSet(fromElement)); }
@Override
public KEY_TYPE FIRST_KEY() { return s.FIRST_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_KEY() { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE LAST_KEY() { return s.LAST_KEY(); }
@Override
public KEY_TYPE POLL_LAST_KEY() { throw new UnsupportedOperationException(); }
}
#endif
/**
* Unmodifyable Set wrapper that helps is used with unmodifyableSet function
* @Type(T)
@ -336,12 +434,21 @@ public class SETS
s = c;
}
#if TYPE_OBJECT
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
@Override
public SET KEY_GENERIC_TYPE copy() { return s.copy(); }
#if !TYPE_OBJECT
@Override
public boolean remove(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
}
#if SORTED_SET_FEATURE
private static class SynchronizedNavigableTrimSet KEY_GENERIC_TYPE extends SynchronizedNavigableSet KEY_GENERIC_TYPE implements ITrimmable
{
ITrimmable trim;
@ -384,27 +491,33 @@ public class SETS
#if !TYPE_OBJECT
@Override
public boolean contains(KEY_TYPE o) { synchronized(mutex) { return n.contains(o); } }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE lower(CLASS_TYPE e) { synchronized(mutex) { return n.lower(e); } }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE floor(CLASS_TYPE e) { synchronized(mutex) { return n.floor(e); } }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE ceiling(CLASS_TYPE e) { synchronized(mutex) { return n.ceiling(e); } }
@Override
@SuppressWarnings("deprecation")
public CLASS_TYPE higher(CLASS_TYPE e) { synchronized(mutex) { return n.higher(e); } }
#endif
@Override
public KEY_TYPE lower(KEY_TYPE e) { synchronized(mutex) { return n.lower(e); } }
@Override
public KEY_TYPE floor(KEY_TYPE e) { synchronized(mutex) { return n.floor(e); } }
@Override
public KEY_TYPE ceiling(KEY_TYPE e) { synchronized(mutex) { return n.ceiling(e); } }
@Override
public KEY_TYPE higher(KEY_TYPE e) { synchronized(mutex) { return n.higher(e); } }
#if !TYPE_OBJECT
@Override
public void setDefaultMaxValue(KEY_TYPE e) { synchronized(mutex) { n.setDefaultMaxValue(e); } }
@Override
public KEY_TYPE getDefaultMaxValue() { synchronized(mutex) { return n.getDefaultMaxValue(); } }
@Override
public void setDefaultMinValue(KEY_TYPE e) { synchronized(mutex) { n.setDefaultMinValue(e); } }
@ -413,28 +526,31 @@ public class SETS
#endif
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { synchronized(mutex) { return synchronize(n.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex); } }
public NAVIGABLE_SET KEY_GENERIC_TYPE copy() { synchronized(mutex) { return n.copy(); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { synchronized(mutex) { return synchronize(n.headSet(toElement, inclusive), mutex); } }
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { synchronized(mutex) { return SETS.synchronize(n.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { synchronized(mutex) { return synchronize(n.tailSet(fromElement, inclusive), mutex); } }
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { synchronized(mutex) { return SETS.synchronize(n.headSet(toElement, inclusive), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { synchronized(mutex) { return SETS.synchronize(n.tailSet(fromElement, inclusive), mutex); } }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { synchronized(mutex) { return n.descendingIterator(); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { synchronized(mutex) { return synchronize(n.descendingSet(), mutex); } }
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { synchronized(mutex) { return SETS.synchronize(n.descendingSet(), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { synchronized(mutex) { return synchronize(n.subSet(fromElement, toElement), mutex); } }
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { synchronized(mutex) { return SETS.synchronize(n.subSet(fromElement, toElement), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { synchronized(mutex) { return synchronize(n.headSet(toElement), mutex); } }
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { synchronized(mutex) { return SETS.synchronize(n.headSet(toElement), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { synchronized(mutex) { return synchronize(n.tailSet(fromElement), mutex); } }
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { synchronized(mutex) { return SETS.synchronize(n.tailSet(fromElement), mutex); } }
}
private static class SynchronizedSortedTrimSet KEY_GENERIC_TYPE extends SynchronizedSortedSet KEY_GENERIC_TYPE implements ITrimmable
@ -472,49 +588,92 @@ public class SETS
s = c;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } }
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToLast(o); } }
@Override
public boolean moveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.moveToFirst(o); } }
@Override
public boolean moveToLast(KEY_TYPE o) { synchronized(mutex) { return s.moveToLast(o); } }
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator(){ synchronized(mutex) { return s.comparator(); } }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { synchronized(mutex) { return s.iterator(); } }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { synchronized(mutex) { return s.iterator(fromElement); } }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { synchronized(mutex) { return synchronize(s.subSet(fromElement, toElement), mutex); } }
public SORTED_SET KEY_GENERIC_TYPE copy() { synchronized(mutex) { return s.copy(); } }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { synchronized(mutex) { return synchronize(s.headSet(toElement), mutex); } }
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { synchronized(mutex) { return SETS.synchronize(s.subSet(fromElement, toElement), mutex); } }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { synchronized(mutex) { return synchronize(s.tailSet(fromElement), mutex); } }
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { synchronized(mutex) { return SETS.synchronize(s.headSet(toElement), mutex); } }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { synchronized(mutex) { return SETS.synchronize(s.tailSet(fromElement), mutex); } }
@Override
public KEY_TYPE FIRST_KEY() { synchronized(mutex) { return s.FIRST_KEY(); } }
@Override
public KEY_TYPE POLL_FIRST_KEY() { synchronized(mutex) { return s.POLL_FIRST_KEY(); } }
@Override
public KEY_TYPE LAST_KEY() { synchronized(mutex) { return s.LAST_KEY(); } }
@Override
public KEY_TYPE POLL_LAST_KEY() { synchronized(mutex) { return s.POLL_LAST_KEY(); } }
}
#endif
#if ORDERED_SET_FEATURE
private static class SynchronizedOrderedTrimSet KEY_GENERIC_TYPE extends SynchronizedOrderedSet KEY_GENERIC_TYPE implements ITrimmable
{
ITrimmable trim;
SynchronizedOrderedTrimSet(ORDERED_SET KEY_GENERIC_TYPE c) {
super(c);
trim = (ITrimmable)c;
}
SynchronizedOrderedTrimSet(ORDERED_SET KEY_GENERIC_TYPE c, Object mutex) {
super(c, mutex);
trim = (ITrimmable)c;
}
@Override
public boolean trim(int size) { synchronized(mutex) { return trim.trim(size); } }
@Override
public void clearAndTrim(int size) { synchronized(mutex) { trim.clearAndTrim(size); } }
}
private static class SynchronizedOrderedSet KEY_GENERIC_TYPE extends SynchronizedSet KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE
{
ORDERED_SET KEY_GENERIC_TYPE s;
SynchronizedOrderedSet(ORDERED_SET KEY_GENERIC_TYPE c) {
super(c);
s = c;
}
SynchronizedOrderedSet(ORDERED_SET KEY_GENERIC_TYPE c, Object mutex) {
super(c, mutex);
s = c;
}
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } }
@Override
public boolean addAndMoveToLast(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToLast(o); } }
@Override
public boolean moveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.moveToFirst(o); } }
@Override
public boolean moveToLast(KEY_TYPE o) { synchronized(mutex) { return s.moveToLast(o); } }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator() { synchronized(mutex) { return s.iterator(); } }
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { synchronized(mutex) { return s.iterator(fromElement); } }
@Override
public ORDERED_SET KEY_GENERIC_TYPE copy() { synchronized(mutex) { return s.copy(); } }
@Override
public KEY_TYPE FIRST_KEY() { synchronized(mutex) { return s.FIRST_KEY(); } }
@Override
public KEY_TYPE POLL_FIRST_KEY() { synchronized(mutex) { return s.POLL_FIRST_KEY(); } }
@Override
public KEY_TYPE LAST_KEY() { synchronized(mutex) { return s.LAST_KEY(); } }
@Override
public KEY_TYPE POLL_LAST_KEY() { synchronized(mutex) { return s.POLL_LAST_KEY(); } }
}
#endif
private static class SynchronizedTrimSet KEY_GENERIC_TYPE extends SynchronizedSet KEY_GENERIC_TYPE implements ITrimmable
{
ITrimmable trim;
@ -538,28 +697,29 @@ public class SETS
private static class SynchronizedSet KEY_GENERIC_TYPE extends SynchronizedCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE
{
#if !TYPE_OBJECT
SET KEY_GENERIC_TYPE s;
#endif
SynchronizedSet(SET KEY_GENERIC_TYPE c) {
super(c);
#if !TYPE_OBJECT
s = c;
#endif
}
SynchronizedSet(SET KEY_GENERIC_TYPE c, Object mutex) {
super(c, mutex);
#if !TYPE_OBJECT
s = c;
#endif
}
#if TYPE_OBJECT
@Override
public KEY_TYPE addOrGet(KEY_TYPE o) { synchronized(mutex) { return s.addOrGet(o); } }
#endif
@Override
public SET KEY_GENERIC_TYPE copy() { synchronized(mutex) { return s.copy(); } }
#if !TYPE_OBJECT
@Override
public boolean remove(KEY_TYPE o) { synchronized(mutex) { return s.remove(o); } }
#endif
}
#endif
}

View File

@ -1,6 +1,7 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
#if PRIMITIVES
import java.util.Spliterator.JAVA_SPLIT_ITERATOR;
@ -329,7 +330,11 @@ public class SPLIT_ITERATORS
}
@Override
public KEY_TYPE NEXT() { return array[index++]; }
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return array[index++];
}
@Override
public boolean hasNext() { return index < fence; }
}

View File

@ -1,5 +1,7 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Objects;
/**
* A Type Specific Strategy class that allows to give control hashcode generation and equals comparason for maps
* @Type(T)
@ -19,6 +21,17 @@ public interface STRATEGY KEY_GENERIC_TYPE
public static GENERIC_KEY_BRACES STRATEGY KEY_GENERIC_TYPE identityStrategy() { return (STRATEGY<KEY_TYPE>)IDENTITY; }
#endif
/**
* Normal Strategy
*/
public static final STRATEGY NO_GENERIC_TYPE NORMAL = new NormalStrategyBRACES();
/**
* @Type(T)
* @return a Normal Strategy that is behaving exactly like the normal Hash Strategy in the Hash Collections
*/
public static GENERIC_KEY_BRACES STRATEGY KEY_GENERIC_TYPE normalStrategy() { return (STRATEGY KEY_GENERIC_TYPE)NORMAL; }
/**
* Type Specific HashCode function
* @param o the element that the hashcode is requested for (if object may be null)
@ -47,5 +60,18 @@ public interface STRATEGY KEY_GENERIC_TYPE
@Override
public boolean equals(KEY_TYPE key, KEY_TYPE value) { return key == value; }
}
#endif
/**
* A Strategy that simulates the normal Hash Collection Behavior if you want to use Hash Control Collections to replace normal ones.
* Only real reason to do that is you have to use this and want to get rid of implementations.
* @Type(T)
*/
public static class NormalStrategy KEY_GENERIC_TYPE implements STRATEGY KEY_GENERIC_TYPE
{
@Override
public int hashCode(KEY_TYPE o) { return KEY_TO_HASH(o); }
@Override
public boolean equals(KEY_TYPE key, KEY_TYPE value) { return EQUALS_KEY_TYPE(key, value); }
}
}

View File

@ -3,9 +3,9 @@ package speiger.src.collections.PACKAGE.utils.maps;
#if !TYPE_BOOLEAN
import java.util.Map;
import java.util.Objects;
#if TYPE_OBJECT
#if TYPE_OBJECT && SORTED_MAP_FEATURE
import java.util.Comparator;
#else
#else if !TYPE_OBJECT
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
#endif
@ -14,25 +14,43 @@ import java.util.function.Consumer;
#if !TYPE_OBJECT && !TYPE_BOOLEAN
import java.util.function.Function;
#endif
#if VALUE_BOOLEAN && JDK_TYPE
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.objects.collections.ObjectIterable;
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.sets.ObjectSet;
#if !TYPE_BOOLEAN
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.utils.ObjectIterators;
import speiger.src.collections.objects.utils.ObjectSets;
#if !TYPE_OBJECT
import speiger.src.collections.objects.sets.ObjectOrderedSet;
#if SORTED_MAP_FEATURE
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#if !VALUE_BOOLEAN || !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.function.FUNCTION;
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
#endif
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
#if !TYPE_BOOLEAN
#if SORTED_MAP_FEATURE
import speiger.src.collections.PACKAGE.maps.interfaces.NAVIGABLE_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
#endif
#if ORDERED_MAP_FEATURE
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
#endif
#if SORTED_MAP_FEATURE
import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET;
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
#endif
#if ORDERED_MAP_FEATURE
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.utils.SETS;
@ -41,11 +59,8 @@ import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
#if !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR;
#endif
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER;
import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_COLLECTIONS;
#if !SAME_TYPE && !VALUE_OBJECT
import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_SETS;
#endif
#endif
/**
* A Helper class that provides you with Singleton/Empty/Synchronized/Unmodifyable Maps
@ -56,7 +71,21 @@ public class MAPS
/**
* Empty Map Variable
*/
public static final MAP NO_KV_GENERIC_TYPE EMPTY = new EmptyMapKV_BRACES();
private static final MAP NO_KV_GENERIC_TYPE EMPTY = new EmptyMapKV_BRACES();
/**
* Empty Map getter function that autocasts to the desired Key and Value
* @Type(T)
* @ValueType(V)
* @return empty map of desired type
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE empty() {
#if TYPE_OBJECT || VALUE_OBJECT
return (MAP KEY_VALUE_GENERIC_TYPE)EMPTY;
#else
return EMPTY;
#endif
}
#endif
/**
@ -102,21 +131,6 @@ public class MAPS
else entries.forEach(action);
}
#if !TYPE_BOOLEAN
/**
* Empty Map getter function that autocasts to the desired Key and Value
* @Type(T)
* @ValueType(V)
* @return empty map of desired type
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE empty() {
#if TYPE_OBJECT || VALUE_OBJECT
return (MAP KEY_VALUE_GENERIC_TYPE)EMPTY;
#else
return EMPTY;
#endif
}
/**
* Helper function that creates a Helper wrapper to synchronize access into the map.
* @param map the map that should be synchronized
@ -139,6 +153,7 @@ public class MAPS
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE synchronize(MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { return map instanceof SynchronizedMap ? map : new SynchronizedMapKV_BRACES(map, mutex); }
#if SORTED_MAP_FEATURE
/**
* Helper function that creates a Helper wrapper to synchronize access into the SortedMap.
* @param map the SortedMap that should be synchronized
@ -161,6 +176,32 @@ public class MAPS
*/
public static GENERIC_KEY_VALUE_BRACES SORTED_MAP KEY_VALUE_GENERIC_TYPE synchronize(SORTED_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { return map instanceof SynchronizedSortedMap ? map : new SynchronizedSortedMapKV_BRACES(map, mutex); }
#endif
#if ORDERED_MAP_FEATURE
/**
* Helper function that creates a Helper wrapper to synchronize access into the OrderedMap.
* @param map the OrderedMap that should be synchronized
* @Type(T)
* @ValueType(V)
* @return a synchronized OrderedMap
* @note if the inputted map is already synchronized then it will just return it instead
* @note iterators do not support synchronization
*/
public static GENERIC_KEY_VALUE_BRACES ORDERED_MAP KEY_VALUE_GENERIC_TYPE synchronize(ORDERED_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof SynchronizedOrderedMap ? map : new SynchronizedOrderedMapKV_BRACES(map); }
/**
* Helper function that creates a Helper wrapper to synchronize access with custom access control into the OrderedMap.
* @param map the OrderedMap that should be synchronized
* @param mutex the object that controls access
* @Type(T)
* @ValueType(V)
* @return a synchronized OrderedMap
* @note if the inputted map is already synchronized then it will just return it instead
* @note iterators do not support synchronization
*/
public static GENERIC_KEY_VALUE_BRACES ORDERED_MAP KEY_VALUE_GENERIC_TYPE synchronize(ORDERED_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { return map instanceof SynchronizedOrderedMap ? map : new SynchronizedOrderedMapKV_BRACES(map, mutex); }
#endif
#if SORTED_MAP_FEATURE
/**
* Helper function that creates a Helper wrapper to synchronize access into the NavigableMap.
* @param map the NavigableMap that should be synchronized
@ -183,6 +224,7 @@ public class MAPS
*/
public static GENERIC_KEY_VALUE_BRACES NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE synchronize(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { return map instanceof SynchronizedNavigableMap ? map : new SynchronizedNavigableMapKV_BRACES(map, mutex); }
#endif
/**
* A Helper function that creates a Helper wrapper to only allow Read Access into the Map
* @param map the map that should be made Unmodifiable
@ -192,6 +234,20 @@ public class MAPS
* @note if the inputted map is already unmodifiable then it will just return it instead
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE unmodifiable(MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof UnmodifyableMap ? map : new UnmodifyableMapKV_BRACES(map); }
#if ORDERED_MAP_FEATURE
/**
* A Helper function that creates a Helper wrapper to only allow Read Access into the OrderedMap
* @param map the OrderedMap that should be made Unmodifiable
* @Type(T)
* @ValueType(V)
* @return a unmodifiable OrderedMap
* @note if the inputted OrderedMap is already unmodifiable then it will just return it instead
*/
public static GENERIC_KEY_VALUE_BRACES ORDERED_MAP KEY_VALUE_GENERIC_TYPE unmodifiable(ORDERED_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof UnmodifyableOrderedMap ? map : new UnmodifyableOrderedMapKV_BRACES(map); }
#endif
#if SORTED_MAP_FEATURE
/**
* A Helper function that creates a Helper wrapper to only allow Read Access into the SortedMap
* @param map the SortedMap that should be made Unmodifiable
@ -201,6 +257,7 @@ public class MAPS
* @note if the inputted SortedMap is already unmodifiable then it will just return it instead
*/
public static GENERIC_KEY_VALUE_BRACES SORTED_MAP KEY_VALUE_GENERIC_TYPE unmodifiable(SORTED_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof UnmodifyableSortedMap ? map : new UnmodifyableSortedMapKV_BRACES(map); }
/**
* A Helper function that creates a Helper wrapper to only allow Read Access into NavigableMap Map
* @param map the NavigableMap that should be made Unmodifiable
@ -210,6 +267,8 @@ public class MAPS
* @note if the inputted NavigableMap is already unmodifiable then it will just return it instead
*/
public static GENERIC_KEY_VALUE_BRACES NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE unmodifiable(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof UnmodifyableNavigableMap ? map : new UnmodifyableNavigableMapKV_BRACES(map); }
#endif
/**
* A Helper function that creates a Unmodifyable Entry
* @param entry the Entry that should be made unmodifiable
@ -217,7 +276,7 @@ public class MAPS
* @ValueType(V)
* @return a Unmodifyable Entry
*/
public static GENERIC_KEY_VALUE_BRACES MAP.Entry KEY_VALUE_GENERIC_TYPE unmodifiable(MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { return entry instanceof UnmodifyableEntry ? entry : new UnmodifyableEntryKV_BRACES(entry); }
public static GENERIC_KEY_VALUE_BRACES MAP.Entry KEY_VALUE_GENERIC_TYPE unmodifiable(MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { return entry instanceof UnmodifyableEntry ? entry : (entry == null ? null : new UnmodifyableEntryKV_BRACES(entry)); }
/**
* A Helper function that creates a Unmodifyable Entry
* @param entry the Entry that should be made unmodifiable
@ -225,7 +284,7 @@ public class MAPS
* @ValueType(V)
* @return a Unmodifyable Entry
*/
public static GENERIC_KEY_VALUE_BRACES MAP.Entry KEY_VALUE_GENERIC_TYPE unmodifiable(Map.Entry<CLASS_TYPE, CLASS_VALUE_TYPE> entry) { return entry instanceof UnmodifyableEntry ? (UnmodifyableEntry KEY_VALUE_GENERIC_TYPE)entry : new UnmodifyableEntryKV_BRACES(entry); }
public static GENERIC_KEY_VALUE_BRACES MAP.Entry KEY_VALUE_GENERIC_TYPE unmodifiable(Map.Entry<CLASS_TYPE, CLASS_VALUE_TYPE> entry) { return entry instanceof UnmodifyableEntry ? (UnmodifyableEntry KEY_VALUE_GENERIC_TYPE)entry : (entry == null ? null : new UnmodifyableEntryKV_BRACES(entry)); }
/**
* Creates a Singleton map from the provided values.
@ -262,6 +321,8 @@ public class MAPS
#if VALUE_PRIMITIVES
@Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@ -277,6 +338,30 @@ public class MAPS
@Override
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return EQUALS_KEY_TYPE(key, this.key) ? value : defaultValue; }
#endif
@Override
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
#if !VALUE_OBJECT
@Override
public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public SingletonMap KEY_VALUE_GENERIC_TYPE copy() { return new SingletonMapKV_BRACES(key, value); }
@Override
public SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = SETS.singleton(key);
@ -285,7 +370,7 @@ public class MAPS
@Override
public VALUE_COLLECTION VALUE_GENERIC_TYPE values() {
if(values == null) values = VALUE_SETS.singleton(value);
if(values == null) values = VALUE_COLLECTIONS.singleton(value);
return values;
}
@Override
@ -308,6 +393,8 @@ public class MAPS
#if VALUE_PRIMITIVES
@Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@ -321,14 +408,38 @@ public class MAPS
public VALUE_TYPE GET_VALUE(KEY_TYPE key) { return getDefaultReturnValue(); }
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return EMPTY_VALUE; }
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return defaultValue; }
#endif
@Override
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
#if !VALUE_OBJECT
@Override
public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public SET KEY_GENERIC_TYPE keySet() { return SETS.empty(); }
@Override
public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { return VALUE_COLLECTIONS.empty(); }
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() { return ObjectSets.empty(); }
@Override
public EmptyMap KEY_VALUE_GENERIC_TYPE copy() { return this; }
}
/**
@ -350,6 +461,7 @@ public class MAPS
public void set(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
}
#if SORTED_MAP_FEATURE
/**
* The Unmodifyable Navigable Map implementation that is sued for the unmodifyableMap function
* @Type(T)
@ -364,31 +476,33 @@ public class MAPS
}
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { return synchronize(map.descendingMap()); }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { return MAPS.unmodifiable(map.descendingMap()); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { return SETS.unmodifiable(map.navigableKeySet()); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE keySet() { return SETS.unmodifiable(map.keySet()); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { return SETS.unmodifiable(map.descendingKeySet()); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { return unmodifiable(map.firstEntry()); }
public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { return MAPS.unmodifiable(map.firstEntry()); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { return unmodifiable(map.lastEntry()); }
public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { return MAPS.unmodifiable(map.lastEntry()); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { throw new UnsupportedOperationException(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { throw new UnsupportedOperationException(); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { return unmodifiable(map.subMap(fromKey, fromInclusive, toKey, toInclusive)); }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { return MAPS.unmodifiable(map.subMap(fromKey, fromInclusive, toKey, toInclusive)); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { return unmodifiable(map.headMap(toKey, inclusive)); }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { return MAPS.unmodifiable(map.headMap(toKey, inclusive)); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { return unmodifiable(map.tailMap(fromKey, inclusive)); }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { return MAPS.unmodifiable(map.tailMap(fromKey, inclusive)); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { return unmodifiable(map.subMap(fromKey, toKey)); }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { return MAPS.unmodifiable(map.subMap(fromKey, toKey)); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { return unmodifiable(map.headMap(toKey)); }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { return MAPS.unmodifiable(map.headMap(toKey)); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { return unmodifiable(map.tailMap(fromKey)); }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { return MAPS.unmodifiable(map.tailMap(fromKey)); }
#if !TYPE_OBJECT
@Override
public void setDefaultMaxValue(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@ -408,24 +522,28 @@ public class MAPS
@Override
public KEY_TYPE ceilingKey(KEY_TYPE key) { return map.ceilingKey(key); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { return unmodifiable(map.lowerEntry(key)); }
public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { return MAPS.unmodifiable(map.lowerEntry(key)); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { return unmodifiable(map.higherEntry(key)); }
public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { return MAPS.unmodifiable(map.higherEntry(key)); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { return unmodifiable(map.floorEntry(key)); }
public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { return MAPS.unmodifiable(map.floorEntry(key)); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { return unmodifiable(map.ceilingEntry(key)); }
public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { return MAPS.unmodifiable(map.ceilingEntry(key)); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy() { return map.copy(); }
}
#endif
#if ORDERED_MAP_FEATURE
/**
* The Unmodifyable Sorted Map implementation that is sued for the unmodifyableMap function
* The Unmodifyable Ordered Map implementation that is sued for the unmodifyableMap function
* @Type(T)
* @ValueType(V)
*/
public static class UnmodifyableSortedMap KEY_VALUE_GENERIC_TYPE extends UnmodifyableMap KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE {
SORTED_MAP KEY_VALUE_GENERIC_TYPE map;
public static class UnmodifyableOrderedMap KEY_VALUE_GENERIC_TYPE extends UnmodifyableMap KEY_VALUE_GENERIC_TYPE implements ORDERED_MAP KEY_VALUE_GENERIC_TYPE {
ORDERED_MAP KEY_VALUE_GENERIC_TYPE map;
UnmodifyableSortedMap(SORTED_MAP KEY_VALUE_GENERIC_TYPE map) {
UnmodifyableOrderedMap(ORDERED_MAP KEY_VALUE_GENERIC_TYPE map) {
super(map);
this.map = map;
}
@ -443,27 +561,74 @@ public class MAPS
@Override
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return map.comparator(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { return unmodifiable(map.subMap(fromKey, toKey)); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { return unmodifiable(map.headMap(toKey)); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { return unmodifiable(map.tailMap(fromKey)); }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() { return map.FIRST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_ENTRY_KEY() { return map.POLL_FIRST_ENTRY_KEY(); }
public KEY_TYPE POLL_FIRST_ENTRY_KEY() { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE LAST_ENTRY_KEY() { return map.LAST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_LAST_ENTRY_KEY() { return map.POLL_LAST_ENTRY_KEY(); }
public KEY_TYPE POLL_LAST_ENTRY_KEY() { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE FIRST_ENTRY_VALUE() { return map.FIRST_ENTRY_VALUE(); }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { return map.LAST_ENTRY_VALUE(); }
@Override
public ORDERED_MAP KEY_VALUE_GENERIC_TYPE copy() { return map.copy(); }
@Override
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keys == null) keys = SETS.unmodifiable(map.keySet());
return (ORDERED_SET KEY_GENERIC_TYPE)keys;
}
@Override
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new UnmodifyableOrderedEntrySetKV_BRACES(map.ENTRY_SET());
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
}
#endif
#if SORTED_MAP_FEATURE
/**
* The Unmodifyable Sorted Map implementation that is sued for the unmodifyableMap function
* @Type(T)
* @ValueType(V)
*/
public static class UnmodifyableSortedMap KEY_VALUE_GENERIC_TYPE extends UnmodifyableMap KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE {
SORTED_MAP KEY_VALUE_GENERIC_TYPE map;
UnmodifyableSortedMap(SORTED_MAP KEY_VALUE_GENERIC_TYPE map) {
super(map);
this.map = map;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return map.comparator(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { return MAPS.unmodifiable(map.subMap(fromKey, toKey)); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { return MAPS.unmodifiable(map.headMap(toKey)); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { return MAPS.unmodifiable(map.tailMap(fromKey)); }
@Override
public SORTED_SET KEY_GENERIC_TYPE keySet() { return SETS.unmodifiable(map.keySet()); }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() { return map.FIRST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_ENTRY_KEY() { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE LAST_ENTRY_KEY() { return map.LAST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_LAST_ENTRY_KEY() { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE FIRST_ENTRY_VALUE() { return map.FIRST_ENTRY_VALUE(); }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { return map.LAST_ENTRY_VALUE(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE copy() { return map.copy(); }
}
#endif
/**
* The Unmodifyable Map implementation that is sued for the unmodifyableMap function
* @Type(T)
@ -486,6 +651,8 @@ public class MAPS
#if VALUE_PRIMITIVES
@Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@ -496,11 +663,44 @@ public class MAPS
public boolean remove(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE GET_VALUE(KEY_TYPE key) { return map.GET_VALUE(key); }
public VALUE_TYPE GET_VALUE(KEY_TYPE key) {
VALUE_TYPE type = map.GET_VALUE(key);
return VALUE_EQUALS(type, map.getDefaultReturnValue()) && !map.containsKey(key) ? getDefaultReturnValue() : type;
}
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return map.getOrDefault(key, defaultValue); }
#endif
@Override
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
#if !VALUE_OBJECT
@Override
public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); }
#endif
@Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public void REPLACE_VALUES(UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); }
@Override
public void REPLACE_VALUES(MAP KEY_VALUE_GENERIC_TYPE m) { throw new UnsupportedOperationException(); }
@Override
public MAP KEY_VALUE_GENERIC_TYPE copy() { return map.copy(); }
@Override
public void clear() { throw new UnsupportedOperationException(); }
@Override
public SET KEY_GENERIC_TYPE keySet() {
@ -521,6 +721,46 @@ public class MAPS
}
}
#if ORDERED_MAP_FEATURE
/**
* The Unmodifyable Ordered Set implementation for the Unmodifyable Ordered Map implementation
* @Type(T)
* @ValueType(V)
*/
public static class UnmodifyableOrderedEntrySet KEY_VALUE_GENERIC_TYPE extends UnmodifyableEntrySet KEY_VALUE_GENERIC_TYPE implements ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>
{
ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> set;
UnmodifyableOrderedEntrySet(ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> c) {
super(c);
set = c;
}
@Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean moveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> copy() { return set.copy(); }
@Override
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iterator() { return ObjectIterators.unmodifiable(set.iterator()); }
@Override
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iterator(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { return ObjectIterators.unmodifiable(set.iterator(fromElement)); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE first() { return set.first(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirst() { throw new UnsupportedOperationException(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE last() { return set.last(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLast() { throw new UnsupportedOperationException(); }
}
#endif
/**
* The Unmodifyable Set implementation for the Unmodifyable Map implementation
* @Type(T)
@ -538,7 +778,7 @@ public class MAPS
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
s.forEach(T -> action.accept(unmodifiable(T)));
s.forEach(T -> action.accept(MAPS.unmodifiable(T)));
}
@Override
@ -548,12 +788,13 @@ public class MAPS
@Override
public boolean hasNext() { return iter.hasNext(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { return unmodifiable(iter.next()); }
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { return MAPS.unmodifiable(iter.next()); }
};
}
}
#if SORTED_MAP_FEATURE
/**
* The Synchronized Navigable Map implementation used by the synchronizedMap helper function
* @Type(T)
@ -573,31 +814,33 @@ public class MAPS
}
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { synchronized(mutex) { return synchronize(map.descendingMap(), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { synchronized(mutex) { return MAPS.synchronize(map.descendingMap(), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { synchronized(mutex) { return SETS.synchronize(map.navigableKeySet(), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { synchronized(mutex) { return SETS.synchronize(map.descendingKeySet(), mutex); } }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE keySet() { synchronized(mutex) { return SETS.synchronize(map.keySet(), mutex); } }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { synchronized(mutex) { return map.firstEntry(); } }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { synchronized(mutex) { return map.firstEntry(); } }
public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { synchronized(mutex) { return map.lastEntry(); } }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { synchronized(mutex) { return map.pollFirstEntry(); } }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { synchronized(mutex) { return map.pollLastEntry(); } }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { synchronized(mutex) { return synchronize(map.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { synchronized(mutex) { return MAPS.synchronize(map.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.headMap(toKey, inclusive), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { synchronized(mutex) { return MAPS.synchronize(map.headMap(toKey, inclusive), mutex); } }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.tailMap(fromKey, inclusive), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { synchronized(mutex) { return MAPS.synchronize(map.tailMap(fromKey, inclusive), mutex); } }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.subMap(fromKey, toKey), mutex); } }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.headMap(toKey), mutex); } }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { synchronized(mutex) { return MAPS.synchronize(map.tailMap(fromKey), mutex); } }
@Override
public KEY_TYPE lowerKey(KEY_TYPE key) { synchronized(mutex) { return map.lowerKey(key); } }
@Override
@ -614,25 +857,27 @@ public class MAPS
public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { synchronized(mutex) { return map.floorEntry(key); } }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { synchronized(mutex) { return map.ceilingEntry(key); } }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy() { synchronized(mutex) { return map.copy(); } }
#if !TYPE_OBJECT
@Override
@Deprecated
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, boolean fromInclusive, CLASS_TYPE toKey, boolean toInclusive) { synchronized(mutex) { return synchronize(map.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, boolean fromInclusive, CLASS_TYPE toKey, boolean toInclusive) { synchronized(mutex) { return MAPS.synchronize(map.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } }
@Override
@Deprecated
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.headMap(toKey, inclusive), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey, boolean inclusive) { synchronized(mutex) { return MAPS.synchronize(map.headMap(toKey, inclusive), mutex); } }
@Override
@Deprecated
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.tailMap(fromKey, inclusive), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey, boolean inclusive) { synchronized(mutex) { return MAPS.synchronize(map.tailMap(fromKey, inclusive), mutex); } }
@Override
@Deprecated
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.subMap(fromKey, toKey), mutex); } }
@Override
@Deprecated
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.headMap(toKey), mutex); } }
@Override
@Deprecated
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } }
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { synchronized(mutex) { return MAPS.synchronize(map.tailMap(fromKey), mutex); } }
@Override
public void setDefaultMaxValue(KEY_TYPE e) { synchronized(mutex) { map.setDefaultMaxValue(e); } }
@Override
@ -668,6 +913,67 @@ public class MAPS
#endif
}
#endif
#if ORDERED_MAP_FEATURE
/**
* The Synchronized Ordered Map implementation used by the synchronizedMap helper function
* @Type(T)
* @ValueType(V)
*/
public static class SynchronizedOrderedMap KEY_VALUE_GENERIC_TYPE extends SynchronizedMap KEY_VALUE_GENERIC_TYPE implements ORDERED_MAP KEY_VALUE_GENERIC_TYPE {
ORDERED_MAP KEY_VALUE_GENERIC_TYPE map;
SynchronizedOrderedMap(ORDERED_MAP KEY_VALUE_GENERIC_TYPE map) {
super(map);
this.map = map;
}
SynchronizedOrderedMap(ORDERED_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) {
super(map, mutex);
this.map = map;
}
@Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putAndMoveToFirst(key, value); } }
@Override
public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putAndMoveToLast(key, value); } }
@Override
public boolean moveToFirst(KEY_TYPE key) { synchronized(mutex) { return map.moveToFirst(key); } }
@Override
public boolean moveToLast(KEY_TYPE key) { synchronized(mutex) { return map.moveToLast(key); } }
@Override
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { synchronized(mutex) { return map.getAndMoveToFirst(key); } }
@Override
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { synchronized(mutex) { return map.getAndMoveToLast(key); } }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() { synchronized(mutex) { return map.FIRST_ENTRY_KEY(); } }
@Override
public KEY_TYPE POLL_FIRST_ENTRY_KEY() { synchronized(mutex) { return map.POLL_FIRST_ENTRY_KEY(); } }
@Override
public KEY_TYPE LAST_ENTRY_KEY() { synchronized(mutex) { return map.LAST_ENTRY_KEY(); } }
@Override
public KEY_TYPE POLL_LAST_ENTRY_KEY() { synchronized(mutex) { return map.POLL_LAST_ENTRY_KEY(); } }
@Override
public VALUE_TYPE FIRST_ENTRY_VALUE() { synchronized(mutex) { return map.FIRST_ENTRY_VALUE(); } }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { synchronized(mutex) { return map.LAST_ENTRY_VALUE(); } }
@Override
public ORDERED_MAP KEY_VALUE_GENERIC_TYPE copy() { synchronized(mutex) { return map.copy(); } }
@Override
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keys == null) keys = SETS.synchronize(map.keySet(), mutex);
return (ORDERED_SET KEY_GENERIC_TYPE)keys;
}
@Override
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = ObjectSets.synchronize(map.ENTRY_SET(), mutex);
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
}
#endif
#if SORTED_MAP_FEATURE
/**
* The Synchronized Sorted Map implementation used by the synchronizedMap helper function
* @Type(T)
@ -686,26 +992,16 @@ public class MAPS
this.map = map;
}
@Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putAndMoveToFirst(key, value); } }
@Override
public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putAndMoveToLast(key, value); } }
@Override
public boolean moveToFirst(KEY_TYPE key) { synchronized(mutex) { return map.moveToFirst(key); } }
@Override
public boolean moveToLast(KEY_TYPE key) { synchronized(mutex) { return map.moveToLast(key); } }
@Override
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { synchronized(mutex) { return map.getAndMoveToFirst(key); } }
@Override
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { synchronized(mutex) { return map.getAndMoveToLast(key); } }
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { synchronized(mutex) { return map.comparator(); } }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.subMap(fromKey, toKey), mutex); } }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.headMap(toKey), mutex); } }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { synchronized(mutex) { return MAPS.synchronize(map.tailMap(fromKey), mutex); } }
@Override
public SORTED_SET KEY_GENERIC_TYPE keySet() { synchronized(mutex) { return SETS.synchronize(map.keySet(), mutex); } }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() { synchronized(mutex) { return map.FIRST_ENTRY_KEY(); } }
@Override
@ -718,6 +1014,8 @@ public class MAPS
public VALUE_TYPE FIRST_ENTRY_VALUE() { synchronized(mutex) { return map.FIRST_ENTRY_VALUE(); } }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { synchronized(mutex) { return map.LAST_ENTRY_VALUE(); } }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE copy() { synchronized(mutex) { return map.copy(); } }
#if !TYPE_OBJECT
@Override
@Deprecated
@ -727,16 +1025,17 @@ public class MAPS
public CLASS_TYPE lastKey() { synchronized(mutex) { return map.lastKey(); } }
@Override
@Deprecated
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.subMap(fromKey, toKey), mutex); } }
@Override
@Deprecated
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey) { synchronized(mutex) { return MAPS.synchronize(map.headMap(toKey), mutex); } }
@Override
@Deprecated
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } }
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { synchronized(mutex) { return MAPS.synchronize(map.tailMap(fromKey), mutex); } }
#endif
}
#endif
/**
* The Synchronized Map implementation used by the synchronizedMap helper function
* @Type(T)
@ -777,6 +1076,8 @@ public class MAPS
#if VALUE_PRIMITIVES
@Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.addTo(key, value); } }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.subFrom(key, value); } }
public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.addToAll(m); } }
#endif
@Override
@ -818,6 +1119,18 @@ public class MAPS
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE_IF_ABSENT(key, mappingFunction); } }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE_IF_PRESENT(key, mappingFunction); } }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { synchronized(mutex) { return map.SUPPLY_IF_ABSENT(key, valueProvider); } }
#if !VALUE_OBJECT
@Override
public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTENonDefault(key, mappingFunction); } }
@Override
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE_IF_ABSENTNonDefault(key, mappingFunction); } }
@Override
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE_IF_PRESENTNonDefault(key, mappingFunction); } }
@Override
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { synchronized(mutex) { return map.SUPPLY_IF_ABSENTNonDefault(key, valueProvider); } }
#endif
@Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.MERGE(key, value, mappingFunction); } }
@Override
@ -829,7 +1142,9 @@ public class MAPS
@Override
public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) { synchronized(mutex) { map.forEach(action); } }
@Override
public int size() { synchronized(mutex) { return super.size(); } }
public int size() { synchronized(mutex) { return map.size(); } }
@Override
public MAP KEY_VALUE_GENERIC_TYPE copy() { synchronized(mutex) { return map.copy(); } }
@Override
public SET KEY_GENERIC_TYPE keySet() {
if(keys == null) keys = SETS.synchronize(map.keySet(), mutex);
@ -902,5 +1217,4 @@ public class MAPS
public void forEach(BiConsumer<? super CLASS_TYPE, ? super CLASS_VALUE_TYPE> action) { synchronized(mutex) { map.forEach(action); } }
#endif
}
#endif
}

View File

@ -0,0 +1,93 @@
package speiger.src.testers.PACKAGE.builder;
import java.util.List;
import com.google.common.collect.testing.AbstractTester;
import com.google.common.collect.testing.CollectionTestSuiteBuilder;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.testers.CollectionRemoveIfTester;
import speiger.src.testers.base.tests.collection.JavaCollectionRemoveIfTester;
import speiger.src.testers.PACKAGE.generators.TEST_COLLECTION_GENERATOR;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionAddAllArrayTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionAddAllTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionAddTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionClearTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionContainsAllTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionContainsAnyTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionContainsTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionCopyTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionEqualsTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionForEachTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionIteratorTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionRemoveAllTester;
#if !TYPE_BOOLEAN
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionRemoveIfTester;
#if !TYPE_OBJECT
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionStreamTester;
#endif
#endif
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionRetainAllTester;
import speiger.src.testers.PACKAGE.tests.collection.FILE_KEY_TYPECollectionToArrayTester;
#if !TYPE_BOOLEAN
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableCountTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableDistinctTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableFilterTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableFindFirstTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableLimitTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableMapTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableMatchesTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterablePeekTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableReduceTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableRepeatTester;
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableSortedTester;
#endif
@SuppressWarnings("javadoc")
public class COLLECTION_TEST_BUILDER KEY_GENERIC_TYPE extends CollectionTestSuiteBuilder<CLASS_TYPE> {
public static GENERIC_KEY_BRACES COLLECTION_TEST_BUILDER KEY_GENERIC_TYPE using(TEST_COLLECTION_GENERATOR KEY_GENERIC_TYPE generator) {
return (COLLECTION_TEST_BUILDER KEY_GENERIC_TYPE) new COLLECTION_TEST_BUILDER KEY_GENERIC_TYPE().usingGenerator(generator);
}
@Override
@SuppressWarnings("rawtypes")
protected List<Class<? extends AbstractTester>> getTesters() {
List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters());
testers.remove(CollectionRemoveIfTester.class);
testers.add(JavaCollectionRemoveIfTester.class);
#if !TYPE_BOOLEAN
testers.add(FILE_KEY_TYPEIterableMapTester.class);
testers.add(FILE_KEY_TYPEIterableFilterTester.class);
testers.add(FILE_KEY_TYPEIterableDistinctTester.class);
testers.add(FILE_KEY_TYPEIterableLimitTester.class);
testers.add(FILE_KEY_TYPEIterableSortedTester.class);
testers.add(FILE_KEY_TYPEIterableMatchesTester.class);
testers.add(FILE_KEY_TYPEIterablePeekTester.class);
testers.add(FILE_KEY_TYPEIterableReduceTester.class);
testers.add(FILE_KEY_TYPEIterableRepeatTester.class);
testers.add(FILE_KEY_TYPEIterableCountTester.class);
testers.add(FILE_KEY_TYPEIterableFindFirstTester.class);
#endif
testers.add(FILE_KEY_TYPECollectionAddAllTester.class);
testers.add(FILE_KEY_TYPECollectionAddAllArrayTester.class);
testers.add(FILE_KEY_TYPECollectionAddTester.class);
testers.add(FILE_KEY_TYPECollectionClearTester.class);
testers.add(FILE_KEY_TYPECollectionContainsAllTester.class);
testers.add(FILE_KEY_TYPECollectionContainsAnyTester.class);
testers.add(FILE_KEY_TYPECollectionContainsTester.class);
testers.add(FILE_KEY_TYPECollectionCopyTester.class);
testers.add(FILE_KEY_TYPECollectionEqualsTester.class);
testers.add(FILE_KEY_TYPECollectionForEachTester.class);
testers.add(FILE_KEY_TYPECollectionIteratorTester.class);
testers.add(FILE_KEY_TYPECollectionRemoveAllTester.class);
testers.add(FILE_KEY_TYPECollectionRetainAllTester.class);
#if !TYPE_BOOLEAN
testers.add(FILE_KEY_TYPECollectionRemoveIfTester.class);
#if !TYPE_OBJECT
testers.add(FILE_KEY_TYPECollectionStreamTester.class);
#endif
#endif
testers.add(FILE_KEY_TYPECollectionToArrayTester.class);
return testers;
}
}

View File

@ -0,0 +1,28 @@
package speiger.src.testers.PACKAGE.builder;
import java.util.List;
import com.google.common.collect.testing.AbstractTester;
import speiger.src.testers.PACKAGE.generators.TEST_QUEUE_GENERATOR;
import speiger.src.testers.PACKAGE.tests.queue.FILE_KEY_TYPEDequeueDequeueTester;
import speiger.src.testers.PACKAGE.tests.queue.FILE_KEY_TYPEDequeueEnqueueTester;
import speiger.src.testers.PACKAGE.tests.queue.FILE_KEY_TYPEDequeueLastTester;
@SuppressWarnings("javadoc")
public class DEQUEUE_TEST_BUILDER KEY_GENERIC_TYPE extends QUEUE_TEST_BUILDER KEY_GENERIC_TYPE
{
public static GENERIC_KEY_BRACES DEQUEUE_TEST_BUILDER KEY_GENERIC_TYPE using(TEST_QUEUE_GENERATOR KEY_GENERIC_TYPE builder) {
return (DEQUEUE_TEST_BUILDER KEY_GENERIC_TYPE)new DEQUEUE_TEST_BUILDER KEY_GENERIC_TYPE().usingGenerator(builder);
}
@Override
@SuppressWarnings("rawtypes")
protected List<Class<? extends AbstractTester>> getTesters() {
List<Class<? extends AbstractTester>> tester = super.getTesters();
tester.add(FILE_KEY_TYPEDequeueDequeueTester.class);
tester.add(FILE_KEY_TYPEDequeueEnqueueTester.class);
tester.add(FILE_KEY_TYPEDequeueLastTester.class);
return tester;
}
}

Some files were not shown because too many files have changed in this diff Show More