+++ /dev/null
-/*
- * Copyright 2000-2011 JetBrains s.r.o.
- *
- * 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.
- */
-package com.intellij.openapi.vcs;
-
-import com.intellij.util.Consumer;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * @author irengrig
- * Date: 2/2/11
- * Time: 10:11 AM
- * Cartesian product
- */
-public class CollectionsMultiplier<T> {
- private List<List<T>> myInner;
-
- public void add(@Nullable final List<T> list) {
- if (list == null || list.isEmpty()) return;
- if (myInner == null) {
- myInner = Collections.singletonList(list);
- return;
- }
- final List<List<T>> copy = myInner;
- myInner = new ArrayList<>();
- for (T t : list) {
- for (List<T> existing : copy) {
- final ArrayList<T> newList = new ArrayList<>(existing);
- newList.add(t);
- myInner.add(newList);
- }
- }
- }
-
- public boolean isEmpty() {
- return myInner == null;
- }
-
- public void iterateResult(final Consumer<List<T>> consumer) {
- if (myInner == null) return;
- for (List<T> list : myInner) {
- consumer.consume(list);
- }
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2010 JetBrains s.r.o.
- *
- * 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.
- */
-package com.intellij.openapi.vcs;
-
-/**
-* @author irengrig
-*/
-public class CompoundNumber {
- private final int myMemberNumber;
- private final int myIdx;
-
- public CompoundNumber(int memberNumber, int idx) {
- myIdx = idx;
- myMemberNumber = memberNumber;
- }
-
- public int getIdx() {
- return myIdx;
- }
-
- public int getMemberNumber() {
- return myMemberNumber;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CompoundNumber that = (CompoundNumber)o;
-
- if (myIdx != that.myIdx) return false;
- if (myMemberNumber != that.myMemberNumber) return false;
-
- return true;
- }
-
- @Override
- public int hashCode() {
- int result = myMemberNumber;
- result = 31 * result + myIdx;
- return result;
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2014 JetBrains s.r.o.
- *
- * 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.
- */
-package com.intellij.openapi.vcs;
-
-import com.intellij.openapi.util.Pair;
-import com.intellij.openapi.util.Ref;
-import com.intellij.util.PairProcessor;
-
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.ListIterator;
-
-/**
- * @author irengrig
- */
-public class MembershipMap<Key, Val> extends AreaMap<Key, Val> {
- public static<Key extends Comparable<Key>, Val> MembershipMap<Key, Val> createMembershipMap(final PairProcessor<Key, Key> keysResemblance) {
- return new MembershipMap<>(keysResemblance, new ComparableComparator<>());
- }
-
- public static<Key, Val> MembershipMap<Key, Val> createMembershipMap(final PairProcessor<Key, Key> keysResemblance, final Comparator<Key> comparator) {
- return new MembershipMap<>(keysResemblance, comparator);
- }
-
- private MembershipMap(final PairProcessor<Key, Key> keysResemblance, final Comparator<Key> comparator) {
- super(keysResemblance, comparator);
- }
-
- public void putOptimal(final Key key, final Val val) {
- final int idx = putIfNoParent(key, val);
- if (idx < 0) return;
-
- if (idx + 1 < myKeys.size()) {
- for (final ListIterator<Key> listIterator = myKeys.listIterator(idx + 1); listIterator.hasNext();) {
- final Key next = listIterator.next();
- if (myKeysResemblance.process(key, next)) {
- listIterator.remove();
- myMap.remove(next);
- } else {
- break;
- }
- }
- }
- }
-
- public void optimizeMap(final PairProcessor<Val, Val> valuesAreas) {
- int i = 0;
- for (Iterator<Key> iterator = myKeys.iterator(); iterator.hasNext();) {
- final Key key = iterator.next();
- final Val value = myMap.get(key);
-
- // go for parents
- for (int j = i - 1; j >= 0; -- j) {
- final Key innerKey = myKeys.get(j);
- if (myKeysResemblance.process(innerKey, key)) {
- if (valuesAreas.process(myMap.get(innerKey), value)) {
- -- i;
- iterator.remove();
- myMap.remove(key);
- }
- // otherwise we found a "parent", and do not remove the child
- break;
- }
- }
- ++ i;
- }
- }
-
- public Pair<Key, Val> getMapping(final Key key) {
- final Ref<Pair<Key, Val>> result = new Ref<>();
- getSimiliar(key, new PairProcessor<Key, Val>() {
- @Override
- public boolean process(final Key key, final Val val) {
- result.set(Pair.create(key, val));
- return true;
- }
- });
- return result.get();
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2011 JetBrains s.r.o.
- *
- * 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.
- */
-package com.intellij.util;
-
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.util.concurrency.Semaphore;
-
-import java.util.ArrayDeque;
-
-/**
- * @author irengrig
- * Date: 7/5/11
- * Time: 1:48 AM
- */
-public class ProducerConsumer<T> {
- public static final int ourDefaultMaxSize = 20;
-
- private final ArrayDeque<T> myQueue;
- private final Consumer<T> myConsumer;
- private final int myMaxSize;
- private final Object myLock;
- private final ConsumerRunnable myConsumerThread;
- private boolean myIsAlive;
-
- public ProducerConsumer(final Consumer<T> consumer) {
- this(consumer, ourDefaultMaxSize);
- }
-
- public void start() {
- myIsAlive = true;
- myConsumerThread.start();
- }
-
- public void stop() {
- synchronized (myLock) {
- myIsAlive = false;
- myLock.notifyAll();
- }
- }
-
- public ProducerConsumer(final Consumer<T> consumer, final int maxSize) {
- this(consumer, maxSize, false);
- }
-
- public ProducerConsumer(final Consumer<T> consumer, final int maxSize, final boolean onPooledThread) {
- myConsumer = consumer;
- myQueue = new ArrayDeque<>();
- myMaxSize = maxSize;
- myLock = new Object();
-
- if (onPooledThread) {
- myConsumerThread = new PooledConsumerRunnable();
- ApplicationManager.getApplication().executeOnPooledThread(myConsumerThread);
- } else {
- myConsumerThread = new ConsumerRunnable();
- }
- }
-
- private class PooledConsumerRunnable extends ConsumerRunnable {
- private final Semaphore mySemaphore;
-
- private PooledConsumerRunnable() {
- mySemaphore = new Semaphore();
- mySemaphore.down();
- }
-
- public void start() {
- mySemaphore.up();
- }
-
- @Override
- protected void waitForStart() {
- mySemaphore.waitFor();
- }
- }
-
- private class ConsumerRunnable implements Runnable {
- private Thread myThread;
-
- public void start() {
- myThread.start();
- }
-
- public void setThread(Thread thread) {
- myThread = thread;
- }
-
- @Override
- public void run() {
- waitForStart();
- synchronized (myLock) {
- while (myIsAlive) {
- if (! myQueue.isEmpty()) {
- myConsumer.consume(myQueue.removeFirst());
- } else {
- try {
- myLock.wait(10);
- }
- catch (InterruptedException e) {
- //
- }
- }
- }
- }
- }
-
- protected void waitForStart() {
- }
- }
-
- public void produce(final T t) {
- synchronized (myLock) {
- while (myQueue.size() >= myMaxSize) {
- try {
- myLock.notifyAll();
- myLock.wait(10);
- }
- catch (InterruptedException e) {
- //
- }
- }
- myQueue.addLast(t);
- myLock.notifyAll();
- }
- }
-}