Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions modules/yup_core/containers/yup_HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ struct DefaultHashFunctions
/** Generates a simple hash from a string. */
static int generateHash (const String& key, int upperLimit) noexcept { return generateHash ((uint32) key.hashCode(), upperLimit); }

/** Generates a simple hash from a StringRef. */
static int generateHash (StringRef key, int upperLimit) noexcept { return generateHash ((uint32) key.hashCode(), upperLimit); }

/** Generates a simple hash from a variant. */
static int generateHash (const var& key, int upperLimit) noexcept { return generateHash (key.toString(), upperLimit); }

Expand Down Expand Up @@ -124,8 +127,8 @@ struct DefaultHashFunctions
*/
template <typename KeyType,
typename ValueType,
class HashFunctionType = DefaultHashFunctions,
class TypeOfCriticalSectionToUse = DummyCriticalSection>
typename HashFunctionType = DefaultHashFunctions,
typename TypeOfCriticalSectionToUse = DummyCriticalSection>
class HashMap
{
private:
Expand Down
63 changes: 63 additions & 0 deletions modules/yup_core/misc/yup_HashGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2024 - kunitoki@gmail.com

YUP is an open source library subject to open-source licensing.

The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

#include <algorithm>

namespace yup
{

/** Hash generator template.

This template provides a mechanism to generate hash values for different types of data.
It uses a multiplier to compute the hash value based on the input data.

@tparam Type The type of the hash value to be generated (e.g., uint32, uint64).
*/
template <typename Type>
struct HashGenerator
{
/** Calculates the hash value for the given character pointer.

@tparam CharPointer The type of the character pointer (e.g., const char*, const wchar_t*).

@param t The character pointer to be hashed.

@return The computed hash value of the input data.
*/
template <typename CharPointer>
static Type calculate (CharPointer t) noexcept
{
Type result = {};

while (! t.isEmpty())
result = ((Type) multiplier) * result + (Type) t.getAndAdvance();

return result;
}

/** The multiplier used in the hash calculation. */
enum
{
multiplier = sizeof (Type) > 4 ? 101 : 31
};
};

} // namespace yup
22 changes: 1 addition & 21 deletions modules/yup_core/text/yup_String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,26 +657,7 @@ yup_wchar String::operator[] (int index) const noexcept
return text[index];
}

template <typename Type>
struct HashGenerator
{
template <typename CharPointer>
static Type calculate (CharPointer t) noexcept
{
Type result = {};

while (! t.isEmpty())
result = ((Type) multiplier) * result + (Type) t.getAndAdvance();

return result;
}

enum
{
multiplier = sizeof (Type) > 4 ? 101 : 31
};
};

//==============================================================================
int String::hashCode() const noexcept { return (int) HashGenerator<uint32>::calculate (text); }

int64 String::hashCode64() const noexcept { return (int64) HashGenerator<uint64>::calculate (text); }
Expand Down Expand Up @@ -2746,4 +2727,3 @@ String String::dedentLines() const
}

} // namespace yup

3 changes: 3 additions & 0 deletions modules/yup_core/text/yup_StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ class YUP_API StringRef final
/** Returns the number of characters in the string. */
int length() const noexcept { return (int) text.length(); }

/** Returns a hash code for the string. */
int hashCode() const noexcept { return (int) HashGenerator<uint32>::calculate (text); }

/** Retrieves a character by index. */
yup_wchar operator[] (int index) const noexcept { return text[index]; }

Expand Down
1 change: 1 addition & 0 deletions modules/yup_core/yup_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ YUP_BEGIN_IGNORE_WARNINGS_MSVC (4514 4996)
YUP_END_IGNORE_WARNINGS_MSVC

#include "misc/yup_MetaProgramming.h"
#include "misc/yup_HashGenerator.h"
#include "text/yup_String.h"
#include "text/yup_StringRef.h"
#include "logging/yup_Logger.h"
Expand Down
102 changes: 3 additions & 99 deletions modules/yup_graphics/drawables/yup_Drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,107 +31,11 @@ SVGGradient::Ptr getGradientById (const SVGData& data, const String& id)
return data.gradientsById[id];
}

