-
Notifications
You must be signed in to change notification settings - Fork 7
/
index_find.cpp
180 lines (146 loc) · 5.62 KB
/
index_find.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// index_find.cpp
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Vyacheslav Brover
*
* File Description:
* Find approximately closest objects ordered by proximity using an index
*
*/
#undef NDEBUG
#include "common.hpp"
using namespace Common_sp;
#include "version.inc"
#include "common.inc"
namespace
{
struct ObjNum
{
string obj;
size_t num {0};
bool operator< (const ObjNum &other) const
{ LESS_PART (other, *this, num);
LESS_PART (*this, other, obj);
return false;
}
};
struct ThisApplication : Application
{
ThisApplication ()
: Application ("Find approximately closest objects ordered by proximity using an index.\n\
An object is a file with a list of attributes.\n\
Average time: O(A S (A (log A + log S) + log N)), where N is the number of objects, and A is the average number of attributes in an object, S = attr_size_max.\n\
")
{
version = VERSION;
addPositional ("dir", "Directory with objects");
addPositional ("index", "Directory with an attribute index for <dir>");
addPositional ("attr_size_max", "Attribute file size limit; objects having only attrbutes whose index files exceed <attr_size_max> are indiscernible");
addPositional ("target", "Target object");
addFlag ("large", "Directory <dir> is large: it is subdivided into subdirectories \"0\" .. \"" + to_string (small_hash_class_max - 1) + "\" which are the hashes of file names");
}
void body () const final
{
const string dir = getArg ("dir");
const string index = getArg ("index");
const streamsize attr_size_max = (streamsize) arg2uint ("attr_size_max");
const string target = getArg ("target");
const bool large = getFlag ("large");
// size(index) = O(N A)
StringVector attrObjs; // Repeated objects
{
StringVector bigAttrs;
// Time = O(A (log N + log A + S))
{
LineInput f (target);
while (f. nextLine ())
{
string& attr = f. line;
trim (attr);
const string attrFName (index + "/" + attr);
if (! fileExists (attrFName))
continue;
if (getFileSize (attrFName) >= attr_size_max)
bigAttrs << attr;
else
attrObjs << StringVector (attrFName, (size_t) 1000, true); // PAR
}
}
// size(attrObjs) = O(A S)
// size(bigAttrs) = O(A)
if (verbose ())
{
PRINT (attrObjs. size ());
PRINT (bigAttrs. size ());
}
// Time: O(A log A)
bigAttrs. sort ();
QC_ASSERT (bigAttrs. isUniq ());
// Time: O(A S (log A + log S))
StringVector objs (attrObjs);
objs. sort ();
objs. uniq ();
// size(objs) = O(A S)
if (verbose ())
PRINT (objs. size ());
// Append: attrObjs
// Time: O(A S (log N + A log A))
if (! bigAttrs. empty ())
for (const string& obj : objs)
{
const string objFName (dir + (large ? "/" + to_string (str2hash_class (obj, false)) : "") + "/" + obj);
if (! fileExists (objFName))
continue;
StringVector attrs (objFName, (size_t) 1000, true); // PAR
attrs. sort ();
QC_ASSERT (attrs. isUniq ());
const size_t n = bigAttrs. getIntersectionSize (attrs); // O(A)
FOR (size_t, i, n)
attrObjs << obj;
}
}
// size(attrObjs) = O(A^2 S)
// Time: O(A^2 S)
unordered_map<string,size_t> obj2num; obj2num. rehash (attrObjs. size ());
for (const string& obj : attrObjs)
obj2num [obj] ++;
// size(obj2num) = O(A^2 S)
// Time: O(A^2 S (log A + log S))
Vector<ObjNum> objNums; objNums. reserve (obj2num. size ());
for (const auto& it : obj2num)
objNums << std::move (ObjNum {it. first, it. second});
objNums. sort ();
// size(objNums) = O(A^2 S)
for (const ObjNum& objNum : objNums)
cout << objNum. obj << endl;
}
};
} // namespace
int main (int argc,
const char* argv[])
{
ThisApplication app;
return app. run (argc, argv);
}