rapidyaml  0.13.0
parse and emit YAML, and do it fast
c4::yml::TagCache Struct Reference

Accelerator structure to reduce memory requirements by enabling reuse of resolved tags. More...

#include <tag.hpp>

Classes

struct  Entry
 
struct  LookupResult
 

Public Types

using Entries = detail::stack< Entry >
 
using const_iterator = id_type
 

Public Member Functions

 TagCache () noexcept
 
LookupResult find (csubstr tag, id_type doc_id, id_type linear_threshold=Entries::sso_size) const noexcept
 
void add (csubstr tag, csubstr resolved, id_type doc_id, const_iterator pos) RYML_NOEXCEPT
 
void clear () noexcept
 

Detailed Description

Accelerator structure to reduce memory requirements by enabling reuse of resolved tags.

Definition at line 70 of file tag.hpp.

Member Typedef Documentation

◆ Entries

using c4::yml::TagCache::Entries = detail::stack<Entry>

Definition at line 78 of file tag.hpp.

◆ const_iterator

Definition at line 79 of file tag.hpp.

Constructor & Destructor Documentation

◆ TagCache()

c4::yml::TagCache::TagCache ( )
inlinenoexcept

Definition at line 89 of file tag.hpp.

89 : m_entries() {}

Member Function Documentation

◆ find()

TagCache::LookupResult c4::yml::TagCache::find ( csubstr  tag,
id_type  doc_id,
id_type  linear_threshold = Entries::sso_size 
) const
noexcept

Definition at line 537 of file tag.cpp.

538 {
539  LookupResult ret = {};
540  id_type sz = m_entries.size();
541  if(sz < linear_threshold) // do a linear search on small size
542  {
543  for(size_t i = 0; i < sz; ++i)
544  {
545  Entry const& C4_RESTRICT e = m_entries[i];
546  if(e.tag == tag && e.doc_id == doc_id)
547  {
548  ret.resolved = e.resolved;
549  ret.pos = i;
550  return ret;
551  }
552  else if(e.tag > tag || ((e.tag == tag) && e.doc_id > doc_id))
553  {
554  ret.pos = i;
555  return ret;
556  }
557  }
558  ret.pos = sz;
559  }
560  else // do a binary search on larger size
561  {
562  id_type first = 0;
563  id_type count = sz;
564  while(count)
565  {
566  id_type halfsz = count / id_type(2);
567  id_type mid = first + halfsz;
568  _RYML_ASSERT_BASIC_(m_entries.m_callbacks, mid < sz);
569  Entry const& C4_RESTRICT e = m_entries[mid];
570  if(e.tag < tag || (e.tag == tag && e.doc_id < doc_id))
571  {
572  first = mid + 1;
573  _RYML_ASSERT_BASIC_(m_entries.m_callbacks, count >= halfsz + 1);
574  count -= halfsz + 1;
575  }
576  else
577  {
578  count = halfsz;
579  }
580  }
581  ret.pos = first;
582  if(first < sz)
583  {
584  Entry const& C4_RESTRICT e = m_entries[first];
585  if(e.tag == tag && e.doc_id == doc_id)
586  {
587  ret.resolved = m_entries[first].resolved;
588  }
589  }
590  }
591  return ret;
592 }
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:244

◆ add()

void c4::yml::TagCache::add ( csubstr  tag,
csubstr  resolved,
id_type  doc_id,
const_iterator  pos 
)

Definition at line 594 of file tag.cpp.

595 {
596  const id_type sz = m_entries.size();
597  _RYML_ASSERT_BASIC_(m_entries.m_callbacks, pos <= sz);
598  _RYML_ASSERT_BASIC_(m_entries.m_callbacks, pos == sz || tag < m_entries[pos].tag || (tag == m_entries[pos].tag && doc_id < m_entries[pos].doc_id));
599  m_entries.resize(sz + 1);
600  if(pos < sz)
601  memmove(m_entries.m_stack + pos + 1, m_entries.m_stack + pos, (sz - pos) * sizeof(Entry));
602  m_entries.m_stack[pos].tag = tag;
603  m_entries.m_stack[pos].resolved = resolved;
604  m_entries.m_stack[pos].doc_id = doc_id;
605  _c4dbgpf("tagcache: add entry @pos={}: docid={} {} -> {}", pos, doc_id, tag, _maybe_null_str(resolved));
606 }

◆ clear()

void c4::yml::TagCache::clear ( )
inlinenoexcept

Definition at line 93 of file tag.hpp.

93 { m_entries.clear(); }

The documentation for this struct was generated from the following files: