Vowpal Wabbit
Public Member Functions | List of all members
DefaultState< audit > Class Template Reference

#include <parse_example_json.h>

Inheritance diagram for DefaultState< audit >:
BaseState< audit >

Public Member Functions

 DefaultState ()
 
BaseState< audit > * Ignore (Context< audit > &ctx, rapidjson::SizeType length)
 
BaseState< audit > * Key (Context< audit > &ctx, const char *str, rapidjson::SizeType length, bool) override
 
BaseState< audit > * String (Context< audit > &ctx, const char *str, rapidjson::SizeType length, bool) override
 
BaseState< audit > * Bool (Context< audit > &ctx, bool b) override
 
BaseState< audit > * StartObject (Context< audit > &ctx) override
 
BaseState< audit > * EndObject (Context< audit > &ctx, rapidjson::SizeType memberCount) override
 
BaseState< audit > * Float (Context< audit > &ctx, float f) override
 
BaseState< audit > * Uint (Context< audit > &ctx, unsigned f) override
 
BaseState< audit > * StartArray (Context< audit > &ctx) override
 
- Public Member Functions inherited from BaseState< audit >
 BaseState (const char *pname)
 
virtual BaseState< audit > * Null (Context< audit > &ctx)
 
virtual BaseState< audit > * EndArray (Context< audit > &ctx, rapidjson::SizeType)
 

Additional Inherited Members

- Public Attributes inherited from BaseState< audit >
const char * name
 

Detailed Description

template<bool audit>
class DefaultState< audit >

Definition at line 606 of file parse_example_json.h.

Constructor & Destructor Documentation

◆ DefaultState()

template<bool audit>
DefaultState< audit >::DefaultState ( )
inline

Definition at line 609 of file parse_example_json.h.

609 : BaseState<audit>("Default") {}

Member Function Documentation

◆ Bool()

template<bool audit>
BaseState<audit>* DefaultState< audit >::Bool ( Context< audit > &  ctx,
bool  b 
)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 784 of file parse_example_json.h.

References Context< audit >::all, Context< audit >::CurrentNamespace(), and Context< audit >::key.

785  {
786  if (b)
787  ctx.CurrentNamespace().AddFeature(ctx.all, ctx.key);
788 
789  return this;
790  }
const char * key
Namespace< audit > & CurrentNamespace()

◆ EndObject()

template<bool audit>
BaseState<audit>* DefaultState< audit >::EndObject ( Context< audit > &  ctx,
rapidjson::SizeType  memberCount 
)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 798 of file parse_example_json.h.

References Context< audit >::all, v_array< T >::begin(), label_type::ccb, polylabel::conditional_contextual_bandit, label_parser::default_label, v_array< T >::end(), Context< audit >::error(), Context< audit >::ex, Context< audit >::example_factory, Context< audit >::example_factory_context, Context< audit >::examples, example::l, Context< audit >::label_index_state, Context< audit >::label_object_state, vw::label_type, parser::lp, Context< audit >::namespace_path, CCB::label::outcome, vw::p, Context< audit >::PopNamespace(), v_array< T >::push_back(), Context< audit >::root_state, v_array< T >::size(), CCB::slot, and CCB::label::type.

