export index from jni and use for fastscrolling. Works, but is a bit slow still.

This commit is contained in:
Eric House 2011-10-27 07:01:57 -07:00
parent 5282246dd6
commit ed055b83c6
3 changed files with 91 additions and 8 deletions

View file

@ -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)

View file

@ -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() );

View file

@ -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 );
}