Skip to content

Commit

Permalink
Inline method push_back for compact string
Browse files Browse the repository at this point in the history
  • Loading branch information
JackyWoo committed Jun 12, 2024
1 parent dcaa2ad commit ee43104
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
15 changes: 1 addition & 14 deletions src/Common/CompactStrings.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "CompactStrings.h"

#include "Service/memcopy.h"
#include <Common/CompactStrings.h>


namespace RK
Expand All @@ -24,17 +22,6 @@ void CompactStrings::reserve(size_t n, size_t total_size_)
data.reserve(AVG_ELEMENT_SIZE_HINT * total_size_);
}

void CompactStrings::push_back(const String & s)
{
const size_t old_size = data.size();
const size_t size_to_append = s.size();
const size_t new_size = old_size + size_to_append;

data.resize(new_size);
memcopy(data.data() + old_size, s.c_str(), size_to_append);
offsets.push_back(new_size);
}

StringRef CompactStrings::operator[](int64_t i) const
{
return StringRef(&data[offsets[i - 1]], offsets[i] - offsets[i - 1]);
Expand Down
17 changes: 14 additions & 3 deletions src/Common/CompactStrings.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include "PODArray.h"
#include "common/StringRef.h"
#include <Common/PODArray.h>
#include <Service/memcopy.h>
#include <common/StringRef.h>


namespace RK
Expand Down Expand Up @@ -48,7 +49,17 @@ class CompactStrings

void reserve(size_t n, size_t total_size = 0);

void push_back(const String & s);
inline void push_back(const String & s)
{
const size_t old_size = data.size();
const size_t size_to_append = s.size();
const size_t new_size = old_size + size_to_append;

data.resize(new_size);
memcopy(data.data() + old_size, s.c_str(), size_to_append);
offsets.push_back(new_size);
}

template <class Ttr> void push_back(Ttr begin, Ttr end)
{
for (Ttr it = begin; it != end; ++it)
Expand Down

0 comments on commit ee43104

Please sign in to comment.