summaryrefslogtreecommitdiff
path: root/src/expr/record.cpp
blob: 2c59962137c81adeddd46d307766edfed73e9bc8 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*********************                                                        */
/*! \file record.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Tim King, Morgan Deters, Paul Meng
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief A class representing a record definition
 **
 ** A class representing a record definition.
 **/

#include "expr/record.h"

#include "base/cvc4_assert.h"
#include "base/output.h"
#include "expr/expr.h"
#include "expr/type.h"


namespace CVC4 {

static Record::FieldVector::const_iterator find(const Record::FieldVector& fields, std::string name){
  for(Record::FieldVector::const_iterator i = fields.begin(), i_end = fields.end(); i != i_end; ++i){
    if((*i).first == name) {
      return i;
    }
  }
  return fields.end();
}

Record::Record(const FieldVector& fields)
    : d_fields(new FieldVector(fields))
{
  Debug("record") << "making " << this << " " << d_fields << std::endl;
}

Record::Record(const Record& other)
    : d_fields(new FieldVector(other.getFields()))
{
  Debug("record") << "copy constructor " << this << " " << d_fields << std::endl;
}

Record::~Record() {
  Debug("record") << "deleting " << this << " " << d_fields << std::endl;
  delete d_fields;
}

Record& Record::operator=(const Record& r) {
  Debug("record") << "setting " << this << " " << d_fields << std::endl;
  Record::FieldVector& local = *d_fields;
  local = r.getFields();
  return *this;
}

const Record::FieldVector& Record::getFields() const {
  return *d_fields;
}

bool Record::contains(const std::string& name) const {
  return find(*d_fields, name) != d_fields->end();
}


size_t Record::getIndex(std::string name) const {
  FieldVector::const_iterator i = find(*d_fields, name);
  PrettyCheckArgument(i != d_fields->end(), name,
                      "requested field `%s' does not exist in record",
                      name.c_str());
  return i - d_fields->begin();
}

size_t Record::getNumFields() const {
  return d_fields->size();
}


std::ostream& operator<<(std::ostream& os, const Record& r) {
  os << "[# ";
  bool first = true;
  const Record::FieldVector& fields = r.getFields();
  for(Record::FieldVector::const_iterator i = fields.begin(),
      i_end = fields.end(); i != i_end; ++i) {
    if(!first) {
      os << ", ";
    }
    os << (*i).first << ":" << (*i).second;
    first = false;
  }
  os << " #]";

  return os;
}

size_t RecordHashFunction::operator()(const Record& r) const {
  size_t n = 0;
  const Record::FieldVector& fields = r.getFields();
  for(Record::FieldVector::const_iterator i = fields.begin(),
      i_end = fields.end(); i != i_end; ++i) {
    n = (n << 3) ^ TypeHashFunction()((*i).second);
  }
  return n;
}

std::ostream& operator<<(std::ostream& out, const RecordUpdate& t) {
  return out << "[" << t.getField() << "]";
}


const std::pair<std::string, Type>& Record::operator[](size_t index) const {
  PrettyCheckArgument(index < d_fields->size(), index,
                      "index out of bounds for record type");
  return (*d_fields)[index];
}

bool Record::operator==(const Record& r) const {
  return (*d_fields) == *(r.d_fields);
}

}/* CVC4 namespace */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback