nlib
Trie.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_SUCCINCT_TRIE_H_
4 #define INCLUDE_NN_NLIB_SUCCINCT_TRIE_H_
5 
6 #include <string.h>
7 
9 #include "nn/nlib/succinct/Sbv.h"
10 #include "nn/nlib/ReallocVec.h"
11 
12 #if defined(_MSC_VER) && defined(nx_succinct_EXPORTS)
13 #undef NLIB_VIS_PUBLIC
14 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
15 #endif
16 
17 NLIB_NAMESPACE_BEGIN
18 namespace succinct {
19 
21  public:
22  Trie() NLIB_NOEXCEPT : prv_(NULL) {}
24  NLIB_MOVE_MEMBER_HELPER_1(Trie, prv_);
25  void swap(Trie& rhs) NLIB_NOEXCEPT {
26  TriePrivate* tmp = rhs.prv_;
27  rhs.prv_ = this->prv_;
28  this->prv_ = tmp;
29  }
30  bool Init() NLIB_NOEXCEPT;
31  typedef bool (*MatchCallback)(const char* first, const char* last, uint32_t nodeid,
32  void* user_obj);
33  void Match(const char* cstr, MatchCallback callback, void* user_obj = NULL) NLIB_NOEXCEPT {
34  this->Match(cstr, nlib_strlen(cstr), callback, user_obj);
35  }
36  void Match(const void* data, size_t n, MatchCallback callback, void* user_obj) NLIB_NOEXCEPT;
37  void Match(const void* data, size_t n, MatchCallback callback) NLIB_NOEXCEPT {
38  Match(data, n, callback, NULL);
39  }
40  void MatchBackward(const void* data, size_t n, MatchCallback callback,
41  void* user_obj) NLIB_NOEXCEPT;
42  void MatchBackward(const void* data, size_t n, MatchCallback callback) NLIB_NOEXCEPT {
43  MatchBackward(data, n, callback, NULL);
44  }
45  errno_t GetCommonPrefixWords(const void* prefix, size_t n,
47  void Reset() NLIB_NOEXCEPT;
48  bool Export(BinaryWriter* w) const NLIB_NOEXCEPT;
49  bool Import(BinaryReader* r) NLIB_NOEXCEPT;
50 
51  private:
52  NLIB_VIS_HIDDEN int Match_(const void* data, const unsigned char* p, uint32_t pos,
53  MatchCallback callback, void* user_obj, bool isfwd) NLIB_NOEXCEPT;
54 
55  private:
56  struct TriePrivate;
57  TriePrivate* prv_;
58  friend class TrieBuilder;
60 };
61 
63  public:
64  TrieBuilder() NLIB_NOEXCEPT : prv_(NULL) {}
66  bool Init() NLIB_NOEXCEPT;
67  Trie* Build(ReallocVec<uint32_t>* keyids = NULL) NLIB_NOEXCEPT;
68  bool AddWord(const char* str) NLIB_NOEXCEPT;
69  bool AddPattern(const void* p, size_t n) NLIB_NOEXCEPT;
70  bool AddWords(const char* str, size_t len) NLIB_NOEXCEPT;
71  bool AddWords(const char* str) NLIB_NOEXCEPT {
72  return AddWords(str, strlen(str));
73  }
74 
75  private:
76  struct TrieBuilderPrivate;
77  TrieBuilderPrivate* prv_;
79 };
80 
81 } // namespace succinct
82 NLIB_NAMESPACE_END
83 
84 #if defined(_MSC_VER) && defined(nx_succinct_EXPORTS)
85 #undef NLIB_VIS_PUBLIC
86 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
87 #endif
88 
89 #endif // INCLUDE_NN_NLIB_SUCCINCT_TRIE_H_
Class to create a Trie object.
Definition: Trie.h:62
Defines the basic classes that form the basis to Rank and Select operations.
Defines the class for constructing and accessing a LOUDS succinct tree.
void swap(Trie &rhs) noexcept
Swaps the contents of an object.
Definition: Trie.h:25
Trie() noexcept
Instantiates the object.
Definition: Trie.h:22
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:145
void Match(const char *cstr, MatchCallback callback, void *user_obj=NULL) noexcept
Inspects the string to detect the target string registered in Trie.
Definition: Trie.h:33
The class for realloc-based implementations of C string vectors.
Definition: ReallocVec.h:214
Implements Trie using LOUDS.
Definition: Trie.h:20
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
bool AddWords(const char *str) noexcept
Adds a string from an array containing a set of target strings. The strings must be delimited by newl...
Definition: Trie.h:71
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
TrieBuilder() noexcept
Instantiates the object.
Definition: Trie.h:64
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
The class for writing binary to streams (to OutputStream).
Definition: BinaryWriter.h:13
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
The class for reading binary from streams (from InputStream).
Definition: BinaryReader.h:13
The class for realloc-based implementations of vectors with POD-type elements.
Definition: ReallocVec.h:18
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24