vtkGeoTreeNodeCache.cxx 4.6 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkGeoTreeNodeCache.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/

#include "vtkGeoTreeNodeCache.h"

#include "vtkGeoTreeNode.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"

vtkStandardNewMacro(vtkGeoTreeNodeCache);
//----------------------------------------------------------------------------
vtkGeoTreeNodeCache::vtkGeoTreeNodeCache()
{
Sankhesh Jhaveri's avatar
Sankhesh Jhaveri committed
31
  VTK_LEGACY_BODY(vtkGeoTreeNodeCache::vtkGeoTreeNodeCache, "VTK 8.2");
32 33
  this->Oldest = nullptr;
  this->Newest = nullptr;
34 35 36 37 38 39 40 41 42 43 44
  this->Size = 0;
  this->CacheMaximumLimit = 500;
  this->CacheMinimumLimit = 250;
}

//----------------------------------------------------------------------------
vtkGeoTreeNodeCache::~vtkGeoTreeNodeCache()
{
  // Break reference loops by explicitly setting all prev/next pointers to null.
  vtkGeoTreeNode* cur;
  for (cur = this->Newest; cur; cur = cur->GetOlder())
45
  {
46 47
    cur->SetOlder(nullptr);
    cur->SetNewer(nullptr);
48
  }
49 50 51 52 53 54
}

//----------------------------------------------------------------------------
void vtkGeoTreeNodeCache::SendToFront(vtkGeoTreeNode* node)
{
  if (node == this->Newest)
55
  {
56
    return;
57
  }
58 59 60 61 62 63

  // Remove from the list if in the list already
  this->RemoveNode(node);

  // Add to the beginning of the list
  if (this->Size > 0)
64
  {
65
    node->SetNewer(nullptr);
66 67 68
    node->SetOlder(this->Newest);
    this->Newest->SetNewer(node);
    this->Newest = node;
69
  }
70
  else
71
  {
72 73
    node->SetNewer(nullptr);
    node->SetOlder(nullptr);
74 75
    this->Newest = node;
    this->Oldest = node;
76
  }
77 78
  this->Size++;
  if (this->Size > this->CacheMaximumLimit)
79
  {
80
    this->TrimToCacheMinimum();
81
  }
82 83 84 85 86 87
}

//----------------------------------------------------------------------------
void vtkGeoTreeNodeCache::TrimToCacheMinimum()
{
  while (this->Size > this->CacheMinimumLimit)
88
  {
89
    vtkGeoTreeNode* node = this->Oldest;
90
    node->GetNewer()->SetOlder(nullptr);
91
    this->Oldest = node->GetNewer();
92 93
    node->SetOlder(nullptr);
    node->SetNewer(nullptr);
94 95 96 97 98 99

    // If this was the last of a set of siblings to leave the list,
    // delete data from all siblings.
    this->DeleteDataFromSiblings(node);

    this->Size--;
100
  }
101 102 103 104 105 106 107 108
}

//----------------------------------------------------------------------------
void vtkGeoTreeNodeCache::DeleteDataFromSiblings(vtkGeoTreeNode* node)
{
  // Delete data from node or siblings if possible.
  vtkGeoTreeNode* parent = node->GetParentTreeNode();
  if (!parent)
109
  {
110
    return;
111
  }
112 113
  bool canDeleteSiblings = true;
  for (int c = 0; c < 4; ++c)
114
  {
115 116
    vtkGeoTreeNode* child = parent->GetChildTreeNode(c);
    if (!child || child->GetOlder() || child->GetNewer() || child == this->Newest)
117
    {
118 119 120
      canDeleteSiblings = false;
      break;
    }
121
  }
122
  if (canDeleteSiblings)
123
  {
124
    for (int c = 0; c < 4; ++c)
125
    {
126 127 128
      vtkGeoTreeNode* child = parent->GetChildTreeNode(c);
      child->DeleteData();
    }
129
  }
130 131 132 133 134 135
}

//----------------------------------------------------------------------------
void vtkGeoTreeNodeCache::RemoveNode(vtkGeoTreeNode* node)
{
  if (!node->GetNewer() && !node->GetOlder() && node != this->Newest)
136
  {
137 138
    // The node is not in the list
    return;
139
  }
140 141

  if (!node->GetNewer())
142
  {
143
    this->Newest = node->GetOlder();
144
  }
145
  else
146
  {
147
    node->GetNewer()->SetOlder(node->GetOlder());
148
  }
149
  if (!node->GetOlder())
150
  {
151
    this->Oldest = node->GetNewer();
152
  }
153
  else
154
  {
155
    node->GetOlder()->SetNewer(node->GetNewer());
156
  }
157 158
  node->SetOlder(nullptr);
  node->SetNewer(nullptr);
159 160 161 162 163 164 165 166 167
  this->Size--;
}

//----------------------------------------------------------------------------
void vtkGeoTreeNodeCache::PrintSelf(ostream & os, vtkIndent indent)
{
  this->Superclass::PrintSelf( os, indent );
  os << indent << "CacheMinimumLimit: " << this->CacheMinimumLimit << endl;
  os << indent << "CacheMaximumLimit: " << this->CacheMaximumLimit << endl;
168
  os << indent << "Size: " << this->Size << endl;
169
}