Panda3D
Loading...
Searching...
No Matches
internalNameCollection.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file internalNameCollection.cxx
10 * @author drose
11 * @date 2002-03-16
12 */
13
15
16#include "indent.h"
17
18/**
19 *
20 */
21InternalNameCollection::
22InternalNameCollection() {
23}
24
25/**
26 *
27 */
28InternalNameCollection::
29InternalNameCollection(const InternalNameCollection &copy) :
30 _names(copy._names)
31{
32}
33
34/**
35 *
36 */
37void InternalNameCollection::
38operator = (const InternalNameCollection &copy) {
39 _names = copy._names;
40}
41
42/**
43 * Adds a new InternalName to the collection.
44 */
46add_name(const InternalName *name) {
47 // If the pointer to our internal array is shared by any other
48 // InternalNameCollections, we have to copy the array now so we won't
49 // inadvertently modify any of our brethren InternalNameCollection objects.
50
51 if (_names.get_ref_count() > 1) {
52 InternalNames old_names = _names;
53 _names = InternalNames::empty_array(0);
54 _names.v() = old_names.v();
55 }
56
57 _names.push_back(name);
58}
59
60/**
61 * Removes the indicated InternalName from the collection. Returns true if
62 * the name was removed, false if it was not a member of the collection.
63 */
65remove_name(const InternalName *name) {
66 int name_index = -1;
67 for (int i = 0; name_index == -1 && i < (int)_names.size(); i++) {
68 if (_names[i] == name) {
69 name_index = i;
70 }
71 }
72
73 if (name_index == -1) {
74 // The indicated name was not a member of the collection.
75 return false;
76 }
77
78 // If the pointer to our internal array is shared by any other
79 // InternalNameCollections, we have to copy the array now so we won't
80 // inadvertently modify any of our brethren InternalNameCollection objects.
81
82 if (_names.get_ref_count() > 1) {
83 InternalNames old_names = _names;
84 _names = InternalNames::empty_array(0);
85 _names.v() = old_names.v();
86 }
87
88 _names.erase(_names.begin() + name_index);
89 return true;
90}
91
92/**
93 * Adds all the InternalNames indicated in the other collection to this name.
94 * The other names are simply appended to the end of the names in this list;
95 * duplicates are not automatically removed.
96 */
99 int other_num_names = other.get_num_names();
100 for (int i = 0; i < other_num_names; i++) {
101 add_name(other.get_name(i));
102 }
103}
104
105
106/**
107 * Removes from this collection all of the InternalNames listed in the other
108 * collection.
109 */
112 InternalNames new_names;
113 int num_names = get_num_names();
114 for (int i = 0; i < num_names; i++) {
115 const InternalName *name = get_name(i);
116 if (!other.has_name(name)) {
117 new_names.push_back(name);
118 }
119 }
120 _names = new_names;
121}
122
123/**
124 * Removes any duplicate entries of the same InternalNames on this collection.
125 * If a InternalName appears multiple times, the first appearance is retained;
126 * subsequent appearances are removed.
127 */
130 InternalNames new_names;
131
132 int num_names = get_num_names();
133 for (int i = 0; i < num_names; i++) {
134 const InternalName *name = get_name(i);
135 bool duplicated = false;
136
137 for (int j = 0; j < i && !duplicated; j++) {
138 duplicated = (name == get_name(j));
139 }
140
141 if (!duplicated) {
142 new_names.push_back(name);
143 }
144 }
145
146 _names = new_names;
147}
148
149/**
150 * Returns true if the indicated InternalName appears in this collection,
151 * false otherwise.
152 */
154has_name(const InternalName *name) const {
155 for (int i = 0; i < get_num_names(); i++) {
156 if (name == get_name(i)) {
157 return true;
158 }
159 }
160 return false;
161}
162
163/**
164 * Removes all InternalNames from the collection.
165 */
167clear() {
168 _names.clear();
169}
170
171/**
172 * Returns the number of InternalNames in the collection.
173 */
175get_num_names() const {
176 return _names.size();
177}
178
179/**
180 * Returns the nth InternalName in the collection.
181 */
183get_name(int index) const {
184 nassertr(index >= 0 && index < (int)_names.size(), nullptr);
185
186 return _names[index];
187}
188
189/**
190 * Returns the nth InternalName in the collection. This is the same as
191 * get_name(), but it may be a more convenient way to access it.
192 */
194operator [] (int index) const {
195 nassertr(index >= 0 && index < (int)_names.size(), nullptr);
196
197 return _names[index];
198}
199
200/**
201 * Returns the number of names in the collection. This is the same thing as
202 * get_num_names().
203 */
205size() const {
206 return _names.size();
207}
208
209/**
210 * Writes a brief one-line description of the InternalNameCollection to the
211 * indicated output stream.
212 */
214output(std::ostream &out) const {
215 if (get_num_names() == 1) {
216 out << "1 InternalName";
217 } else {
218 out << get_num_names() << " InternalNames";
219 }
220}
221
222/**
223 * Writes a complete multi-line description of the InternalNameCollection to
224 * the indicated output stream.
225 */
227write(std::ostream &out, int indent_level) const {
228 for (int i = 0; i < get_num_names(); i++) {
229 indent(out, indent_level) << *get_name(i) << "\n";
230 }
231}
void add_names_from(const InternalNameCollection &other)
Adds all the InternalNames indicated in the other collection to this name.
int size() const
Returns the number of names in the collection.
get_name
Returns the nth InternalName in the collection.
bool has_name(const InternalName *name) const
Returns true if the indicated InternalName appears in this collection, false otherwise.
void remove_duplicate_names()
Removes any duplicate entries of the same InternalNames on this collection.
void clear()
Removes all InternalNames from the collection.
void write(std::ostream &out, int indent_level=0) const
Writes a complete multi-line description of the InternalNameCollection to the indicated output stream...
void add_name(const InternalName *name)
Adds a new InternalName to the collection.
bool remove_name(const InternalName *name)
Removes the indicated InternalName from the collection.
void remove_names_from(const InternalNameCollection &other)
Removes from this collection all of the InternalNames listed in the other collection.
get_num_names
Returns the number of InternalNames in the collection.
const InternalName * operator[](int index) const
Returns the nth InternalName in the collection.
void output(std::ostream &out) const
Writes a brief one-line description of the InternalNameCollection to the indicated output stream.
Encodes a string name in a hash table, mapping it to a pointer.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.