Skip to content

Commit

Permalink
refactor animal module data (cieslarmichal#584)
Browse files Browse the repository at this point in the history
* replace map with unordered map

* refactor animal module data
  • Loading branch information
cieslarmichal authored and 00thirdeye00 committed Jun 20, 2024
1 parent f4ec12b commit 94f28e4
Show file tree
Hide file tree
Showing 44 changed files with 3,658 additions and 3,759 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(FAKER_SOURCES
src/modules/airline/Airline.cpp
src/modules/airline/AirlineData.cpp
src/modules/animal/Animal.cpp
src/modules/animal/AnimalData.cpp
src/modules/book/Book.cpp
src/modules/color/Color.cpp
src/modules/commerce/Commerce.cpp
Expand Down
32 changes: 16 additions & 16 deletions include/faker-cxx/Animal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

namespace faker
{
Expand All @@ -16,7 +16,7 @@ class Animal
* Animal::bear() // "Polar bear"
* @endcode
*/
static std::string bear();
static std::string_view bear();

/**
* @brief Returns a random species of bird.
Expand All @@ -27,7 +27,7 @@ class Animal
* Animal::bird() // "Black-bellied Whistling-Duck"
* @endcode
*/
static std::string bird();
static std::string_view bird();

/**
* @brief Returns a random species of cat.
Expand All @@ -38,7 +38,7 @@ class Animal
* Animal::cat() // "Chartreux"
* @endcode
*/
static std::string cat();
static std::string_view cat();

/**
* @brief Returns a random species of cetacean.
Expand All @@ -49,7 +49,7 @@ class Animal
* Animal::cetacean() // "Blue Whale"
* @endcode
*/
static std::string cetacean();
static std::string_view cetacean();

/**
* @brief Returns a random species of cow.
Expand All @@ -60,7 +60,7 @@ class Animal
* Animal::cow() // "American Angus"
* @endcode
*/
static std::string cow();
static std::string_view cow();

/**
* @brief Returns a random species of crocodilia.
Expand All @@ -71,7 +71,7 @@ class Animal
* Animal::crocodile() // "Dwarf Crocodile"
* @endcode
*/
static std::string crocodile();
static std::string_view crocodile();

/**
* @brief Returns a random species of dog.
Expand All @@ -82,7 +82,7 @@ class Animal
* Animal::dog() // "Shiba Inu"
* @endcode
*/
static std::string dog();
static std::string_view dog();

/**
* @brief Returns a random species of fish.
Expand All @@ -93,7 +93,7 @@ class Animal
* Animal::fish() // "Silver carp"
* @endcode
*/
static std::string fish();
static std::string_view fish();

/**
* @brief Returns a random species of horse.
Expand All @@ -104,7 +104,7 @@ class Animal
* Animal::horse() // "Fjord Horse"
* @endcode
*/
static std::string horse();
static std::string_view horse();

/**
* @brief Returns a random species of insect.
Expand All @@ -115,7 +115,7 @@ class Animal
* Animal::insect() // "Bee"
* @endcode
*/
static std::string insect();
static std::string_view insect();

/**
* @brief Returns a random species of lion.
Expand All @@ -126,7 +126,7 @@ class Animal
* Animal::lion() // "West African Lion"
* @endcode
*/
static std::string lion();
static std::string_view lion();

/**
* @brief Returns a random species of rabbit.
Expand All @@ -137,7 +137,7 @@ class Animal
* Animal::rabbit() // "Californian"
* @endcode
*/
static std::string rabbit();
static std::string_view rabbit();

/**
* @brief Returns a random species of rodent.
Expand All @@ -148,7 +148,7 @@ class Animal
* Animal::rodent() // "Chinchilla"
* @endcode
*/
static std::string rodent();
static std::string_view rodent();

/**
* @brief Returns a random species of snake.
Expand All @@ -159,7 +159,7 @@ class Animal
* Animal::snake() // "Boa constrictor"
* @endcode
*/
static std::string snake();
static std::string_view snake();

/**
* @brief Returns a random type of animal.
Expand All @@ -170,6 +170,6 @@ class Animal
* Animal::type() // "dog"
* @endcode
*/
static std::string type();
static std::string_view type();
};
}
6 changes: 3 additions & 3 deletions include/faker-cxx/Phone.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <map>
#include <optional>
#include <string>
#include <unordered_map>

namespace faker
{
Expand Down Expand Up @@ -95,8 +95,8 @@ class Phone
static std::string areaCode();

private:
static std::map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap();
static std::map<PhoneNumberCountryFormat, std::string> phoneNumberFormatMap;
static std::unordered_map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap();
static std::unordered_map<PhoneNumberCountryFormat, std::string> phoneNumberFormatMap;
};

enum class PhoneNumberCountryFormat
Expand Down
10 changes: 5 additions & 5 deletions include/faker-cxx/String.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include <limits>
#include <map>
#include <optional>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>

#include "RandomGenerator.h"
#include "types/Hex.h"
Expand All @@ -27,16 +27,16 @@ struct CharCount
};

/**
* A std::map where user can specify the count required for specific chars
* A std::unordered_map where user can specify the count required for specific chars
*/
using GuaranteeMap = std::map<char, CharCount>;
using GuaranteeMap = std::unordered_map<char, CharCount>;

/**
* @brief Checks if the given guarantee map is valid for given targetCharacters and length.
*
* @returns a bool.
*
* @param guarantee A std::map that maps the count range of specific characters required
* @param guarantee A std::unordered_map that maps the count range of specific characters required
* @param targetCharacters A std::string consisting of all chars available for that string generating function
* @param length The number of characters to generate.
*
Expand All @@ -54,7 +54,7 @@ bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
*
* @returns least required std::string
*
* @param guarantee A std::map<char,CharCount> which stores the guarantee specified by the user
* @param guarantee A std::unordered_map<char,CharCount> which stores the guarantee specified by the user
*
* @code
* GuaranteeMap guarantee { {'0',{3,10}},{'a',{6,8}} }; // "000aaaaaa"
Expand Down
5 changes: 3 additions & 2 deletions src/common/FormatHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace faker
{

std::string FormatHelper::fillTokenValues(const std::string& format,
std::map<std::string, std::function<std::string()>> tokenValueGenerators)
std::string
FormatHelper::fillTokenValues(const std::string& format,
std::unordered_map<std::string, std::function<std::string()>> tokenValueGenerators)
{
std::string filledFormat;

Expand Down
7 changes: 4 additions & 3 deletions src/common/FormatHelper.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <functional>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

#if !defined(HAS_STD_FORMAT)
Expand Down Expand Up @@ -30,7 +30,8 @@ class FormatHelper
}
#endif

static std::string fillTokenValues(const std::string& format,
std::map<std::string, std::function<std::string()>> tokenValueGenerators);
static std::string
fillTokenValues(const std::string& format,
std::unordered_map<std::string, std::function<std::string()>> tokenValueGenerators);
};
}
2 changes: 1 addition & 1 deletion src/common/PrecisionMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace faker
{
const std::map<Precision, unsigned> PrecisionMapper::precisionToDecimalPlacesMapping{
const std::unordered_map<Precision, unsigned> PrecisionMapper::precisionToDecimalPlacesMapping{
{Precision::ZeroDp, 0}, {Precision::OneDp, 1}, {Precision::TwoDp, 2}, {Precision::ThreeDp, 3},
{Precision::FourDp, 4}, {Precision::FiveDp, 5}, {Precision::SixDp, 6}, {Precision::SevenDp, 7}};

Expand Down
4 changes: 2 additions & 2 deletions src/common/PrecisionMapper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <map>
#include <unordered_map>

#include "faker-cxx/types/Precision.h"

Expand All @@ -12,6 +12,6 @@ class PrecisionMapper
static unsigned mapToDecimalPlaces(Precision precision);

private:
static const std::map<Precision, unsigned> precisionToDecimalPlacesMapping;
static const std::unordered_map<Precision, unsigned> precisionToDecimalPlacesMapping;
};
}
76 changes: 31 additions & 45 deletions src/modules/animal/Animal.cpp
Original file line number Diff line number Diff line change
@@ -1,96 +1,82 @@
#include "faker-cxx/Animal.h"

#include "data/Bears.h"
#include "data/Birds.h"
#include "data/Cats.h"
#include "data/Cetaceans.h"
#include "data/Cows.h"
#include "data/Crocodiles.h"
#include "data/Dogs.h"
#include "data/Fishes.h"
#include "data/Horses.h"
#include "data/Insects.h"
#include "data/Lions.h"
#include "data/Rabbits.h"
#include "data/Rodents.h"
#include "data/Snakes.h"
#include "data/Types.h"
#include "AnimalData.h"
#include "faker-cxx/Helper.h"

namespace faker
{
std::string Animal::bear()
std::string_view Animal::bear()
{
return Helper::arrayElement<std::string>(bears);
return Helper::arrayElement(bears);
}

std::string Animal::bird()
std::string_view Animal::bird()
{
return Helper::arrayElement<std::string>(birds);
return Helper::arrayElement(birds);
}

std::string Animal::cat()
std::string_view Animal::cat()
{
return Helper::arrayElement<std::string>(cats);
return Helper::arrayElement(cats);
}

std::string Animal::cetacean()
std::string_view Animal::cetacean()
{
return Helper::arrayElement<std::string>(cetaceans);
return Helper::arrayElement(cetaceans);
}

std::string Animal::cow()
std::string_view Animal::cow()
{
return Helper::arrayElement<std::string>(cows);
return Helper::arrayElement(cows);
}

std::string Animal::crocodile()
std::string_view Animal::crocodile()
{
return Helper::arrayElement<std::string>(faker::crocodiles);
return Helper::arrayElement(faker::crocodiles);
}

std::string Animal::dog()
std::string_view Animal::dog()
{
return Helper::arrayElement<std::string>(dogs);
return Helper::arrayElement(dogs);
}

std::string Animal::fish()
std::string_view Animal::fish()
{
return Helper::arrayElement<std::string>(fishes);
return Helper::arrayElement(fishes);
}

std::string Animal::horse()
std::string_view Animal::horse()
{
return Helper::arrayElement<std::string>(horses);
return Helper::arrayElement(horses);
}

std::string Animal::insect()
std::string_view Animal::insect()
{
return Helper::arrayElement<std::string>(insects);
return Helper::arrayElement(insects);
}

std::string Animal::lion()
std::string_view Animal::lion()
{
return Helper::arrayElement<std::string>(lions);
return Helper::arrayElement(lions);
}

std::string Animal::rabbit()
std::string_view Animal::rabbit()
{
return Helper::arrayElement<std::string>(rabbits);
return Helper::arrayElement(rabbits);
}

std::string Animal::rodent()
std::string_view Animal::rodent()
{
return Helper::arrayElement<std::string>(rodents);
return Helper::arrayElement(rodents);
}

std::string Animal::snake()
std::string_view Animal::snake()
{
return Helper::arrayElement<std::string>(snakes);
return Helper::arrayElement(snakes);
}

std::string Animal::type()
std::string_view Animal::type()
{
return Helper::arrayElement<std::string>(types);
return Helper::arrayElement(types);
}
}
Loading

0 comments on commit 94f28e4

Please sign in to comment.