Code:

package in.susam;

import java.io.File; import java.io.IOException;

import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.util.Version; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field;

public class LuceneDemo { static final String INDEX_DIR = "index1"; public static void main(String[] args) throws Exception { write(); search("content", "integer"); search("tags", "rhyme"); } static void write() throws IOException { // Create index IndexWriter writer = new IndexWriter( FSDirectory.open(new File(INDEX_DIR)), new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED); Document doc; String field; String text; // Add first document doc = new Document();

field = "title"; text = "Humpty Dumpty sat on a wall"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED));

field = "content"; text = "Humpty Dumpty sat on a wall.\n" + "Humpty Dumpty had a great fall;\n" + "All the King's horses and all the King's men,\n" + "Couldn't put Humpty together again."; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); field = "tags"; text = "rhyme"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);

// Add second document doc = new Document();

field = "title"; text = "Jack and Jill went up the hill"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); field = "content"; text = "Jack and Jill went up the hill\n" + "To fetch a pail of water.\n" + "Jack fell down and broke his crown,\n" + "And Jill came tumbling after.\n"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED));

field = "tags"; text = "rhyme"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc); // Add third document doc = new Document(); field = "title"; text = "Fermat's Last Theorem"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED));

field = "content"; text = "In number theory, Fermat's Last Theorem states that no " + "three positive integers a, b, and c can satisfy the " + "equation for a^n + b^n = c^n for any integer value of n " + "greater than two."; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); field = "tags"; text = "math, theorem"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc); // Add fourth document doc = new Document(); field = "title"; text = "Euler's theorem"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); field = "content"; text = "Euler's theorem states that if n is a positive integer and " + "a is a positive integer coprime to n, then a^phi(n) = 1 " + "(mod n) where phi(n) is Euler's totient function."; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED)); field = "tags"; text = "math, theorem"; doc.add(new Field(field, text, Field.Store.YES, Field.Index.ANALYZED));

writer.addDocument(doc);

writer.close(); } static void search(String field, String query) throws IOException, ParseException { IndexSearcher searcher = new IndexSearcher( FSDirectory.open(new File(INDEX_DIR)), true); QueryParser parser = new QueryParser( Version.LUCENE_30, field, new StandardAnalyzer(Version.LUCENE_30)); TopDocs docs = searcher.search(parser.parse(query), 10); System.out.println("Query: '" + query + "' in '" + field + "'"); System.out.println("Total hits: " + docs.totalHits); System.out.println(""); for (int i = 0; i < docs.scoreDocs.length; i++) { ScoreDoc hit = docs.scoreDocs[i]; Document doc = searcher.doc(hit.doc); System.out.println("#" + i); System.out.println("title: " + doc.get("title")); System.out.println("content: " + doc.get("content")); System.out.println("tags: " + doc.get("tags")); System.out.println("id: " + hit.doc); System.out.println("score: " + hit.score); System.out.println(); } } }

Output:
Query: 'integer' in 'content'
Total hits: 2

#0 title: Euler's theorem content: Euler's theorem states that if n is a positive integer and a is a positive integer coprime to n, then a^phi(n) = 1 (mod n) where phi(n) is Euler's totient function. tags: math, theorem id: 3 score: 0.34144828

#1 title: Fermat's Last Theorem content: In number theory, Fermat's Last Theorem states that no three positive integers a, b, and c can satisfy the equation for a^n + b^n = c^n for any integer value of n greater than two. tags: math, theorem id: 2 score: 0.24144039

Query: 'rhyme' in 'tags' Total hits: 2

#0 title: Humpty Dumpty sat on a wall content: Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall; All the King's horses and all the King's men, Couldn't put Humpty together again. tags: rhyme id: 0 score: 1.287682

#1 title: Jack and Jill went up the hill content: Jack and Jill went up the hill To fetch a pail of water. Jack fell down and broke his crown, And Jill came tumbling after.

tags: rhyme id: 1 score: 1.287682

Comparing outputs from QueryPerformanceCounter() of windows.h, clock() of time.h and time() of time.h

Experiment done with Microsoft Visual Studio 2005 Version 8.0.50727.42 (RTM.050727-4200)

A normal debug run:

QueryPerformanceCounter(): ticks elapsed: 7945650
QueryPerformanceCounter(): QueryPerformanceFrequency(): 3579545
QueryPerformanceCounter(): time elapsed: 2.21974
clock(): ticks elapsed: 2263
clock(): CLOCKS_PER_SEC: 1000
clock(): time elapsed: 2.263
time(): time elapsed: 3
Stopped at a breakpoint for about 40 seconds before continuing:
QueryPerformanceCounter(): ticks elapsed: 151086932
QueryPerformanceCounter(): QueryPerformanceFrequency(): 3579545
QueryPerformanceCounter(): time elapsed: 42.2084
clock(): ticks elapsed: 42331
clock(): CLOCKS_PER_SEC: 1000
clock(): time elapsed: 42.331
time(): time elapsed: 43

