- introduced generalized method to organize ranked results (2 new classes) - added a post-ranking after snippet-fetch (before: only listed) using the new ranking data structures - fixed some missing data fields in RWI ranking attributes and correct hand-over between data structures git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4498 6c8d7289-2bf4-0310-a012-ef5d649a1542pull/1/head
parent
f4c73d8c68
commit
727feb4358
@ -0,0 +1,147 @@
|
||||
// kelondroSortStack.java
|
||||
// (C) 2008 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
// first published 20.02.2008 on http://yacy.net
|
||||
//
|
||||
// This is a part of YaCy, a peer-to-peer based web search engine
|
||||
//
|
||||
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
||||
// $LastChangedRevision: 1986 $
|
||||
// $LastChangedBy: orbiter $
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program 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 General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package de.anomic.kelondro;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class kelondroSortStack<E> {
|
||||
|
||||
// implements a stack where elements 'float' on-top of the stack according to a weight value.
|
||||
// objects pushed on the stack must implement the hashCode() method to provide a handle
|
||||
// for a double-check.
|
||||
|
||||
protected TreeMap<Long, E> onstack; // object within the stack
|
||||
protected HashSet<Integer> instack; // keeps track which element has been on the stack or is now in the offstack
|
||||
protected int maxsize;
|
||||
|
||||
public kelondroSortStack(int maxsize) {
|
||||
// the maxsize is the maximum number of entries in the stack
|
||||
// if this is set to -1, the size is unlimited
|
||||
this.onstack = new TreeMap<Long, E>();
|
||||
this.instack = new HashSet<Integer>();
|
||||
this.maxsize = maxsize;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.onstack.size();
|
||||
}
|
||||
|
||||
public synchronized void push(stackElement se) {
|
||||
push(se.element, se.weight);
|
||||
}
|
||||
|
||||
public synchronized void push(E element, long weight) {
|
||||
if (exists(element)) return;
|
||||
|
||||
// manipulate weight in such a way that it has no conflicts
|
||||
Long w = new Long(weight);
|
||||
while (this.onstack.containsKey(w)) w = new Long(w.longValue() + 1);
|
||||
|
||||
// put the element on the stack
|
||||
this.onstack.put(w, element);
|
||||
|
||||
// register it for double-check
|
||||
this.instack.add(element.hashCode());
|
||||
|
||||
// check maximum size of the stack an remove elements if the stack gets too large
|
||||
if (this.maxsize <= 0) return;
|
||||
while ((this.onstack.size() > 0) && (this.onstack.size() > this.maxsize)) {
|
||||
this.onstack.remove(this.onstack.lastKey());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized stackElement top() {
|
||||
// returns the element that is currently on top of the stack
|
||||
if (this.onstack.size() == 0) return null;
|
||||
Long w = this.onstack.firstKey();
|
||||
E element = this.onstack.get(w);
|
||||
return new stackElement(element, w.longValue());
|
||||
}
|
||||
|
||||
public synchronized stackElement pop() {
|
||||
// returns the element that is currently on top of the stack
|
||||
// it is removed and added to the offstack list
|
||||
// this is exactly the same as element(offstack.size())
|
||||
if (this.onstack.size() == 0) return null;
|
||||
Long w = this.onstack.firstKey();
|
||||
E element = this.onstack.remove(w);
|
||||
stackElement se = new stackElement(element, w.longValue());
|
||||
return se;
|
||||
}
|
||||
|
||||
public boolean exists(E element) {
|
||||
// uses the hashCode of the element to find out of the element had been on the list or the stack
|
||||
return this.instack.contains(element.hashCode());
|
||||
}
|
||||
|
||||
public boolean exists(int hashcode) {
|
||||
// uses the hashCode of the element to find out of the element had been on the list or the stack
|
||||
return this.instack.contains(hashcode);
|
||||
}
|
||||
|
||||
public stackElement get(int hashcode) {
|
||||
Iterator<Map.Entry<Long, E>> i = this.onstack.entrySet().iterator();
|
||||
Map.Entry<Long, E> entry;
|
||||
while (i.hasNext()) {
|
||||
entry = i.next();
|
||||
if (entry.getValue().hashCode() == hashcode) return new stackElement(entry.getValue(), entry.getKey().longValue());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public stackElement remove(int hashcode) {
|
||||
Iterator<Map.Entry<Long, E>> i = this.onstack.entrySet().iterator();
|
||||
Map.Entry<Long, E> entry;
|
||||
stackElement se;
|
||||
while (i.hasNext()) {
|
||||
entry = i.next();
|
||||
if (entry.getValue().hashCode() == hashcode) {
|
||||
se = new stackElement(entry.getValue(), entry.getKey().longValue());
|
||||
this.onstack.remove(se.weight);
|
||||
return se;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean bottom(long weight) {
|
||||
// returns true if the element with that weight would be on the bottom of the stack after inserting
|
||||
return weight > this.onstack.lastKey().longValue();
|
||||
}
|
||||
|
||||
public class stackElement {
|
||||
public long weight;
|
||||
public E element;
|
||||
public stackElement(E element, long weight) {
|
||||
this.element = element;
|
||||
this.weight = weight;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
// kelondroSortStore.java
|
||||
// (C) 2008 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
// first published 20.02.2008 on http://yacy.net
|
||||
//
|
||||
// This is a part of YaCy, a peer-to-peer based web search engine
|
||||
//
|
||||
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
||||
// $LastChangedRevision: 1986 $
|
||||
// $LastChangedBy: orbiter $
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program 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 General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package de.anomic.kelondro;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class kelondroSortStore<E> extends kelondroSortStack<E> {
|
||||
|
||||
// extends the sortStack in such a way that it adds a list where objects, that had
|
||||
// been pulled from the stack with pop are listed. Provides access methods to address
|
||||
// specific elements in the list.
|
||||
|
||||
private ArrayList<stackElement> offstack; // objects that had been on the stack but had been removed
|
||||
|
||||
public kelondroSortStore(int maxsize) {
|
||||
super(maxsize);
|
||||
this.offstack = new ArrayList<stackElement>();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return super.onstack.size() + this.offstack.size();
|
||||
}
|
||||
|
||||
public int sizeStore() {
|
||||
return this.offstack.size();
|
||||
}
|
||||
|
||||
public synchronized void push(E element, long weight) {
|
||||
super.push(element, weight);
|
||||
if (this.maxsize <= 0) return;
|
||||
while ((this.onstack.size() > 0) && (super.onstack.size() + this.offstack.size() > this.maxsize)) {
|
||||
this.onstack.remove(this.onstack.lastKey());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized stackElement pop() {
|
||||
// returns the element that is currently on top of the stack
|
||||
// it is removed and added to the offstack list
|
||||
// this is exactly the same as element(offstack.size())
|
||||
stackElement se = super.pop();
|
||||
if (se == null) return null;
|
||||
this.offstack.add(se);
|
||||
return se;
|
||||
}
|
||||
|
||||
public synchronized stackElement element(int position) {
|
||||
// returns an element from a specific position. It is either taken from the offstack,
|
||||
// or removed from the onstack.
|
||||
// The offstack will grow if elements are not from the offstack and present at the onstack.
|
||||
if (position < this.offstack.size()) {
|
||||
return this.offstack.get(position);
|
||||
}
|
||||
if (position >= size()) return null; // we don't have that element
|
||||
while (position >= this.offstack.size()) {
|
||||
Long w = this.onstack.firstKey();
|
||||
E element = this.onstack.remove(w);
|
||||
stackElement se = new stackElement(element, w.longValue());
|
||||
this.offstack.add(se);
|
||||
}
|
||||
return this.offstack.get(position);
|
||||
}
|
||||
|
||||
public ArrayList<stackElement> list(int count) {
|
||||
// returns the specific amount of entries. If they are not yet present in the offstack, they are shifted there from the onstack
|
||||
// if count is < 0 then all elements are taken
|
||||
// the returned list is not cloned from the internal list and shall not be modified in any way (read-only)
|
||||
if (count < 0) {
|
||||
// shift all elements
|
||||
while (this.onstack.size() > 0) {
|
||||
Long w = this.onstack.firstKey();
|
||||
E element = this.onstack.remove(w);
|
||||
stackElement se = new stackElement(element, w.longValue());
|
||||
this.offstack.add(se);
|
||||
}
|
||||
return this.offstack;
|
||||
}
|
||||
if (size() < count) throw new RuntimeException("list(" + count + ") exceeded avaiable number of elements (" + size() + ")");
|
||||
while (this.onstack.size() < count) {
|
||||
Long w = this.onstack.firstKey();
|
||||
E element = this.onstack.remove(w);
|
||||
stackElement se = new stackElement(element, w.longValue());
|
||||
this.offstack.add(se);
|
||||
}
|
||||
return this.offstack;
|
||||
}
|
||||
|
||||
public stackElement get(int hashcode) {
|
||||
stackElement se = super.get(hashcode);
|
||||
if (se != null) return se;
|
||||
Iterator<stackElement> j = this.offstack.iterator();
|
||||
while (j.hasNext()) {
|
||||
se = j.next();
|
||||
if (se.element.hashCode() == hashcode) return se;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public stackElement remove(int hashcode) {
|
||||
stackElement se = super.remove(hashcode);
|
||||
if (se != null) return se;
|
||||
for (int j = 0; j < this.offstack.size(); j++) {
|
||||
se = this.offstack.get(j);
|
||||
if (se.element.hashCode() == hashcode) {
|
||||
this.offstack.remove(j);
|
||||
return se;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue