STXXL处理字符串

时间:2015-05-11 08:42:47

标签: c++ string c++11 bigdata stxxl

我正在处理大型数据集。请问您如何在我想要与stxxl一起使用的类中存储字符串?我已经阅读了几个讨论,并且到处都说字符串不是POD类型因此它不能存储在stxxl :: vector中,但我不确定,因为我尝试了它并且我检查了数据并且一切似乎都很好。我在这里看到了一个方法https://github.com/Project-OSRM/osrm-backend/blob/725b93a961625a7b04d54806d7e0f80252a6bcd0/extractor/extraction_containers.hpp并且他们使用stxxl :: vector,所以也许库更新了以支持std :: string?

class HighWay
{
private:
    uint64_t id; 
    string name;
    int speed;
    string attributes; //additional attributes of way
    string edges; //written uint64_t from,uint64_t to, int distance  written as string
    string nodes; //vector<uint64_t> written as string
public:
    HighWay() = default;
    void setId(uint64_t _id) {
        id = boost::lexical_cast<string>(_id);
    }
void setName(string _name) {
    name = _name;
}
void setSpeed(int _speed) {
    speed = _speed;
}
void setAttributes(string _attributes) {
    attributes = _attributes;
}
void setEdges(string _edges) {
    edges = _edges;
}

void setNodes(vector<uint64_t>refs) {
    stringstream s;
    uint64_t i = 0;
    for (; i < refs.size()-1;i++) {
        s << boost::lexical_cast<uint64_t>(refs[i]) << " ";
    }
    s << boost::lexical_cast<uint64_t>(refs[i]);

    nodes = s.str();
}

uint64_t getId() {
    return id;
}
string getName() {
    return name;
}
int getSpeed() {
    return speed;
}
string getAttributes() {
    return attributes;
}
string getEdges() {
    return edges;
}

std::vector<int64_t> getNodes() {
    stringstream s(nodes);
    uint64_t node;
    std::vector<int64_t> result;
    while (s >> node) {
        result.push_back(static_cast<int64_t>(node));
    }

    return result;
}
};

我还创建了将字符串存储为POD的代码,将字符串存储在char的向量中,并在映射中记住数组中的下限和上限索引。但是这种方法导致应用程序中使用了许多std :: maps。

//class to store in map
struct TypeName{
    uint64_t start;
    uint64_t end;
};

std::istream& operator >> (std::istream& i, TypeName& entry)
{
    i >> entry.start;
    i >> entry.end;
    return i;
}
std::ostream& operator << (std::ostream& i, const TypeName& entry)
{
    i << entry.start << " ";
    i << entry.end;
    return i;
}

struct PoiCategories{
   uint64_t start;
   uint64_t end;
};

std::istream& operator >> (std::istream& i,PoiCategories& entry)
{
    i >> entry.start;
    i >> entry.end;
    return i;
}

std::ostream& operator << (std::ostream& i, const PoiCategories& entry)
{
    i << entry.start << " ";
    i << entry.end;
    return i;
}

 //object i want to store
struct Poi {
    Poi() = default;
    uint64_t id;
    char type;
    uint64_t id_in_pois; //id in vector pois

void addCategories(
    vector<int> &kats, //categories to insert
    stxxl::vector<uint64_t> &categories, //vector to store category
    std::unordered_map <uint64_t, PoiCategories> &idPoi_categories //index to vector categories to retrieve all categories for Poi
    )
{
    size_t  start = categories.size();
    for (auto & kat : kats) {
        categories.push_back(kat);
    }
    size_t end = categories.size() - 1;
    idPoi_categories.insert(make_pair(id, PoiCategories{start, end }));


}

vector<int> getCategories(
    stxxl::vector<uint64_t> &categories,
    std::unordered_map <uint64_t, PoiKategorie> &idPoi_categories
    )
{
    std::vector<int> result;
    PoiCategories bounds = idPoi_categories.find(id)->second;
    for (size_t i = bounds.start; i <= bounds.end; i++) {
        result.push_back(categories[i]);
    }

    return result;
}

};

我的应用程序中的问题是我存储了一些字符串数据,主要是街道和POI的名称。也许我使用错误的库。如果是这样,你能否建议我在预处理时更好地存储数据?

1 个答案:

答案 0 :(得分:0)

它确实被禁止了,但违反无POD规则的症状通常是不可预测的。只要字符串全部适合内存,它就可能出现,但在这种情况下你无论如何都不需要STXXL。