Changes:

Index:
src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java
===================================================================
--- src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java
(revision 925455)
+++ src/main/java/org/apache/commons/math/util/ResizableDoubleArray.java
(working copy)
@@ -157,6 +157,14 @@
     public ResizableDoubleArray(int initialCapacity) {
         setInitialCapacity(initialCapacity);
         internalArray = new double[this.initialCapacity];
+        System.out.println("---- initialCapacity: " + initialCapacity);
+        System.out.println("---- expansionMode: " +
+                           (expansionMode == 0 ? "MULTIPLICATIVE_MODE"
+                                               : "ADDITIVE_MODE" ));
+        System.out.println("---- expansionFactor: " + expansionFactor);
+        System.out.println("---- contractionCriteria: " +
+                           contractionCriteria);
+        System.out.println();
     }
 
     /**
@@ -264,14 +272,33 @@
      * @param value to be added to end of array
      */
     public synchronized void addElement(double value) {
+        System.out.println("---- addElement(" + value + ")");
+        System.out.println("---- startIndex: " + startIndex);
+        System.out.println("---- internalArray.length: " +
+                           internalArray.length);
         numElements++;
+        System.out.println("---- numElements incremented to: " +
+                           numElements);
+
         if ((startIndex + numElements) > internalArray.length) {
+            System.out.println("---- expanding ...");
             expand();
+            System.out.println("---- expanded; internalArray.length: "
+
+                               internalArray.length);
         }
         internalArray[startIndex + (numElements - 1)] = value;
+        System.out.print("---- internalArray: ");
+        for (int i = 0; i < startIndex + numElements; i++) {
+            System.out.print(internalArray[i] + ", ");
+        }
+        System.out.println();
         if (shouldContract()) {
+            System.out.println("---- contracting ...");
             contract();
+            System.out.println("---- contracted; internalArray.length:
" +
+                               internalArray.length);
         }
+        System.out.println();
     }
 
     /**
Test Program:
import org.apache.commons.math.util.ResizableDoubleArray;

public class RDAContractExpand { public static void main(String[] args) { ResizableDoubleArray rda = new ResizableDoubleArray(10); for (int i = 0; i < 10; i++) rda.addElement(i); } }

Output:
---- initialCapacity: 10
---- expansionMode: MULTIPLICATIVE_MODE
---- expansionFactor: 2.0
---- contractionCriteria: 2.5

---- addElement(0.0) ---- startIndex: 0 ---- internalArray.length: 10 ---- numElements incremented to: 1 ---- internalArray: 0.0, ---- contracting ... ---- contracted; internalArray.length: 2

---- addElement(1.0) ---- startIndex: 0 ---- internalArray.length: 2 ---- numElements incremented to: 2 ---- internalArray: 0.0, 1.0,

---- addElement(2.0) ---- startIndex: 0 ---- internalArray.length: 2 ---- numElements incremented to: 3 ---- expanding ... ---- expanded; internalArray.length: 4 ---- internalArray: 0.0, 1.0, 2.0,

---- addElement(3.0) ---- startIndex: 0 ---- internalArray.length: 4 ---- numElements incremented to: 4 ---- internalArray: 0.0, 1.0, 2.0, 3.0,

---- addElement(4.0) ---- startIndex: 0 ---- internalArray.length: 4 ---- numElements incremented to: 5 ---- expanding ... ---- expanded; internalArray.length: 8 ---- internalArray: 0.0, 1.0, 2.0, 3.0, 4.0,

---- addElement(5.0) ---- startIndex: 0 ---- internalArray.length: 8 ---- numElements incremented to: 6 ---- internalArray: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0,

---- addElement(6.0) ---- startIndex: 0 ---- internalArray.length: 8 ---- numElements incremented to: 7 ---- internalArray: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,

---- addElement(7.0) ---- startIndex: 0 ---- internalArray.length: 8 ---- numElements incremented to: 8 ---- internalArray: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,

---- addElement(8.0) ---- startIndex: 0 ---- internalArray.length: 8 ---- numElements incremented to: 9 ---- expanding ... ---- expanded; internalArray.length: 16 ---- internalArray: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,

---- addElement(9.0) ---- startIndex: 0 ---- internalArray.length: 16 ---- numElements incremented to: 10 ---- internalArray: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,

Newer | Older
RSS