aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Native/Tango.ProtoTest/PMR/OutputLiquid.pb.cc
blob: 1532229a6e144513b608ba967fc0c2c6a861e582 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: OutputLiquid.proto

#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
#include "OutputLiquid.pb.h"

#include <algorithm>

#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/port.h>
#include <google/protobuf/stubs/once.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/wire_format_lite_inl.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/reflection_ops.h>
#include <google/protobuf/wire_format.h>
// @@protoc_insertion_point(includes)

namespace Tango {
namespace PMR {
namespace ColorLab {
class OutputLiquidDefaultTypeInternal {
public:
 ::google::protobuf::internal::ExplicitlyConstructed<OutputLiquid>
     _instance;
} _OutputLiquid_default_instance_;

namespace protobuf_OutputLiquid_2eproto {


namespace {

::google::protobuf::Metadata file_level_metadata[1];

}  // namespace

PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTableField
    const TableStruct::entries[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
  {0, 0, 0, ::google::protobuf::internal::kInvalidMask, 0, 0},
};

PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::AuxillaryParseTableField
    const TableStruct::aux[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
  ::google::protobuf::internal::AuxillaryParseTableField(),
};
PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTable const
    TableStruct::schema[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
  { NULL, NULL, 0, -1, -1, -1, -1, NULL, false },
};

const ::google::protobuf::uint32 TableStruct::offsets[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
  ~0u,  // no _has_bits_
  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(OutputLiquid, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(OutputLiquid, liquidtype_),
  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(OutputLiquid, volume_),
};
static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
  { 0, -1, sizeof(OutputLiquid)},
};

static ::google::protobuf::Message const * const file_default_instances[] = {
  reinterpret_cast<const ::google::protobuf::Message*>(&_OutputLiquid_default_instance_),
};

namespace {

void protobuf_AssignDescriptors() {
  AddDescriptors();
  ::google::protobuf::MessageFactory* factory = NULL;
  AssignDescriptors(
      "OutputLiquid.proto", schemas, file_default_instances, TableStruct::offsets, factory,
      file_level_metadata, NULL, NULL);
}

void protobuf_AssignDescriptorsOnce() {
  static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
  ::google::protobuf::GoogleOnceInit(&once, &protobuf_AssignDescriptors);
}

void protobuf_RegisterTypes(const ::std::string&) GOOGLE_ATTRIBUTE_COLD;
void protobuf_RegisterTypes(const ::std::string&) {
  protobuf_AssignDescriptorsOnce();
  ::google::protobuf::internal::RegisterAllTypes(file_level_metadata, 1);
}

}  // namespace
void TableStruct::InitDefaultsImpl() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  ::google::protobuf::internal::InitProtobufDefaults();
  ::Tango::PMR::ColorLab::protobuf_LiquidType_2eproto::InitDefaults();
  _OutputLiquid_default_instance_._instance.DefaultConstruct();
  ::google::protobuf::internal::OnShutdownDestroyMessage(
      &_OutputLiquid_default_instance_);}

void InitDefaults() {
  static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
  ::google::protobuf::GoogleOnceInit(&once, &TableStruct::InitDefaultsImpl);
}
namespace {
void AddDescriptorsImpl() {
  InitDefaults();
  static const char descriptor[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {
      "\n\022OutputLiquid.proto\022\022Tango.PMR.ColorLab"
      "\032\020LiquidType.proto\"R\n\014OutputLiquid\0222\n\nLi"
      "quidType\030\001 \001(\0162\036.Tango.PMR.ColorLab.Liqu"
      "idType\022\016\n\006Volume\030\004 \001(\001B\036\n\034com.twine.tang"
      "o.pmr.colorlabb\006proto3"
  };
  ::google::protobuf::DescriptorPool::InternalAddGeneratedFile(
      descriptor, 182);
  ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
    "OutputLiquid.proto", &protobuf_RegisterTypes);
  ::Tango::PMR::ColorLab::protobuf_LiquidType_2eproto::AddDescriptors();
}
} // anonymous namespace

void AddDescriptors() {
  static GOOGLE_PROTOBUF_DECLARE_ONCE(once);
  ::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl);
}
// Force AddDescriptors() to be called at dynamic initialization time.
struct StaticDescriptorInitializer {
  StaticDescriptorInitializer() {
    AddDescriptors();
  }
} static_descriptor_initializer;

}  // namespace protobuf_OutputLiquid_2eproto


// ===================================================================

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int OutputLiquid::kLiquidTypeFieldNumber;
const int OutputLiquid::kVolumeFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

OutputLiquid::OutputLiquid()
  : ::google::protobuf::Message(), _internal_metadata_(NULL) {
  if (GOOGLE_PREDICT_TRUE(this != internal_default_instance())) {
    protobuf_OutputLiquid_2eproto::InitDefaults();
  }
  SharedCtor();
  // @@protoc_insertion_point(constructor:Tango.PMR.ColorLab.OutputLiquid)
}
OutputLiquid::OutputLiquid(const OutputLiquid& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(NULL),
      _cached_size_(0) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::memcpy(&liquidtype_, &from.liquidtype_,
    static_cast<size_t>(reinterpret_cast<char*>(&volume_) -
    reinterpret_cast<char*>(&liquidtype_)) + sizeof(volume_));
  // @@protoc_insertion_point(copy_constructor:Tango.PMR.ColorLab.OutputLiquid)
}

void OutputLiquid::SharedCtor() {
  ::memset(&liquidtype_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&volume_) -
      reinterpret_cast<char*>(&liquidtype_)) + sizeof(volume_));
  _cached_size_ = 0;
}

