fix comment, expose verify_list

This commit is contained in:
David Rose 2002-03-28 01:50:54 +00:00
parent 8f72ae5079
commit 333bfc3050
5 changed files with 102 additions and 121 deletions

View File

@ -73,8 +73,6 @@ ConfigureFn(config_util) {
//const bool track_memory_usage = config_util.GetBool("track-memory-usage", false); //const bool track_memory_usage = config_util.GetBool("track-memory-usage", false);
const bool paranoid_ordered_vector = config_util.GetBool("paranoid-ordered-vector", false);
DSearchPath & DSearchPath &
get_model_path() { get_model_path() {
static DSearchPath *model_path = NULL; static DSearchPath *model_path = NULL;

View File

@ -36,8 +36,6 @@ NotifyCategoryDecl(bam, EXPCL_PANDA, EXPTP_PANDA);
//extern EXPCL_PANDA const bool track_memory_usage; //extern EXPCL_PANDA const bool track_memory_usage;
extern EXPCL_PANDA const bool paranoid_ordered_vector;
// These are functions instead of constant variables because they are // These are functions instead of constant variables because they are
// computed based on the concatenation of all appearances of the // computed based on the concatenation of all appearances of the
// corresponding variable in the config files. // corresponding variable in the config files.

View File

@ -326,7 +326,6 @@ insert_unique(const ordered_vector<Key, Compare>::VALUE_TYPE &key) {
} }
ITERATOR result = _vector.insert(position, key); ITERATOR result = _vector.insert(position, key);
verify_list();
return pair<ITERATOR, bool>(result, true); return pair<ITERATOR, bool>(result, true);
} }
@ -348,7 +347,6 @@ insert_nonunique(const ordered_vector<Key, Compare>::VALUE_TYPE &key) {
nassertr(position >= begin() && position <= end(), end()); nassertr(position >= begin() && position <= end(), end());
ITERATOR result = _vector.insert(position, key); ITERATOR result = _vector.insert(position, key);
verify_list();
return result; return result;
} }
@ -662,45 +660,9 @@ find_insert_position(ordered_vector<Key, Compare>::ITERATOR first,
ordered_vector<Key, Compare>::ITERATOR last, ordered_vector<Key, Compare>::ITERATOR last,
const ordered_vector<Key, Compare>::KEY_TYPE &key) { const ordered_vector<Key, Compare>::KEY_TYPE &key) {
ITERATOR result = r_find_insert_position(first, last, key); ITERATOR result = r_find_insert_position(first, last, key);
#ifndef NDEBUG
// Verify the result.
if (paranoid_ordered_vector) {
// If there is a node before the indicated position, this node
// must not precede it lexicograpically.
if (first < result) {
nassertr(!_compare(key, *(result - 1)), result);
}
// If there is a node after the indicated position, it must not
// precede this node lexicographically.
if (result < last) {
nassertr(!_compare(*(result), key), result);
}
}
#endif
return result; return result;
} }
////////////////////////////////////////////////////////////////////
// Function: ordered_vector::verify_list
// Access: Private
// Description: Ensures that the indicated range of elements is
// sorted correctly, if paranoid_ordered_vector is set.
// Generates an assertion failure (and returns false) if
// this is not the case; otherwise, returns true.
////////////////////////////////////////////////////////////////////
template<class Key, class Compare>
INLINE bool ordered_vector<Key, Compare>::
verify_list() {
#ifndef NDEBUG
if (paranoid_ordered_vector) {
return verify_list_impl(begin(), end());
}
#endif
return true;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ov_set::Constructor // Function: ov_set::Constructor
// Access: Public // Access: Public
@ -771,6 +733,17 @@ sort() {
ordered_vector<Key, Compare>::sort_unique(); ordered_vector<Key, Compare>::sort_unique();
} }
////////////////////////////////////////////////////////////////////
// Function: ov_set::verify_list
// Access: Public
// Description: Maps to verify_list_unique().
////////////////////////////////////////////////////////////////////
template<class Key, class Compare>
INLINE bool ov_set<Key, Compare>::
verify_list() const {
return ordered_vector<Key, Compare>::verify_list_unique();
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ov_multiset::Constructor // Function: ov_multiset::Constructor
// Access: Public // Access: Public
@ -840,3 +813,14 @@ INLINE void ov_multiset<Key, Compare>::
sort() { sort() {
ordered_vector<Key, Compare>::sort_nonunique(); ordered_vector<Key, Compare>::sort_nonunique();
} }
////////////////////////////////////////////////////////////////////
// Function: ov_multiset::verify_list
// Access: Public
// Description: Maps to verify_list_nonunique().
////////////////////////////////////////////////////////////////////
template<class Key, class Compare>
INLINE bool ov_multiset<Key, Compare>::
verify_list() const {
return ordered_vector<Key, Compare>::verify_list_nonunique();
}

View File

@ -62,7 +62,6 @@ insert_unique(ordered_vector<Key, Compare>::ITERATOR position,
// Otherwise, we may insert where the caller requested. // Otherwise, we may insert where the caller requested.
ITERATOR result = _vector.insert(position, key); ITERATOR result = _vector.insert(position, key);
verify_list();
return result; return result;
} }
@ -100,10 +99,62 @@ insert_nonunique(ordered_vector<Key, Compare>::ITERATOR position,
// Otherwise, we may insert where the caller requested. // Otherwise, we may insert where the caller requested.
ITERATOR result = _vector.insert(position, key); ITERATOR result = _vector.insert(position, key);
verify_list();
return result; return result;
} }
////////////////////////////////////////////////////////////////////
// Function: ordered_vector::verify_list_unique
// Access: Public
// Description: Ensures that the indicated range of elements is
// sorted correctly. Returns true if this is the case;
// otherwise, returns false.
////////////////////////////////////////////////////////////////////
template<class Key, class Compare>
bool ordered_vector<Key, Compare>::
verify_list_unique() const {
if (!empty()) {
CONST_ITERATOR prev = begin();
CONST_ITERATOR i = begin();
++i;
while (i < end()) {
bool ordered_correctly = _compare(*prev, *i);
if (!ordered_correctly) {
return true;
}
prev = i;
++i;
}
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: ordered_vector::verify_list_nonunique
// Access: Public
// Description: Ensures that the indicated range of elements is
// sorted correctly. Returns true if this is the case;
// otherwise, returns false.
////////////////////////////////////////////////////////////////////
template<class Key, class Compare>
bool ordered_vector<Key, Compare>::
verify_list_nonunique() const {
if (!empty()) {
CONST_ITERATOR prev = begin();
CONST_ITERATOR i = begin();
++i;
while (i < end()) {
bool ordered_correctly = !_compare(*i, *prev);
if (!ordered_correctly) {
return true;
}
prev = i;
++i;
}
}
return true;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ordered_vector::r_find_insert_position // Function: ordered_vector::r_find_insert_position
// Access: Private // Access: Private
@ -356,28 +407,3 @@ r_equal_range(ordered_vector<Key, Compare>::CONST_ITERATOR first,
return pair_type(lower, upper); return pair_type(lower, upper);
} }
} }
#ifndef NDEBUG
////////////////////////////////////////////////////////////////////
// Function: ordered_vector::verify_list_impl
// Access: Private
// Description: The non-inline implementation of verify_list().
////////////////////////////////////////////////////////////////////
template<class Key, class Compare>
bool ordered_vector<Key, Compare>::
verify_list_impl(ordered_vector<Key, Compare>::ITERATOR first,
ordered_vector<Key, Compare>::ITERATOR last) {
if (first < last) {
ITERATOR prev = first;
ITERATOR i = first;
++i;
while (i < last) {
bool ordered_correctly = !_compare(*i, *prev);
nassertr(ordered_correctly, false);
prev = i;
++i;
}
}
return true;
}
#endif // NDEBUG

