Skip to content

Commit 92b1f9b

Browse files
committed
Making it more standard
1 parent 734fbdd commit 92b1f9b

7 files changed

+59
-55
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ add_library(FastPFOR STATIC
114114
src/bitpackingunaligned.cpp
115115
src/horizontalbitpacking.cpp
116116
src/simdunalignedbitpacking.cpp
117+
src/codecfactory.cpp
117118
src/simdbitpacking.cpp
118119
src/varintdecode.c
119120
src/streamvbyte.c)

example.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
int main() {
1616
using namespace FastPForLib;
17+
CODECFactory factory;
1718

1819
// We pick a CODEC
19-
IntegerCODEC &codec = *CODECFactory::getFromName("simdfastpfor256");
20+
IntegerCODEC &codec = *factory.getFromName("simdfastpfor256");
2021
// could use others, e.g., "simdbinarypacking", "varintg8iu"
2122
////////////
2223
//

headers/codecfactory.h

+7-33
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,19 @@ namespace FastPForLib {
4141
typedef std::map<std::string, std::shared_ptr<IntegerCODEC>> CodecMap;
4242

4343
/**
44-
* This class is a convenience class to generate codecs quickly.
45-
* It cannot be used safely in a multithreaded context where
46-
* each thread should have a different IntegerCODEC.
44+
* You should have at least one factory per thread.
4745
*/
4846
class CODECFactory {
4947
public:
50-
static CodecMap scodecmap;
48+
CODECFactory();
5149

52-
// hacked for convenience
53-
static std::vector<std::shared_ptr<IntegerCODEC>> allSchemes() {
54-
std::vector<std::shared_ptr<IntegerCODEC>> ans;
55-
for (auto i = scodecmap.begin(); i != scodecmap.end(); ++i) {
56-
ans.push_back(i->second);
57-
}
58-
return ans;
59-
}
50+
std::vector<std::shared_ptr<IntegerCODEC>> allSchemes();
6051

61-
static std::vector<std::string> allNames() {
62-
std::vector<std::string> ans;
63-
for (auto i = scodecmap.begin(); i != scodecmap.end(); ++i) {
64-
ans.push_back(i->first);
65-
}
66-
return ans;
67-
}
52+
std::vector<std::string> allNames();
6853

69-
static std::shared_ptr<IntegerCODEC> &getFromName(std::string name) {
70-
if (scodecmap.find(name) == scodecmap.end()) {
71-
std::cerr << "name " << name << " does not refer to a CODEC."
72-
<< std::endl;
73-
std::cerr << "possible choices:" << std::endl;
74-
for (auto i = scodecmap.begin(); i != scodecmap.end(); ++i) {
75-
std::cerr << static_cast<std::string>(i->first)
76-
<< std::endl; // useless cast, but just to be clear
77-
}
78-
std::cerr << "for now, I'm going to just return 'copy'" << std::endl;
79-
return scodecmap["copy"];
80-
}
81-
return scodecmap[name];
82-
}
54+
std::shared_ptr<IntegerCODEC> &getFromName(std::string name);
55+
private:
56+
CodecMap scodecmap;
8357
};
8458

8559

src/codecfactory.cpp

+35-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
#include "codecfactory.h"
22

3-
/**
4-
* We moved part of the factor to its own cpp file because some users
5-
* create multiple instances. Note that the factory is not meant to be
6-
* a safe class, it is a convenience class and you should just have one
7-
* instance of it per project.
8-
*/
93
namespace FastPForLib {
4+
std::vector<std::shared_ptr<IntegerCODEC>> CODECFactory::allSchemes() {
5+
std::vector<std::shared_ptr<IntegerCODEC>> ans;
6+
for (auto i = scodecmap.begin(); i != scodecmap.end(); ++i) {
7+
ans.push_back(i->second);
8+
}
9+
return ans;
10+
}
11+
12+
std::vector<std::string> CODECFactory::allNames() {
13+
std::vector<std::string> ans;
14+
for (auto i = scodecmap.begin(); i != scodecmap.end(); ++i) {
15+
ans.push_back(i->first);
16+
}
17+
return ans;
18+
}
19+
20+
std::shared_ptr<IntegerCODEC> &CODECFactory::getFromName(std::string name) {
21+
if (scodecmap.find(name) == scodecmap.end()) {
22+
std::cerr << "name " << name << " does not refer to a CODEC." << std::endl;
23+
std::cerr << "possible choices:" << std::endl;
24+
for (auto i = scodecmap.begin(); i != scodecmap.end(); ++i) {
25+
std::cerr << static_cast<std::string>(i->first)
26+
<< std::endl; // useless cast, but just to be clear
27+
}
28+
std::cerr << "for now, I'm going to just return 'copy'" << std::endl;
29+
return scodecmap["copy"];
30+
}
31+
return scodecmap[name];
32+
}
1033

1134
// C++11 allows better than this, but neither Microsoft nor Intel support C++11
1235
// fully.
@@ -41,13 +64,13 @@ inline CodecMap initializefactory() {
4164
map["pfor2008"] = std::shared_ptr<IntegerCODEC>(
4265
new CompositeCodec<PFor2008, VariableByte>());
4366
map["simdnewpfor"] = std::shared_ptr<IntegerCODEC>(
44-
new CompositeCodec<SIMDNewPFor<4, Simple16<false> >, VariableByte>());
67+
new CompositeCodec<SIMDNewPFor<4, Simple16<false>>, VariableByte>());
4568
map["newpfor"] = std::shared_ptr<IntegerCODEC>(
46-
new CompositeCodec<NewPFor<4, Simple16<false> >, VariableByte>());
69+
new CompositeCodec<NewPFor<4, Simple16<false>>, VariableByte>());
4770
map["optpfor"] = std::shared_ptr<IntegerCODEC>(
48-
new CompositeCodec<OPTPFor<4, Simple16<false> >, VariableByte>());
71+
new CompositeCodec<OPTPFor<4, Simple16<false>>, VariableByte>());
4972
map["simdoptpfor"] = std::shared_ptr<IntegerCODEC>(
50-
new CompositeCodec<SIMDOPTPFor<4, Simple16<false> >, VariableByte>());
73+
new CompositeCodec<SIMDOPTPFor<4, Simple16<false>>, VariableByte>());
5174
map["varint"] = std::shared_ptr<IntegerCODEC>(new VariableByte());
5275
map["vbyte"] = std::shared_ptr<IntegerCODEC>(new VByte());
5376
map["maskedvbyte"] = std::shared_ptr<IntegerCODEC>(new MaskedVByte());
@@ -73,5 +96,5 @@ inline CodecMap initializefactory() {
7396
map["copy"] = std::shared_ptr<IntegerCODEC>(new JustCopy());
7497
return map;
7598
}
76-
CodecMap CODECFactory::scodecmap = initializefactory();
77-
}
99+
CODECFactory::CODECFactory() : scodecmap(initializefactory()) {}
100+
} // namespace FastPForLib

