git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7248 6c8d7289-2bf4-0310-a012-ef5d649a1542pull/1/head
parent
e8f90201a5
commit
e4d561971e
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* DynamicScore
|
||||||
|
* Copyright 2010 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany
|
||||||
|
* First released 14.10.2010 at http://yacy.net
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program in the file lgpl21.txt
|
||||||
|
* If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.yacy.cora.storage;
|
||||||
|
|
||||||
|
public interface DynamicScore<E> extends StaticScore<E> {
|
||||||
|
|
||||||
|
public void incScore(final E obj);
|
||||||
|
|
||||||
|
public void decScore(final E obj);
|
||||||
|
|
||||||
|
public void addScore(final E obj, final int incrementScore);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
/**
|
||||||
|
* IntScore
|
||||||
|
* Copyright 2010 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany
|
||||||
|
* First released 14.10.2010 at http://yacy.net
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program in the file lgpl21.txt
|
||||||
|
* If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.yacy.cora.storage;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class acts as a replacement for Long and shall be used as counter object in Object-Counter relations
|
||||||
|
* The use case of this class is given when an value element of a map must be increased or decreased. If
|
||||||
|
* the normal Long class is used, the new value must be rewritten to the map with an increased and newly allocated number object
|
||||||
|
* When using this class, then only the score of the Number object can be changed without the need of
|
||||||
|
* rewriting the new key value to a map.
|
||||||
|
*/
|
||||||
|
public class IntScore implements Comparable<IntScore>, Comparator<IntScore> {
|
||||||
|
|
||||||
|
public static IntScore ZERO = new IntScore(0);
|
||||||
|
public static IntScore ONE = new IntScore(1);
|
||||||
|
|
||||||
|
private int value;
|
||||||
|
|
||||||
|
public IntScore(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final static IntScore valueOf(final int n) {
|
||||||
|
return new IntScore(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int intValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void inc() {
|
||||||
|
this.value++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void inc(int n) {
|
||||||
|
this.value += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dec() {
|
||||||
|
this.value--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dec(int n) {
|
||||||
|
this.value -= n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int n) {
|
||||||
|
this.value = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void min(int n) {
|
||||||
|
if (n < this.value) this.value = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void max(int n) {
|
||||||
|
if (n > this.value) this.value = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
return (o instanceof IntScore) && this.value == ((IntScore) o).value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
return (int) (this.value ^ (this.value >>> 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compareTo(IntScore o) {
|
||||||
|
int thisVal = this.value;
|
||||||
|
int anotherVal = o.value;
|
||||||
|
return thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compare(IntScore o1, IntScore o2) {
|
||||||
|
return o1.compareTo(o2);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,265 @@
|
|||||||
|
/**
|
||||||
|
* ScoreMap
|
||||||
|
* Copyright 2010 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany
|
||||||
|
* First released 14.10.2010 at http://yacy.net
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program in the file lgpl21.txt
|
||||||
|
* If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.yacy.cora.storage;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
|
||||||
|
public class ScoreMap<E> implements DynamicScore<E> {
|
||||||
|
|
||||||
|
protected final Map<E, IntScore> map; // a mapping from a reference to the cluster key
|
||||||
|
private long gcount;
|
||||||
|
|
||||||
|
public ScoreMap() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScoreMap(Comparator<? super E> comparator) {
|
||||||
|
if (comparator == null) {
|
||||||
|
map = new HashMap<E, IntScore>();
|
||||||
|
} else {
|
||||||
|
map = new TreeMap<E, IntScore>(comparator);
|
||||||
|
}
|
||||||
|
gcount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void clear() {
|
||||||
|
map.clear();
|
||||||
|
gcount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* shrink the cluster to a demanded size
|
||||||
|
* @param maxsize
|
||||||
|
*/
|
||||||
|
public void shrinkToMaxSize(int maxsize) {
|
||||||
|
if (this.map.size() <= maxsize) return;
|
||||||
|
int minScore = getMinScore();
|
||||||
|
while (this.map.size() > maxsize) {
|
||||||
|
minScore++;
|
||||||
|
shrinkToMinScore(minScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* shrink the cluster in such a way that the smallest score is equal or greater than a given minScore
|
||||||
|
* @param minScore
|
||||||
|
*/
|
||||||
|
public void shrinkToMinScore(int minScore) {
|
||||||
|
synchronized (this) {
|
||||||
|
Iterator<Map.Entry<E, IntScore>> i = this.map.entrySet().iterator();
|
||||||
|
Map.Entry<E, IntScore> entry;
|
||||||
|
while (i.hasNext()) {
|
||||||
|
entry = i.next();
|
||||||
|
if (entry.getValue().intValue() < minScore) i.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized long totalCount() {
|
||||||
|
return gcount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized int size() {
|
||||||
|
return map.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean isEmpty() {
|
||||||
|
return map.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void incScore(final E obj) {
|
||||||
|
if (obj == null) return;
|
||||||
|
synchronized (this) {
|
||||||
|
IntScore score = this.map.get(obj);
|
||||||
|
if (score == null) {
|
||||||
|
this.map.put(obj, IntScore.ONE);
|
||||||
|
} else {
|
||||||
|
score.inc();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// increase overall counter
|
||||||
|
gcount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void decScore(final E obj) {
|
||||||
|
if (obj == null) return;
|
||||||
|
synchronized (this) {
|
||||||
|
IntScore score = this.map.get(obj);
|
||||||
|
if (score == null) {
|
||||||
|
this.map.put(obj, IntScore.valueOf(-1));
|
||||||
|
} else {
|
||||||
|
score.dec();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// increase overall counter
|
||||||
|
gcount--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(final E obj, final int newScore) {
|
||||||
|
if (obj == null) return;
|
||||||
|
synchronized (this) {
|
||||||
|
IntScore score = this.map.get(obj);
|
||||||
|
if (score == null) {
|
||||||
|
this.map.put(obj, IntScore.ONE);
|
||||||
|
} else {
|
||||||
|
gcount -= score.intValue();
|
||||||
|
score.set(newScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// increase overall counter
|
||||||
|
gcount += newScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addScore(final E obj, final int incrementScore) {
|
||||||
|
if (obj == null) return;
|
||||||
|
synchronized (this) {
|
||||||
|
IntScore score = this.map.get(obj);
|
||||||
|
if (score == null) {
|
||||||
|
this.map.put(obj, IntScore.valueOf(incrementScore));
|
||||||
|
} else {
|
||||||
|
score.inc(incrementScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// increase overall counter
|
||||||
|
gcount += incrementScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int deleteScore(final E obj) {
|
||||||
|
// deletes entry and returns previous score
|
||||||
|
if (obj == null) return 0;
|
||||||
|
final IntScore score;
|
||||||
|
synchronized (this) {
|
||||||
|
score = map.remove(obj);
|
||||||
|
if (score == null) return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// decrease overall counter
|
||||||
|
gcount -= score.intValue();
|
||||||
|
return score.intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean existsScore(final E obj) {
|
||||||
|
return map.containsKey(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getScore(final E obj) {
|
||||||
|
if (obj == null) return 0;
|
||||||
|
final IntScore score;
|
||||||
|
synchronized (this) {
|
||||||
|
score = map.get(obj);
|
||||||
|
}
|
||||||
|
if (score == null) return 0;
|
||||||
|
return score.intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxScore() {
|
||||||
|
if (map.isEmpty()) return -1;
|
||||||
|
int maxScore = Integer.MIN_VALUE;
|
||||||
|
synchronized (this) {
|
||||||
|
for (Map.Entry<E, IntScore> entry: this.map.entrySet()) if (entry.getValue().intValue() > maxScore) {
|
||||||
|
maxScore = entry.getValue().intValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return maxScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMinScore() {
|
||||||
|
if (map.isEmpty()) return -1;
|
||||||
|
int minScore = Integer.MAX_VALUE;
|
||||||
|
synchronized (this) {
|
||||||
|
for (Map.Entry<E, IntScore> entry: this.map.entrySet()) if (entry.getValue().intValue() < minScore) {
|
||||||
|
minScore = entry.getValue().intValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return minScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E getMaxObject() {
|
||||||
|
if (map.isEmpty()) return null;
|
||||||
|
E maxObject = null;
|
||||||
|
int maxScore = Integer.MIN_VALUE;
|
||||||
|
synchronized (this) {
|
||||||
|
for (Map.Entry<E, IntScore> entry: this.map.entrySet()) if (entry.getValue().intValue() > maxScore) {
|
||||||
|
maxScore = entry.getValue().intValue();
|
||||||
|
maxObject = entry.getKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return maxObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E getMinObject() {
|
||||||
|
if (map.isEmpty()) return null;
|
||||||
|
E minObject = null;
|
||||||
|
int minScore = Integer.MAX_VALUE;
|
||||||
|
synchronized (this) {
|
||||||
|
for (Map.Entry<E, IntScore> entry: this.map.entrySet()) if (entry.getValue().intValue() < minScore) {
|
||||||
|
minScore = entry.getValue().intValue();
|
||||||
|
minObject = entry.getKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return minObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return map.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<E> scores(boolean up) {
|
||||||
|
synchronized (this) {
|
||||||
|
// re-organize entries
|
||||||
|
TreeMap<IntScore, Set<E>> m = new TreeMap<IntScore, Set<E>>();
|
||||||
|
Set<E> s;
|
||||||
|
for (Map.Entry<E, IntScore> entry: this.map.entrySet()) {
|
||||||
|
s = m.get(entry.getValue());
|
||||||
|
if (s == null) {
|
||||||
|
s = this.map instanceof TreeMap ? new TreeSet<E>(((TreeMap<E, IntScore>) this.map).comparator()) : new HashSet<E>();
|
||||||
|
s.add(entry.getKey());
|
||||||
|
m.put(entry.getValue(), s);
|
||||||
|
} else {
|
||||||
|
s.add(entry.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// flatten result
|
||||||
|
List<E> l = new ArrayList<E>(this.map.size());
|
||||||
|
for (Set<E> f: m.values()) {
|
||||||
|
for (E e: f) l.add(e);
|
||||||
|
}
|
||||||
|
if (up) return l.iterator();
|
||||||
|
|
||||||
|
// optionally reverse list
|
||||||
|
List<E> r = new ArrayList<E>(l.size());
|
||||||
|
for (int i = l.size() - 1; i >= 0; i--) r.add(r.get(i));
|
||||||
|
return r.iterator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
* StaticScore
|
||||||
|
* Copyright 2010 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany
|
||||||
|
* First released 14.10.2010 at http://yacy.net
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program in the file lgpl21.txt
|
||||||
|
* If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.yacy.cora.storage;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public interface StaticScore<E> {
|
||||||
|
|
||||||
|
public void clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* shrink the cluster to a demanded size
|
||||||
|
* @param maxsize
|
||||||
|
*/
|
||||||
|
public void shrinkToMaxSize(int maxsize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* shrink the cluster in such a way that the smallest score is equal or greater than a given minScore
|
||||||
|
* @param minScore
|
||||||
|
*/
|
||||||
|
public void shrinkToMinScore(int minScore);
|
||||||
|
|
||||||
|
public long totalCount();
|
||||||
|
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
public boolean isEmpty();
|
||||||
|
|
||||||
|
public void setScore(final E obj, final int newScore);
|
||||||
|
|
||||||
|
public int deleteScore(final E obj);
|
||||||
|
|
||||||
|
public boolean existsScore(final E obj);
|
||||||
|
|
||||||
|
public int getScore(final E obj);
|
||||||
|
|
||||||
|
public int getMaxScore();
|
||||||
|
|
||||||
|
public int getMinScore();
|
||||||
|
|
||||||
|
public E getMaxObject();
|
||||||
|
|
||||||
|
public E getMinObject();
|
||||||
|
|
||||||
|
public String toString();
|
||||||
|
|
||||||
|
public Iterator<E> scores(final boolean up);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue