vtkGeoTreeNodeCache.cxx 4.53 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()
{
31 32
  this->Oldest = nullptr;
  this->Newest = nullptr;
33 34 35 36 37 38 39 40 41 42 43
  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())
44
  {
45 46
    cur->SetOlder(nullptr);
    cur->SetNewer(nullptr);
47
  }
48 49 50 51 52 53
}

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

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

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

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

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

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

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

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

  if (!node->GetNewer())
141
  {
142
    this->Newest = node->GetOlder();
143
  }
144
  else
145
  {
146
    node->GetNewer()->SetOlder(node->GetOlder());
147
  }
148
  if (!node->GetOlder())
149
  {
150
    this->Oldest = node->GetNewer();
151
  }
152
  else
153
  {
154
    node->GetOlder()->SetNewer(node->GetNewer());
155
  }
156 157
  node->SetOlder(nullptr);
  node->SetNewer(nullptr);
158 159 160 161 162 163 164 165 166
  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;
167
  os << indent << "Size: " << this->Size << endl;
168
}