Browse Source

Replacing some containers

Michael Ragazzon 6 years ago
parent
commit
c307140b9b

+ 1 - 1
Include/Rocket/Controls/DataQuery.h

@@ -124,7 +124,7 @@ private:
 
 	typedef std::vector< Rocket::Core::StringList > Rows;
 	Rows rows;
-	typedef std::map< Rocket::Core::String, size_t > FieldIndices;
+	typedef Core::UnorderedMap< Rocket::Core::String, size_t > FieldIndices;
 	FieldIndices field_indices;
 	
 	void LoadRow();

+ 2 - 2
Include/Rocket/Controls/DataSource.h

@@ -29,9 +29,9 @@
 #define ROCKETCONTROLSDATASOURCE_H
 
 #include "Header.h"
+#include "../Core/Types.h"
 #include "../Core/String.h"
 #include <list>
-#include <map>
 
 namespace Rocket {
 namespace Controls {
@@ -96,7 +96,7 @@ class ROCKETCONTROLS_API DataSource
 		void NotifyRowChange(const Rocket::Core::String& table);
 
 		/// Helper function for building a result set.
-		typedef std::map< Rocket::Core::String, Rocket::Core::String > RowMap;
+		typedef Core::UnorderedMap< Rocket::Core::String, Rocket::Core::String > RowMap;
 		void BuildRowEntries(Rocket::Core::StringList& row, const RowMap& row_map, const Rocket::Core::StringList& columns);
 
 	private:

+ 1 - 1
Include/Rocket/Core/FontProvider.h

@@ -60,7 +60,7 @@ public:
 
 protected:
 
-    typedef std::map< String, FontFamily*, StringUtilities::StringComparei > FontFamilyMap;
+    typedef UnorderedMap< String, FontFamily*> FontFamilyMap;
     FontFamilyMap font_families;
 };
 

+ 1 - 1
Include/Rocket/Core/PropertyParser.h

@@ -35,7 +35,7 @@
 namespace Rocket {
 namespace Core {
 
-typedef std::map < String, int, StringUtilities::StringComparei > ParameterMap;
+typedef UnorderedMap< String, int > ParameterMap;
 
 /**
 	A property parser takes a property declaration in string form, validates it, and converts it to a Property.

+ 1 - 1
Include/Rocket/Core/Types.h

@@ -104,7 +104,7 @@ typedef uintptr_t DecoratorDataHandle;
 
 // List of elements.
 typedef std::vector< Element* > ElementList;
-typedef std::set< String > PseudoClassList;
+typedef std::vector< String > PseudoClassList;
 typedef std::unordered_set< String > PropertyNameList;
 typedef std::unordered_set< String > AttributeNameList;
 typedef std::vector< ElementAnimation > ElementAnimationList;

+ 2 - 1
Samples/basic/animation/src/main.cpp

@@ -138,7 +138,8 @@ public:
 		  Restructuring update loop: 34.5  [f9892a9]
 		  Element constructor, remove geometry database, remove update() from Context::render: 38.0  [1aab59e]
 		  Replace Dictionary with unordered_flat_map: 40.0  [b04b4e5]
-		  Dirty flag for structure changes: 43.0
+		  Dirty flag for structure changes: 43.0  [fdf6f53]
+		  Replacing containers: 46.0
 		
 		*/
 		

+ 4 - 3
Source/Core/BitmapFont/FontProvider.cpp

@@ -144,14 +144,15 @@ bool FontProvider::LoadFontFace(const byte* data, int data_length, const String&
 // Adds a loaded face to the appropriate font family.
 bool FontProvider::AddFace(void* face, const String& family, Font::Style style, Font::Weight weight, bool release_stream)
 {
+	String family_lower = ToLower(family);
 	Rocket::Core::FontFamily* font_family = NULL;
-	FontFamilyMap::iterator iterator = instance->font_families.find(family);
+	FontFamilyMap::iterator iterator = instance->font_families.find(family_lower);
 	if (iterator != instance->font_families.end())
 		font_family = (*iterator).second;
 	else
 	{
-		font_family = new FontFamily(family);
-		instance->font_families[family] = font_family;
+		font_family = new FontFamily(family_lower);
+		instance->font_families[family_lower] = font_family;
 	}
 
 	return font_family->AddFace((BitmapFontDefinitions *) face, style, weight, release_stream);

+ 13 - 4
Source/Core/DecoratorTiled.cpp

@@ -65,11 +65,20 @@ DecoratorTiled::Tile::Tile()
 	texcoords_absolute[1][1] = false;
 }
 
+struct TileDataMapCmp {
+	RenderInterface* find;
+
+	using Element = std::pair< RenderInterface*, DecoratorTiled::Tile::TileData >;
+	bool operator() (const Element& el) const {
+		return el.first == find;
+	}
+};
+
 // Calculates the tile's dimensions from the texture and texture coordinates.
 void DecoratorTiled::Tile::CalculateDimensions(Element* element, const Texture& texture)
 {
 	RenderInterface* render_interface = element->GetRenderInterface();
-	TileDataMap::iterator data_iterator = data.find(render_interface);
+	TileDataMap::iterator data_iterator = std::find_if(data.begin(), data.end(), TileDataMapCmp{ render_interface });
 	if (data_iterator == data.end())
 	{
 		TileData new_data;
@@ -90,7 +99,7 @@ void DecoratorTiled::Tile::CalculateDimensions(Element* element, const Texture&
 		new_data.dimensions.x = Math::AbsoluteValue((new_data.texcoords[1].x * texture_dimensions.x) - (new_data.texcoords[0].x * texture_dimensions.x));
 		new_data.dimensions.y = Math::AbsoluteValue((new_data.texcoords[1].y * texture_dimensions.y) - (new_data.texcoords[0].y * texture_dimensions.y));
 
-		data[render_interface] = new_data;
+		data.emplace_back( render_interface,  new_data );
 	}
 }
 
@@ -98,7 +107,7 @@ void DecoratorTiled::Tile::CalculateDimensions(Element* element, const Texture&
 Vector2f DecoratorTiled::Tile::GetDimensions(Element* element)
 {
 	RenderInterface* render_interface = element->GetRenderInterface();
-	TileDataMap::iterator data_iterator = data.find(render_interface);
+	TileDataMap::iterator data_iterator = std::find_if(data.begin(), data.end(), TileDataMapCmp{ render_interface });
 	if (data_iterator == data.end())
 		return Vector2f(0, 0);
 
@@ -116,7 +125,7 @@ void DecoratorTiled::Tile::GenerateGeometry(std::vector< Vertex >& vertices, std
     // Apply opacity
     quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
 	
-	TileDataMap::iterator data_iterator = data.find(render_interface);
+	TileDataMap::iterator data_iterator = std::find_if(data.begin(), data.end(), TileDataMapCmp{ render_interface });
 	if (data_iterator == data.end())
 		return;
 

+ 1 - 1
Source/Core/DecoratorTiled.h

@@ -105,7 +105,7 @@ public:
 			Vector2f texcoords[2];
 		};
 
-		typedef std::map< RenderInterface*, TileData > TileDataMap;
+		typedef std::vector< std::pair< RenderInterface*, TileData > > TileDataMap;
 
 		int texture_index;
 		Vector2f texcoords[2];

+ 1 - 1
Source/Core/ElementDecoration.h

@@ -85,7 +85,7 @@ private:
 	typedef std::vector< DecoratorHandle > DecoratorList;
 	typedef std::pair< PseudoClassList, int > PseudoClassDecoratorIndex;
 	typedef std::vector< PseudoClassDecoratorIndex > PseudoClassDecoratorIndexList;
-	typedef std::map< String, PseudoClassDecoratorIndexList > DecoratorIndex;
+	typedef UnorderedMap< String, PseudoClassDecoratorIndexList > DecoratorIndex;
 
 	// The element this decorator belongs to
 	Element* element;

+ 3 - 5
Source/Core/ElementDefinition.cpp

@@ -203,7 +203,7 @@ void ElementDefinition::GetDefinedProperties(PropertyNameList& property_names, c
 					continue;
 				}
 
-				if (pseudo_classes.find(rule_pseudo_classes[j]) == pseudo_classes.end())
+				if (std::find(pseudo_classes.begin(), pseudo_classes.end(), rule_pseudo_classes[j]) == pseudo_classes.end())
 				{			
 					rule_valid = false;
 					break;
@@ -255,9 +255,7 @@ bool ElementDefinition::IterateProperties(int& index, const PseudoClassList& pse
 				if (property_count > index)
 				{
 					// Copy the list of pseudo-classes.
-					property_pseudo_classes.clear();
-					for (size_t k = 0; k < (*i).second[j].first.size(); ++k)
-						property_pseudo_classes.insert((*i).second[j].first[k]);
+					property_pseudo_classes = (*i).second[j].first;
 
 					property_name = (*i).first;
 					property = &((*i).second[j].second);
@@ -559,7 +557,7 @@ bool ElementDefinition::IsPseudoClassRuleApplicable(const StringList& rule_pseud
 {
 	for (StringList::size_type i = 0; i < rule_pseudo_classes.size(); ++i)
 	{
-		if (element_pseudo_classes.find(rule_pseudo_classes[i]) == element_pseudo_classes.end())
+		if (std::find(element_pseudo_classes.begin(), element_pseudo_classes.end(), rule_pseudo_classes[i]) == element_pseudo_classes.end())
 			return false;
 	}
 

+ 3 - 3
Source/Core/ElementDefinition.h

@@ -128,12 +128,12 @@ protected:
 
 private:
 	typedef std::pair< String, PropertyDictionary > PropertyGroup;
-	typedef std::map< String, PropertyGroup > PropertyGroupMap;
+	typedef UnorderedMap< String, PropertyGroup > PropertyGroupMap;
 
 	typedef std::vector< std::pair< StringList, int > > PseudoClassFontEffectIndex;
-	typedef std::map< String, PseudoClassFontEffectIndex > FontEffectIndex;
+	typedef UnorderedMap< String, PseudoClassFontEffectIndex > FontEffectIndex;
 
-	typedef std::map< String, PseudoClassVolatility > PseudoClassVolatilityMap;
+	typedef UnorderedMap< String, PseudoClassVolatility > PseudoClassVolatilityMap;
 
 	// Finds all propery declarations for a group.
 	void BuildPropertyGroup(PropertyGroupMap& groups, const String& group_type, const PropertyDictionary& element_properties, const PropertyGroupMap* default_properties = NULL);

+ 13 - 3
Source/Core/ElementStyle.cpp

@@ -231,9 +231,19 @@ void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
 	auto pseudo_classes_before = pseudo_classes;
 
 	if (activate)
-		pseudo_classes.insert(pseudo_class);
+		pseudo_classes.push_back(pseudo_class);
 	else
-		pseudo_classes.erase(pseudo_class);
+	{
+		// In case of duplicates, we do a loop here. We could do a sort and unique instead,
+		// but that might even be slower for a small list with few duplicates, which
+		// is probably the most common case.
+		auto it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
+		while(it != pseudo_classes.end())
+		{
+			pseudo_classes.erase(it);
+			it = std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class);
+		}
+	}
 
 	if (pseudo_classes.size() != num_pseudo_classes)
 	{
@@ -268,7 +278,7 @@ void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
 // Checks if a specific pseudo-class has been set on the element.
 bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
 {
-	return (pseudo_classes.find(pseudo_class) != pseudo_classes.end());
+	return (std::find(pseudo_classes.begin(), pseudo_classes.end(), pseudo_class) != pseudo_classes.end());
 }
 
 const PseudoClassList& ElementStyle::GetActivePseudoClasses() const

+ 2 - 1
Source/Core/FontProvider.cpp

@@ -35,7 +35,8 @@ namespace Core {
 // Returns a handle to a font face that can be used to position and render text.
 Rocket::Core::FontFaceHandle* FontProvider::GetFontFaceHandle(const String& family, const String& charset, Font::Style style, Font::Weight weight, int size)
 {
-	FontFamilyMap::iterator iterator = font_families.find(family);
+	String family_lower = ToLower(family);
+	FontFamilyMap::iterator iterator = font_families.find(family_lower);
 	if (iterator == font_families.end())
 		return NULL;
 

+ 4 - 3
Source/Core/FreeType/FontProvider.cpp

@@ -188,14 +188,15 @@ bool FontProvider::LoadFontFace(const byte* data, int data_length, const String&
 // Adds a loaded face to the appropriate font family.
 bool FontProvider::AddFace(void* face, const String& family, Font::Style style, Font::Weight weight, bool release_stream)
 {
+	String family_lower = ToLower(family);
 	FontFamily* font_family = NULL;
-	FontFamilyMap::iterator iterator = font_families.find(family);
+	FontFamilyMap::iterator iterator = font_families.find(family_lower);
 	if (iterator != font_families.end())
 		font_family = (FontFamily*)(*iterator).second;
 	else
 	{
-		font_family = new FontFamily(family);
-		font_families[family] = font_family;
+		font_family = new FontFamily(family_lower);
+		font_families[family_lower] = font_family;
 	}
 
 	return font_family->AddFace((FT_Face) face, style, weight, release_stream);

+ 1 - 1
Source/Core/PropertyParserColour.cpp

@@ -153,7 +153,7 @@ bool PropertyParserColour::ParseValue(Property& property, const String& value, c
 	else
 	{
 		// Check for the specification of an HTML colour.
-		ColourMap::const_iterator iterator = html_colours.find(value);
+		ColourMap::const_iterator iterator = html_colours.find(ToLower(value));
 		if (iterator == html_colours.end())
 			return false;
 		else

+ 1 - 1
Source/Core/PropertyParserColour.h

@@ -57,7 +57,7 @@ public:
 	virtual void Release();
 
 private:
-	typedef std::map< String, Colourb, StringUtilities::StringComparei > ColourMap;
+	typedef UnorderedMap< String, Colourb> ColourMap;
 	ColourMap html_colours;
 };
 

+ 1 - 1
Source/Core/PropertyParserKeyword.cpp

@@ -42,7 +42,7 @@ PropertyParserKeyword::~PropertyParserKeyword()
 // Called to parse a RCSS keyword declaration.
 bool PropertyParserKeyword::ParseValue(Property& property, const String& value, const ParameterMap& parameters) const
 {
-	ParameterMap::const_iterator iterator = parameters.find(value);
+	ParameterMap::const_iterator iterator = parameters.find(ToLower(value));
 	if (iterator == parameters.end())
 		return false;
 

+ 4 - 1
Source/Core/StyleSheetNode.cpp

@@ -240,7 +240,10 @@ bool StyleSheetNode::GetVolatilePseudoClasses(PseudoClassList& volatile_pseudo_c
 			self_volatile = (*i).second->GetVolatilePseudoClasses(volatile_pseudo_classes) | self_volatile;
 
 		if (self_volatile)
-			volatile_pseudo_classes.insert(name);
+		{
+			if (auto it = std::find(volatile_pseudo_classes.begin(), volatile_pseudo_classes.end(), name); it == volatile_pseudo_classes.end())
+				volatile_pseudo_classes.push_back(name);
+		}
 
 		return self_volatile;
 	}