summaryrefslogtreecommitdiff
path: root/klm/search/header.hh
blob: 25550dbed7a13c356601e335ced4528c67ff279b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef SEARCH_HEADER__
#define SEARCH_HEADER__

// Header consisting of Score, Arity, and Note

#include "search/note.hh"
#include "search/types.hh"

#include <stdint.h>

namespace search {

// Copying is shallow.  
class Header {
  public:
    bool Valid() const { return base_; }

    Score GetScore() const {
      return *reinterpret_cast<const float*>(base_);
    }
    void SetScore(Score to) {
      *reinterpret_cast<float*>(base_) = to;
    }
    bool operator<(const Header &other) const {
      return GetScore() < other.GetScore();
    }

    Arity GetArity() const {
      return *reinterpret_cast<const Arity*>(base_ + sizeof(Score));
    }

    Note GetNote() const {
      return *reinterpret_cast<const Note*>(base_ + sizeof(Score) + sizeof(Arity));
    }
    void SetNote(Note to) {
      *reinterpret_cast<Note*>(base_ + sizeof(Score) + sizeof(Arity)) = to;
    }

  protected:
    Header() : base_(NULL) {}

    Header(void *base, Arity arity) : base_(static_cast<uint8_t*>(base)) {
      *reinterpret_cast<Arity*>(base_ + sizeof(Score)) = arity;
    }

    static const std::size_t kHeaderSize = sizeof(Score) + sizeof(Arity) + sizeof(Note);

    uint8_t *After() { return base_ + kHeaderSize; }
    const uint8_t *After() const { return base_ + kHeaderSize; }

  private:
    uint8_t *base_;
};

} // namespace search

#endif // SEARCH_HEADER__