1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.anac.y2019.harddealer.math3.util;
|
---|
18 |
|
---|
19 | import java.io.Serializable;
|
---|
20 |
|
---|
21 | import agents.anac.y2019.harddealer.math3.exception.MathIllegalArgumentException;
|
---|
22 |
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Classic median of 3 strategy given begin and end indices.
|
---|
26 | * @since 3.4
|
---|
27 | */
|
---|
28 | public class MedianOf3PivotingStrategy implements PivotingStrategyInterface, Serializable {
|
---|
29 |
|
---|
30 | /** Serializable UID. */
|
---|
31 | private static final long serialVersionUID = 20140713L;
|
---|
32 |
|
---|
33 | /**{@inheritDoc}
|
---|
34 | * This in specific makes use of median of 3 pivoting.
|
---|
35 | * @return The index corresponding to a pivot chosen between the
|
---|
36 | * first, middle and the last indices of the array slice
|
---|
37 | * @throws MathIllegalArgumentException when indices exceeds range
|
---|
38 | */
|
---|
39 | public int pivotIndex(final double[] work, final int begin, final int end)
|
---|
40 | throws MathIllegalArgumentException {
|
---|
41 | MathArrays.verifyValues(work, begin, end-begin);
|
---|
42 | final int inclusiveEnd = end - 1;
|
---|
43 | final int middle = begin + (inclusiveEnd - begin) / 2;
|
---|
44 | final double wBegin = work[begin];
|
---|
45 | final double wMiddle = work[middle];
|
---|
46 | final double wEnd = work[inclusiveEnd];
|
---|
47 |
|
---|
48 | if (wBegin < wMiddle) {
|
---|
49 | if (wMiddle < wEnd) {
|
---|
50 | return middle;
|
---|
51 | } else {
|
---|
52 | return wBegin < wEnd ? inclusiveEnd : begin;
|
---|
53 | }
|
---|
54 | } else {
|
---|
55 | if (wBegin < wEnd) {
|
---|
56 | return begin;
|
---|
57 | } else {
|
---|
58 | return wMiddle < wEnd ? inclusiveEnd : middle;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|