Refine search
void calculateNextPermutation() { j = list.size() - 1; int s = 0; // Handle the special case of an empty list. Skip the calculation of the // next permutation. if (j == -1) { return; } while (true) { int q = c[j] + o[j]; if (q < 0) { switchDirection(); continue; } if (q == j + 1) { if (j == 0) { break; } s++; switchDirection(); continue; } Collections.swap(list, j - c[j] + s, j - q + s); c[j] = q; break; } }
/** * Randomize server list using local IPv4 address hash as a seed. * * @return a copy of the original list with elements in the random order */ public static <T extends EurekaEndpoint> List<T> randomize(List<T> list) { List<T> randomList = new ArrayList<>(list); if (randomList.size() < 2) { return randomList; } Random random = new Random(LOCAL_IPV4_ADDRESS.hashCode()); int last = randomList.size() - 1; for (int i = 0; i < last; i++) { int pos = random.nextInt(randomList.size() - i); if (pos != i) { Collections.swap(randomList, i, pos); } } return randomList; }
public class Permute{ static void permute(java.util.List<Integer> arr, int k){ for(int i = k; i < arr.size(); i++){ java.util.Collections.swap(arr, i, k); permute(arr, k+1); java.util.Collections.swap(arr, k, i); } if (k == arr.size() -1){ System.out.println(java.util.Arrays.toString(arr.toArray())); } } public static void main(String[] args){ Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0); } }
void calculateNextPermutation() { j = list.size() - 1; int s = 0; // Handle the special case of an empty list. Skip the calculation of the // next permutation. if (j == -1) { return; } while (true) { int q = c[j] + o[j]; if (q < 0) { switchDirection(); continue; } if (q == j + 1) { if (j == 0) { break; } s++; switchDirection(); continue; } Collections.swap(list, j - c[j] + s, j - q + s); c[j] = q; break; } }
void calculateNextPermutation() { j = list.size() - 1; int s = 0; // Handle the special case of an empty list. Skip the calculation of the // next permutation. if (j == -1) { return; } while (true) { int q = c[j] + o[j]; if (q < 0) { switchDirection(); continue; } if (q == j + 1) { if (j == 0) { break; } s++; switchDirection(); continue; } Collections.swap(list, j - c[j] + s, j - q + s); c[j] = q; break; } }
private static void choosePlayerSample(GlowServer server, PaperServerListPingEvent event) { ThreadLocalRandom random = ThreadLocalRandom.current(); List<Player> players = new ArrayList<>(server.getOnlinePlayers()); int sampleCount = server.getPlayerSampleCount(); if (players.size() <= sampleCount) { sampleCount = players.size(); } else { // Send a random subset of players (modified Fisher-Yates shuffle) for (int i = 0; i < sampleCount; i++) { Collections.swap(players, i, random.nextInt(i, players.size())); } } // Add selected players to the event for (int i = 0; i < sampleCount; i++) { event.getPlayerSample().add(players.get(i).getPlayerProfile()); } }
private Pojo distinct(Pojo pojo, String names) { if (!Lang.isEmpty(names) && names.length() != 0) { List<String> nameList = Arrays.asList(names.trim().split(",")); if (names.toLowerCase().contains(DISTINCT)) { pojo.append(Pojos.Items.wrap(DISTINCT)); // distinct只能作用一个在一个字段上 // 做一个排序处理,把DISTINCT的字段移到第一个 for (int i = 0; i < nameList.size(); ++i) { if (nameList.get(i).toLowerCase().contains(DISTINCT)) { Collections.swap(nameList, 0, i); // 为了让字段作为正则匹配到列,删除DINSTINCT nameList.set(0, nameList.get(0).toLowerCase().replace(DISTINCT, "").toLowerCase()); break; } } } StringBuilder sb = new StringBuilder(); for (String name : nameList) { sb.append(name.trim()); sb.append("|"); } sb.setLength(sb.length() - 1); pojo.getContext().setFieldMatcher(FieldMatcher.make(sb.toString(), null, true)); } return pojo; }
private void expandPopularItems() { // for now just expand the first dribbble image per page which should be // the most popular according to our weighing & sorting List<Integer> expandedPositions = new ArrayList<>(); int page = -1; final int count = items.size(); for (int i = 0; i < count; i++) { PlaidItem item = getItem(i); if (item instanceof Shot && item.getPage() > page) { item.setColspan(columns); page = item.getPage(); expandedPositions.add(i); } else { item.setColspan(1); } } // make sure that any expanded items are at the start of a row // so that we don't leave any gaps in the grid for (int expandedPos = 0; expandedPos < expandedPositions.size(); expandedPos++) { int pos = expandedPositions.get(expandedPos); int extraSpannedSpaces = expandedPos * (columns - 1); int rowPosition = (pos + extraSpannedSpaces) % columns; if (rowPosition != 0) { int swapWith = pos + (columns - rowPosition); if (swapWith < items.size()) { Collections.swap(items, pos, swapWith); } } } }
void calculateNextPermutation() { j = list.size() - 1; int s = 0; // Handle the special case of an empty list. Skip the calculation of the // next permutation. if (j == -1) { return; } while (true) { int q = c[j] + o[j]; if (q < 0) { switchDirection(); continue; } if (q == j + 1) { if (j == 0) { break; } s++; switchDirection(); continue; } Collections.swap(list, j - c[j] + s, j - q + s); c[j] = q; break; } }
int evictionLimit = registrySize - registrySizeThreshold; int toEvict = Math.min(expiredLeases.size(), evictionLimit); if (toEvict > 0) { logger.info("Evicting {} items (expired={}, evictionLimit={})", toEvict, expiredLeases.size(), evictionLimit); int next = i + random.nextInt(expiredLeases.size() - i); Collections.swap(expiredLeases, i, next); Lease<InstanceInfo> lease = expiredLeases.get(i);
/** * Test the equality with a few variations on ordering * * @param expected List of expected values * @param input List of actual values */ public void assertVariationsEqual(List<NodeValue<String, Integer>> expected, List<NodeValue<String, Integer>> input) { List<NodeValue<String, Integer>> copy = new ArrayList<NodeValue<String, Integer>>(input); for(int i = 0; i < Math.min(5, copy.size()); i++) { int j = random.nextInt(copy.size()); int k = random.nextInt(copy.size()); Collections.swap(copy, j, k); Set<NodeValue<String, Integer>> expSet = Sets.newHashSet(expected); List<NodeValue<String, Integer>> repairs = repairer.getRepairs(copy); Set<NodeValue<String, Integer>> repairSet = Sets.newHashSet(repairs); assertEquals("Repairs list contains duplicates on iteration" + i + ".", repairs.size(), repairSet.size()); assertEquals("Expected repairs do not equal found repairs on iteration " + i + " : ", expSet, repairSet); } }
for (int cacheId = 1; cacheId < cacheIds.size(); cacheId++) { GridCacheContext<?, ?> currCctx = cacheContext(cacheIds.get(cacheId)); Collections.swap(cacheIds, 0, cacheId); throw new CacheServerNotFoundException("Failed to find data nodes for cache: " + cctx.name()); for (int i = 1; i < cacheIds.size(); i++) { GridCacheContext<?,?> extraCctx = cacheContext(cacheIds.get(i));