0031221: Visualization - selection filter in context
[occt.git] / src / SelectMgr / SelectMgr_ToleranceMap.cxx
1 // Copyright (c) 2016 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <SelectMgr_ToleranceMap.hxx>
15
16 //=======================================================================
17 // function: SelectMgr_ToleranceMap
18 // purpose :
19 //=======================================================================
20 SelectMgr_ToleranceMap::SelectMgr_ToleranceMap()
21 : myLargestKey (-1),
22   myCustomTolerance (-1)
23 {
24   //
25 }
26
27 //=======================================================================
28 // function: ~SelectMgr_ToleranceMap
29 // purpose :
30 //=======================================================================
31 SelectMgr_ToleranceMap::~SelectMgr_ToleranceMap()
32 {
33   myTolerances.Clear();
34 }
35
36 //=======================================================================
37 // function: Add
38 // purpose :
39 //=======================================================================
40 void SelectMgr_ToleranceMap::Add (const Standard_Integer& theTolerance)
41 {
42   if (Standard_Integer* aFreq = myTolerances.ChangeSeek (theTolerance))
43   {
44     ++(*aFreq);
45     if (*aFreq == 1 && theTolerance != myLargestKey)
46     {
47       myLargestKey = Max (theTolerance, myLargestKey);
48     }
49     return;
50   }
51
52   myTolerances.Bind (theTolerance, 1);
53   if (myTolerances.Extent() == 1)
54   {
55     myLargestKey = theTolerance;
56   }
57   else
58   {
59     myLargestKey = Max (theTolerance, myLargestKey);
60   }
61 }
62
63 //=======================================================================
64 // function: Decrement
65 // purpose :
66 //=======================================================================
67 void SelectMgr_ToleranceMap::Decrement (const Standard_Integer& theTolerance)
68 {
69   Standard_Integer* aFreq = myTolerances.ChangeSeek (theTolerance);
70   if (aFreq == NULL)
71   {
72     return;
73   }
74
75   Standard_ProgramError_Raise_if (*aFreq == 0, "SelectMgr_ToleranceMap::Decrement() - internal error");
76   --(*aFreq);
77
78   if (theTolerance == myLargestKey
79   && *aFreq == 0)
80   {
81     myLargestKey = 0;
82     for (NCollection_DataMap<Standard_Integer, Standard_Integer>::Iterator anIter (myTolerances); anIter.More(); anIter.Next())
83     {
84       if (anIter.Value() != 0)
85       {
86         myLargestKey = Max (myLargestKey, anIter.Key());
87       }
88     }
89   }
90 }