View File

@ -26,53 +26,31 @@
#include "pset.h" #include "pset.h"
#include <algorithm> #include <algorithm>
// Two different compilers that both should have known better had // There are some inheritance issues with template classes and typedef
// problems parsing the inheritance of typedefs in the template // names. Template classes that inherit typedef names from their base
// classes below. Both gcc 2.95.3 and the Intel Windows compiler (not // class, which is also a template class, may confuse the typedef
// sure of the version) got confused in different ways. It is a // names with globally scoped template names. In particular, the
// mystery how these compilers are able to handle the actual STL // local "iterator" type is easily confused with the std::iterator
// headers, which do this sort of thing all over the place. // template class.
// One effective workaround for both compilers seems to be to rename // To work around this problem, as well as a problem in gcc 2.95.3
// the typedef names slightly. If the following symbol is declared, // with value_type etc. not inheriting properly (even though we
// the macros in this file will do the job of renaming the typedef // explicitly typedef them in the derived class), we rename the
// names for these broken compilers. We should probably make this a // questionable typedefs here so that they no longer conflict with the
// configurable parameter, but since it doesn't do any harm to leave // global template classes.
// it declared even for non-broken compilers, we might as well just
// leave it alone.
#define BROKEN_TYPEDEF_INHERITANCE 1
// Maybe eventually, when STL is more than only about ten years old #define KEY_TYPE key_type_0
// and compiler support of anything more than trivial template classes #define VALUE_TYPE value_type_0
// is more universal, we can pull this nonsense out of here. #define REFERENCE reference_0
#define CONST_REFERENCE const_reference_0
#ifdef BROKEN_TYPEDEF_INHERITANCE #define KEY_COMPARE key_compare_0
#define KEY_TYPE key_type_0 #define VALUE_COMPARE value_compare_0
#define VALUE_TYPE value_type_0 #define ITERATOR iterator_0
#define REFERENCE reference_0 #define CONST_ITERATOR const_iterator_0
#define CONST_REFERENCE const_reference_0 #define REVERSE_ITERATOR reverse_iterator_0
#define KEY_COMPARE key_compare_0 #define CONST_REVERSE_ITERATOR const_reverse_iterator_0
#define VALUE_COMPARE value_compare_0 #define DIFFERENCE_TYPE difference_type_0
#define ITERATOR iterator_0 #define SIZE_TYPE size_type_0
#define CONST_ITERATOR const_iterator_0
#define REVERSE_ITERATOR reverse_iterator_0
#define CONST_REVERSE_ITERATOR const_reverse_iterator_0
#define DIFFERENCE_TYPE difference_type_0
#define SIZE_TYPE size_type_0
#else
#define KEY_TYPE key_type
#define VALUE_TYPE value_type
#define REFERENCE reference
#define CONST_REFERENCE const_reference
#define KEY_COMPARE key_compare
#define VALUE_COMPARE value_compare
#define ITERATOR iterator
#define CONST_ITERATOR const_iterator
#define REVERSE_ITERATOR reverse_iterator
#define CONST_REVERSE_ITERATOR const_reverse_iterator
#define DIFFERENCE_TYPE difference_type
#define SIZE_TYPE size_type
#endif
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Class : ordered_vector // Class : ordered_vector
@ -135,7 +113,6 @@ public:
typedef Vector::difference_type DIFFERENCE_TYPE; typedef Vector::difference_type DIFFERENCE_TYPE;
typedef Vector::size_type SIZE_TYPE; typedef Vector::size_type SIZE_TYPE;
#ifdef BROKEN_TYPEDEF_INHERITANCE
// Since the #define symbols do not actually expand to the correct // Since the #define symbols do not actually expand to the correct
// names, we have to re-typedef them so callers can reference them // names, we have to re-typedef them so callers can reference them
// by their correct, lowercase names. // by their correct, lowercase names.
@ -151,7 +128,6 @@ public:
typedef CONST_REVERSE_ITERATOR const_reverse_iterator; typedef CONST_REVERSE_ITERATOR const_reverse_iterator;
typedef DIFFERENCE_TYPE difference_type; typedef DIFFERENCE_TYPE difference_type;
typedef SIZE_TYPE size_type; typedef SIZE_TYPE size_type;
#endif
public: public:
// Constructors. We don't implement the whole slew of STL // Constructors. We don't implement the whole slew of STL
@ -221,6 +197,8 @@ public:
INLINE void reserve(SIZE_TYPE n); INLINE void reserve(SIZE_TYPE n);
INLINE void sort_unique(); INLINE void sort_unique();
INLINE void sort_nonunique(); INLINE void sort_nonunique();
bool verify_list_unique() const;
bool verify_list_nonunique() const;
INLINE void push_back(const VALUE_TYPE &key); INLINE void push_back(const VALUE_TYPE &key);
@ -245,11 +223,6 @@ private:
pair<CONST_ITERATOR, CONST_ITERATOR> pair<CONST_ITERATOR, CONST_ITERATOR>
r_equal_range(CONST_ITERATOR first, CONST_ITERATOR last, r_equal_range(CONST_ITERATOR first, CONST_ITERATOR last,
const KEY_TYPE &key) const; const KEY_TYPE &key) const;
INLINE bool verify_list();
#ifndef NDEBUG
bool verify_list_impl(ITERATOR first, ITERATOR last);
#endif
// This function object is used in sort_unique(). It returns true // This function object is used in sort_unique(). It returns true
// if two consecutive sorted elements are equivalent. // if two consecutive sorted elements are equivalent.
@ -290,6 +263,7 @@ public:
INLINE pair<ITERATOR, bool> insert(const VALUE_TYPE &key0); INLINE pair<ITERATOR, bool> insert(const VALUE_TYPE &key0);
INLINE void sort(); INLINE void sort();
INLINE bool verify_list() const;
}; };
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -309,6 +283,7 @@ public:
INLINE ITERATOR insert(const VALUE_TYPE &key); INLINE ITERATOR insert(const VALUE_TYPE &key);
INLINE void sort(); INLINE void sort();
INLINE bool verify_list() const;
}; };
#include "ordered_vector.I" #include "ordered_vector.I"