Vowpal Wabbit
action_score.h
Go to the documentation of this file.
1 #pragma once
2 namespace ACTION_SCORE
3 {
5 {
6  uint32_t action;
7  float score;
8 };
9 
11 
12 class score_iterator : public virtual std::iterator<std::random_access_iterator_tag, // iterator_category
13  float, // value_type
14  long, // difference_type
15  float*, // pointer
16  float // reference
17  >
18 {
20 
21  public:
23 
25  {
26  ++_p;
27  return *this;
28  }
29 
30  score_iterator operator+(size_t n) { return {_p + n}; }
31 
32  bool operator==(const score_iterator& other) const { return _p == other._p; }
33 
34  bool operator!=(const score_iterator& other) const { return _p != other._p; }
35 
36  bool operator<(const score_iterator& other) const { return _p < other._p; }
37 
38  size_t operator-(const score_iterator& other) const { return _p - other._p; }
39 
40  float& operator*() { return _p->score; }
41 };
42 
43 inline score_iterator begin_scores(action_scores& a_s) { return {a_s.begin()}; }
44 
45 inline score_iterator end_scores(action_scores& a_s) { return {a_s.end()}; }
46 
47 inline int cmp(size_t a, size_t b)
48 {
49  if (a == b)
50  return 0;
51  if (a > b)
52  return 1;
53  return -1;
54 }
55 
56 inline int score_comp(const void* p1, const void* p2)
57 {
58  action_score* s1 = (action_score*)p1;
59  action_score* s2 = (action_score*)p2;
60  // Most sorting algos do not guarantee the output order of elements that compare equal.
61  // Tie-breaking on the index ensures that the result is deterministic across platforms.
62  // However, this forces a strict ordering, rather than a weak ordering, which carries a performance cost.
63  if (s2->score == s1->score)
64  return cmp(s1->action, s2->action);
65  else if (s2->score >= s1->score)
66  return -1;
67  else
68  return 1;
69 }
70 
71 inline int reverse_order(const void* p1, const void* p2) { return score_comp(p2, p1); }
72 
74 
75 void delete_action_scores(void* v);
76 } // namespace ACTION_SCORE
score_iterator operator+(size_t n)
Definition: action_score.h:30
bool operator!=(const score_iterator &other) const
Definition: action_score.h:34
int score_comp(const void *p1, const void *p2)
Definition: action_score.h:56
v_array< action_score > action_scores
Definition: action_score.h:10
size_t operator-(const score_iterator &other) const
Definition: action_score.h:38
T *& begin()
Definition: v_array.h:42
bool operator<(const score_iterator &other) const
Definition: action_score.h:36
score_iterator begin_scores(action_scores &a_s)
Definition: action_score.h:43
score_iterator(action_score *p)
Definition: action_score.h:22
score_iterator end_scores(action_scores &a_s)
Definition: action_score.h:45
void delete_action_scores(void *v)
Definition: action_score.cc:29
T *& end()
Definition: v_array.h:43
constexpr uint64_t a
Definition: rand48.cc:11
score_iterator & operator++()
Definition: action_score.h:24
int cmp(size_t a, size_t b)
Definition: action_score.h:47
bool operator==(const score_iterator &other) const
Definition: action_score.h:32
void print_action_score(int f, v_array< action_score > &a_s, v_array< char > &tag)
Definition: action_score.cc:8
float f
Definition: cache.cc:40
int reverse_order(const void *p1, const void *p2)
Definition: action_score.h:71