OutputLiquid::~OutputLiquid() {
  // @@protoc_insertion_point(destructor:Tango.PMR.ColorLab.OutputLiquid)
  SharedDtor();
}

void OutputLiquid::SharedDtor() {
}

void OutputLiquid::SetCachedSize(int size) const {
  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
  _cached_size_ = size;
  GOOGLE_SAFE_CONCURRENT_WRITES_END();
}
const ::google::protobuf::Descriptor* OutputLiquid::descriptor() {
  protobuf_OutputLiquid_2eproto::protobuf_AssignDescriptorsOnce();
  return protobuf_OutputLiquid_2eproto::file_level_metadata[kIndexInFileMessages].descriptor;
}

const OutputLiquid& OutputLiquid::default_instance() {
  protobuf_OutputLiquid_2eproto::InitDefaults();
  return *internal_default_instance();
}

OutputLiquid* OutputLiquid::New(::google::protobuf::Arena* arena) const {
  OutputLiquid* n = new OutputLiquid;
  if (arena != NULL) {
    arena->Own(n);
  }
  return n;
}

void OutputLiquid::Clear() {
// @@protoc_insertion_point(message_clear_start:Tango.PMR.ColorLab.OutputLiquid)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  ::memset(&liquidtype_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&volume_) -
      reinterpret_cast<char*>(&liquidtype_)) + sizeof(volume_));
  _internal_metadata_.Clear();
}

bool OutputLiquid::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!GOOGLE_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:Tango.PMR.ColorLab.OutputLiquid)
  for (;;) {
    ::std::pair< ::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .Tango.PMR.ColorLab.LiquidType LiquidType = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) ==
            static_cast< ::google::protobuf::uint8>(8u /* 8 & 0xFF */)) {
          int value;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_liquidtype(static_cast< ::Tango::PMR::ColorLab::LiquidType >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // double Volume = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) ==
            static_cast< ::google::protobuf::uint8>(33u /* 33 & 0xFF */)) {

          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   double, ::google::protobuf::internal::WireFormatLite::TYPE_DOUBLE>(
                 input, &volume_)));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:Tango.PMR.ColorLab.OutputLiquid)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:Tango.PMR.ColorLab.OutputLiquid)
  return false;
#undef DO_
}

void OutputLiquid::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:Tango.PMR.ColorLab.OutputLiquid)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .Tango.PMR.ColorLab.LiquidType LiquidType = 1;
  if (this->liquidtype() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->liquidtype(), output);
  }

  // double Volume = 4;
  if (this->volume() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteDouble(4, this->volume(), output);
  }

  if ((_internal_metadata_.have_unknown_fields() &&  ::google::protobuf::internal::GetProto3PreserveUnknownsDefault())) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        (::google::protobuf::internal::GetProto3PreserveUnknownsDefault()   ? _internal_metadata_.unknown_fields()   : _internal_metadata_.default_instance()), output);
  }
  // @@protoc_insertion_point(serialize_end:Tango.PMR.ColorLab.OutputLiquid)
}

