Skip to content

Commit 3151795

Browse files
committed
EncodedBlocks supports encode(.. with any iterator rather than pointer only
1 parent a10ae93 commit 3151795

File tree

1 file changed

+20
-19
lines changed
  • DataFormats/Detectors/Common/include/DetectorsCommonDataFormats

1 file changed

+20
-19
lines changed

DataFormats/Detectors/Common/include/DetectorsCommonDataFormats/EncodedBlocks.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,8 @@ class EncodedBlocks
380380
}
381381

382382
/// encode vector src to bloc at provided slot
383-
template <typename S, typename VB>
384-
void encode(const S* const srcBegin, const S* const srcEnd, int slot, uint8_t probabilityBits, Metadata::OptStore opt, VB* buffer = nullptr,
385-
const void* encoderExt = nullptr);
383+
template <typename S_IT, typename VB>
384+
void encode(const S_IT srcBegin, const S_IT srcEnd, int slot, uint8_t probabilityBits, Metadata::OptStore opt, VB* buffer = nullptr, const void* encoderExt = nullptr);
386385

387386
/// decode block at provided slot to destination vector (will be resized as needed)
388387
template <typename VD>
@@ -725,21 +724,21 @@ void EncodedBlocks<H, N, W>::decode(D* dest, // destination
725724

726725
///_____________________________________________________________________________
727726
template <typename H, int N, typename W>
728-
template <typename S, typename VB>
729-
void EncodedBlocks<H, N, W>::encode(const S* const srcBegin, // begin of source message
730-
const S* const srcEnd, // end of source message
727+
template <typename S_IT, typename VB>
728+
void EncodedBlocks<H, N, W>::encode(const S_IT srcBegin, // iterator begin of source message
729+
const S_IT srcEnd, // iterator end of source message
731730
int slot, // slot in encoded data to fill
732731
uint8_t probabilityBits, // encoding into
733732
Metadata::OptStore opt, // option for data compression
734733
VB* buffer, // optional buffer (vector) providing memory for encoded blocks
735734
const void* encoderExt) // optional external encoder
736-
737735
{
738736
// fill a new block
739737
assert(slot == mRegistry.nFilledBlocks);
740738
mRegistry.nFilledBlocks++;
741-
using stream_t = typename o2::rans::Encoder64<S>::stream_t;
742-
739+
using STYP = typename std::iterator_traits<S_IT>::value_type;
740+
using stream_t = typename o2::rans::Encoder64<STYP>::stream_t;
741+
;
743742
const size_t messageLength = std::distance(srcBegin, srcEnd);
744743
// cover three cases:
745744
// * empty source message: no entropy coding
@@ -776,29 +775,29 @@ void EncodedBlocks<H, N, W>::encode(const S* const srcBegin, // begin of source
776775
// case 3: message where entropy coding should be applied
777776
if (opt == Metadata::OptStore::EENCODE) {
778777
// build symbol statistics
779-
const o2::rans::LiteralEncoder64<S>* encoder = reinterpret_cast<const o2::rans::LiteralEncoder64<S>*>(encoderExt);
780-
std::unique_ptr<o2::rans::LiteralEncoder64<S>> encoderLoc;
778+
const o2::rans::LiteralEncoder64<STYP>* encoder = reinterpret_cast<const o2::rans::LiteralEncoder64<STYP>*>(encoderExt);
779+
std::unique_ptr<o2::rans::LiteralEncoder64<STYP>> encoderLoc;
781780
std::unique_ptr<o2::rans::FrequencyTable> frequencies = nullptr;
782781
int dictSize = 0;
783782
if (!encoder) { // no external encoder provide, create one on spot
784783
frequencies = std::make_unique<o2::rans::FrequencyTable>();
785784
frequencies->addSamples(srcBegin, srcEnd);
786-
encoderLoc = std::make_unique<o2::rans::LiteralEncoder64<S>>(*frequencies, probabilityBits);
785+
encoderLoc = std::make_unique<o2::rans::LiteralEncoder64<STYP>>(*frequencies, probabilityBits);
787786
encoder = encoderLoc.get();
788787
dictSize = frequencies->size();
789788
}
790789

791790
// estimate size of encode buffer
792-
int dataSize = rans::calculateMaxBufferSize(messageLength, encoder->getAlphabetRangeBits(), sizeof(S)); // size in bytes
791+
int dataSize = rans::calculateMaxBufferSize(messageLength, encoder->getAlphabetRangeBits(), sizeof(STYP)); // size in bytes
793792
// preliminary expansion of storage based on dict size + estimated size of encode buffer
794-
dataSize = dataSize / sizeof(W) + (sizeof(S) < sizeof(W)); // size in words of output stream
793+
dataSize = dataSize / sizeof(W) + (sizeof(STYP) < sizeof(W)); // size in words of output stream
795794
expandStorage(dictSize + dataSize);
796795
//store dictionary first
797796
if (dictSize) {
798797
bl->storeDict(dictSize, frequencies->data());
799798
}
800799
// vector of incompressible literal symbols
801-
std::vector<S> literals;
800+
std::vector<STYP> literals;
802801
// directly encode source message into block buffer.
803802
const auto encodedMessageEnd = encoder->process(bl->getCreateData(), bl->getCreateData() + dataSize, srcBegin, srcEnd, literals);
804803
dataSize = encodedMessageEnd - bl->getData();
@@ -809,20 +808,22 @@ void EncodedBlocks<H, N, W>::encode(const S* const srcBegin, // begin of source
809808

810809
int literalSize = 0;
811810
if (literals.size()) {
812-
literalSize = (literals.size() * sizeof(S)) / sizeof(stream_t) + (sizeof(S) < sizeof(stream_t));
811+
literalSize = (literals.size() * sizeof(STYP)) / sizeof(stream_t) + (sizeof(STYP) < sizeof(stream_t));
813812
expandStorage(literalSize);
814813
bl->storeLiterals(literalSize, reinterpret_cast<const stream_t*>(literals.data()));
815814
}
816815
*meta = Metadata{messageLength, literals.size(), sizeof(uint64_t), sizeof(stream_t), static_cast<uint8_t>(encoder->getProbabilityBits()), opt,
817816
encoder->getMinSymbol(), encoder->getMaxSymbol(), dictSize, dataSize, literalSize};
818817

819818
} else { // store original data w/o EEncoding
820-
const size_t szb = messageLength * sizeof(S);
821-
const int dataSize = szb / sizeof(stream_t) + (sizeof(S) < sizeof(stream_t));
819+
const size_t szb = messageLength * sizeof(STYP);
820+
const int dataSize = szb / sizeof(stream_t) + (sizeof(STYP) < sizeof(stream_t));
822821
// no dictionary needed
823822
expandStorage(dataSize);
824823
*meta = Metadata{messageLength, 0, sizeof(uint64_t), sizeof(stream_t), probabilityBits, opt, 0, 0, 0, dataSize, 0};
825-
bl->storeData(meta->nDataWords, reinterpret_cast<const W*>(srcBegin));
824+
// provided iterator is not necessarily pointer, need to use intermediate vector!!!
825+
std::vector<STYP> vtmp(srcBegin, srcEnd);
826+
bl->storeData(meta->nDataWords, reinterpret_cast<const W*>(vtmp.data()));
826827
}
827828
// resize block if necessary
828829
}

0 commit comments

Comments
 (0)