src/codecs.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,8 @@ void message() {
727727
cout << "the --codecs flag takes as an argument"
728728
" a comma-separated list of schemes chosen among those:"
729729
<< endl;
730-
vector<string> all = CODECFactory::allNames();
730+
CODECFactory factory;
731+
vector<string> all = factory.allNames();
731732
for (auto i = all.begin(); i != all.end(); ++i) {
732733
cout << *i;
733734
if (i + 1 == all.end())
@@ -751,8 +752,9 @@ int main(int argc, char **argv) {
751752
bool computeentropy = false;
752753

753754
bool splitlongarrays = true;
755+
CODECFactory factory;
754756
vector<shared_ptr<IntegerCODEC>> tmp =
755-
CODECFactory::allSchemes(); // the default
757+
factory.allSchemes(); // the default
756758
vector<algostats> myalgos;
757759
for (auto &i : tmp) {
758760
myalgos.push_back(algostats(i));
@@ -780,9 +782,9 @@ int main(int argc, char **argv) {
780782
cout << "# pretty name = " << *i << endl;
781783
if (i->at(0) == '@') { // SIMD
782784
myalgos.push_back(algostats(
783-
CODECFactory::getFromName(i->substr(1, i->size() - 1)), true));
785+
factory.getFromName(i->substr(1, i->size() - 1)), true));
784786
} else {
785-
myalgos.push_back(algostats(CODECFactory::getFromName(*i)));
787+
myalgos.push_back(algostats(factory.getFromName(*i)));
786788
}
787789
cout << "# added '" << myalgos.back().name() << "'" << endl;
788790
}

src/inmemorybenchmark.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,8 @@ void message(const char *prog) {
697697
<< endl;
698698
cerr << "Use the --codecs flag to specify the schemes." << endl;
699699
cerr << " schemes include:" << endl;
700-
vector<string> all = CODECFactory::allNames();
700+
CODECFactory factory;
701+
vector<string> all = factory.allNames();
701702
for (auto i = all.begin(); i != all.end(); ++i) {
702703
cerr << *i << endl;
703704
}
@@ -713,8 +714,9 @@ int main(int argc, char **argv) {
713714
size_t MINLENGTH = 1;
714715
size_t MAXLENGTH = (std::numeric_limits<uint32_t>::max)();
715716
size_t MAXCOUNTER = (std::numeric_limits<std::size_t>::max)();
717+
CODECFactory factory;
716718
vector<shared_ptr<IntegerCODEC>> tmp =
717-
CODECFactory::allSchemes(); // the default
719+
factory.allSchemes(); // the default
718720
vector<algostats> myalgos;
719721
for (auto i = tmp.begin(); i != tmp.end(); ++i) {
720722
myalgos.push_back(algostats(*i));
@@ -759,9 +761,9 @@ int main(int argc, char **argv) {
759761
if (i->at(0) == '@') { // SIMD
760762
string namewithoutprefix = i->substr(1, i->size() - 1);
761763
myalgos.push_back(
762-
algostats(CODECFactory::getFromName(namewithoutprefix), true));
764+
algostats(factory.getFromName(namewithoutprefix), true));
763765
} else {
764-
myalgos.push_back(algostats(CODECFactory::getFromName(*i)));
766+
myalgos.push_back(algostats(factory.getFromName(*i)));
765767
}
766768
cout << "# added '" << myalgos.back().name() << "'" << endl;
767769
}

src/unit.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ using namespace std;
1818
using namespace FastPForLib;
1919

2020
int main() {
21-
vector<shared_ptr<IntegerCODEC>> myalgos = CODECFactory::allSchemes();
21+
CODECFactory factory;
22+
vector<shared_ptr<IntegerCODEC>> myalgos = factory.allSchemes();
2223
for (uint32_t b = 0; b <= 28; ++b) {
2324

2425
cout << "testing... b = " << b << endl;

0 commit comments

Comments
 (0)