Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 cn.hbu.cs.esearch.util; import java.io.IOException; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; public class ArrayDocIdSet extends DocIdSet { private final int[] docIds; private final int lengthMinusone; public ArrayDocIdSet(int[] docids) { docIds = docids; if (docids != null) { lengthMinusone = docids.length - 1; } else { lengthMinusone = -1; } } final private static int binarySearch(int[] a, int key) { return binarySearch(a, key, 0, a.length - 1); } final private static int binarySearch(int[] a, int key, int low, int high) { while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; int cmp; cmp = midVal - key; if (cmp < 0) { low = mid + 1; } else if (cmp > 0) { high = mid - 1; } else { return mid; } } return -(low + 1); } @Override public DocIdSetIterator iterator() { return new DocIdSetIterator() { int doc = -1; int current = -1; int largest = lengthMinusone; @Override public int docID() { return doc; } @Override public int nextDoc() throws IOException { if (current < lengthMinusone) { current++; doc = docIds[current]; return doc; } return DocIdSetIterator.NO_MORE_DOCS; } @Override public int advance(int target) throws IOException { int idx = current < 0 ? binarySearch(docIds, target) : binarySearch(docIds, target, current, largest); if (idx < 0) { idx = -(idx + 1); if (idx >= docIds.length) { return DocIdSetIterator.NO_MORE_DOCS; } } current = idx; doc = docIds[current]; return doc; } @Override public long cost() { // TODO Auto-generated method stub return 0; } }; } }