::google::protobuf::uint8* OutputLiquid::InternalSerializeWithCachedSizesToArray(
    bool deterministic, ::google::protobuf::uint8* target) const {
  (void)deterministic; // Unused
  // @@protoc_insertion_point(serialize_to_array_start:Tango.PMR.ColorLab.OutputLiquid)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .Tango.PMR.ColorLab.LiquidType LiquidType = 1;
  if (this->liquidtype() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->liquidtype(), target);
  }

  // double Volume = 4;
  if (this->volume() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteDoubleToArray(4, this->volume(), target);
  }

  if ((_internal_metadata_.have_unknown_fields() &&  ::google::protobuf::internal::GetProto3PreserveUnknownsDefault())) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        (::google::protobuf::internal::GetProto3PreserveUnknownsDefault()   ? _internal_metadata_.unknown_fields()   : _internal_metadata_.default_instance()), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:Tango.PMR.ColorLab.OutputLiquid)
  return target;
}

size_t OutputLiquid::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:Tango.PMR.ColorLab.OutputLiquid)
  size_t total_size = 0;

  if ((_internal_metadata_.have_unknown_fields() &&  ::google::protobuf::internal::GetProto3PreserveUnknownsDefault())) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        (::google::protobuf::internal::GetProto3PreserveUnknownsDefault()   ? _internal_metadata_.unknown_fields()   : _internal_metadata_.default_instance()));
  }
  // .Tango.PMR.ColorLab.LiquidType LiquidType = 1;
  if (this->liquidtype() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->liquidtype());
  }

  // double Volume = 4;
  if (this->volume() != 0) {
    total_size += 1 + 8;
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
  _cached_size_ = cached_size;
  GOOGLE_SAFE_CONCURRENT_WRITES_END();
  return total_size;
}

void OutputLiquid::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:Tango.PMR.ColorLab.OutputLiquid)
  GOOGLE_DCHECK_NE(&from, this);
  const OutputLiquid* source =
      ::google::protobuf::internal::DynamicCastToGenerated<const OutputLiquid>(
          &from);
  if (source == NULL) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:Tango.PMR.ColorLab.OutputLiquid)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:Tango.PMR.ColorLab.OutputLiquid)
    MergeFrom(*source);
  }
}

void OutputLiquid::MergeFrom(const OutputLiquid& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:Tango.PMR.ColorLab.OutputLiquid)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.liquidtype() != 0) {
    set_liquidtype(from.liquidtype());
  }
  if (from.volume() != 0) {
    set_volume(from.volume());
  }
}

void OutputLiquid::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:Tango.PMR.ColorLab.OutputLiquid)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void OutputLiquid::CopyFrom(const OutputLiquid& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:Tango.PMR.ColorLab.OutputLiquid)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool OutputLiquid::IsInitialized() const {
  return true;
}

void OutputLiquid::Swap(OutputLiquid* other) {
  if (other == this) return;
  InternalSwap(other);
}
void OutputLiquid::InternalSwap(OutputLiquid* other) {
  using std::swap;
  swap(liquidtype_, other->liquidtype_);
  swap(volume_, other->volume_);
  _internal_metadata_.Swap(&other->_internal_metadata_);
  swap(_cached_size_, other->_cached_size_);
}

::google::protobuf::Metadata OutputLiquid::GetMetadata() const {
  protobuf_OutputLiquid_2eproto::protobuf_AssignDescriptorsOnce();
  return protobuf_OutputLiquid_2eproto::file_level_metadata[kIndexInFileMessages];
}

#if PROTOBUF_INLINE_NOT_IN_HEADERS
// OutputLiquid

// .Tango.PMR.ColorLab.LiquidType LiquidType = 1;
void OutputLiquid::clear_liquidtype() {
  liquidtype_ = 0;
}
::Tango::PMR::ColorLab::LiquidType OutputLiquid::liquidtype() const {
  // @@protoc_insertion_point(field_get:Tango.PMR.ColorLab.OutputLiquid.LiquidType)
  return static_cast< ::Tango::PMR::ColorLab::LiquidType >(liquidtype_);
}
void OutputLiquid::set_liquidtype(::Tango::PMR::ColorLab::LiquidType value) {
  
  liquidtype_ = value;
  // @@protoc_insertion_point(field_set:Tango.PMR.ColorLab.OutputLiquid.LiquidType)
}

