Home | History | Annotate | Download | only in marisa-trie
      1 diff --git a/Android.mk b/Android.mk
      2 index 9da2083..9d5fc12 100644
      3 --- a/Android.mk
      4 +++ b/Android.mk
      5 @@ -16,11 +16,9 @@ LOCAL_PATH := $(call my-dir)
      6  
      7  include $(CLEAR_VARS)
      8  
      9 -LOCAL_C_INCLUDES += $(LOCAL_PATH)/lib/marisa
     10 -LOCAL_CFLAGS += -fexceptions
     11 -LOCAL_CPP_EXTENSION := .cc
     12 +LOCAL_C_INCLUDES += $(LOCAL_PATH)/lib/marisa external/stlport
     13  
     14 -LOCAL_NDK_STL_VARIANT := gnustl_static
     15 +LOCAL_CPP_EXTENSION := .cc
     16  
     17  LOCAL_SRC_FILES := lib/marisa/base.cc \
     18  	lib/marisa/intvector.cc \
     19 @@ -36,7 +34,7 @@ LOCAL_SRC_FILES := lib/marisa/base.cc \
     20  	lib/marisa/writer.cc
     21  
     22  LOCAL_MODULE := libmarisa-trie
     23 +LOCAL_MODULE_TAGS := optional
     24  
     25 -LOCAL_SDK_VERSION := 14
     26 -
     27 +include external/stlport/libstlport.mk
     28  include $(BUILD_STATIC_LIBRARY)
     29 diff --git a/lib/marisa/base.h b/lib/marisa/base.h
     30 index c2b2b07..731b24a 100644
     31 --- a/lib/marisa/base.h
     32 +++ b/lib/marisa/base.h
     33 @@ -13,6 +13,10 @@
     34  #include <stddef.h>
     35  #endif  // __cplusplus
     36  
     37 +#if defined(__ANDROID__)
     38 +#include <android/log.h>
     39 +#endif  // __ANDROID__
     40 +
     41  #ifdef __cplusplus
     42  extern "C" {
     43  #endif  // __cplusplus
     44 @@ -183,8 +187,22 @@ class Exception {
     45  };
     46  
     47  // MARISA_THROW adds a filename and a line number to an exception.
     48 +#if !defined(__ANDROID__)
     49  #define MARISA_THROW(status) \
     50    (throw Exception(__FILE__, __LINE__, status))
     51 +#else
     52 +
     53 +inline int android_log_exception(int status) {
     54 +  char tmpbuf[100];
     55 +  snprintf(tmpbuf, sizeof(tmpbuf), "marisa exception: %d", status);
     56 +  __android_log_write(ANDROID_LOG_ERROR, "marisa-trie", tmpbuf);
     57 +  return 0;
     58 +}
     59 +
     60 +#define MARISA_THROW(status) \
     61 +  (android_log_exception(status))
     62 +
     63 +#endif  // __ANDROID__
     64  
     65  // MARISA_THROW_IF throws an exception with `status' if `cond' is true.
     66  #define MARISA_THROW_IF(cond, status) \
     67 diff --git a/lib/marisa/reader.cc b/lib/marisa/reader.cc
     68 index 6ecc797..df144ef 100644
     69 --- a/lib/marisa/reader.cc
     70 +++ b/lib/marisa/reader.cc
     71 @@ -80,11 +80,7 @@ void Reader::read_data(void *buf, std::size_t size) {
     72        MARISA_THROW(MARISA_IO_ERROR);
     73      }
     74    } else if (stream_ != NULL) {
     75 -    try {
     76 -      if (!stream_->read(static_cast<char *>(buf), size)) {
     77 -        MARISA_THROW(MARISA_IO_ERROR);
     78 -      }
     79 -    } catch (const std::ios_base::failure &) {
     80 +    if (!stream_->read(static_cast<char *>(buf), size)) {
     81        MARISA_THROW(MARISA_IO_ERROR);
     82      }
     83    } else {
     84 diff --git a/lib/marisa/trie-build.cc b/lib/marisa/trie-build.cc
     85 index 4421432..fb7f072 100644
     86 --- a/lib/marisa/trie-build.cc
     87 +++ b/lib/marisa/trie-build.cc
     88 @@ -63,15 +63,9 @@ void Trie::build_trie(Vector<Key<String> > &keys,
     89      build_trie(keys, static_cast<UInt32 *>(NULL), flags);
     90      return;
     91    }
     92 -  try {
     93 -    std::vector<UInt32> temp_key_ids(keys.size());
     94 -    build_trie(keys, temp_key_ids.empty() ? NULL : &temp_key_ids[0], flags);
     95 -    key_ids->swap(temp_key_ids);
     96 -  } catch (const std::bad_alloc &) {
     97 -    MARISA_THROW(MARISA_MEMORY_ERROR);
     98 -  } catch (const std::length_error &) {
     99 -    MARISA_THROW(MARISA_SIZE_ERROR);
    100 -  }
    101 +  std::vector<UInt32> temp_key_ids(keys.size());
    102 +  build_trie(keys, temp_key_ids.empty() ? NULL : &temp_key_ids[0], flags);
    103 +  key_ids->swap(temp_key_ids);
    104  }
    105  
    106  void Trie::build_trie(Vector<Key<String> > &keys,
    107 @@ -168,7 +162,7 @@ void Trie::build_trie(Vector<Key<T> > &keys,
    108  
    109  template <typename T>
    110  void Trie::build_cur(Vector<Key<T> > &keys,
    111 -    Vector<UInt32> *terminals, Progress &progress) try {
    112 +    Vector<UInt32> *terminals, Progress &progress) {
    113    num_keys_ = sort_keys(keys);
    114    louds_.push_back(true);
    115    louds_.push_back(false);
    116 @@ -261,10 +255,6 @@ void Trie::build_cur(Vector<Key<T> > &keys,
    117  
    118    build_terminals(keys, terminals);
    119    keys.swap(&rest_keys);
    120 -} catch (const std::bad_alloc &) {
    121 -  MARISA_THROW(MARISA_MEMORY_ERROR);
    122 -} catch (const std::length_error &) {
    123 -  MARISA_THROW(MARISA_SIZE_ERROR);
    124  }
    125  
    126  void Trie::build_next(Vector<Key<String> > &keys,
    127 diff --git a/lib/marisa/trie-c.cc b/lib/marisa/trie-c.cc
    128 index 00ebe80..9e11405 100644
    129 --- a/lib/marisa/trie-c.cc
    130 +++ b/lib/marisa/trie-c.cc
    131 @@ -79,106 +79,88 @@ marisa_status marisa_end(marisa_trie *h) {
    132  
    133  marisa_status marisa_build(marisa_trie *h, const char * const *keys,
    134      size_t num_keys, const size_t *key_lengths, const double *key_weights,
    135 -    marisa_uint32 *key_ids, int flags) try {
    136 +    marisa_uint32 *key_ids, int flags) {
    137    if (h == NULL) {
    138      return MARISA_HANDLE_ERROR;
    139    }
    140    h->trie.build(keys, num_keys, key_lengths, key_weights, key_ids, flags);
    141    h->mapper.clear();
    142    return MARISA_OK;
    143 -} catch (const marisa::Exception &ex) {
    144 -  return ex.status();
    145  }
    146  
    147  marisa_status marisa_mmap(marisa_trie *h, const char *filename,
    148 -    long offset, int whence) try {
    149 +    long offset, int whence) {
    150    if (h == NULL) {
    151      return MARISA_HANDLE_ERROR;
    152    }
    153    h->trie.mmap(&h->mapper, filename, offset, whence);
    154    return MARISA_OK;
    155 -} catch (const marisa::Exception &ex) {
    156 -  return ex.status();
    157  }
    158  
    159 -marisa_status marisa_map(marisa_trie *h, const void *ptr, size_t size) try {
    160 +marisa_status marisa_map(marisa_trie *h, const void *ptr, size_t size) {
    161    if (h == NULL) {
    162      return MARISA_HANDLE_ERROR;
    163    }
    164    h->trie.map(ptr, size);
    165    h->mapper.clear();
    166    return MARISA_OK;
    167 -} catch (const marisa::Exception &ex) {
    168 -  return ex.status();
    169  }
    170  
    171  marisa_status marisa_load(marisa_trie *h, const char *filename,
    172 -    long offset, int whence) try {
    173 +    long offset, int whence) {
    174    if (h == NULL) {
    175      return MARISA_HANDLE_ERROR;
    176    }
    177    h->trie.load(filename, offset, whence);
    178    h->mapper.clear();
    179    return MARISA_OK;
    180 -} catch (const marisa::Exception &ex) {
    181 -  return ex.status();
    182  }
    183  
    184 -marisa_status marisa_fread(marisa_trie *h, FILE *file) try {
    185 +marisa_status marisa_fread(marisa_trie *h, FILE *file) {
    186    if (h == NULL) {
    187      return MARISA_HANDLE_ERROR;
    188    }
    189    h->trie.fread(file);
    190    h->mapper.clear();
    191    return MARISA_OK;
    192 -} catch (const marisa::Exception &ex) {
    193 -  return ex.status();
    194  }
    195  
    196 -marisa_status marisa_read(marisa_trie *h, int fd) try {
    197 +marisa_status marisa_read(marisa_trie *h, int fd) {
    198    if (h == NULL) {
    199      return MARISA_HANDLE_ERROR;
    200    }
    201    h->trie.read(fd);
    202    h->mapper.clear();
    203    return MARISA_OK;
    204 -} catch (const marisa::Exception &ex) {
    205 -  return ex.status();
    206  }
    207  
    208  marisa_status marisa_save(const marisa_trie *h, const char *filename,
    209 -    int trunc_flag, long offset, int whence) try {
    210 +    int trunc_flag, long offset, int whence) {
    211    if (h == NULL) {
    212      return MARISA_HANDLE_ERROR;
    213    }
    214    h->trie.save(filename, trunc_flag != 0, offset, whence);
    215    return MARISA_OK;
    216 -} catch (const marisa::Exception &ex) {
    217 -  return ex.status();
    218  }
    219  
    220 -marisa_status marisa_fwrite(const marisa_trie *h, FILE *file) try {
    221 +marisa_status marisa_fwrite(const marisa_trie *h, FILE *file) {
    222    if (h == NULL) {
    223      return MARISA_HANDLE_ERROR;
    224    }
    225    h->trie.fwrite(file);
    226    return MARISA_OK;
    227 -} catch (const marisa::Exception &ex) {
    228 -  return ex.status();
    229  }
    230  
    231 -marisa_status marisa_write(const marisa_trie *h, int fd) try {
    232 +marisa_status marisa_write(const marisa_trie *h, int fd) {
    233    if (h == NULL) {
    234      return MARISA_HANDLE_ERROR;
    235    }
    236    h->trie.write(fd);
    237    return MARISA_OK;
    238 -} catch (const marisa::Exception &ex) {
    239 -  return ex.status();
    240  }
    241  
    242  marisa_status marisa_restore(const marisa_trie *h, marisa_uint32 key_id,
    243 -    char *key_buf, size_t key_buf_size, size_t *key_length) try {
    244 +    char *key_buf, size_t key_buf_size, size_t *key_length) {
    245    if (h == NULL) {
    246      return MARISA_HANDLE_ERROR;
    247    } else if (key_length == NULL) {
    248 @@ -186,12 +168,10 @@ marisa_status marisa_restore(const marisa_trie *h, marisa_uint32 key_id,
    249    }
    250    *key_length = h->trie.restore(key_id, key_buf, key_buf_size);
    251    return MARISA_OK;
    252 -} catch (const marisa::Exception &ex) {
    253 -  return ex.status();
    254  }
    255  
    256  marisa_status marisa_lookup(const marisa_trie *h,
    257 -    const char *ptr, size_t length, marisa_uint32 *key_id) try {
    258 +    const char *ptr, size_t length, marisa_uint32 *key_id) {
    259    if (h == NULL) {
    260      return MARISA_HANDLE_ERROR;
    261    } else if (key_id == NULL) {
    262 @@ -203,14 +183,12 @@ marisa_status marisa_lookup(const marisa_trie *h,
    263      *key_id = h->trie.lookup(ptr, length);
    264    }
    265    return MARISA_OK;
    266 -} catch (const marisa::Exception &ex) {
    267 -  return ex.status();
    268  }
    269  
    270  marisa_status marisa_find(const marisa_trie *h,
    271      const char *ptr, size_t length,
    272      marisa_uint32 *key_ids, size_t *key_lengths,
    273 -    size_t max_num_results, size_t *num_results) try {
    274 +    size_t max_num_results, size_t *num_results) {
    275    if (h == NULL) {
    276      return MARISA_HANDLE_ERROR;
    277    } else if (num_results == NULL) {
    278 @@ -223,8 +201,6 @@ marisa_status marisa_find(const marisa_trie *h,
    279          key_ids, key_lengths, max_num_results);
    280    }
    281    return MARISA_OK;
    282 -} catch (const marisa::Exception &ex) {
    283 -  return ex.status();
    284  }
    285  
    286  marisa_status marisa_find_first(const marisa_trie *h,
    287 @@ -262,7 +238,7 @@ marisa_status marisa_find_last(const marisa_trie *h,
    288  marisa_status marisa_find_callback(const marisa_trie *h,
    289      const char *ptr, size_t length,
    290      int (*callback)(void *, marisa_uint32, size_t),
    291 -    void *first_arg_to_callback) try {
    292 +    void *first_arg_to_callback) {
    293    if (h == NULL) {
    294      return MARISA_HANDLE_ERROR;
    295    } else if (callback == NULL) {
    296 @@ -276,8 +252,6 @@ marisa_status marisa_find_callback(const marisa_trie *h,
    297          ::FindCallback(callback, first_arg_to_callback));
    298    }
    299    return MARISA_OK;
    300 -} catch (const marisa::Exception &ex) {
    301 -  return ex.status();
    302  }
    303  
    304  marisa_status marisa_predict(const marisa_trie *h,
    305 @@ -289,7 +263,7 @@ marisa_status marisa_predict(const marisa_trie *h,
    306  
    307  marisa_status marisa_predict_breadth_first(const marisa_trie *h,
    308      const char *ptr, size_t length, marisa_uint32 *key_ids,
    309 -    size_t max_num_results, size_t *num_results) try {
    310 +    size_t max_num_results, size_t *num_results) {
    311    if (h == NULL) {
    312      return MARISA_HANDLE_ERROR;
    313    } else if (num_results == NULL) {
    314 @@ -303,13 +277,11 @@ marisa_status marisa_predict_breadth_first(const marisa_trie *h,
    315          ptr, length, key_ids, NULL, max_num_results);
    316    }
    317    return MARISA_OK;
    318 -} catch (const marisa::Exception &ex) {
    319 -  return ex.status();
    320  }
    321  
    322  marisa_status marisa_predict_depth_first(const marisa_trie *h,
    323      const char *ptr, size_t length, marisa_uint32 *key_ids,
    324 -    size_t max_num_results, size_t *num_results) try {
    325 +    size_t max_num_results, size_t *num_results) {
    326    if (h == NULL) {
    327      return MARISA_HANDLE_ERROR;
    328    } else if (num_results == NULL) {
    329 @@ -323,14 +295,12 @@ marisa_status marisa_predict_depth_first(const marisa_trie *h,
    330          ptr, length, key_ids, NULL, max_num_results);
    331    }
    332    return MARISA_OK;
    333 -} catch (const marisa::Exception &ex) {
    334 -  return ex.status();
    335  }
    336  
    337  marisa_status marisa_predict_callback(const marisa_trie *h,
    338      const char *ptr, size_t length,
    339      int (*callback)(void *, marisa_uint32, const char *, size_t),
    340 -    void *first_arg_to_callback) try {
    341 +    void *first_arg_to_callback) {
    342    if (h == NULL) {
    343      return MARISA_HANDLE_ERROR;
    344    } else if (callback == NULL) {
    345 @@ -344,8 +314,6 @@ marisa_status marisa_predict_callback(const marisa_trie *h,
    346          ::PredictCallback(callback, first_arg_to_callback));
    347    }
    348    return MARISA_OK;
    349 -} catch (const marisa::Exception &ex) {
    350 -  return ex.status();
    351  }
    352  
    353  size_t marisa_get_num_tries(const marisa_trie *h) {
    354 diff --git a/lib/marisa/trie-inline.h b/lib/marisa/trie-inline.h
    355 index 6b1e502..2c9218c 100644
    356 --- a/lib/marisa/trie-inline.h
    357 +++ b/lib/marisa/trie-inline.h
    358 @@ -179,7 +179,7 @@ inline bool Trie::find_child(UInt32 &node, T query,
    359  }
    360  
    361  template <typename T, typename U>
    362 -std::size_t Trie::find_callback_(T query, U callback) const try {
    363 +std::size_t Trie::find_callback_(T query, U callback) const {
    364    std::size_t count = 0;
    365    UInt32 node = 0;
    366    std::size_t pos = 0;
    367 @@ -192,10 +192,6 @@ std::size_t Trie::find_callback_(T query, U callback) const try {
    368      }
    369    } while (!query.ends_at(pos) && find_child<T>(node, query, pos));
    370    return count;
    371 -} catch (const std::bad_alloc &) {
    372 -  MARISA_THROW(MARISA_MEMORY_ERROR);
    373 -} catch (const std::length_error &) {
    374 -  MARISA_THROW(MARISA_SIZE_ERROR);
    375  }
    376  
    377  template <typename T>
    378 @@ -235,7 +231,7 @@ inline bool Trie::predict_child(UInt32 &node, T query, std::size_t &pos,
    379  }
    380  
    381  template <typename T, typename U>
    382 -std::size_t Trie::predict_callback_(T query, U callback) const try {
    383 +std::size_t Trie::predict_callback_(T query, U callback) const {
    384    std::string key;
    385    UInt32 node = 0;
    386    std::size_t pos = 0;
    387 @@ -299,10 +295,6 @@ std::size_t Trie::predict_callback_(T query, U callback) const try {
    388      ++stack_pos;
    389    }
    390    return count;
    391 -} catch (const std::bad_alloc &) {
    392 -  MARISA_THROW(MARISA_MEMORY_ERROR);
    393 -} catch (const std::length_error &) {
    394 -  MARISA_THROW(MARISA_SIZE_ERROR);
    395  }
    396  
    397  inline UInt32 Trie::key_id_to_node(UInt32 key_id) const {
    398 diff --git a/lib/marisa/trie-search.cc b/lib/marisa/trie-search.cc
    399 index 1f35681..098e0b3 100644
    400 --- a/lib/marisa/trie-search.cc
    401 +++ b/lib/marisa/trie-search.cc
    402 @@ -247,30 +247,22 @@ std::size_t Trie::predict_depth_first(
    403  
    404  void Trie::restore_(UInt32 key_id, std::string *key) const {
    405    const std::size_t start_pos = key->length();
    406 -  try {
    407 -    UInt32 node = key_id_to_node(key_id);
    408 -    while (node != 0) {
    409 -      if (has_link(node)) {
    410 -        const std::size_t prev_pos = key->length();
    411 -        if (has_trie()) {
    412 -          trie_->trie_restore(get_link(node), key);
    413 -        } else {
    414 -          tail_restore(node, key);
    415 -        }
    416 -        std::reverse(key->begin() + prev_pos, key->end());
    417 +  UInt32 node = key_id_to_node(key_id);
    418 +  while (node != 0) {
    419 +    if (has_link(node)) {
    420 +      const std::size_t prev_pos = key->length();
    421 +      if (has_trie()) {
    422 +        trie_->trie_restore(get_link(node), key);
    423        } else {
    424 -        *key += labels_[node];
    425 +        tail_restore(node, key);
    426        }
    427 -      node = get_parent(node);
    428 -    }
    429 -    std::reverse(key->begin() + start_pos, key->end());
    430 -  } catch (const std::bad_alloc &) {
    431 -    key->resize(start_pos);
    432 -    MARISA_THROW(MARISA_MEMORY_ERROR);
    433 -  } catch (const std::length_error &) {
    434 -    key->resize(start_pos);
    435 -    MARISA_THROW(MARISA_SIZE_ERROR);
    436 +      std::reverse(key->begin() + prev_pos, key->end());
    437 +    } else {
    438 +      *key += labels_[node];
    439 +    }
    440 +    node = get_parent(node);
    441    }
    442 +  std::reverse(key->begin() + start_pos, key->end());
    443  }
    444  
    445  void Trie::trie_restore(UInt32 node, std::string *key) const {
    446 @@ -468,7 +460,7 @@ template std::size_t Trie::tail_match<const Query &>(UInt32 node,
    447  
    448  template <typename T, typename U, typename V>
    449  std::size_t Trie::find_(T query, U key_ids, V key_lengths,
    450 -    std::size_t max_num_results) const try {
    451 +    std::size_t max_num_results) const {
    452    if (max_num_results == 0) {
    453      return 0;
    454    }
    455 @@ -489,10 +481,6 @@ std::size_t Trie::find_(T query, U key_ids, V key_lengths,
    456      }
    457    } while (!query.ends_at(pos) && find_child<T>(node, query, pos));
    458    return count;
    459 -} catch (const std::bad_alloc &) {
    460 -  MARISA_THROW(MARISA_MEMORY_ERROR);
    461 -} catch (const std::length_error &) {
    462 -  MARISA_THROW(MARISA_SIZE_ERROR);
    463  }
    464  
    465  template <typename T>
    466 @@ -533,7 +521,7 @@ UInt32 Trie::find_last_(T query, std::size_t *key_length) const {
    467  
    468  template <typename T, typename U, typename V>
    469  std::size_t Trie::predict_breadth_first_(T query, U key_ids, V keys,
    470 -    std::size_t max_num_results) const try {
    471 +    std::size_t max_num_results) const {
    472    if (max_num_results == 0) {
    473      return 0;
    474    }
    475 @@ -596,15 +584,11 @@ std::size_t Trie::predict_breadth_first_(T query, U key_ids, V keys,
    476      node_end = louds_pos_to_node(get_child(node_end), node_end);
    477    }
    478    return count;
    479 -} catch (const std::bad_alloc &) {
    480 -  MARISA_THROW(MARISA_MEMORY_ERROR);
    481 -} catch (const std::length_error &) {
    482 -  MARISA_THROW(MARISA_SIZE_ERROR);
    483  }
    484  
    485  template <typename T, typename U, typename V>
    486  std::size_t Trie::predict_depth_first_(T query, U key_ids, V keys,
    487 -    std::size_t max_num_results) const try {
    488 +    std::size_t max_num_results) const {
    489    if (max_num_results == 0) {
    490      return 0;
    491    } else if (keys.is_valid()) {
    492 @@ -665,10 +649,6 @@ std::size_t Trie::predict_depth_first_(T query, U key_ids, V keys,
    493      ++stack_pos;
    494    }
    495    return count;
    496 -} catch (const std::bad_alloc &) {
    497 -  MARISA_THROW(MARISA_MEMORY_ERROR);
    498 -} catch (const std::length_error &) {
    499 -  MARISA_THROW(MARISA_SIZE_ERROR);
    500  }
    501  
    502  template <typename T>
    503 diff --git a/lib/marisa/writer.cc b/lib/marisa/writer.cc
    504 index 2256f59..55dcb97 100644
    505 --- a/lib/marisa/writer.cc
    506 +++ b/lib/marisa/writer.cc
    507 @@ -92,11 +92,7 @@ void Writer::write_data(const void *data, std::size_t size) {
    508        MARISA_THROW(MARISA_IO_ERROR);
    509      }
    510    } else if (stream_ != NULL) {
    511 -    try {
    512 -      if (!stream_->write(static_cast<const char *>(data), size)) {
    513 -        MARISA_THROW(MARISA_IO_ERROR);
    514 -      }
    515 -    } catch (const std::ios_base::failure &) {
    516 +    if (!stream_->write(static_cast<const char *>(data), size)) {
    517        MARISA_THROW(MARISA_IO_ERROR);
    518      }
    519    } else {
    520