799  {
800  BaseState<audit>* return_state = ctx.PopNamespace();
801 
802  if (ctx.namespace_path.empty())
803  {
804  int label_index = ctx.label_index_state.index;
805  // we're at the end of the example
806  if (label_index >= 0)
807  {
808  // skip shared example
809  label_index++;
810  if (label_index >= (int)ctx.examples->size())
811  {
812  ctx.error() << "Out of bounds error: _labelIndex must be smaller than number of actions! _labelIndex="
813  << (label_index - 1) << " Number of actions=" << ctx.examples->size() - 1 << " ";
814  return nullptr;
815  }
816 
817  // apply labelIndex
818  ctx.ex = (*ctx.examples)[label_index];
819 
820  // reset for next example
821  ctx.label_index_state.index = -1;
822  }
823 
824  // inject label
825  ctx.label_object_state.EndObject(ctx, memberCount);
826 
827  // If we are in CCB mode and there have been no slots. Check label cost, prob and action were passed. In that
828  // case this is CB, so generate a single slot with this info.
829  if (ctx.all->label_type == label_type::ccb)
830  {
831  auto num_slots = std::count_if(ctx.examples->begin(), ctx.examples->end(),
833  if (num_slots == 0 && ctx.label_object_state.found_cb)
834  {
835  ctx.ex = &(*ctx.example_factory)(ctx.example_factory_context);
836  ctx.all->p->lp.default_label(&ctx.ex->l);
838  ctx.examples->push_back(ctx.ex);
839 
840  auto outcome = new CCB::conditional_contextual_bandit_outcome();
841  outcome->cost = ctx.label_object_state.cb_label.cost;
842  outcome->probabilities.push_back(
843  {ctx.label_object_state.cb_label.action, ctx.label_object_state.cb_label.probability});
844  ctx.ex->l.conditional_contextual_bandit.outcome = outcome;
845  }
846  }
847  }
848 
849  // if we're at the top-level go back to ds_state
850  return ctx.namespace_path.empty() ? ctx.root_state : return_state;
851  }
BaseState< audit > * root_state
LabelObjectState< audit > label_object_state
std::vector< Namespace< audit > > namespace_path
LabelIndexState< audit > label_index_state
void(* default_label)(void *)
Definition: label_parser.h:12
label_type::label_type_t label_type
Definition: global_data.h:550
example * ex
std::stringstream & error()
T *& begin()
Definition: v_array.h:42
size_t size() const
Definition: v_array.h:68
void * example_factory_context
parser * p
Definition: global_data.h:377
VW::example_factory_t example_factory
void push_back(const T &new_ele)
Definition: v_array.h:107
v_array< example * > * examples
BaseState< audit > * PopNamespace()
T *& end()
Definition: v_array.h:43
example_type type
Definition: ccb_label.h:32
polylabel l
Definition: example.h:57
CCB::label conditional_contextual_bandit
Definition: example.h:32
conditional_contextual_bandit_outcome * outcome
Definition: ccb_label.h:34
label_parser lp
Definition: parser.h:102

◆ Float()

template<bool audit>
BaseState<audit>* DefaultState< audit >::Float ( Context< audit > &  ctx,
float  f 
)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 853 of file parse_example_json.h.

References Context< audit >::all, Context< audit >::CurrentNamespace(), VW::hash_feature(), and Context< audit >::key.

854  {
855  auto& ns = ctx.CurrentNamespace();
856  ns.AddFeature(f, VW::hash_feature(*ctx.all, ctx.key, ns.namespace_hash), ctx.key);
857 
858  return this;
859  }
const char * key
Namespace< audit > & CurrentNamespace()
uint64_t hash_feature(vw &all, const std::string &s, uint64_t u)
Definition: vw.h:153
float f
Definition: cache.cc:40

◆ Ignore()

template<bool audit>
BaseState<audit>* DefaultState< audit >::Ignore ( Context< audit > &  ctx,
rapidjson::SizeType  length 
)
inline

Definition at line 611 of file parse_example_json.h.

References Context< audit >::error(), Context< audit >::ignore_state, Context< audit >::stream, and Context< audit >::stream_end.

612  {
613  // fast ignore
614  // skip key + \0 + "
615  char* head = ctx.stream->src_ + length + 2;
616  if (head >= ctx.stream_end || *head != ':')
617  {
618  ctx.error() << "Expected ':' found '" << *head << "'";
619  return nullptr;
620  }
621  head++;
622 
623  // scan for ,}
624  // support { { ... } }
625  int depth = 0, sq_depth = 0;
626  bool stop = false;
627  while (!stop)
628  {
629  switch (*head)
630  {
631  case '\0':
632  ctx.error() << "Found EOF";
633  return nullptr;
634  case '"':
635  {
636  // skip strings
637  bool stopInner = false;
638  while (!stopInner)
639  {
640  head++;
641  switch (*head)
642  {
643  case '\0':
644  ctx.error() << "Found EOF";
645  return nullptr;
646  case '\\':
647  head++;
648  break;
649  case '"':
650  stopInner = true;
651  break;
652  }
653  }
654  break;
655  }
656  case '{':
657  depth++;
658  break;
659  case '}':
660  if (depth == 0 && sq_depth == 0)
661  stop = true;
662  else
663  depth--;
664  break;
665  case '[':
666  sq_depth++;
667  break;
668  case ']':
669  if (depth == 0 && sq_depth == 0)
670  stop = true;
671  else
672  sq_depth--;
673  break;
674  case ',':
675  if (depth == 0 && sq_depth == 0)
676  stop = true;
677  break;
678  }
679  head++;
680  }
681 
682  // skip key + \0 + ":
683  char* value = ctx.stream->src_ + length + 3;
684  if (value >= ctx.stream_end)
685  {
686  ctx.error() << "Found EOF";
687  return nullptr;
688  }
689 
690  *value = '0';
691  value++;
692  memset(value, ' ', head - value - 1);
693 
694  return &ctx.ignore_state;
695  }
std::stringstream & error()
const char * stream_end
rapidjson::InsituStringStream * stream
IgnoreState< audit > ignore_state

◆ Key()

template<bool audit>
BaseState<audit>* DefaultState< audit >::Key ( Context< audit > &  ctx,
const char *  str,
rapidjson::SizeType  length,
bool   
)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 697 of file parse_example_json.h.

References _stricmp, Context< audit >::array_float_state, Context< audit >::array_uint_state, Context< audit >::error(), Context< audit >::key, Context< audit >::key_length, Context< audit >::label_index_state, Context< audit >::label_object_state, Context< audit >::label_single_property_state, Context< audit >::label_state, Context< audit >::multi_state, ArrayToVectorState< audit, T >::output_array, ArrayToVectorState< audit, T >::return_state, Context< audit >::slots_state, Context< audit >::tag_state, and Context< audit >::text_state.

698  {
699  ctx.key = str;
700  ctx.key_length = length;
701 
702  if (length > 0 && str[0] == '_')
703  {
704  // match _label*
705  if (ctx.key_length >= 6 && !strncmp(ctx.key, "_label", 6))
706  {
707  if (ctx.key_length >= 7 && ctx.key[6] == '_')
708  return &ctx.label_single_property_state;
709  else if (ctx.key_length == 6)
710  return &ctx.label_state;
711  else if (ctx.key_length == 11 && !_stricmp(ctx.key, "_labelIndex"))
712  return &ctx.label_index_state;
713  else
714  {
715  ctx.error() << "Unsupported key '" << ctx.key << "' len: " << length;
716  return nullptr;
717  }
718  }
719 
720  if (ctx.key_length == 5 && !strcmp(ctx.key, "_text"))
721  return &ctx.text_state;
722 
723  // TODO: _multi in _multi...
724  if (ctx.key_length == 6 && !strcmp(ctx.key, "_multi"))
725  return &ctx.multi_state;
726 
727  if (ctx.key_length == 6 && !strcmp(ctx.key, "_slots"))
728  return &ctx.slots_state;
729 
730  if (ctx.key_length == 4 && !_stricmp(ctx.key, "_tag"))
731  return &ctx.tag_state;
732 
733  if (ctx.key_length == 4 && !_stricmp(ctx.key, "_inc"))
734  {
736  ctx.array_uint_state.return_state = this;
737  return &ctx.array_uint_state;
738  }
739 
740  if (ctx.key_length == 2 && ctx.key[1] == 'a')
741  {
743  ctx.array_uint_state.return_state = this;
744  return &ctx.array_uint_state;
745  }
746 
747  if (ctx.key_length == 2 && ctx.key[1] == 'p')
748  {
750  ctx.array_float_state.return_state = this;
751  return &ctx.array_float_state;
752  }
753 
754  return Ignore(ctx, length);
755  }
756 
757  return this;
758  }
LabelObjectState< audit > label_object_state
const char * key
rapidjson::SizeType key_length
ArrayToVectorState< audit, float > array_float_state
BaseState< audit > * Ignore(Context< audit > &ctx, rapidjson::SizeType length)
LabelIndexState< audit > label_index_state
LabelState< audit > label_state
MultiState< audit > multi_state
std::stringstream & error()
LabelSinglePropertyState< audit > label_single_property_state
ArrayToVectorState< audit, unsigned > array_uint_state
#define _stricmp
SlotsState< audit > slots_state
BaseState< audit > * return_state
TextState< audit > text_state
TagState< audit > tag_state
std::vector< T > * output_array

◆ StartArray()

template<bool audit>
BaseState<audit>* DefaultState< audit >::StartArray ( Context< audit > &  ctx)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 863 of file parse_example_json.h.

References Context< audit >::array_state.

863 { return ctx.array_state.StartArray(ctx); }
ArrayState< audit > array_state

◆ StartObject()

template<bool audit>
BaseState<audit>* DefaultState< audit >::StartObject ( Context< audit > &  ctx)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 792 of file parse_example_json.h.

References Context< audit >::key, and Context< audit >::PushNamespace().

793  {
794  ctx.PushNamespace(ctx.key, this);
795  return this;
796  }
const char * key
void PushNamespace(const char *ns, BaseState< audit > *return_state)

◆ String()

template<bool audit>
BaseState<audit>* DefaultState< audit >::String ( Context< audit > &  ctx,
const char *  str,
rapidjson::SizeType  length,
bool   
)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 760 of file parse_example_json.h.

References Context< audit >::all, Context< audit >::CurrentNamespace(), Context< audit >::key, and Context< audit >::key_length.

761  {
762  // string escape
763  const char* end = str + length;
764  for (char* p = (char*)str; p != end; p++)
765  {
766  switch (*p)
767  {
768  case ' ':
769  case '\t':
770  case '|':
771  case ':':
772  *p = '_';
773  }
774  }
775 
776  char* prepend = (char*)str - ctx.key_length;
777  memmove(prepend, ctx.key, ctx.key_length);
778 
779  ctx.CurrentNamespace().AddFeature(ctx.all, prepend);
780 
781  return this;
782  }
const char * key
rapidjson::SizeType key_length
Namespace< audit > & CurrentNamespace()

◆ Uint()

template<bool audit>
BaseState<audit>* DefaultState< audit >::Uint ( Context< audit > &  ctx,
unsigned  f 
)
inlineoverridevirtual

Reimplemented from BaseState< audit >.

Definition at line 861 of file parse_example_json.h.

861 { return Float(ctx, (float)f); }
BaseState< audit > * Float(Context< audit > &ctx, float f) override
float f
Definition: cache.cc:40

The documentation for this class was generated from the following file: