From ed055b83c6ecd1ff77dc5906145300cf86653f50 Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 27 Oct 2011 07:01:57 -0700 Subject: [PATCH] export index from jni and use for fastscrolling. Works, but is a bit slow still. --- xwords4/android/XWords4/jni/xwjni.c | 61 ++++++++++++++++++- .../android/xw4/DictBrowseActivity.java | 35 ++++++++--- .../org/eehouse/android/xw4/jni/XwJNI.java | 3 + 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/xwords4/android/XWords4/jni/xwjni.c b/xwords4/android/XWords4/jni/xwjni.c index bcd6559ca..cbb8db847 100644 --- a/xwords4/android/XWords4/jni/xwjni.c +++ b/xwords4/android/XWords4/jni/xwjni.c @@ -1266,8 +1266,12 @@ Java_org_eehouse_android_xw4_jni_XwJNI_server_1sendChat typedef struct _DictIterData { JNIEnv* env; JNIUtilCtxt* jniutil; + VTableMgr* vtMgr; DictionaryCtxt* dict; DictWord word; + XWStreamCtxt* prefixStream; + DictIndex* indices; + XP_U16 nIndices; #ifdef MEM_DEBUG MemPoolCtx* mpool; #endif @@ -1287,6 +1291,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1init DictionaryCtxt* dict = makeDict( MPPARM(mpool) env, jniutil, NULL, jDictBytes, jpath, NULL ); if ( !!dict ) { + data->vtMgr = make_vtablemgr( MPPARM_NOCOMMA(mpool) ); data->jniutil = jniutil; data->dict = dict; #ifdef MEM_DEBUG @@ -1313,6 +1318,13 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1destroy if ( NULL != data ) { dict_destroy( data->dict ); destroyJNIUtil( &data->jniutil ); + if ( NULL != data->prefixStream ) { + stream_destroy( data->prefixStream ); + } + if ( !!data->indices ) { + XP_FREE( data->mpool, data->indices ); + } + vtmgr_destroy( MPPARM(data->mpool) data->vtMgr ); #ifdef MEM_DEBUG MemPoolCtx* mpool = data->mpool; #endif @@ -1332,10 +1344,57 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1wordCount if ( NULL != data ) { result = data->word.wordCount; } - LOG_RETURNF( "%d", result ); return result; } +JNIEXPORT void JNICALL +Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1makeIndex +( JNIEnv* env, jclass C, jint closure ) +{ + DictIterData* data = (DictIterData*)closure; + if ( NULL != data ) { + XP_U16 depth = 2; + DictIndex indices[depth*32*32]; + XWStreamCtxt* stream = mem_stream_make( MPPARM(data->mpool) data->vtMgr, + NULL, 0, NULL ); + XP_U16 nIndices = dict_makeIndex( data->dict, depth, indices, VSIZE(indices), + stream ); + + data->indices = XP_MALLOC( data->mpool, nIndices * sizeof(*data->indices) ); + XP_MEMCPY( data->indices, indices, nIndices * sizeof(*data->indices) ); + data->nIndices = nIndices; + data->prefixStream = stream; + } +} + +JNIEXPORT jstring JNICALL +Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1getPrefixes +( JNIEnv* env, jclass C, jint closure ) +{ + jstring result = NULL; + DictIterData* data = (DictIterData*)closure; + if ( NULL != data && NULL != data->prefixStream ) { + result = streamToJString( MPPARM(data->mpool) env, data->prefixStream ); + (*env)->DeleteLocalRef( env, result ); + } + return result; +} + +JNIEXPORT jintArray JNICALL +Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1getIndices +( JNIEnv* env, jclass C , jint closure ) +{ + jintArray jindices = NULL; + DictIterData* data = (DictIterData*)closure; + if ( NULL != data ) { + XP_ASSERT( !!data->indices ); + XP_ASSERT( sizeof(jint) == sizeof(data->indices[0]) ); + jindices = makeIntArray( env, data->nIndices, data->indices ); + (*env)->DeleteLocalRef( env, jindices ); + } + return jindices; +} + JNIEXPORT jstring JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1nthWord ( JNIEnv* env, jclass C, jint closure, jint nn) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseActivity.java index f1ad85d36..620962c4f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseActivity.java @@ -23,12 +23,14 @@ package org.eehouse.android.xw4; import android.content.Intent; import android.database.DataSetObserver; import android.os.Bundle; +import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.SectionIndexer; import android.widget.TextView; +import java.util.Arrays; import junit.framework.Assert; @@ -51,6 +53,9 @@ public class DictBrowseActivity extends XWListActivity { private class DictListAdapter extends BaseAdapter implements SectionIndexer { + private String[] m_prefixes; + private int[] m_indices; + public Object getItem( int position ) { TextView text = new TextView( DictBrowseActivity.this ); @@ -76,21 +81,36 @@ public class DictBrowseActivity extends XWListActivity { // SectionIndexer public int getPositionForSection( int section ) { - Utils.logf( "DictBrowseActivity: getPositionForSection" ); - return 0; + return m_indices[section]; } public int getSectionForPosition( int position ) { - Utils.logf( "DictBrowseActivity: getSectionForPosition" ); - return 0; + int section = Arrays.binarySearch( m_indices, position ); + if ( section < 0 ) { + section *= -1; + } + if ( section >= m_indices.length ) { + section = m_indices.length - 1; + } + // for ( section = 0; section < m_indices.length - 1; ++section ) { + // if ( position <= m_indices[section] ) { + // break; + // } + // } + // Utils.logf( "DictBrowseActivity: getSectionForPosition" ); + return section; } public Object[] getSections() { - Utils.logf( "DictBrowseActivity: getSections" ); - String[] sections = { "AA", "BB", "CC", "DD", "EE" }; - return sections; + String prefs = XwJNI.dict_iter_getPrefixes( m_dictClosure ); + m_prefixes = TextUtils.split( prefs, "\n" ); + m_indices = XwJNI.dict_iter_getIndices( m_dictClosure ); + Utils.logf( "len(m_indices): %d; len(m_prefixes): %d", + m_indices.length, m_prefixes.length ); + Assert.assertTrue( m_indices.length == m_prefixes.length ); + return m_prefixes; } } @@ -108,6 +128,7 @@ public class DictBrowseActivity extends XWListActivity { DictUtils.DictPairs pairs = DictUtils.openDicts( this, names ); m_dictClosure = XwJNI.dict_iter_init( pairs.m_bytes[0], pairs.m_paths[0], JNIUtilsImpl.get() ); + XwJNI.dict_iter_makeIndex( m_dictClosure ); setContentView( R.layout.dict_browser ); setListAdapter( new DictListAdapter() ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java index 90504ad8f..6d431d27c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java @@ -241,4 +241,7 @@ public class XwJNI { public static native void dict_iter_destroy( int closure ); public static native int dict_iter_wordCount( int closure ); public static native String dict_iter_nthWord( int closure, int nn ); + public static native void dict_iter_makeIndex( int closure ); + public static native String dict_iter_getPrefixes( int closure ); + public static native int[] dict_iter_getIndices( int closure ); }