Reference documentation for deal.II version 8.4.2
cell_id.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2015 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__cell_id_h
17 #define dealii__cell_id_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 
22 #include <vector>
23 #include <iostream>
24 
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 
55 class CellId
56 {
57 public:
61  explicit CellId(unsigned int coarse_cell_id_, std::vector<unsigned char> id_)
62  : coarse_cell_id(coarse_cell_id_), id(id_)
63  {}
64 
69  : coarse_cell_id(-1)
70  {}
71 
75  std::string to_string() const;
76 
80  bool operator== (const CellId &other) const;
81 
85  bool operator!= (const CellId &other) const;
86 
90  bool operator<(const CellId &other) const;
91 
92  friend std::istream &operator>> (std::istream &is, CellId &cid);
93  friend std::ostream &operator<< (std::ostream &os, const CellId &cid);
94 private:
95  unsigned int coarse_cell_id;
96  std::vector<unsigned char> id;
97 };
98 
102 inline std::ostream &operator<< (std::ostream &os, const CellId &cid)
103 {
104  os << cid.coarse_cell_id << '_' << cid.id.size() << ':';
105  for (unsigned int i=0; i<cid.id.size(); ++i)
106  os << static_cast<int>(cid.id[i]);
107  return os;
108 }
109 
113 inline std::istream &operator>> (std::istream &is, CellId &cid)
114 {
115  unsigned int cellid;
116  is >> cellid;
117  if (is.eof())
118  return is;
119 
120  cid.coarse_cell_id = cellid;
121  char dummy;
122  is >> dummy;
123  Assert(dummy=='_', ExcMessage("invalid CellId"));
124  unsigned int idsize;
125  is >> idsize;
126  is >> dummy;
127  Assert(dummy==':', ExcMessage("invalid CellId"));
128 
129  char value;
130  cid.id.clear();
131  for (unsigned int i=0; i<idsize; ++i)
132  {
133  is >> value;
134  cid.id.push_back(value-'0');
135  }
136  return is;
137 }
138 
139 inline bool
140 CellId::operator== (const CellId &other) const
141 {
142  if (this->coarse_cell_id != other.coarse_cell_id)
143  return false;
144  return id == other.id;
145 }
146 
147 inline bool
148 CellId::operator!= (const CellId &other) const
149 {
150  return !(*this == other);
151 }
152 
153 inline
154 bool CellId::operator<(const CellId &other) const
155 {
156  if (this->coarse_cell_id != other.coarse_cell_id)
157  return this->coarse_cell_id < other.coarse_cell_id;
158 
159  unsigned int idx = 0;
160  while (idx < id.size())
161  {
162  if (idx>=other.id.size())
163  return false;
164 
165  if (id[idx] != other.id[idx])
166  return id[idx] < other.id[idx];
167 
168  ++idx;
169  }
170 
171  if (id.size() == other.id.size())
172  return false;
173  return true; // other.id is longer
174 }
175 
176 DEAL_II_NAMESPACE_CLOSE
177 
178 #endif
::ExceptionBase & ExcMessage(std::string arg1)
CellId()
Definition: cell_id.h:68
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition: cell_id.h:102
#define Assert(cond, exc)
Definition: exceptions.h:294
bool operator<(const CellId &other) const
Definition: cell_id.h:154
bool operator!=(const CellId &other) const
Definition: cell_id.h:148
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition: cell_id.h:113
bool operator==(const CellId &other) const
Definition: cell_id.h:140
std::string to_string() const
Definition: cell_id.cc:23
Definition: cell_id.h:55
CellId(unsigned int coarse_cell_id_, std::vector< unsigned char > id_)
Definition: cell_id.h:61