// double Volume = 4;
void OutputLiquid::clear_volume() {
  volume_ = 0;
}
double OutputLiquid::volume() const {
  // @@protoc_insertion_point(field_get:Tango.PMR.ColorLab.OutputLiquid.Volume)
  return volume_;
}
void OutputLiquid::set_volume(double value) {
  
  volume_ = value;
  // @@protoc_insertion_point(field_set:Tango.PMR.ColorLab.OutputLiquid.Volume)
}

#endif  // PROTOBUF_INLINE_NOT_IN_HEADERS

// @@protoc_insertion_point(namespace_scope)

}  // namespace ColorLab
}  // namespace PMR
}  // namespace Tango

// @@protoc_insertion_point(global_scope)
">foreach (CollapsedLineSection cs in newNode.collapsedSections) { newNode.lineNode.AddDirectlyCollapsed(cs); } } newNode.collapsedSections = removedNode.collapsedSections; MergeCollapsedSectionsIfPossible(newNode); } bool inRemoval; List<HeightTreeNode> nodesToCheckForMerging; void BeginRemoval() { Debug.Assert(!inRemoval); if (nodesToCheckForMerging == null) { nodesToCheckForMerging = new List<HeightTreeNode>(); } inRemoval = true; } void EndRemoval() { Debug.Assert(inRemoval); inRemoval = false; foreach (HeightTreeNode node in nodesToCheckForMerging) { MergeCollapsedSectionsIfPossible(node); } nodesToCheckForMerging.Clear(); } void MergeCollapsedSectionsIfPossible(HeightTreeNode node) { Debug.Assert(node != null); if (inRemoval) { nodesToCheckForMerging.Add(node); return; } // now check if we need to merge collapsedSections together bool merged = false; var collapsedL = node.lineNode.collapsedSections; if (collapsedL != null) { for (int i = collapsedL.Count - 1; i >= 0; i--) { CollapsedLineSection cs = collapsedL[i]; if (cs.Start == node.documentLine || cs.End == node.documentLine) continue; if (node.left == null || (node.left.collapsedSections != null && node.left.collapsedSections.Contains(cs))) { if (node.right == null || (node.right.collapsedSections != null && node.right.collapsedSections.Contains(cs))) { // all children of node contain cs: -> merge! if (node.left != null) node.left.RemoveDirectlyCollapsed(cs); if (node.right != null) node.right.RemoveDirectlyCollapsed(cs); collapsedL.RemoveAt(i); node.AddDirectlyCollapsed(cs); merged = true; } } } if (collapsedL.Count == 0) node.lineNode.collapsedSections = null; } if (merged && node.parent != null) { MergeCollapsedSectionsIfPossible(node.parent); } } #endregion #region GetNodeBy... / Get...FromNode HeightTreeNode GetNodeByIndex(int index) { Debug.Assert(index >= 0); Debug.Assert(index < root.totalCount); HeightTreeNode node = root; while (true) { if (node.left != null && index < node.left.totalCount) { node = node.left; } else { if (node.left != null) { index -= node.left.totalCount; } if (index == 0) return node; index--; node = node.right; } } } HeightTreeNode GetNodeByVisualPosition(double position) { HeightTreeNode node = root; while (true) { double positionAfterLeft = position; if (node.left != null) { positionAfterLeft -= node.left.totalHeight; if (positionAfterLeft < 0) { // Descend into left node = node.left; continue; } } double positionBeforeRight = positionAfterLeft - node.lineNode.TotalHeight; if (positionBeforeRight < 0) { // Found the correct node return node; } if (node.right == null || node.right.totalHeight == 0) { // Can happen when position>node.totalHeight, // i.e. at the end of the document, or due to rounding errors in previous loop iterations. // If node.lineNode isn't collapsed, return that. // Also return node.lineNode if there is no previous node that we could return instead. if (node.lineNode.TotalHeight > 0 || node.left == null) return node; // Otherwise, descend into left (find the last non-collapsed node) node = node.left; } else { // Descend into right position = positionBeforeRight; node = node.right; } } } static double GetVisualPositionFromNode(HeightTreeNode node) { double position = (node.left != null) ? node.left.totalHeight : 0; while (node.parent != null) { if (node.IsDirectlyCollapsed) position = 0; if (node == node.parent.right) { if (node.parent.left != null) position += node.parent.left.totalHeight; position += node.parent.lineNode.TotalHeight; } node = node.parent; } return position; } #endregion #region Public methods public DocumentLine GetLineByNumber(int number) { return GetNodeByIndex(number - 1).documentLine; } public DocumentLine GetLineByVisualPosition(double position) { return GetNodeByVisualPosition(position).documentLine; } public double GetVisualPosition(DocumentLine line) { return GetVisualPositionFromNode(GetNode(line)); } public double GetHeight(DocumentLine line) { return GetNode(line).lineNode.height; } public void SetHeight(DocumentLine line, double val) { var node = GetNode(line); node.lineNode.height = val; UpdateAfterChildrenChange(node); } public bool GetIsCollapsed(int lineNumber) { var node = GetNodeByIndex(lineNumber - 1); return node.lineNode.IsDirectlyCollapsed || GetIsCollapedFromNode(node); } /// <summary> /// Collapses the specified text section. /// Runtime: O(log n) /// </summary> public CollapsedLineSection CollapseText(DocumentLine start, DocumentLine end) { if (!document.Lines.Contains(start)) throw new ArgumentException("Line is not part of this document", "start"); if (!document.Lines.Contains(end)) throw new ArgumentException("Line is not part of this document", "end"); int length = end.LineNumber - start.LineNumber + 1; if (length < 0) throw new ArgumentException("start must be a line before end"); CollapsedLineSection section = new CollapsedLineSection(this, start, end); AddCollapsedSection(section, length); #if DEBUG CheckProperties(); #endif return section; } #endregion #region LineCount & TotalHeight public int LineCount { get { return root.totalCount; } } public double TotalHeight { get { return root.totalHeight; } } #endregion #region GetAllCollapsedSections IEnumerable<HeightTreeNode> AllNodes { get { if (root != null) { HeightTreeNode node = root.LeftMost; while (node != null) { yield return node; node = node.Successor; } } } } internal IEnumerable<CollapsedLineSection> GetAllCollapsedSections() { List<CollapsedLineSection> emptyCSList = new List<CollapsedLineSection>(); return System.Linq.Enumerable.Distinct( System.Linq.Enumerable.SelectMany( AllNodes, node => System.Linq.Enumerable.Concat(node.lineNode.collapsedSections ?? emptyCSList, node.collapsedSections ?? emptyCSList) )); } #endregion #region CheckProperties #if DEBUG [Conditional("DATACONSISTENCYTEST")] internal void CheckProperties() { CheckProperties(root); foreach (CollapsedLineSection cs in GetAllCollapsedSections()) { Debug.Assert(GetNode(cs.Start).lineNode.collapsedSections.Contains(cs)); Debug.Assert(GetNode(cs.End).lineNode.collapsedSections.Contains(cs)); int endLine = cs.End.LineNumber; for (int i = cs.Start.LineNumber; i <= endLine; i++) { CheckIsInSection(cs, GetLineByNumber(i)); } } // check red-black property: int blackCount = -1; CheckNodeProperties(root, null, RED, 0, ref blackCount); } void CheckIsInSection(CollapsedLineSection cs, DocumentLine line) { HeightTreeNode node = GetNode(line); if (node.lineNode.collapsedSections != null && node.lineNode.collapsedSections.Contains(cs)) return; while (node != null) { if (node.collapsedSections != null && node.collapsedSections.Contains(cs)) return; node = node.parent; } throw new InvalidOperationException(cs + " not found for line " + line); } void CheckProperties(HeightTreeNode node) { int totalCount = 1; double totalHeight = node.lineNode.TotalHeight; if (node.lineNode.IsDirectlyCollapsed) Debug.Assert(node.lineNode.collapsedSections.Count > 0); if (node.left != null) { CheckProperties(node.left); totalCount += node.left.totalCount; totalHeight += node.left.totalHeight; CheckAllContainedIn(node.left.collapsedSections, node.lineNode.collapsedSections); } if (node.right != null) { CheckProperties(node.right); totalCount += node.right.totalCount; totalHeight += node.right.totalHeight; CheckAllContainedIn(node.right.collapsedSections, node.lineNode.collapsedSections); } if (node.left != null && node.right != null) { if (node.left.collapsedSections != null && node.right.collapsedSections != null) { var intersection = System.Linq.Enumerable.Intersect(node.left.collapsedSections, node.right.collapsedSections); Debug.Assert(System.Linq.Enumerable.Count(intersection) == 0); } } if (node.IsDirectlyCollapsed) { Debug.Assert(node.collapsedSections.Count > 0); totalHeight = 0; } Debug.Assert(node.totalCount == totalCount); Debug.Assert(node.totalHeight.IsClose(totalHeight)); } /// <summary> /// Checks that all elements in list1 are contained in list2. /// </summary> static void CheckAllContainedIn(IEnumerable<CollapsedLineSection> list1, ICollection<CollapsedLineSection> list2) { if (list1 == null) list1 = new List<CollapsedLineSection>(); if (list2 == null) list2 = new List<CollapsedLineSection>(); foreach (CollapsedLineSection cs in list1) { Debug.Assert(list2.Contains(cs)); } } /* 1. A node is either red or black. 2. The root is black. 3. All leaves are black. (The leaves are the NIL children.) 4. Both children of every red node are black. (So every red node must have a black parent.) 5. Every simple path from a node to a descendant leaf contains the same number of black nodes. (Not counting the leaf node.) */ void CheckNodeProperties(HeightTreeNode node, HeightTreeNode parentNode, bool parentColor, int blackCount, ref int expectedBlackCount) { if (node == null) return; Debug.Assert(node.parent == parentNode); if (parentColor == RED) { Debug.Assert(node.color == BLACK); } if (node.color == BLACK) { blackCount++; } if (node.left == null && node.right == null) { // node is a leaf node: if (expectedBlackCount == -1) expectedBlackCount = blackCount; else Debug.Assert(expectedBlackCount == blackCount); } CheckNodeProperties(node.left, node, node.color, blackCount, ref expectedBlackCount); CheckNodeProperties(node.right, node, node.color, blackCount, ref expectedBlackCount); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public string GetTreeAsString() { StringBuilder b = new StringBuilder(); AppendTreeToString(root, b, 0); return b.ToString(); } static void AppendTreeToString(HeightTreeNode node, StringBuilder b, int indent) { if (node.color == RED) b.Append("RED "); else b.Append("BLACK "); b.AppendLine(node.ToString()); indent += 2; if (node.left != null) { b.Append(' ', indent); b.Append("L: "); AppendTreeToString(node.left, b, indent); } if (node.right != null) { b.Append(' ', indent); b.Append("R: "); AppendTreeToString(node.right, b, indent); } } #endif #endregion #region Red/Black Tree const bool RED = true; const bool BLACK = false; void InsertAsLeft(HeightTreeNode parentNode, HeightTreeNode newNode) { Debug.Assert(parentNode.left == null); parentNode.left = newNode; newNode.parent = parentNode; newNode.color = RED; UpdateAfterChildrenChange(parentNode); FixTreeOnInsert(newNode); } void InsertAsRight(HeightTreeNode parentNode, HeightTreeNode newNode) { Debug.Assert(parentNode.right == null); parentNode.right = newNode; newNode.parent = parentNode; newNode.color = RED; UpdateAfterChildrenChange(parentNode); FixTreeOnInsert(newNode); } void FixTreeOnInsert(HeightTreeNode node) { Debug.Assert(node != null); Debug.Assert(node.color == RED); Debug.Assert(node.left == null || node.left.color == BLACK); Debug.Assert(node.right == null || node.right.color == BLACK); HeightTreeNode parentNode = node.parent; if (parentNode == null) { // we inserted in the root -> the node must be black // since this is a root node, making the node black increments the number of black nodes // on all paths by one, so it is still the same for all paths. node.color = BLACK; return; } if (parentNode.color == BLACK) { // if the parent node where we inserted was black, our red node is placed correctly. // since we inserted a red node, the number of black nodes on each path is unchanged // -> the tree is still balanced return; } // parentNode is red, so there is a conflict here! // because the root is black, parentNode is not the root -> there is a grandparent node HeightTreeNode grandparentNode = parentNode.parent; HeightTreeNode uncleNode = Sibling(parentNode); if (uncleNode != null && uncleNode.color == RED) { parentNode.color = BLACK; uncleNode.color = BLACK; grandparentNode.color = RED; FixTreeOnInsert(grandparentNode); return; } // now we know: parent is red but uncle is black // First rotation: if (node == parentNode.right && parentNode == grandparentNode.left) { RotateLeft(parentNode); node = node.left; } else if (node == parentNode.left && parentNode == grandparentNode.right) { RotateRight(parentNode); node = node.right; } // because node might have changed, reassign variables: parentNode = node.parent; grandparentNode = parentNode.parent; // Now recolor a bit: parentNode.color = BLACK; grandparentNode.color = RED; // Second rotation: if (node == parentNode.left && parentNode == grandparentNode.left) { RotateRight(grandparentNode); } else { // because of the first rotation, this is guaranteed: Debug.Assert(node == parentNode.right && parentNode == grandparentNode.right); RotateLeft(grandparentNode); } } void RemoveNode(HeightTreeNode removedNode) { if (removedNode.left != null && removedNode.right != null) { // replace removedNode with it's in-order successor HeightTreeNode leftMost = removedNode.right.LeftMost; HeightTreeNode parentOfLeftMost = leftMost.parent; RemoveNode(leftMost); // remove leftMost from its current location BeforeNodeReplace(removedNode, leftMost, parentOfLeftMost); // and overwrite the removedNode with it ReplaceNode(removedNode, leftMost); leftMost.left = removedNode.left; if (leftMost.left != null) leftMost.left.parent = leftMost; leftMost.right = removedNode.right; if (leftMost.right != null) leftMost.right.parent = leftMost; leftMost.color = removedNode.color; UpdateAfterChildrenChange(leftMost); if (leftMost.parent != null) UpdateAfterChildrenChange(leftMost.parent); return; } // now either removedNode.left or removedNode.right is null // get the remaining child HeightTreeNode parentNode = removedNode.parent; HeightTreeNode childNode = removedNode.left ?? removedNode.right; BeforeNodeRemove(removedNode); ReplaceNode(removedNode, childNode); if (parentNode != null) UpdateAfterChildrenChange(parentNode); if (removedNode.color == BLACK) { if (childNode != null && childNode.color == RED) { childNode.color = BLACK; } else { FixTreeOnDelete(childNode, parentNode); } } } void FixTreeOnDelete(HeightTreeNode node, HeightTreeNode parentNode) { Debug.Assert(node == null || node.parent == parentNode); if (parentNode == null) return; // warning: node may be null HeightTreeNode sibling = Sibling(node, parentNode); if (sibling.color == RED) { parentNode.color = RED; sibling.color = BLACK; if (node == parentNode.left) { RotateLeft(parentNode); } else { RotateRight(parentNode); } sibling = Sibling(node, parentNode); // update value of sibling after rotation } if (parentNode.color == BLACK && sibling.color == BLACK && GetColor(sibling.left) == BLACK && GetColor(sibling.right) == BLACK) { sibling.color = RED; FixTreeOnDelete(parentNode, parentNode.parent); return; } if (parentNode.color == RED && sibling.color == BLACK && GetColor(sibling.left) == BLACK && GetColor(sibling.right) == BLACK) { sibling.color = RED; parentNode.color = BLACK; return; } if (node == parentNode.left && sibling.color == BLACK && GetColor(sibling.left) == RED && GetColor(sibling.right) == BLACK) { sibling.color = RED; sibling.left.color = BLACK; RotateRight(sibling); } else if (node == parentNode.right && sibling.color == BLACK && GetColor(sibling.right) == RED && GetColor(sibling.left) == BLACK) { sibling.color = RED; sibling.right.color = BLACK; RotateLeft(sibling); } sibling = Sibling(node, parentNode); // update value of sibling after rotation sibling.color = parentNode.color; parentNode.color = BLACK; if (node == parentNode.left) { if (sibling.right != null) { Debug.Assert(sibling.right.color == RED); sibling.right.color = BLACK; } RotateLeft(parentNode); } else { if (sibling.left != null) { Debug.Assert(sibling.left.color == RED); sibling.left.color = BLACK; } RotateRight(parentNode); } } void ReplaceNode(HeightTreeNode replacedNode, HeightTreeNode newNode) { if (replacedNode.parent == null) { Debug.Assert(replacedNode == root); root = newNode; } else { if (replacedNode.parent.left == replacedNode) replacedNode.parent.left = newNode; else replacedNode.parent.right = newNode; } if (newNode != null) { newNode.parent = replacedNode.parent; } replacedNode.parent = null; } void RotateLeft(HeightTreeNode p) { // let q be p's right child HeightTreeNode q = p.right; Debug.Assert(q != null); Debug.Assert(q.parent == p); // set q to be the new root ReplaceNode(p, q); // set p's right child to be q's left child p.right = q.left; if (p.right != null) p.right.parent = p; // set q's left child to be p q.left = p; p.parent = q; UpdateAfterRotateLeft(p); } void RotateRight(HeightTreeNode p) { // let q be p's left child HeightTreeNode q = p.left; Debug.Assert(q != null); Debug.Assert(q.parent == p); // set q to be the new root ReplaceNode(p, q); // set p's left child to be q's right child p.left = q.right; if (p.left != null) p.left.parent = p; // set q's right child to be p q.right = p; p.parent = q; UpdateAfterRotateRight(p); } static HeightTreeNode Sibling(HeightTreeNode node) { if (node == node.parent.left) return node.parent.right; else return node.parent.left; } static HeightTreeNode Sibling(HeightTreeNode node, HeightTreeNode parentNode) { Debug.Assert(node == null || node.parent == parentNode); if (node == parentNode.left) return parentNode.right; else return parentNode.left; } static bool GetColor(HeightTreeNode node) { return node != null ? node.color : BLACK; } #endregion #region Collapsing support static bool GetIsCollapedFromNode(HeightTreeNode node) { while (node != null) { if (node.IsDirectlyCollapsed) return true; node = node.parent; } return false; } internal void AddCollapsedSection(CollapsedLineSection section, int sectionLength) { AddRemoveCollapsedSection(section, sectionLength, true); } void AddRemoveCollapsedSection(CollapsedLineSection section, int sectionLength, bool add) { Debug.Assert(sectionLength > 0); HeightTreeNode node = GetNode(section.Start); // Go up in the tree. while (true) { // Mark all middle nodes as collapsed if (add) node.lineNode.AddDirectlyCollapsed(section); else node.lineNode.RemoveDirectlyCollapsed(section); sectionLength -= 1; if (sectionLength == 0) { // we are done! Debug.Assert(node.documentLine == section.End); break; } // Mark all right subtrees as collapsed. if (node.right != null) { if (node.right.totalCount < sectionLength) { if (add) node.right.AddDirectlyCollapsed(section); else node.right.RemoveDirectlyCollapsed(section); sectionLength -= node.right.totalCount; } else { // mark partially into the right subtree: go down the right subtree. AddRemoveCollapsedSectionDown(section, node.right, sectionLength, add); break; } } // go up to the next node HeightTreeNode parentNode = node.parent; Debug.Assert(parentNode != null); while (parentNode.right == node) { node = parentNode; parentNode = node.parent; Debug.Assert(parentNode != null); } node = parentNode; } UpdateAugmentedData(GetNode(section.Start), UpdateAfterChildrenChangeRecursionMode.WholeBranch); UpdateAugmentedData(GetNode(section.End), UpdateAfterChildrenChangeRecursionMode.WholeBranch); } static void AddRemoveCollapsedSectionDown(CollapsedLineSection section, HeightTreeNode node, int sectionLength, bool add) { while (true) { if (node.left != null) { if (node.left.totalCount < sectionLength) { // mark left subtree if (add) node.left.AddDirectlyCollapsed(section); else node.left.RemoveDirectlyCollapsed(section); sectionLength -= node.left.totalCount; } else { // mark only inside the left subtree node = node.left; Debug.Assert(node != null); continue; } } if (add) node.lineNode.AddDirectlyCollapsed(section); else node.lineNode.RemoveDirectlyCollapsed(section); sectionLength -= 1; if (sectionLength == 0) { // done! Debug.Assert(node.documentLine == section.End); break; } // mark inside right subtree: node = node.right; Debug.Assert(node != null); } } public void Uncollapse(CollapsedLineSection section) { int sectionLength = section.End.LineNumber - section.Start.LineNumber + 1; AddRemoveCollapsedSection(section, sectionLength, false); // do not call CheckProperties() in here - Uncollapse is also called during line removals } #endregion } }