dictionary.h
Go to the documentation of this file.
1 //File: $Id$
2 // Author: John Wu <John.Wu at ACM.org>
3 // Copyright (c) 2000-2016 the Regents of the University of California
4 #ifndef IBIS_DICTIONARY_H
5 #define IBIS_DICTIONARY_H
6 #include "array_t.h"
9 #if defined(HAVE_UNORDERED_MAP)
10 #include <unordered_map>
11 #elif defined(__GNUC__) && (__GNUC__+0 <= 4 || ( __GNUC__+0 == 4 && __GNUC_MINOR__+0 <= 5))
12 #include <backward/hash_map>
13 #else
14 #include <unordered_map>
15 #endif
16 
17 namespace std {
18  // specialization of hash<> on const char*
19  template <> struct hash< const char* > {
20  size_t operator()(const char* x) const;
21  };
22 }
23 
43 class FASTBIT_CXX_DLLSPEC ibis::dictionary {
44 public:
45  ~dictionary() {clear();}
46  dictionary();
47  dictionary(const dictionary& dic);
48 
51  uint32_t size() const {return raw_.size();}
52 
53  const char* operator[](uint32_t i) const;
54  uint32_t operator[](const char* str) const;
55  const char* find(const char* str) const;
56  void patternSearch(const char* pat, array_t<uint32_t>& matches) const;
57 
58  uint32_t insert(const char*, uint32_t);
59  uint32_t insert(const char*);
60  uint32_t insertRaw(char*);
61  uint32_t appendOrdered(const char*);
62 
63  void clear();
64  void swap(dictionary&);
65 
66  int read(const char*);
67  int write(const char*) const;
68 
69  int fromASCII(std::istream &);
70  void toASCII(std::ostream &) const;
71 
72  void sort(array_t<uint32_t>&);
73  int merge(const dictionary&);
74  int morph(const dictionary&, array_t<uint32_t>&) const;
75 
76  bool equal_to(const ibis::dictionary&) const;
77 
78  void copy(const dictionary& rhs);
79 
80 protected:
81 
87  array_t<char*> buffer_;
90  typedef
91 #if defined(HAVE_UNORDERED_MAP)
92  std::unordered_map
93 #elif defined(__GNUC__) && (__GNUC__+0 <= 4 || ( __GNUC__+0 == 4 && __GNUC_MINOR__+0 <= 5))
94  __gnu_cxx::hash_map
95 #else
96  std::unordered_map
97 #endif
98  <const char*, uint32_t, std::hash<const char*>,
100  MYMAP key_;
101 
102  int readRaw(const char*, FILE *);
103  int readKeys0(const char*, FILE *);
104  int readKeys1(const char*, FILE *);
105  int readKeys2(const char*, FILE *);
106  void mergeBuffers() const;
107  int writeKeys(FILE*, uint32_t, array_t<uint64_t>&,
108  array_t<uint32_t>&) const;
109  int writeBuffer(FILE*, uint32_t, array_t<uint64_t>&,
110  array_t<uint32_t>&) const;
111 
112 private:
113  dictionary& operator=(const dictionary&);
114 }; // ibis::dictionary
115 
117 inline void ibis::dictionary::swap(ibis::dictionary& rhs) {
118  raw_.swap(rhs.raw_);
119  key_.swap(rhs.key_);
120  buffer_.swap(rhs.buffer_);
121 } // ibis::dictionary::swap
122 
126 inline const char* ibis::dictionary::operator[](uint32_t i) const {
127  return (i < raw_.size() ? raw_[i] : static_cast<const char*>(0));
128 } // int to string
129 
134 inline const char* ibis::dictionary::find(const char* str) const {
135  const char* ret = 0;
136  const uint32_t ind = operator[](str);
137  if (ind < raw_.size())
138  ret = raw_[ind];
139  return ret;
140 } // ibis::dictionary::find
141 #endif // IBIS_DICTIONARY_H
const char * find(const char *str) const
Find the given string in the dictionary.
Definition: dict-0.h:92
int64_t write(int, const void *, int64_t)
A wrapper over POSIX write function.
Definition: util.cpp:999
const char * operator[](uint32_t i) const
Return a string corresponding to the integer.
Definition: dict-0.h:84
Definition: dictionary.h:19
size_t find(const std::vector< T > &, const T &, size_t)
Find the first position where the value is no less than val.
Definition: utilidor.cpp:3795
STL namespace.
void swap(array_t< T > &rhs)
Swap the content of two array_t objects.
Definition: array_t.h:184
The current implementation of FastBit is code named IBIS; most data structures and functions are in t...
Definition: bord.h:16
std::unordered_map< const char *, uint32_t, std::hash< const char * >, std::equal_to< const char * > > MYMAP
Member variable key_ contains the hash_map that connects a string value to an integer.
Definition: dictionary.h:99
int copy(const char *to, const char *from)
Copy file named "from" to a file named "to".
Definition: util.cpp:894
array_t< const char * > key_
Member variable key_ contains the string values in alphabetic order.
Definition: dict-0.h:58
uint32_t size() const
Return the number of entries in the dictionary.
Definition: dictionary.h:51
Definition: util.h:349
int64_t read(int, void *, int64_t)
A wrapper over POSIX read function.
Definition: util.cpp:973
void clear(ibis::array_t< ibis::bitvector * > &bv)
Clear an array of bit vectors.
Definition: bitvector.cpp:4662
array_t< const char * > raw_
Member variable raw_ contains the string values in the order of the code assignment.
Definition: dict-0.h:56
void swap(dictionary &)
Swap the content of two dictionaries.
Definition: dict-0.h:74
array_t< char * > buffer_
Member varaible buffer_ contains a list of pointers to the memory that holds the strings.
Definition: dict-0.h:64
Provide a dual-directional mapping between strings and integers.
Definition: dict-0.h:19

Make It A Bit Faster
Contact us
Disclaimers
FastBit source code
FastBit mailing list archive