SVGGradient::Ptr resolveGradient (const SVGData& data, SVGGradient::Ptr gradient)
{
if (gradient == nullptr || gradient->href.isEmpty())
return gradient;

auto referencedGradient = getGradientById (data, gradient->href);
if (referencedGradient == nullptr)
return gradient;

referencedGradient = resolveGradient (data, referencedGradient);

SVGGradient::Ptr resolved = new SVGGradient;
resolved->type = gradient->type;
resolved->id = gradient->id;
resolved->units = referencedGradient->units;
resolved->spreadMethod = referencedGradient->spreadMethod;
resolved->start = referencedGradient->start;
resolved->end = referencedGradient->end;
resolved->center = referencedGradient->center;
resolved->radius = referencedGradient->radius;
resolved->focal = referencedGradient->focal;
resolved->transform = referencedGradient->transform;
resolved->stops = referencedGradient->stops;
resolved->hasStart = referencedGradient->hasStart;
resolved->hasEnd = referencedGradient->hasEnd;
resolved->hasCenter = referencedGradient->hasCenter;
resolved->hasRadius = referencedGradient->hasRadius;
resolved->hasFocal = referencedGradient->hasFocal;
resolved->hasUnits = referencedGradient->hasUnits;
resolved->hasSpreadMethod = referencedGradient->hasSpreadMethod;

if (gradient->hasStart)
{
resolved->start = gradient->start;
resolved->hasStart = true;
}
if (gradient->hasEnd)
{
resolved->end = gradient->end;
resolved->hasEnd = true;
}
if (gradient->hasCenter)
{
resolved->center = gradient->center;
resolved->hasCenter = true;
}
if (gradient->hasRadius)
{
resolved->radius = gradient->radius;
resolved->hasRadius = true;
}
if (gradient->hasFocal)
{
resolved->focal = gradient->focal;
resolved->hasFocal = true;
}

if (! gradient->transform.isIdentity())
resolved->transform = gradient->transform;
if (gradient->hasUnits)
{
resolved->units = gradient->units;
resolved->hasUnits = true;
}
if (gradient->hasSpreadMethod)
{
resolved->spreadMethod = gradient->spreadMethod;
resolved->hasSpreadMethod = true;
}
if (! gradient->stops.empty())
resolved->stops = gradient->stops;

return resolved;
}

SVGFilter::Ptr getFilterById (const SVGData& data, const String& id)
{
return data.filtersById[id];
}

SVGFilter::Ptr resolveFilter (const SVGData& data, SVGFilter::Ptr filter)
{
if (filter == nullptr || filter->href.isEmpty())
return filter;

auto referencedFilter = resolveFilter (data, getFilterById (data, filter->href));
if (referencedFilter == nullptr)
return filter;

SVGFilter::Ptr resolved = new SVGFilter;
resolved->id = filter->id;
resolved->href = filter->href;

if (! filter->primitives.empty())
resolved->primitives = filter->primitives;
else
resolved->primitives = referencedFilter->primitives;

return resolved;
}

AffineTransform createGradientSpaceTransform (const SVGGradient& gradient, const Rectangle<float>* objectBounds)
{
const bool hasBounds = objectBounds != nullptr && objectBounds->getWidth() > 0.0f && objectBounds->getHeight() > 0.0f;
Expand Down Expand Up @@ -387,7 +291,7 @@ void Drawable::paintElement (Graphics& g,

if (element.filterUrl)
{
if (auto filter = resolveFilter (data, getFilterById (data, *element.filterUrl)))
if (auto filter = getFilterById (data, *element.filterUrl))
{
for (const auto& primitive : filter->primitives)
{
Expand Down Expand Up @@ -626,7 +530,7 @@ void Drawable::paintElement (Graphics& g,
{
if (auto gradient = getGradientById (data, *element.fillUrl))
{
auto resolvedGradient = resolveGradient (data, gradient);
auto resolvedGradient = gradient;
std::optional<Rectangle<float>> gradientBounds;

if (element.path)
Expand Down Expand Up @@ -780,7 +684,7 @@ void Drawable::paintElement (Graphics& g,
{
if (auto gradient = getGradientById (data, *element.strokeUrl))
{
auto resolvedGradient = resolveGradient (data, gradient);
auto resolvedGradient = gradient;
std::optional<Rectangle<float>> gradientBounds;

if (element.path)
Expand Down
Loading
Loading