00001
00002 package de.aitools.aq.invertedindex.core;
00003
00004 import java.io.PrintStream;
00005 import java.util.Iterator;
00006 import java.util.NoSuchElementException;
00007
00008 import com.sun.jna.Native;
00009 import com.sun.jna.Pointer;
00010 import com.sun.jna.Structure;
00011
00012 import de.aitools.aq.check.A;
00013 import de.aitools.aq.invertedindex.usage.UsingSearcher;
00014 import de.aitools.aq.value.Value;
00015 import de.aitools.aq.value.single.SingleInt;
00016 import de.aitools.aq.value.single.SingleString;
00017
00024 public class Postlist<V extends Value> implements Iterable<V> {
00025
00026 static {
00027 Native.register(Configuration.NATIVE_LIB);
00028 }
00029
00036 public static class Head extends Structure {
00037
00043 public int getValueCount() {
00044 return valueCount;
00045 }
00046
00054 public short getValueSize() {
00055 return valueSize;
00056 }
00057
00064 public int getTotalSize() {
00065 return totalSize;
00066 }
00067
00075 public PrintStream print(PrintStream stream) {
00076 stream.format("valueCount : %1$d\n", getValueCount());
00077 stream.format("valueSize : %1$d\n", getValueSize());
00078 stream.format("totalSize : %1$d", getTotalSize());
00079 return stream;
00080 }
00081
00082
00083 public int valueCount;
00084 public short valueSize;
00085 public int totalSize;
00086 }
00087
00088 private class PostlistIterator implements Iterator<V> {
00089
00090 @Override
00091 public boolean hasNext() {
00092 return index < head.valueCount;
00093 }
00094
00095 @Override
00096 public V next() {
00097 if (index >= head.valueCount) {
00098 throw new NoSuchElementException();
00099 }
00100 V value = null;
00101 try {
00102 value = clazz.newInstance();
00103 value.copyFrom(raw_postlist_next(nativePostlist));
00104 ++index;
00105 } catch (InstantiationException e) {
00106 e.printStackTrace();
00107 } catch (IllegalAccessException e) {
00108 e.printStackTrace();
00109 }
00110 return value;
00111 }
00112
00113 @Override
00114 public void remove() {
00115 throw new UnsupportedOperationException();
00116 }
00117
00118 public void rewind() {
00119 raw_postlist_rewind(nativePostlist);
00120 index = 0;
00121 }
00122
00123 private int index;
00124 }
00125
00130 protected Postlist(Class<V> clazz, Pointer nativePtr, Head head) {
00131 A.CHECK_NOT_NULL(clazz, nativePtr, head);
00132 this.iterator = new PostlistIterator();
00133 this.nativePostlist = nativePtr;
00134 this.clazz = clazz;
00135 this.head = head;
00136 }
00137
00138 @Override
00139 public Iterator<V> iterator() {
00140 return iterator;
00141 }
00142
00152 public void deleteNative() {
00153 if (nativePostlist == null) return;
00154 raw_postlist_delete(nativePostlist);
00155 nativePostlist = null;
00156 head.valueCount = 0;
00157 head.valueSize = 0;
00158 head.totalSize = 0;
00159 }
00160
00161 @Override
00162 protected void finalize() throws Throwable {
00163 this.deleteNative();
00164 super.finalize();
00165 }
00166
00172 public Head getHead() {
00173 return head;
00174 }
00175
00181 public boolean isEmpty() {
00182 return head.valueCount == 0;
00183 }
00184
00190 public int size() {
00191 return head.valueCount;
00192 }
00193
00197 public void rewind() {
00198 iterator.rewind();
00199 }
00200
00201 private PostlistIterator iterator;
00202 private Pointer nativePostlist;
00203 private Class<V> clazz;
00204 private Head head;
00205
00206
00207
00208
00209
00210 private native Pointer raw_postlist_next(Pointer raw_postlist);
00211 private native void raw_postlist_rewind(Pointer raw_postlist);
00212 private native void raw_postlist_delete(Pointer raw_postlist);
00213 }