diff --git a/arduino/Embedded_Template_Library.h b/arduino/Embedded_Template_Library.h index 5959207e..aed4c865 100644 --- a/arduino/Embedded_Template_Library.h +++ b/arduino/Embedded_Template_Library.h @@ -2,79 +2,79 @@ #ifndef ETL_EMBEDDED_TEMPLATE_LIBRARY_INCLUDED #define ETL_EMBEDDED_TEMPLATE_LIBRARY_INCLUDED -#if defined(TEENSYDUINO) +#if defined(TEENSYDUINO) - #if defined(__AVR_ATmega32U4__) - #define ARDUINO_BOARD "Teensy 2.0" - #elif defined(__AVR_AT90USB1286__) - #define ARDUINO_BOARD "Teensy++ 2.0" - #elif defined(__MK20DX128__) - #define ARDUINO_BOARD "Teensy 3.0" - #elif defined(__MK20DX256__) - #define ARDUINO_BOARD "Teensy 3.2" // and Teensy 3.1 - #elif defined(__MKL26Z64__) - #define ARDUINO_BOARD "Teensy LC" - #elif defined(__MK64FX512__) - #define ARDUINO_BOARD "Teensy 3.5" - #elif defined(__MK66FX1M0__) - #define ARDUINO_BOARD "Teensy 3.6" - #else - #define ARDUINO_BOARD "Unknown" - #endif + #if defined(__AVR_ATmega32U4__) + #define ARDUINO_BOARD "Teensy 2.0" + #elif defined(__AVR_AT90USB1286__) + #define ARDUINO_BOARD "Teensy++ 2.0" + #elif defined(__MK20DX128__) + #define ARDUINO_BOARD "Teensy 3.0" + #elif defined(__MK20DX256__) + #define ARDUINO_BOARD "Teensy 3.2" // and Teensy 3.1 + #elif defined(__MKL26Z64__) + #define ARDUINO_BOARD "Teensy LC" + #elif defined(__MK64FX512__) + #define ARDUINO_BOARD "Teensy 3.5" + #elif defined(__MK66FX1M0__) + #define ARDUINO_BOARD "Teensy 3.6" + #else + #define ARDUINO_BOARD "Unknown" + #endif #else // --------------- Arduino ------------------ - #if defined(ARDUINO_AVR_ADK) - #define ARDUINO_BOARD "Mega Adk" - #elif defined(ARDUINO_AVR_BT) - #define ARDUINO_BOARD "Bt" - #elif defined(ARDUINO_AVR_DUEMILANOVE) - #define ARDUINO_BOARD "Duemilanove" - #elif defined(ARDUINO_AVR_ESPLORA) - #define ARDUINO_BOARD "Esplora" - #elif defined(ARDUINO_AVR_ETHERNET) - #define ARDUINO_BOARD "Ethernet" - #elif defined(ARDUINO_AVR_FIO) - #define ARDUINO_BOARD "Fio" - #elif defined(ARDUINO_AVR_GEMMA) - #define ARDUINO_BOARD "Gemma" - #elif defined(ARDUINO_AVR_LEONARDO) - #define ARDUINO_BOARD "Leonardo" - #elif defined(ARDUINO_AVR_LILYPAD) - #define ARDUINO_BOARD "Lilypad" - #elif defined(ARDUINO_AVR_LILYPAD_USB) - #define ARDUINO_BOARD "Lilypad Usb" - #elif defined(ARDUINO_AVR_MEGA) - #define ARDUINO_BOARD "Mega" - #elif defined(ARDUINO_AVR_MEGA2560) - #define ARDUINO_BOARD "Mega 2560" - #elif defined(ARDUINO_AVR_MICRO) - #define ARDUINO_BOARD "Micro" - #elif defined(ARDUINO_AVR_MINI) - #define ARDUINO_BOARD "Mini" - #elif defined(ARDUINO_AVR_NANO) - #define ARDUINO_BOARD "Nano" - #elif defined(ARDUINO_AVR_NG) - #define ARDUINO_BOARD "NG" - #elif defined(ARDUINO_AVR_PRO) - #define ARDUINO_BOARD "Pro" - #elif defined(ARDUINO_AVR_ROBOT_CONTROL) - #define ARDUINO_BOARD "Robot Ctrl" - #elif defined(ARDUINO_AVR_ROBOT_MOTOR) - #define ARDUINO_BOARD "Robot Motor" - #elif defined(ARDUINO_AVR_UNO) - #define ARDUINO_BOARD "Uno" - #elif defined(ARDUINO_AVR_YUN) - #define ARDUINO_BOARD "Yun" - #elif defined(ARDUINO_SAM_DUE) - #define ARDUINO_BOARD "Due" - #elif defined(ARDUINO_SAMD_ZERO) - #define ARDUINO_BOARD "Zero" - #elif defined(ARDUINO_ARC32_TOOLS) - #define ARDUINO_BOARD "101" - #else - #define ARDUINO_BOARD "Unknown" - #endif + #if defined(ARDUINO_AVR_ADK) + #define ARDUINO_BOARD "Mega Adk" + #elif defined(ARDUINO_AVR_BT) + #define ARDUINO_BOARD "Bt" + #elif defined(ARDUINO_AVR_DUEMILANOVE) + #define ARDUINO_BOARD "Duemilanove" + #elif defined(ARDUINO_AVR_ESPLORA) + #define ARDUINO_BOARD "Esplora" + #elif defined(ARDUINO_AVR_ETHERNET) + #define ARDUINO_BOARD "Ethernet" + #elif defined(ARDUINO_AVR_FIO) + #define ARDUINO_BOARD "Fio" + #elif defined(ARDUINO_AVR_GEMMA) + #define ARDUINO_BOARD "Gemma" + #elif defined(ARDUINO_AVR_LEONARDO) + #define ARDUINO_BOARD "Leonardo" + #elif defined(ARDUINO_AVR_LILYPAD) + #define ARDUINO_BOARD "Lilypad" + #elif defined(ARDUINO_AVR_LILYPAD_USB) + #define ARDUINO_BOARD "Lilypad Usb" + #elif defined(ARDUINO_AVR_MEGA) + #define ARDUINO_BOARD "Mega" + #elif defined(ARDUINO_AVR_MEGA2560) + #define ARDUINO_BOARD "Mega 2560" + #elif defined(ARDUINO_AVR_MICRO) + #define ARDUINO_BOARD "Micro" + #elif defined(ARDUINO_AVR_MINI) + #define ARDUINO_BOARD "Mini" + #elif defined(ARDUINO_AVR_NANO) + #define ARDUINO_BOARD "Nano" + #elif defined(ARDUINO_AVR_NG) + #define ARDUINO_BOARD "NG" + #elif defined(ARDUINO_AVR_PRO) + #define ARDUINO_BOARD "Pro" + #elif defined(ARDUINO_AVR_ROBOT_CONTROL) + #define ARDUINO_BOARD "Robot Ctrl" + #elif defined(ARDUINO_AVR_ROBOT_MOTOR) + #define ARDUINO_BOARD "Robot Motor" + #elif defined(ARDUINO_AVR_UNO) + #define ARDUINO_BOARD "Uno" + #elif defined(ARDUINO_AVR_YUN) + #define ARDUINO_BOARD "Yun" + #elif defined(ARDUINO_SAM_DUE) + #define ARDUINO_BOARD "Due" + #elif defined(ARDUINO_SAMD_ZERO) + #define ARDUINO_BOARD "Zero" + #elif defined(ARDUINO_ARC32_TOOLS) + #define ARDUINO_BOARD "101" + #else + #define ARDUINO_BOARD "Unknown" + #endif #endif #endif diff --git a/examples/ArmTimerCallbacks - C++/RTE/Device/STM32F401RETx/system_stm32f4xx.c b/examples/ArmTimerCallbacks - C++/RTE/Device/STM32F401RETx/system_stm32f4xx.c index bca0633d..62183db5 100644 --- a/examples/ArmTimerCallbacks - C++/RTE/Device/STM32F401RETx/system_stm32f4xx.c +++ b/examples/ArmTimerCallbacks - C++/RTE/Device/STM32F401RETx/system_stm32f4xx.c @@ -1,176 +1,179 @@ /** - ****************************************************************************** - * @file system_stm32f4xx.c - * @author MCD Application Team - * @version V2.6.0 - * @date 04-November-2016 - * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File. - * - * This file provides two functions and one global variable to be called from - * user application: - * - SystemInit(): This function is called at startup just after reset and - * before branch to main program. This call is made inside - * the "startup_stm32f4xx.s" file. - * - * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used - * by the user application to setup the SysTick - * timer or configure other parameters. - * - * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must - * be called whenever the core clock is changed - * during program execution. - * - * - ****************************************************************************** - * @attention - * - *

© COPYRIGHT 2016 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file system_stm32f4xx.c + * @author MCD Application Team + * @version V2.6.0 + * @date 04-November-2016 + * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File. + * + * This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32f4xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be + *used by the user application to setup the SysTick timer or configure other + *parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT 2016 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without + *modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + *notice, this list of conditions and the following disclaimer in the + *documentation and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + *ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + *LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + *INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + *CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + *ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ /** @addtogroup CMSIS - * @{ - */ + * @{ + */ /** @addtogroup stm32f4xx_system - * @{ - */ - -/** @addtogroup STM32F4xx_System_Private_Includes - * @{ - */ + * @{ + */ +/** @addtogroup STM32F4xx_System_Private_Includes + * @{ + */ #include "stm32f4xx.h" -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ +#if !defined(HSE_VALUE) + #define HSE_VALUE \ + ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz \ + */ +#endif /* HSE_VALUE */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ +#if !defined(HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F4xx_System_Private_TypesDefinitions - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F4xx_System_Private_Defines - * @{ - */ + * @{ + */ /************************* Miscellaneous Configuration ************************/ -/*!< Uncomment the following line if you need to use external SRAM or SDRAM as data memory */ -#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\ - || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) +/*!< Uncomment the following line if you need to use external SRAM or SDRAM as + * data memory */ +#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) \ + || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) /* #define DATA_IN_ExtSRAM */ -#endif /* STM32F40xxx || STM32F41xxx || STM32F42xxx || STM32F43xxx || STM32F469xx || STM32F479xx ||\ - STM32F412Zx || STM32F412Vx */ - -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) +#endif /* STM32F40xxx || STM32F41xxx || STM32F42xxx || STM32F43xxx || \ + STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */ + +#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F446xx) || defined(STM32F469xx) \ + || defined(STM32F479xx) /* #define DATA_IN_ExtSDRAM */ -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\ - STM32F479xx */ +#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || \ + STM32F446xx || STM32F469xx || STM32F479xx */ /*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */ /* #define VECT_TAB_SRAM */ -#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET \ + 0x00 /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ /******************************************************************************/ /** - * @} - */ + * @} + */ /** @addtogroup STM32F4xx_System_Private_Macros - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F4xx_System_Private_Variables - * @{ - */ - /* This variable is updated in three ways: - 1) by calling CMSIS function SystemCoreClockUpdate() - 2) by calling HAL API function HAL_RCC_GetHCLKFreq() - 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency - Note: If you use this function to configure the system clock; then there - is no need to call the 2 first functions listed above, since SystemCoreClock - variable is updated automatically. - */ -uint32_t SystemCoreClock = 16000000; + * @{ + */ +/* This variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock + frequency Note: If you use this function to configure the system clock; then + there is no need to call the 2 first functions listed above, since + SystemCoreClock variable is updated automatically. +*/ +uint32_t SystemCoreClock = 16000000; const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4}; /** - * @} - */ + * @} + */ /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes - * @{ - */ + * @{ + */ -#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) - static void SystemInit_ExtMemCtl(void); +#if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM) +static void SystemInit_ExtMemCtl(void); #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F4xx_System_Private_Functions - * @{ - */ + * @{ + */ /** - * @brief Setup the microcontroller system - * Initialize the FPU setting, vector table location and External memory - * configuration. - * @param None - * @retval None - */ + * @brief Setup the microcontroller system + * Initialize the FPU setting, vector table location and External memory + * configuration. + * @param None + * @retval None + */ void SystemInit(void) { - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ - #endif +/* FPU settings ------------------------------------------------------------*/ +#if (__FPU_PRESENT == 1) && (__FPU_USED == 1) + SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* set CP10 and CP11 Full Access */ +#endif /* Reset the RCC clock configuration to the default reset state ------------*/ /* Set HSION bit */ RCC->CR |= (uint32_t)0x00000001; @@ -190,8 +193,8 @@ void SystemInit(void) /* Disable all interrupts */ RCC->CIR = 0x00000000; -#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) - SystemInit_ExtMemCtl(); +#if defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM) + SystemInit_ExtMemCtl(); #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ /* Configure the Vector Table location add offset address ------------------*/ @@ -203,64 +206,62 @@ void SystemInit(void) } /** - * @brief Update SystemCoreClock variable according to Clock Register Values. - * The SystemCoreClock variable contains the core clock (HCLK), it can - * be used by the user application to setup the SysTick timer or configure - * other parameters. - * - * @note Each time the core clock (HCLK) changes, this function must be called - * to update SystemCoreClock variable value. Otherwise, any configuration - * based on this variable will be incorrect. - * - * @note - The system frequency computed by this function is not the real - * frequency in the chip. It is calculated based on the predefined - * constant and the selected clock source: - * - * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) - * - * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) - * - * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) - * or HSI_VALUE(*) multiplied/divided by the PLL factors. - * - * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value - * 16 MHz) but the real value may vary depending on the variations - * in voltage and temperature. - * - * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value - * depends on the application requirements), user has to ensure that HSE_VALUE - * is same as the real frequency of the crystal used. Otherwise, this function - * may have wrong result. - * - * - The result of this function could be not correct when using fractional - * value for HSE crystal. - * - * @param None - * @retval None - */ + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or + * configure other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any + * configuration based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the + * HSI_VALUE(*) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the + * HSE_VALUE(**) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the + * HSE_VALUE(**) or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file + * (default value 16 MHz) but the real value may vary depending on the + * variations in voltage and temperature. + * + * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file + * (its value depends on the application requirements), user has to ensure that + * HSE_VALUE is same as the real frequency of the crystal used. Otherwise, this + * function may have wrong result. + * + * - The result of this function could be not correct when using + * fractional value for HSE crystal. + * + * @param None + * @retval None + */ void SystemCoreClockUpdate(void) { uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2; - + /* Get SYSCLK source -------------------------------------------------------*/ tmp = RCC->CFGR & RCC_CFGR_SWS; switch (tmp) { - case 0x00: /* HSI used as system clock source */ - SystemCoreClock = HSI_VALUE; - break; - case 0x04: /* HSE used as system clock source */ - SystemCoreClock = HSE_VALUE; - break; - case 0x08: /* PLL used as system clock source */ + case 0x00: /* HSI used as system clock source */ SystemCoreClock = HSI_VALUE; break; + case 0x04: /* HSE used as system clock source */ SystemCoreClock = HSE_VALUE; break; + case 0x08: /* PLL used as system clock source */ /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N SYSCLK = PLL_VCO / PLL_P - */ + */ pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22; - pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; - + pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; + if (pllsource != 0) { /* HSE used as PLL clock source */ @@ -272,12 +273,10 @@ void SystemCoreClockUpdate(void) pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); } - pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2; - SystemCoreClock = pllvco/pllp; - break; - default: - SystemCoreClock = HSI_VALUE; + pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> 16) + 1) * 2; + SystemCoreClock = pllvco / pllp; break; + default: SystemCoreClock = HSI_VALUE; break; } /* Compute HCLK frequency --------------------------------------------------*/ /* Get HCLK prescaler */ @@ -286,478 +285,486 @@ void SystemCoreClockUpdate(void) SystemCoreClock >>= tmp; } -#if defined (DATA_IN_ExtSRAM) && defined (DATA_IN_ExtSDRAM) -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) +#if defined(DATA_IN_ExtSRAM) && defined(DATA_IN_ExtSDRAM) + #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx) /** - * @brief Setup the external memory controller. - * Called in startup_stm32f4xx.s before jump to main. - * This function configures the external memories (SRAM/SDRAM) - * This SRAM/SDRAM will be used as program data memory (including heap and stack). - * @param None - * @retval None - */ + * @brief Setup the external memory controller. + * Called in startup_stm32f4xx.s before jump to main. + * This function configures the external memories (SRAM/SDRAM) + * This SRAM/SDRAM will be used as program data memory (including heap + * and stack). + * @param None + * @retval None + */ void SystemInit_ExtMemCtl(void) { __IO uint32_t tmp = 0x00; - register uint32_t tmpreg = 0, timeout = 0xFFFF; + register uint32_t tmpreg = 0, timeout = 0xFFFF; register __IO uint32_t index; - /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */ + /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock + */ RCC->AHB1ENR |= 0x000001F8; /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN); - + /* Connect PDx pins to FMC Alternate function */ - GPIOD->AFR[0] = 0x00CCC0CC; - GPIOD->AFR[1] = 0xCCCCCCCC; - /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xAAAA0A8A; - /* Configure PDx pins speed to 100 MHz */ + GPIOD->AFR[0] = 0x00CCC0CC; + GPIOD->AFR[1] = 0xCCCCCCCC; + /* Configure PDx pins in Alternate function mode */ + GPIOD->MODER = 0xAAAA0A8A; + /* Configure PDx pins speed to 100 MHz */ GPIOD->OSPEEDR = 0xFFFF0FCF; - /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; + /* Configure PDx pins Output type to push-pull */ + GPIOD->OTYPER = 0x00000000; + /* No pull-up, pull-down for PDx pins */ + GPIOD->PUPDR = 0x00000000; /* Connect PEx pins to FMC Alternate function */ - GPIOE->AFR[0] = 0xC00CC0CC; - GPIOE->AFR[1] = 0xCCCCCCCC; - /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xAAAA828A; - /* Configure PEx pins speed to 100 MHz */ + GPIOE->AFR[0] = 0xC00CC0CC; + GPIOE->AFR[1] = 0xCCCCCCCC; + /* Configure PEx pins in Alternate function mode */ + GPIOE->MODER = 0xAAAA828A; + /* Configure PEx pins speed to 100 MHz */ GPIOE->OSPEEDR = 0xFFFFC3CF; - /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; - /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; - + /* Configure PEx pins Output type to push-pull */ + GPIOE->OTYPER = 0x00000000; + /* No pull-up, pull-down for PEx pins */ + GPIOE->PUPDR = 0x00000000; + /* Connect PFx pins to FMC Alternate function */ - GPIOF->AFR[0] = 0xCCCCCCCC; - GPIOF->AFR[1] = 0xCCCCCCCC; - /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xAA800AAA; - /* Configure PFx pins speed to 50 MHz */ + GPIOF->AFR[0] = 0xCCCCCCCC; + GPIOF->AFR[1] = 0xCCCCCCCC; + /* Configure PFx pins in Alternate function mode */ + GPIOF->MODER = 0xAA800AAA; + /* Configure PFx pins speed to 50 MHz */ GPIOF->OSPEEDR = 0xAA800AAA; - /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; - /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; + /* Configure PFx pins Output type to push-pull */ + GPIOF->OTYPER = 0x00000000; + /* No pull-up, pull-down for PFx pins */ + GPIOF->PUPDR = 0x00000000; /* Connect PGx pins to FMC Alternate function */ - GPIOG->AFR[0] = 0xCCCCCCCC; - GPIOG->AFR[1] = 0xCCCCCCCC; - /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0xAAAAAAAA; - /* Configure PGx pins speed to 50 MHz */ + GPIOG->AFR[0] = 0xCCCCCCCC; + GPIOG->AFR[1] = 0xCCCCCCCC; + /* Configure PGx pins in Alternate function mode */ + GPIOG->MODER = 0xAAAAAAAA; + /* Configure PGx pins speed to 50 MHz */ GPIOG->OSPEEDR = 0xAAAAAAAA; - /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; - /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; - + /* Configure PGx pins Output type to push-pull */ + GPIOG->OTYPER = 0x00000000; + /* No pull-up, pull-down for PGx pins */ + GPIOG->PUPDR = 0x00000000; + /* Connect PHx pins to FMC Alternate function */ - GPIOH->AFR[0] = 0x00C0CC00; - GPIOH->AFR[1] = 0xCCCCCCCC; - /* Configure PHx pins in Alternate function mode */ - GPIOH->MODER = 0xAAAA08A0; - /* Configure PHx pins speed to 50 MHz */ + GPIOH->AFR[0] = 0x00C0CC00; + GPIOH->AFR[1] = 0xCCCCCCCC; + /* Configure PHx pins in Alternate function mode */ + GPIOH->MODER = 0xAAAA08A0; + /* Configure PHx pins speed to 50 MHz */ GPIOH->OSPEEDR = 0xAAAA08A0; - /* Configure PHx pins Output type to push-pull */ - GPIOH->OTYPER = 0x00000000; - /* No pull-up, pull-down for PHx pins */ - GPIOH->PUPDR = 0x00000000; - + /* Configure PHx pins Output type to push-pull */ + GPIOH->OTYPER = 0x00000000; + /* No pull-up, pull-down for PHx pins */ + GPIOH->PUPDR = 0x00000000; + /* Connect PIx pins to FMC Alternate function */ - GPIOI->AFR[0] = 0xCCCCCCCC; - GPIOI->AFR[1] = 0x00000CC0; - /* Configure PIx pins in Alternate function mode */ - GPIOI->MODER = 0x0028AAAA; - /* Configure PIx pins speed to 50 MHz */ + GPIOI->AFR[0] = 0xCCCCCCCC; + GPIOI->AFR[1] = 0x00000CC0; + /* Configure PIx pins in Alternate function mode */ + GPIOI->MODER = 0x0028AAAA; + /* Configure PIx pins speed to 50 MHz */ GPIOI->OSPEEDR = 0x0028AAAA; - /* Configure PIx pins Output type to push-pull */ - GPIOI->OTYPER = 0x00000000; - /* No pull-up, pull-down for PIx pins */ - GPIOI->PUPDR = 0x00000000; - -/*-- FMC Configuration -------------------------------------------------------*/ + /* Configure PIx pins Output type to push-pull */ + GPIOI->OTYPER = 0x00000000; + /* No pull-up, pull-down for PIx pins */ + GPIOI->PUPDR = 0x00000000; + + /*-- FMC Configuration + * -------------------------------------------------------*/ /* Enable the FMC interface clock */ RCC->AHB3ENR |= 0x00000001; /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); FMC_Bank5_6->SDCR[0] = 0x000019E4; - FMC_Bank5_6->SDTR[0] = 0x01115351; - + FMC_Bank5_6->SDTR[0] = 0x01115351; + /* SDRAM initialization sequence */ /* Clock enable command */ - FMC_Bank5_6->SDCMR = 0x00000011; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - while((tmpreg != 0) && (timeout-- > 0)) + FMC_Bank5_6->SDCMR = 0x00000011; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } /* Delay */ - for (index = 0; index<1000; index++); - + for (index = 0; index < 1000; index++); + /* PALL command */ - FMC_Bank5_6->SDCMR = 0x00000012; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) + FMC_Bank5_6->SDCMR = 0x00000012; + timeout = 0xFFFF; + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } - + /* Auto refresh command */ FMC_Bank5_6->SDCMR = 0x00000073; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) + timeout = 0xFFFF; + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } - + /* MRD register program */ FMC_Bank5_6->SDCMR = 0x00046014; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) + timeout = 0xFFFF; + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + } + /* Set refresh count */ - tmpreg = FMC_Bank5_6->SDRTR; - FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1)); - + tmpreg = FMC_Bank5_6->SDRTR; + FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C << 1)); + /* Disable write protection */ - tmpreg = FMC_Bank5_6->SDCR[0]; + tmpreg = FMC_Bank5_6->SDCR[0]; FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF); -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) + #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) /* Configure and enable Bank1_SRAM2 */ FMC_Bank1->BTCR[2] = 0x00001011; FMC_Bank1->BTCR[3] = 0x00000201; FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ -#if defined(STM32F469xx) || defined(STM32F479xx) + #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ + #if defined(STM32F469xx) || defined(STM32F479xx) /* Configure and enable Bank1_SRAM2 */ FMC_Bank1->BTCR[2] = 0x00001091; FMC_Bank1->BTCR[3] = 0x00110212; FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F469xx || STM32F479xx */ + #endif /* STM32F469xx || STM32F479xx */ - (void)(tmp); + (void)(tmp); } -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */ -#elif defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) + #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || \ + STM32F469xx || STM32F479xx */ +#elif defined(DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM) /** - * @brief Setup the external memory controller. - * Called in startup_stm32f4xx.s before jump to main. - * This function configures the external memories (SRAM/SDRAM) - * This SRAM/SDRAM will be used as program data memory (including heap and stack). - * @param None - * @retval None - */ + * @brief Setup the external memory controller. + * Called in startup_stm32f4xx.s before jump to main. + * This function configures the external memories (SRAM/SDRAM) + * This SRAM/SDRAM will be used as program data memory (including heap + * and stack). + * @param None + * @retval None + */ void SystemInit_ExtMemCtl(void) { __IO uint32_t tmp = 0x00; -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) -#if defined (DATA_IN_ExtSDRAM) - register uint32_t tmpreg = 0, timeout = 0xFFFF; + #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F446xx) || defined(STM32F469xx) \ + || defined(STM32F479xx) + #if defined(DATA_IN_ExtSDRAM) + register uint32_t tmpreg = 0, timeout = 0xFFFF; register __IO uint32_t index; -#if defined(STM32F446xx) + #if defined(STM32F446xx) /* Enable GPIOA, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG interface clock */ RCC->AHB1ENR |= 0x0000007D; -#else - /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface + #else + /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */ RCC->AHB1ENR |= 0x000001F8; -#endif /* STM32F446xx */ + #endif /* STM32F446xx */ /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN); - -#if defined(STM32F446xx) + + #if defined(STM32F446xx) /* Connect PAx pins to FMC Alternate function */ - GPIOA->AFR[0] |= 0xC0000000; - GPIOA->AFR[1] |= 0x00000000; + GPIOA->AFR[0] |= 0xC0000000; + GPIOA->AFR[1] |= 0x00000000; /* Configure PDx pins in Alternate function mode */ - GPIOA->MODER |= 0x00008000; + GPIOA->MODER |= 0x00008000; /* Configure PDx pins speed to 50 MHz */ GPIOA->OSPEEDR |= 0x00008000; /* Configure PDx pins Output type to push-pull */ - GPIOA->OTYPER |= 0x00000000; + GPIOA->OTYPER |= 0x00000000; /* No pull-up, pull-down for PDx pins */ - GPIOA->PUPDR |= 0x00000000; + GPIOA->PUPDR |= 0x00000000; /* Connect PCx pins to FMC Alternate function */ - GPIOC->AFR[0] |= 0x00CC0000; - GPIOC->AFR[1] |= 0x00000000; + GPIOC->AFR[0] |= 0x00CC0000; + GPIOC->AFR[1] |= 0x00000000; /* Configure PDx pins in Alternate function mode */ - GPIOC->MODER |= 0x00000A00; + GPIOC->MODER |= 0x00000A00; /* Configure PDx pins speed to 50 MHz */ GPIOC->OSPEEDR |= 0x00000A00; /* Configure PDx pins Output type to push-pull */ - GPIOC->OTYPER |= 0x00000000; + GPIOC->OTYPER |= 0x00000000; /* No pull-up, pull-down for PDx pins */ - GPIOC->PUPDR |= 0x00000000; -#endif /* STM32F446xx */ + GPIOC->PUPDR |= 0x00000000; + #endif /* STM32F446xx */ /* Connect PDx pins to FMC Alternate function */ - GPIOD->AFR[0] = 0x000000CC; - GPIOD->AFR[1] = 0xCC000CCC; - /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xA02A000A; - /* Configure PDx pins speed to 50 MHz */ + GPIOD->AFR[0] = 0x000000CC; + GPIOD->AFR[1] = 0xCC000CCC; + /* Configure PDx pins in Alternate function mode */ + GPIOD->MODER = 0xA02A000A; + /* Configure PDx pins speed to 50 MHz */ GPIOD->OSPEEDR = 0xA02A000A; - /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; + /* Configure PDx pins Output type to push-pull */ + GPIOD->OTYPER = 0x00000000; + /* No pull-up, pull-down for PDx pins */ + GPIOD->PUPDR = 0x00000000; /* Connect PEx pins to FMC Alternate function */ - GPIOE->AFR[0] = 0xC00000CC; - GPIOE->AFR[1] = 0xCCCCCCCC; - /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xAAAA800A; - /* Configure PEx pins speed to 50 MHz */ + GPIOE->AFR[0] = 0xC00000CC; + GPIOE->AFR[1] = 0xCCCCCCCC; + /* Configure PEx pins in Alternate function mode */ + GPIOE->MODER = 0xAAAA800A; + /* Configure PEx pins speed to 50 MHz */ GPIOE->OSPEEDR = 0xAAAA800A; - /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; - /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; + /* Configure PEx pins Output type to push-pull */ + GPIOE->OTYPER = 0x00000000; + /* No pull-up, pull-down for PEx pins */ + GPIOE->PUPDR = 0x00000000; /* Connect PFx pins to FMC Alternate function */ - GPIOF->AFR[0] = 0xCCCCCCCC; - GPIOF->AFR[1] = 0xCCCCCCCC; - /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xAA800AAA; - /* Configure PFx pins speed to 50 MHz */ + GPIOF->AFR[0] = 0xCCCCCCCC; + GPIOF->AFR[1] = 0xCCCCCCCC; + /* Configure PFx pins in Alternate function mode */ + GPIOF->MODER = 0xAA800AAA; + /* Configure PFx pins speed to 50 MHz */ GPIOF->OSPEEDR = 0xAA800AAA; - /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; - /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; + /* Configure PFx pins Output type to push-pull */ + GPIOF->OTYPER = 0x00000000; + /* No pull-up, pull-down for PFx pins */ + GPIOF->PUPDR = 0x00000000; /* Connect PGx pins to FMC Alternate function */ - GPIOG->AFR[0] = 0xCCCCCCCC; - GPIOG->AFR[1] = 0xCCCCCCCC; - /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0xAAAAAAAA; - /* Configure PGx pins speed to 50 MHz */ + GPIOG->AFR[0] = 0xCCCCCCCC; + GPIOG->AFR[1] = 0xCCCCCCCC; + /* Configure PGx pins in Alternate function mode */ + GPIOG->MODER = 0xAAAAAAAA; + /* Configure PGx pins speed to 50 MHz */ GPIOG->OSPEEDR = 0xAAAAAAAA; - /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; - /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; + /* Configure PGx pins Output type to push-pull */ + GPIOG->OTYPER = 0x00000000; + /* No pull-up, pull-down for PGx pins */ + GPIOG->PUPDR = 0x00000000; -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) + #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx) /* Connect PHx pins to FMC Alternate function */ - GPIOH->AFR[0] = 0x00C0CC00; - GPIOH->AFR[1] = 0xCCCCCCCC; - /* Configure PHx pins in Alternate function mode */ - GPIOH->MODER = 0xAAAA08A0; - /* Configure PHx pins speed to 50 MHz */ + GPIOH->AFR[0] = 0x00C0CC00; + GPIOH->AFR[1] = 0xCCCCCCCC; + /* Configure PHx pins in Alternate function mode */ + GPIOH->MODER = 0xAAAA08A0; + /* Configure PHx pins speed to 50 MHz */ GPIOH->OSPEEDR = 0xAAAA08A0; - /* Configure PHx pins Output type to push-pull */ - GPIOH->OTYPER = 0x00000000; - /* No pull-up, pull-down for PHx pins */ - GPIOH->PUPDR = 0x00000000; - + /* Configure PHx pins Output type to push-pull */ + GPIOH->OTYPER = 0x00000000; + /* No pull-up, pull-down for PHx pins */ + GPIOH->PUPDR = 0x00000000; + /* Connect PIx pins to FMC Alternate function */ - GPIOI->AFR[0] = 0xCCCCCCCC; - GPIOI->AFR[1] = 0x00000CC0; - /* Configure PIx pins in Alternate function mode */ - GPIOI->MODER = 0x0028AAAA; - /* Configure PIx pins speed to 50 MHz */ + GPIOI->AFR[0] = 0xCCCCCCCC; + GPIOI->AFR[1] = 0x00000CC0; + /* Configure PIx pins in Alternate function mode */ + GPIOI->MODER = 0x0028AAAA; + /* Configure PIx pins speed to 50 MHz */ GPIOI->OSPEEDR = 0x0028AAAA; - /* Configure PIx pins Output type to push-pull */ - GPIOI->OTYPER = 0x00000000; - /* No pull-up, pull-down for PIx pins */ - GPIOI->PUPDR = 0x00000000; -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */ - -/*-- FMC Configuration -------------------------------------------------------*/ + /* Configure PIx pins Output type to push-pull */ + GPIOI->OTYPER = 0x00000000; + /* No pull-up, pull-down for PIx pins */ + GPIOI->PUPDR = 0x00000000; + #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || \ + STM32F469xx || STM32F479xx */ + + /*-- FMC Configuration + * -------------------------------------------------------*/ /* Enable the FMC interface clock */ RCC->AHB3ENR |= 0x00000001; /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); - /* Configure and enable SDRAM bank1 */ -#if defined(STM32F446xx) + /* Configure and enable SDRAM bank1 */ + #if defined(STM32F446xx) FMC_Bank5_6->SDCR[0] = 0x00001954; -#else + #else FMC_Bank5_6->SDCR[0] = 0x000019E4; -#endif /* STM32F446xx */ - FMC_Bank5_6->SDTR[0] = 0x01115351; - + #endif /* STM32F446xx */ + FMC_Bank5_6->SDTR[0] = 0x01115351; + /* SDRAM initialization sequence */ /* Clock enable command */ - FMC_Bank5_6->SDCMR = 0x00000011; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - while((tmpreg != 0) && (timeout-- > 0)) + FMC_Bank5_6->SDCMR = 0x00000011; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } /* Delay */ - for (index = 0; index<1000; index++); - + for (index = 0; index < 1000; index++); + /* PALL command */ - FMC_Bank5_6->SDCMR = 0x00000012; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) + FMC_Bank5_6->SDCMR = 0x00000012; + timeout = 0xFFFF; + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } - - /* Auto refresh command */ -#if defined(STM32F446xx) + + /* Auto refresh command */ + #if defined(STM32F446xx) FMC_Bank5_6->SDCMR = 0x000000F3; -#else + #else FMC_Bank5_6->SDCMR = 0x00000073; -#endif /* STM32F446xx */ + #endif /* STM32F446xx */ timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } - - /* MRD register program */ -#if defined(STM32F446xx) + + /* MRD register program */ + #if defined(STM32F446xx) FMC_Bank5_6->SDCMR = 0x00044014; -#else + #else FMC_Bank5_6->SDCMR = 0x00046014; -#endif /* STM32F446xx */ + #endif /* STM32F446xx */ timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) + while ((tmpreg != 0) && (timeout-- > 0)) { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - + tmpreg = FMC_Bank5_6->SDSR & 0x00000020; + } + /* Set refresh count */ tmpreg = FMC_Bank5_6->SDRTR; -#if defined(STM32F446xx) - FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C<<1)); -#else - FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1)); -#endif /* STM32F446xx */ - + #if defined(STM32F446xx) + FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C << 1)); + #else + FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C << 1)); + #endif /* STM32F446xx */ + /* Disable write protection */ - tmpreg = FMC_Bank5_6->SDCR[0]; + tmpreg = FMC_Bank5_6->SDCR[0]; FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF); -#endif /* DATA_IN_ExtSDRAM */ -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */ + #endif /* DATA_IN_ExtSDRAM */ + #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || \ + STM32F446xx || STM32F469xx || STM32F479xx */ -#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\ - || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) + #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) \ + || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) -#if defined(DATA_IN_ExtSRAM) -/*-- GPIOs Configuration -----------------------------------------------------*/ - /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ - RCC->AHB1ENR |= 0x00000078; + #if defined(DATA_IN_ExtSRAM) + /*-- GPIOs Configuration + * -----------------------------------------------------*/ + /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ + RCC->AHB1ENR |= 0x00000078; /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN); - + /* Connect PDx pins to FMC Alternate function */ - GPIOD->AFR[0] = 0x00CCC0CC; - GPIOD->AFR[1] = 0xCCCCCCCC; - /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xAAAA0A8A; - /* Configure PDx pins speed to 100 MHz */ + GPIOD->AFR[0] = 0x00CCC0CC; + GPIOD->AFR[1] = 0xCCCCCCCC; + /* Configure PDx pins in Alternate function mode */ + GPIOD->MODER = 0xAAAA0A8A; + /* Configure PDx pins speed to 100 MHz */ GPIOD->OSPEEDR = 0xFFFF0FCF; - /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; + /* Configure PDx pins Output type to push-pull */ + GPIOD->OTYPER = 0x00000000; + /* No pull-up, pull-down for PDx pins */ + GPIOD->PUPDR = 0x00000000; /* Connect PEx pins to FMC Alternate function */ - GPIOE->AFR[0] = 0xC00CC0CC; - GPIOE->AFR[1] = 0xCCCCCCCC; - /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xAAAA828A; - /* Configure PEx pins speed to 100 MHz */ + GPIOE->AFR[0] = 0xC00CC0CC; + GPIOE->AFR[1] = 0xCCCCCCCC; + /* Configure PEx pins in Alternate function mode */ + GPIOE->MODER = 0xAAAA828A; + /* Configure PEx pins speed to 100 MHz */ GPIOE->OSPEEDR = 0xFFFFC3CF; - /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; - /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; + /* Configure PEx pins Output type to push-pull */ + GPIOE->OTYPER = 0x00000000; + /* No pull-up, pull-down for PEx pins */ + GPIOE->PUPDR = 0x00000000; /* Connect PFx pins to FMC Alternate function */ - GPIOF->AFR[0] = 0x00CCCCCC; - GPIOF->AFR[1] = 0xCCCC0000; - /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xAA000AAA; - /* Configure PFx pins speed to 100 MHz */ + GPIOF->AFR[0] = 0x00CCCCCC; + GPIOF->AFR[1] = 0xCCCC0000; + /* Configure PFx pins in Alternate function mode */ + GPIOF->MODER = 0xAA000AAA; + /* Configure PFx pins speed to 100 MHz */ GPIOF->OSPEEDR = 0xFF000FFF; - /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; - /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; + /* Configure PFx pins Output type to push-pull */ + GPIOF->OTYPER = 0x00000000; + /* No pull-up, pull-down for PFx pins */ + GPIOF->PUPDR = 0x00000000; /* Connect PGx pins to FMC Alternate function */ - GPIOG->AFR[0] = 0x00CCCCCC; - GPIOG->AFR[1] = 0x000000C0; - /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0x00085AAA; - /* Configure PGx pins speed to 100 MHz */ + GPIOG->AFR[0] = 0x00CCCCCC; + GPIOG->AFR[1] = 0x000000C0; + /* Configure PGx pins in Alternate function mode */ + GPIOG->MODER = 0x00085AAA; + /* Configure PGx pins speed to 100 MHz */ GPIOG->OSPEEDR = 0x000CAFFF; - /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; - /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; - -/*-- FMC/FSMC Configuration --------------------------------------------------*/ - /* Enable the FMC/FSMC interface clock */ - RCC->AHB3ENR |= 0x00000001; + /* Configure PGx pins Output type to push-pull */ + GPIOG->OTYPER = 0x00000000; + /* No pull-up, pull-down for PGx pins */ + GPIOG->PUPDR = 0x00000000; -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) + /*-- FMC/FSMC Configuration + * --------------------------------------------------*/ + /* Enable the FMC/FSMC interface clock */ + RCC->AHB3ENR |= 0x00000001; + + #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); /* Configure and enable Bank1_SRAM2 */ FMC_Bank1->BTCR[2] = 0x00001011; FMC_Bank1->BTCR[3] = 0x00000201; FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ -#if defined(STM32F469xx) || defined(STM32F479xx) + #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ + #if defined(STM32F469xx) || defined(STM32F479xx) /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); /* Configure and enable Bank1_SRAM2 */ FMC_Bank1->BTCR[2] = 0x00001091; FMC_Bank1->BTCR[3] = 0x00110212; FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F469xx || STM32F479xx */ -#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)\ - || defined(STM32F412Zx) || defined(STM32F412Vx) + #endif /* STM32F469xx || STM32F479xx */ + #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F412Zx) || defined(STM32F412Vx) /* Delay after an RCC peripheral clock enabling */ tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FSMCEN); /* Configure and enable Bank1_SRAM2 */ FSMC_Bank1->BTCR[2] = 0x00001011; FSMC_Bank1->BTCR[3] = 0x00000201; FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF; -#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F412Zx || STM32F412Vx */ + #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || \ + STM32F412Zx || STM32F412Vx */ -#endif /* DATA_IN_ExtSRAM */ -#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\ - STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */ - (void)(tmp); + #endif /* DATA_IN_ExtSRAM */ + #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || \ + STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || \ + STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */ + (void)(tmp); } -#endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */ +#endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */ /** - * @} - */ + * @} + */ /** - * @} - */ + * @} + */ /** - * @} - */ + * @} + */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/examples/ArmTimerCallbacks - C++/etl_profile.h b/examples/ArmTimerCallbacks - C++/etl_profile.h index 84bbd550..d6eda2a5 100644 --- a/examples/ArmTimerCallbacks - C++/etl_profile.h +++ b/examples/ArmTimerCallbacks - C++/etl_profile.h @@ -10,6 +10,6 @@ #define ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK #define ETL_NO_STL -//#include "etl/profiles/auto.h" +// #include "etl/profiles/auto.h" #endif diff --git a/examples/ArmTimerCallbacks - C++/main.cpp b/examples/ArmTimerCallbacks - C++/main.cpp index 72692618..4a2ccb28 100644 --- a/examples/ArmTimerCallbacks - C++/main.cpp +++ b/examples/ArmTimerCallbacks - C++/main.cpp @@ -1,23 +1,23 @@ #include -//#if (__cplusplus < 201103L) +// #if (__cplusplus < 201103L) extern "C" { -//#endif -#include "Board_LED.h" // ::Board Support:LED +// #endif #include "Board_Buttons.h" // ::Board Support:Buttons -//#if (__cplusplus < 201103L) +#include "Board_LED.h" // ::Board Support:LED + // #if (__cplusplus < 201103L) } -//#endif +// #endif -#include "stm32f4xx.h" // Device header +#include "stm32f4xx.h" // Device header -#include "etl/function.h" -#include "etl/callback_timer.h" -#include "etl/vector.h" -#include "etl/iterator.h" #include "etl/binary.h" +#include "etl/callback_timer.h" +#include "etl/function.h" +#include "etl/iterator.h" +#include "etl/vector.h" struct FP { @@ -28,7 +28,7 @@ static etl::vector power_callbacks; void register_poweroff_callback(void (*function)()) { - FP fp = { function }; + FP fp = {function}; power_callbacks.push_back(fp); } @@ -45,38 +45,38 @@ etl::timer::id::type swap_timers; * SystemCoreClockConfigure: configure SystemCoreClock using HSI (HSE is not populated on Nucleo board) *----------------------------------------------------------------------------*/ -void SystemCoreClockConfigure(void) { +void SystemCoreClockConfigure(void) +{ + RCC->CR |= ((uint32_t)RCC_CR_HSION); // Enable HSI + while ((RCC->CR & RCC_CR_HSIRDY) == 0); // Wait for HSI Ready - RCC->CR |= ((uint32_t)RCC_CR_HSION); // Enable HSI - while ((RCC->CR & RCC_CR_HSIRDY) == 0); // Wait for HSI Ready + RCC->CFGR = RCC_CFGR_SW_HSI; // HSI is system clock + while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI); // Wait for HSI used as system clock - RCC->CFGR = RCC_CFGR_SW_HSI; // HSI is system clock - while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI); // Wait for HSI used as system clock + FLASH->ACR = FLASH_ACR_PRFTEN; // Enable Prefetch Buffer + FLASH->ACR |= FLASH_ACR_ICEN; // Instruction cache enable + FLASH->ACR |= FLASH_ACR_DCEN; // Data cache enable + FLASH->ACR |= FLASH_ACR_LATENCY_5WS; // Flash 5 wait state - FLASH->ACR = FLASH_ACR_PRFTEN; // Enable Prefetch Buffer - FLASH->ACR |= FLASH_ACR_ICEN; // Instruction cache enable - FLASH->ACR |= FLASH_ACR_DCEN; // Data cache enable - FLASH->ACR |= FLASH_ACR_LATENCY_5WS; // Flash 5 wait state + RCC->CFGR |= RCC_CFGR_HPRE_DIV1; // HCLK = SYSCLK + RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; // APB1 = HCLK/4 + RCC->CFGR |= RCC_CFGR_PPRE2_DIV2; // APB2 = HCLK/2 - RCC->CFGR |= RCC_CFGR_HPRE_DIV1; // HCLK = SYSCLK - RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; // APB1 = HCLK/4 - RCC->CFGR |= RCC_CFGR_PPRE2_DIV2; // APB2 = HCLK/2 - - RCC->CR &= ~RCC_CR_PLLON; // Disable PLL + RCC->CR &= ~RCC_CR_PLLON; // Disable PLL // PLL configuration: VCO = HSI/M * N, Sysclk = VCO/P - RCC->PLLCFGR = ( 16ul | // PLL_M = 16 - (384ul << 6U) | // PLL_N = 384 - ( 3ul << 16U) | // PLL_P = 8 - (RCC_PLLCFGR_PLLSRC_HSI) | // PLL_SRC = HSI - ( 8ul << 24U) ); // PLL_Q = 8 + RCC->PLLCFGR = (16ul | // PLL_M = 16 + (384ul << 6U) | // PLL_N = 384 + (3ul << 16U) | // PLL_P = 8 + (RCC_PLLCFGR_PLLSRC_HSI) | // PLL_SRC = HSI + (8ul << 24U)); // PLL_Q = 8 - RCC->CR |= RCC_CR_PLLON; // Enable PLL - while((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP(); // Wait till PLL is ready + RCC->CR |= RCC_CR_PLLON; // Enable PLL + while ((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP(); // Wait till PLL is ready - RCC->CFGR &= ~RCC_CFGR_SW; // Select PLL as system clock source - RCC->CFGR |= RCC_CFGR_SW_PLL; - while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); // Wait till PLL is system clock src + RCC->CFGR &= ~RCC_CFGR_SW; // Select PLL as system clock source + RCC->CFGR |= RCC_CFGR_SW_PLL; + while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); // Wait till PLL is system clock src } void StartTimers() @@ -123,7 +123,7 @@ void LedToggle() int main() { - SystemCoreClockConfigure(); // configure HSI as System Clock + SystemCoreClockConfigure(); // configure HSI as System Clock SystemCoreClockUpdate(); LED_Initialize(); @@ -131,10 +131,10 @@ int main() // The LEDs will start flashing fast after 2 seconds. // After another 5 seconds they will start flashing slower. - short_toggle = callback_timer.register_timer(LedToggle, 50, etl::timer::mode::REPEATING); - long_toggle = callback_timer.register_timer(LedToggle, 100, etl::timer::mode::REPEATING); + short_toggle = callback_timer.register_timer(LedToggle, 50, etl::timer::mode::REPEATING); + long_toggle = callback_timer.register_timer(LedToggle, 100, etl::timer::mode::REPEATING); start_timers = callback_timer.register_timer(StartTimers, 2000, etl::timer::mode::SINGLE_SHOT); - swap_timers = callback_timer.register_timer(SwapTimers, 1500, etl::timer::mode::SINGLE_SHOT); + swap_timers = callback_timer.register_timer(SwapTimers, 1500, etl::timer::mode::SINGLE_SHOT); SysTick_Config(SystemCoreClock / 1000); @@ -152,7 +152,7 @@ extern "C" { void SysTick_Handler() { - const uint32_t TICK = 1U; + const uint32_t TICK = 1U; static uint32_t nticks = TICK; if (callback_timer.tick(nticks)) diff --git a/examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp b/examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp index bbf6debf..859b92c8 100644 --- a/examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp +++ b/examples/FunctionInterruptSimulation-Delegates/FunctionInterruptSimulation.cpp @@ -11,8 +11,8 @@ enum VectorId USART1_IRQ_HANDLER = 52, USART2_IRQ_HANDLER = 53, VECTOR_ID_END, - VECTOR_ID_OFFSET = TIM1_CC_IRQ_HANDLER, - VECTOR_ID_RANGE = VECTOR_ID_END - VECTOR_ID_OFFSET + VECTOR_ID_OFFSET = TIM1_CC_IRQ_HANDLER, + VECTOR_ID_RANGE = VECTOR_ID_END - VECTOR_ID_OFFSET }; typedef etl::delegate_service InterruptVectors; @@ -91,8 +91,8 @@ public: // Constructor. Uart(int port_id, size_t interruptId) - : port_id(port_id), - callback(etl::delegate::create(*this)) + : port_id(port_id) + , callback(etl::delegate::create(*this)) { GetInterruptVectorsInstance().register_delegate(interruptId, callback); } @@ -119,7 +119,8 @@ Uart uart1(0, USART1_IRQ_HANDLER); Uart uart2(1, USART2_IRQ_HANDLER); // Declare a global callback for the timer. -// Uses the most efficient callback type for a class, as everything is known at compile time. +// Uses the most efficient callback type for a class, as everything is known at +// compile time. etl::delegate timer_member_callback = etl::delegate::create(); // Declare the callbacks for the free functions. diff --git a/examples/FunctionInterruptSimulation/FunctionInterruptSimulation.cpp b/examples/FunctionInterruptSimulation/FunctionInterruptSimulation.cpp index 9eb6cb98..8e680e3d 100644 --- a/examples/FunctionInterruptSimulation/FunctionInterruptSimulation.cpp +++ b/examples/FunctionInterruptSimulation/FunctionInterruptSimulation.cpp @@ -1,7 +1,7 @@ #include -#include "etl/function.h" #include "etl/callback_service.h" +#include "etl/function.h" enum VectorId { @@ -11,8 +11,8 @@ enum VectorId USART1_IRQ_HANDLER = 52, USART2_IRQ_HANDLER = 53, VECTOR_ID_END, - VECTOR_ID_OFFSET = TIM1_CC_IRQ_HANDLER, - VECTOR_ID_RANGE = VECTOR_ID_END - VECTOR_ID_OFFSET + VECTOR_ID_OFFSET = TIM1_CC_IRQ_HANDLER, + VECTOR_ID_RANGE = VECTOR_ID_END - VECTOR_ID_OFFSET }; typedef etl::callback_service InterruptVectors; @@ -91,8 +91,8 @@ public: // Constructor. Uart(int port_id, int interruptId) - : port_id(port_id), - callback(*this) + : port_id(port_id) + , callback(*this) { GetInterruptVectorsInstance().register_callback(interruptId, callback); } @@ -119,7 +119,8 @@ Uart uart1(0, USART1_IRQ_HANDLER); Uart uart2(1, USART2_IRQ_HANDLER); // Declare a global callback for the timer. -// Uses the most efficient callback type for a class, as everything is known at compile time. +// Uses the most efficient callback type for a class, as everything is known at +// compile time. etl::function_imp timer_member_callback; // Declare the callbacks for the free functions. diff --git a/examples/MutexMessageRouter/MutexMessageRouter.cpp b/examples/MutexMessageRouter/MutexMessageRouter.cpp index 1d612673..b04a61f4 100644 --- a/examples/MutexMessageRouter/MutexMessageRouter.cpp +++ b/examples/MutexMessageRouter/MutexMessageRouter.cpp @@ -1,13 +1,13 @@ -#include -#include #include -#include #include +#include +#include +#include -#include "etl/mutex.h" #include "etl/message.h" #include "etl/message_router.h" +#include "etl/mutex.h" //************************************* struct Message1 : public etl::message<1> @@ -48,9 +48,7 @@ public: } //***************** - void on_receive_unknown(const etl::imessage&) - { - } + void on_receive_unknown(const etl::imessage&) {} std::string result; @@ -61,7 +59,7 @@ private: //************************************* etl::atomic start = false; -Router router; +Router router; //************************************* void thread1() @@ -106,5 +104,3 @@ int main() return 0; } - - diff --git a/examples/QueuedFSM/QueuedFSM.cpp b/examples/QueuedFSM/QueuedFSM.cpp index a0cbd029..5bda8913 100644 --- a/examples/QueuedFSM/QueuedFSM.cpp +++ b/examples/QueuedFSM/QueuedFSM.cpp @@ -31,9 +31,9 @@ SOFTWARE. // derived class. //***************************************************************************** -#include "etl/queue.h" #include "etl/fsm.h" #include "etl/message_packet.h" +#include "etl/queue.h" #include #include @@ -122,7 +122,7 @@ public: while (!queue.empty()) { message_packet& packet = queue.front(); - etl::imessage& msg = packet.get(); + etl::imessage& msg = packet.get(); std::cout << "Processing message " << int(msg.get_message_id()) << std::endl; // Call the base class's receive function. @@ -258,7 +258,7 @@ int main() State2 state2; // The list of states. - etl::ifsm_state* state_list[] = { &state1, &state2 }; + etl::ifsm_state* state_list[] = {&state1, &state2}; // Define some messages. Message1 m1(1); @@ -285,4 +285,3 @@ int main() return 0; } - diff --git a/examples/QueuedMessageRouter/QueuedMessageRouter.cpp b/examples/QueuedMessageRouter/QueuedMessageRouter.cpp index f4348e5e..e9d815d2 100644 --- a/examples/QueuedMessageRouter/QueuedMessageRouter.cpp +++ b/examples/QueuedMessageRouter/QueuedMessageRouter.cpp @@ -1,6 +1,6 @@ -#include "etl/queue.h" #include "etl/message_router.h" +#include "etl/queue.h" #include #include @@ -81,7 +81,7 @@ public: while (!queue.empty()) { message_packet& packet = queue.front(); - etl::imessage& msg = packet.get(); + etl::imessage& msg = packet.get(); std::cout << "Processing message " << int(msg.get_message_id()) << std::endl; // Call the base class's receive function. @@ -146,4 +146,3 @@ int main() return 0; } - diff --git a/examples/Scheduler/Scheduler.cpp b/examples/Scheduler/Scheduler.cpp index 015822ae..3ea957be 100644 --- a/examples/Scheduler/Scheduler.cpp +++ b/examples/Scheduler/Scheduler.cpp @@ -31,9 +31,9 @@ SOFTWARE. // Experiment with the different scheduling policies. //***************************************************************************** +#include "etl/function.h" #include "etl/scheduler.h" #include "etl/task.h" -#include "etl/function.h" #include #include @@ -50,13 +50,13 @@ public: : task(1) , work(3) { - } //************************************* uint32_t task_request_work() const { - return work; // How much work do we still have to do? This could be a message queue length. + return work; // How much work do we still have to do? This could be a + // message queue length. } //************************************* @@ -83,13 +83,13 @@ public: : task(2) , work(4) { - } //************************************* uint32_t task_request_work() const { - return work; // How much work do we still have to do? This could be a message queue length. + return work; // How much work do we still have to do? This could be a + // message queue length. } //************************************* @@ -115,7 +115,6 @@ public: Idle(etl::ischeduler& scheduler_) : scheduler(scheduler_) { - } //************************************* @@ -181,4 +180,3 @@ int main() return 0; } - diff --git a/examples/SharedMessage/SharedMessage.cpp b/examples/SharedMessage/SharedMessage.cpp index caaf4a0b..7bcbc27f 100644 --- a/examples/SharedMessage/SharedMessage.cpp +++ b/examples/SharedMessage/SharedMessage.cpp @@ -2,18 +2,18 @@ // Shared message example //***************************************************************************** -#include "etl/shared_message.h" -#include "etl/message.h" -#include "etl/reference_counted_message_pool.h" -#include "etl/message_router.h" -#include "etl/message_bus.h" #include "etl/fixed_sized_memory_block_allocator.h" +#include "etl/message.h" +#include "etl/message_bus.h" +#include "etl/message_router.h" #include "etl/queue.h" +#include "etl/reference_counted_message_pool.h" +#include "etl/shared_message.h" -#include #include -#include +#include #include +#include constexpr etl::message_router_id_t RouterId1 = 1U; constexpr etl::message_router_id_t RouterId2 = 2U; @@ -26,9 +26,8 @@ struct Message1 : public etl::message<1> Message1(std::string s_) : s(s_) { - } - + std::string s; }; @@ -40,10 +39,10 @@ struct Message2 : public etl::message<2> Message2(std::string s_) : s(s_) { - } - + std::string s; + char data[100]; }; @@ -55,7 +54,6 @@ struct Message3 : public etl::message<3> Message3(std::string s_) : s(s_) { - } std::string s; @@ -199,7 +197,7 @@ struct Bus : public etl::message_bus<2U> //***************************************************************************** MessageRouter1 router1; MessageRouter2 router2; -Bus bus; +Bus bus; //***************************************************************************** // The thread safe message pool. Uses atomic uint32_t for counting. @@ -230,8 +228,8 @@ private: }; //***************************************************************************** -// The memory block allocator that supplies the pool with memory -// to store reference counted messages in. +// The memory block allocator that supplies the pool with memory +// to store reference counted messages in. // The reference counted message parameters type for the messages we will use. using message_parameters_small = MessagePool::pool_message_parameters; @@ -243,10 +241,12 @@ constexpr size_t max_alignment_small = message_parameters_small::max_alignment; constexpr size_t max_size_large = message_parameters_large::max_size; constexpr size_t max_alignment_large = message_parameters_large::max_alignment; -// A fixed memory block allocator for 4 items, using the parameters from the smaller messages. +// A fixed memory block allocator for 4 items, using the parameters from the +// smaller messages. etl::fixed_sized_memory_block_allocator memory_allocator; -// A fixed memory block allocator for 4 items, using the parameters from the larger message. +// A fixed memory block allocator for 4 items, using the parameters from the +// larger message. etl::fixed_sized_memory_block_allocator memory_allocator_successor; //***************************************************************************** @@ -256,8 +256,8 @@ etl::fixed_sized_memory_block_allocator MessagePool message_pool(memory_allocator); //***************************************************************************** -// A statically allocated reference counted message that is never allocated or released by a pool. -// Contains a copy of Message3("Three"). +// A statically allocated reference counted message that is never allocated or +// released by a pool. Contains a copy of Message3("Three"). //***************************************************************************** etl::persistent_message pm3(Message3("Three")); @@ -269,10 +269,15 @@ int main() Message1 m1("One"); Message2 m2("Two"); - - etl::shared_message sm1(message_pool, m1); // Created a shared message by allocating a reference counted message from message_pool containing a copy of m1. - etl::shared_message sm2(message_pool, m2); // Created a shared message by allocating a reference counted message from message_pool containing a copy of m2. - etl::shared_message sm3(pm3); // Created a shared message from a statically allocated persistent message. + + etl::shared_message sm1(message_pool, + m1); // Created a shared message by allocating a reference counted message + // from message_pool containing a copy of m1. + etl::shared_message sm2(message_pool, + m2); // Created a shared message by allocating a reference counted message + // from message_pool containing a copy of m2. + etl::shared_message sm3(pm3); // Created a shared message from a statically + // allocated persistent message. bus.subscribe(router1); // Subscribe router1 to the bus. bus.subscribe(router2); // Subscribe router2 to the bus. diff --git a/examples/UniquePtrWithPool/Main.cpp b/examples/UniquePtrWithPool/Main.cpp index c3172ff9..30347951 100644 --- a/examples/UniquePtrWithPool/Main.cpp +++ b/examples/UniquePtrWithPool/Main.cpp @@ -6,7 +6,8 @@ struct S { S(int a_, double b_) - : a(a_), b(b_) + : a(a_) + , b(b_) { } @@ -17,11 +18,11 @@ struct S int main() { etl::pool pool; - auto pool_deleter = [&pool](auto ptr) - { - std::cout << "Releasing S(" << ptr->a << ", " << ptr->b << ") back to pool." << std::endl; - pool.destroy(ptr); - }; + auto pool_deleter = [&pool](auto ptr) + { + std::cout << "Releasing S(" << ptr->a << ", " << ptr->b << ") back to pool." << std::endl; + pool.destroy(ptr); + }; using Unique = etl::unique_ptr; Unique us1(pool.create(1, 2), pool_deleter); diff --git a/examples/platformio/src/main.cpp b/examples/platformio/src/main.cpp index 66295c17..0686ba82 100644 --- a/examples/platformio/src/main.cpp +++ b/examples/platformio/src/main.cpp @@ -88,35 +88,35 @@ public: // Notify observers about a position event. void Position_Event() { - Position position = { 100, 200 }; + Position position = {100, 200}; notify_observers(position); } // Notify observers about a button up event. void Button_Event_Up() { - Button button = { Button::Up }; + Button button = {Button::Up}; notify_observers(button); } // Notify observers about a button down event. void Button_Event_Down() { - Button button = { Button::Down }; + Button button = {Button::Down}; notify_observers(button); } // Notify observers about a wheel up event. void Wheel_Event_Up() { - Wheel wheel = { 50 }; + Wheel wheel = {50}; notify_observers(wheel); } // Notify observers about a wheel down event. void Wheel_Event_Down() { - Wheel wheel = { -25 }; + Wheel wheel = {-25}; notify_observers(wheel); } }; @@ -137,6 +137,6 @@ int main() mouse_driver.Position_Event(); mouse_driver.Wheel_Event_Down(); mouse_driver.Wheel_Event_Up(); - + return 0; } diff --git a/include/etl/absolute.h b/include/etl/absolute.h index fc43717f..bfa68642 100644 --- a/include/etl/absolute.h +++ b/include/etl/absolute.h @@ -32,9 +32,9 @@ SOFTWARE. #define ETL_ABSOLUTE_INCLUDED #include "platform.h" -#include "type_traits.h" -#include "integral_limits.h" #include "error_handler.h" +#include "integral_limits.h" +#include "type_traits.h" namespace etl { @@ -52,26 +52,20 @@ namespace etl ETL_ASSERT_FAIL(ETL_ERROR_GENERIC("absolute value of minimum signed integer is undefined")); return T(0); } - } + } // namespace private_absolute //*************************************************************************** // For signed types. //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR - typename etl::enable_if::value && etl::is_integral::value, T>::type - absolute(T value) + ETL_NODISCARD ETL_CONSTEXPR typename etl::enable_if< etl::is_signed::value && etl::is_integral::value, T>::type absolute(T value) { - return (value == etl::integral_limits::min) ? etl::private_absolute::signed_min_error() - : static_cast((value < T(0)) ? -value : value); + return (value == etl::integral_limits::min) ? etl::private_absolute::signed_min_error() : static_cast((value < T(0)) ? -value : value); } template - ETL_NODISCARD - ETL_CONSTEXPR - typename etl::enable_if::value && !etl::is_integral::value, T>::type - absolute(T value) ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR typename etl::enable_if< etl::is_signed::value && !etl::is_integral::value, T>::type absolute(T value) + ETL_NOEXCEPT { return (value < T(0)) ? -value : value; } @@ -80,10 +74,7 @@ namespace etl // For unsigned types. //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR - typename etl::enable_if::value, T>::type - absolute(T value) ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR typename etl::enable_if::value, T>::type absolute(T value) ETL_NOEXCEPT { return value; } @@ -93,15 +84,14 @@ namespace etl // Returns the result as the unsigned type. //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR - typename etl::enable_if::value, typename etl::make_unsigned::type>::type - absolute_unsigned(T value) ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR typename etl::enable_if::value, typename etl::make_unsigned::type>::type absolute_unsigned(T value) + ETL_NOEXCEPT { typedef typename etl::make_unsigned::type TReturn; return (value == etl::integral_limits::min) ? (etl::integral_limits::max / 2U) + 1U - : (value < T(0)) ? TReturn(-value) : TReturn(value); + : (value < T(0)) ? TReturn(-value) + : TReturn(value); } //*************************************************************************** @@ -109,14 +99,10 @@ namespace etl // Returns the result as the unsigned type. //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR - typename etl::enable_if::value, T>::type - absolute_unsigned(T value) ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR typename etl::enable_if::value, T>::type absolute_unsigned(T value) ETL_NOEXCEPT { return etl::absolute(value); } -} +} // namespace etl #endif - diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 3843ea9b..0a4348a2 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -39,16 +39,16 @@ SOFTWARE. ///\ingroup utilities #include "platform.h" -#include "type_traits.h" -#include "iterator.h" -#include "functional.h" -#include "utility.h" -#include "largest.h" -#include "gcd.h" #include "error_handler.h" #include "exception.h" -#include "ranges.h" +#include "functional.h" +#include "gcd.h" #include "invoke.h" +#include "iterator.h" +#include "largest.h" +#include "ranges.h" +#include "type_traits.h" +#include "utility.h" #include #include @@ -57,10 +57,10 @@ SOFTWARE. #if ETL_USING_STL #include - #include - #include #include + #include #include + #include #endif namespace etl @@ -68,11 +68,12 @@ namespace etl // Declare prototypes of the ETL's sort functions template #if ETL_USING_STD_NAMESPACE - ETL_CONSTEXPR20 + ETL_CONSTEXPR20 #else ETL_CONSTEXPR14 #endif - void shell_sort(TIterator first, TIterator last); + void + shell_sort(TIterator first, TIterator last); template #if ETL_USING_STD_NAMESPACE @@ -80,7 +81,8 @@ namespace etl #else ETL_CONSTEXPR14 #endif - void shell_sort(TIterator first, TIterator last, TCompare compare); + void + shell_sort(TIterator first, TIterator last, TCompare compare); template ETL_CONSTEXPR14 void insertion_sort(TIterator first, TIterator last); @@ -108,7 +110,7 @@ namespace etl } }; -} +} // namespace etl //***************************************************************************** // Algorithms defined by the ETL @@ -128,8 +130,8 @@ namespace etl static void do_swap(TIterator1 a, TIterator2 b) { typename etl::iterator_traits::value_type tmp = *a; - *a = *b; - *b = tmp; + *a = *b; + *b = tmp; } }; @@ -144,7 +146,7 @@ namespace etl swap(*a, *b); } }; - } + } // namespace private_algorithm //*************************************************************************** // iter_swap @@ -155,7 +157,8 @@ namespace etl #else ETL_CONSTEXPR14 #endif - void iter_swap(TIterator1 a, TIterator2 b) + void + iter_swap(TIterator1 a, TIterator2 b) { typedef etl::iterator_traits traits1; typedef etl::iterator_traits traits2; @@ -166,9 +169,7 @@ namespace etl typedef typename traits1::reference r1; typedef typename traits2::reference r2; - const bool use_swap = etl::is_same::value && - etl::is_reference::value && - etl::is_reference::value; + const bool use_swap = etl::is_same::value && etl::is_reference::value && etl::is_reference::value; private_algorithm::swap_impl::do_swap(a, b); } @@ -182,9 +183,8 @@ namespace etl #else ETL_CONSTEXPR14 #endif - TIterator2 swap_ranges(TIterator1 first1, - TIterator1 last1, - TIterator2 first2) + TIterator2 + swap_ranges(TIterator1 first1, TIterator1 last1, TIterator2 first2) { while (first1 != last1) { @@ -199,8 +199,7 @@ namespace etl //*************************************************************************** // generate template - ETL_CONSTEXPR14 - void generate(TIterator db, TIterator de, TFunction funct) + ETL_CONSTEXPR14 void generate(TIterator db, TIterator de, TFunction funct) { while (db != de) { @@ -210,7 +209,7 @@ namespace etl //*************************************************************************** // copy -#if ETL_USING_STL && ETL_USING_CPP20 +#if ETL_USING_STL && ETL_USING_CPP20 // Use the STL constexpr implementation. template constexpr TIterator2 copy(TIterator1 sb, TIterator1 se, TIterator2 db) @@ -243,8 +242,7 @@ namespace etl } #else template - ETL_CONSTEXPR14 - TIterator2 reverse_copy(TIterator1 sb, TIterator1 se, TIterator2 db) + ETL_CONSTEXPR14 TIterator2 reverse_copy(TIterator1 sb, TIterator1 se, TIterator2 db) { while (sb != se) { @@ -340,9 +338,9 @@ namespace etl template ETL_CONSTEXPR20 TIterator2 move_backward(TIterator1 sb, TIterator1 se, TIterator2 de) { -#include "etl/private/diagnostic_array_bounds_push.h" + #include "etl/private/diagnostic_array_bounds_push.h" return std::move_backward(sb, se, de); -#include "etl/private/diagnostic_pop.h" + #include "etl/private/diagnostic_pop.h" } #elif ETL_USING_CPP11 // For C++11 @@ -370,9 +368,7 @@ namespace etl //*************************************************************************** // Pointers template - ETL_CONSTEXPR14 - typename etl::enable_if::value, void>::type - reverse(TIterator b, TIterator e) + ETL_CONSTEXPR14 typename etl::enable_if::value, void>::type reverse(TIterator b, TIterator e) { if (b != e) { @@ -386,9 +382,7 @@ namespace etl // Non-pointers template - ETL_CONSTEXPR14 - typename etl::enable_if::value, void>::type - reverse(TIterator b, TIterator e) + ETL_CONSTEXPR14 typename etl::enable_if::value, void>::type reverse(TIterator b, TIterator e) { while ((b != e) && (b != --e)) { @@ -399,10 +393,8 @@ namespace etl //*************************************************************************** // lower_bound //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator lower_bound(TIterator first, TIterator last, const TValue& value, TCompare compare) + template + ETL_NODISCARD ETL_CONSTEXPR14 TIterator lower_bound(TIterator first, TIterator last, const TValue& value, TCompare compare) { typedef typename etl::iterator_traits::difference_type difference_t; @@ -410,7 +402,7 @@ namespace etl while (count > 0) { - TIterator itr = first; + TIterator itr = first; difference_t step = count / 2; etl::advance(itr, step); @@ -429,10 +421,8 @@ namespace etl return first; } - template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator lower_bound(TIterator first, TIterator last, const TValue& value) + template + ETL_NODISCARD ETL_CONSTEXPR14 TIterator lower_bound(TIterator first, TIterator last, const TValue& value) { typedef etl::less::value_type> compare; @@ -442,10 +432,8 @@ namespace etl //*************************************************************************** // upper_bound //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator upper_bound(TIterator first, TIterator last, const TValue& value, TCompare compare) + template + ETL_NODISCARD ETL_CONSTEXPR14 TIterator upper_bound(TIterator first, TIterator last, const TValue& value, TCompare compare) { typedef typename etl::iterator_traits::difference_type difference_t; @@ -453,7 +441,7 @@ namespace etl while (count > 0) { - TIterator itr = first; + TIterator itr = first; difference_t step = count / 2; etl::advance(itr, step); @@ -472,10 +460,8 @@ namespace etl return first; } - template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator upper_bound(TIterator first, TIterator last, const TValue& value) + template + ETL_NODISCARD ETL_CONSTEXPR14 TIterator upper_bound(TIterator first, TIterator last, const TValue& value) { typedef etl::less::value_type> compare; @@ -485,32 +471,26 @@ namespace etl //*************************************************************************** // equal_range //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - ETL_OR_STD::pair equal_range(TIterator first, TIterator last, const TValue& value, TCompare compare) + template + ETL_NODISCARD ETL_CONSTEXPR14 ETL_OR_STD::pair equal_range(TIterator first, TIterator last, const TValue& value, TCompare compare) { - return ETL_OR_STD::make_pair(etl::lower_bound(first, last, value, compare), - etl::upper_bound(first, last, value, compare)); + return ETL_OR_STD::make_pair(etl::lower_bound(first, last, value, compare), etl::upper_bound(first, last, value, compare)); } - template + template ETL_NODISCARD ETL_OR_STD::pair equal_range(TIterator first, TIterator last, const TValue& value) { typedef etl::less::value_type> compare; - return ETL_OR_STD::make_pair(etl::lower_bound(first, last, value, compare()), - etl::upper_bound(first, last, value, compare())); + return ETL_OR_STD::make_pair(etl::lower_bound(first, last, value, compare()), etl::upper_bound(first, last, value, compare())); } //*************************************************************************** // binary_search //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool binary_search(TIterator first, TIterator last, const T& value, Compare compare) + ETL_NODISCARD ETL_CONSTEXPR14 bool binary_search(TIterator first, TIterator last, const T& value, Compare compare) { first = etl::lower_bound(first, last, value, compare); @@ -518,9 +498,7 @@ namespace etl } template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool binary_search(TIterator first, TIterator last, const T& value) + ETL_NODISCARD ETL_CONSTEXPR14 bool binary_search(TIterator first, TIterator last, const T& value) { typedef etl::less::value_type> compare; @@ -531,9 +509,7 @@ namespace etl // find_if //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator find_if(TIterator first, TIterator last, TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator find_if(TIterator first, TIterator last, TUnaryPredicate predicate) { while (first != last) { @@ -552,9 +528,7 @@ namespace etl // find //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator find(TIterator first, TIterator last, const T& value) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator find(TIterator first, TIterator last, const T& value) { while (first != last) { @@ -572,13 +546,13 @@ namespace etl //*************************************************************************** // fill #if ETL_USING_STL && ETL_USING_CPP20 - template + template constexpr void fill(TIterator first, TIterator last, const TValue& value) { std::fill(first, last, value); } #else - template + template ETL_CONSTEXPR14 void fill(TIterator first, TIterator last, const TValue& value) { while (first != last) @@ -592,13 +566,13 @@ namespace etl //*************************************************************************** // fill_n #if ETL_USING_STL && ETL_USING_CPP20 - template + template constexpr TIterator fill_n(TIterator first, TSize count, const TValue& value) { return std::fill_n(first, count, value); } #else - template + template ETL_CONSTEXPR14 TIterator fill_n(TIterator first, TSize count, const TValue& value) { while (count != 0) @@ -615,9 +589,7 @@ namespace etl // count //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - typename etl::iterator_traits::difference_type count(TIterator first, TIterator last, const T& value) + ETL_NODISCARD ETL_CONSTEXPR14 typename etl::iterator_traits::difference_type count(TIterator first, TIterator last, const T& value) { typename iterator_traits::difference_type n = 0; @@ -638,10 +610,7 @@ namespace etl // count_if //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - typename etl::iterator_traits::difference_type - count_if(TIterator first, TIterator last, TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 typename etl::iterator_traits::difference_type count_if(TIterator first, TIterator last, TUnaryPredicate predicate) { typename iterator_traits::difference_type n = 0; @@ -664,8 +633,7 @@ namespace etl // Three parameter template [[nodiscard]] - constexpr - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2) + constexpr bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2) { return std::equal(first1, last1, first2); } @@ -673,8 +641,7 @@ namespace etl // Three parameter + predicate template [[nodiscard]] - constexpr - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TPredicate predicate) + constexpr bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TPredicate predicate) { return std::equal(first1, last1, first2, predicate); } @@ -682,8 +649,7 @@ namespace etl // Four parameter template [[nodiscard]] - constexpr - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2) + constexpr bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2) { return std::equal(first1, last1, first2, last2); } @@ -691,8 +657,7 @@ namespace etl // Four parameter + Predicate template [[nodiscard]] - constexpr - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2, TPredicate predicate) + constexpr bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2, TPredicate predicate) { return std::equal(first1, last1, first2, last2, predicate); } @@ -700,9 +665,7 @@ namespace etl #else template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2) + ETL_NODISCARD ETL_CONSTEXPR14 bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2) { while (first1 != last1) { @@ -720,9 +683,7 @@ namespace etl // Predicate template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TPredicate predicate) { while (first1 != last1) { @@ -740,9 +701,7 @@ namespace etl // Four parameter template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2) + ETL_NODISCARD ETL_CONSTEXPR14 bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2) { while ((first1 != last1) && (first2 != last2)) { @@ -760,13 +719,11 @@ namespace etl // Four parameter, Predicate template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2, TPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool equal(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2, TPredicate predicate) { while ((first1 != last1) && (first2 != last2)) { - if (!predicate(*first1 , *first2)) + if (!predicate(*first1, *first2)) { return false; } @@ -783,11 +740,7 @@ namespace etl // lexicographical_compare //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool lexicographical_compare(TIterator1 first1, TIterator1 last1, - TIterator2 first2, TIterator2 last2, - TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 bool lexicographical_compare(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2, TCompare compare) { while ((first1 != last1) && (first2 != last2)) { @@ -810,10 +763,7 @@ namespace etl // lexicographical_compare template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool lexicographical_compare(TIterator1 first1, TIterator1 last1, - TIterator2 first2, TIterator2 last2) + ETL_NODISCARD ETL_CONSTEXPR14 bool lexicographical_compare(TIterator1 first1, TIterator1 last1, TIterator2 first2, TIterator2 last2) { typedef etl::less::value_type> compare; @@ -824,17 +774,13 @@ namespace etl // min //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR - const T& min(const T& a, const T& b, TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR const T& min(const T& a, const T& b, TCompare compare) { return (compare(a, b)) ? a : b; } template - ETL_NODISCARD - ETL_CONSTEXPR - const T& min(const T& a, const T& b) + ETL_NODISCARD ETL_CONSTEXPR const T& min(const T& a, const T& b) { typedef etl::less compare; @@ -845,17 +791,13 @@ namespace etl // max //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR - const T& max(const T& a, const T& b, TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR const T& max(const T& a, const T& b, TCompare compare) { return (compare(a, b)) ? b : a; } template - ETL_NODISCARD - ETL_CONSTEXPR - const T& max(const T& a, const T& b) + ETL_NODISCARD ETL_CONSTEXPR const T& max(const T& a, const T& b) { typedef etl::less compare; @@ -866,8 +808,7 @@ namespace etl // for_each //*************************************************************************** template - ETL_CONSTEXPR14 - TUnaryOperation for_each(TIterator first, TIterator last, TUnaryOperation unary_operation) + ETL_CONSTEXPR14 TUnaryOperation for_each(TIterator first, TIterator last, TUnaryOperation unary_operation) { while (first != last) { @@ -882,8 +823,7 @@ namespace etl // transform //*************************************************************************** template - ETL_CONSTEXPR14 - TIteratorOut transform(TIteratorIn first1, TIteratorIn last1, TIteratorOut d_first, TUnaryOperation unary_operation) + ETL_CONSTEXPR14 TIteratorOut transform(TIteratorIn first1, TIteratorIn last1, TIteratorOut d_first, TUnaryOperation unary_operation) { while (first1 != last1) { @@ -897,8 +837,8 @@ namespace etl } template - ETL_CONSTEXPR14 - TIteratorOut transform(TIteratorIn1 first1, TIteratorIn1 last1, TIteratorIn2 first2, TIteratorOut d_first, TBinaryOperation binary_operation) + ETL_CONSTEXPR14 TIteratorOut transform(TIteratorIn1 first1, TIteratorIn1 last1, TIteratorIn2 first2, TIteratorOut d_first, + TBinaryOperation binary_operation) { while (first1 != last1) { @@ -960,8 +900,8 @@ namespace etl while ((value_index > top_index) && compare(first[parent], value)) { first[value_index] = ETL_MOVE(first[parent]); - value_index = parent; - parent = (value_index - 1) / 2; + value_index = parent; + parent = (value_index - 1) / 2; } first[value_index] = ETL_MOVE(value); @@ -972,7 +912,7 @@ namespace etl ETL_CONSTEXPR14 void adjust_heap(TIterator first, TDistance value_index, TDistance length, TValue value, TCompare compare) { TDistance top_index = value_index; - TDistance child2nd = (2 * value_index) + 2; + TDistance child2nd = (2 * value_index) + 2; while (child2nd < length) { @@ -982,14 +922,14 @@ namespace etl } first[value_index] = ETL_MOVE(first[child2nd]); - value_index = child2nd; - child2nd = 2 * (child2nd + 1); + value_index = child2nd; + child2nd = 2 * (child2nd + 1); } if (child2nd == length) { first[value_index] = ETL_MOVE(first[child2nd - 1]); - value_index = child2nd - 1; + value_index = child2nd - 1; } push_heap(first, value_index, top_index, ETL_MOVE(value), compare); @@ -1016,26 +956,24 @@ namespace etl return true; } - } + } // namespace private_heap // Pop Heap template - ETL_CONSTEXPR14 - void pop_heap(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 void pop_heap(TIterator first, TIterator last, TCompare compare) { - typedef typename etl::iterator_traits::value_type value_t; + typedef typename etl::iterator_traits::value_type value_t; typedef typename etl::iterator_traits::difference_type distance_t; value_t value = ETL_MOVE(last[-1]); - last[-1] = ETL_MOVE(first[0]); + last[-1] = ETL_MOVE(first[0]); private_heap::adjust_heap(first, distance_t(0), distance_t(last - first - 1), ETL_MOVE(value), compare); } // Pop Heap template - ETL_CONSTEXPR14 - void pop_heap(TIterator first, TIterator last) + ETL_CONSTEXPR14 void pop_heap(TIterator first, TIterator last) { typedef etl::less::value_type> compare; @@ -1044,8 +982,7 @@ namespace etl // Push Heap template - ETL_CONSTEXPR14 - void push_heap(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 void push_heap(TIterator first, TIterator last, TCompare compare) { typedef typename etl::iterator_traits::difference_type difference_t; typedef typename etl::iterator_traits::value_type value_t; @@ -1055,8 +992,7 @@ namespace etl // Push Heap template - ETL_CONSTEXPR14 - void push_heap(TIterator first, TIterator last) + ETL_CONSTEXPR14 void push_heap(TIterator first, TIterator last) { typedef etl::less::value_type> compare; @@ -1065,8 +1001,7 @@ namespace etl // Make Heap template - ETL_CONSTEXPR14 - void make_heap(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 void make_heap(TIterator first, TIterator last, TCompare compare) { typedef typename etl::iterator_traits::difference_type difference_t; @@ -1093,8 +1028,7 @@ namespace etl // Make Heap template - ETL_CONSTEXPR14 - void make_heap(TIterator first, TIterator last) + ETL_CONSTEXPR14 void make_heap(TIterator first, TIterator last) { typedef etl::less::value_type> compare; @@ -1103,9 +1037,7 @@ namespace etl // Is Heap template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_heap(TIterator first, TIterator last) + ETL_NODISCARD ETL_CONSTEXPR14 bool is_heap(TIterator first, TIterator last) { typedef etl::less::value_type> compare; @@ -1114,17 +1046,14 @@ namespace etl // Is Heap template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_heap(TIterator first, TIterator last, TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 bool is_heap(TIterator first, TIterator last, TCompare compare) { return private_heap::is_heap(first, last - first, compare); } // Sort Heap template - ETL_CONSTEXPR14 - void sort_heap(TIterator first, TIterator last) + ETL_CONSTEXPR14 void sort_heap(TIterator first, TIterator last) { while (first != last) { @@ -1135,8 +1064,7 @@ namespace etl // Sort Heap template - ETL_CONSTEXPR14 - void sort_heap(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 void sort_heap(TIterator first, TIterator last, TCompare compare) { while (first != last) { @@ -1148,11 +1076,10 @@ namespace etl //*************************************************************************** /// partial_sort ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/partial_sort"> //*************************************************************************** template - ETL_CONSTEXPR14 - void partial_sort(TIterator first, TIterator middle, TIterator last, TCompare compare) + ETL_CONSTEXPR14 void partial_sort(TIterator first, TIterator middle, TIterator last, TCompare compare) { if (first == middle) { @@ -1160,7 +1087,7 @@ namespace etl } typedef typename etl::iterator_traits::value_type value_t; - typedef typename etl::iterator_traits::difference_type difference_t; + typedef typename etl::iterator_traits::difference_type difference_t; etl::make_heap(first, middle, compare); @@ -1169,7 +1096,7 @@ namespace etl if (compare(*i, *first)) { value_t value = ETL_MOVE(*i); - *i = ETL_MOVE(*first); + *i = ETL_MOVE(*first); private_heap::adjust_heap(first, difference_t(0), difference_t(middle - first), ETL_MOVE(value), compare); } @@ -1181,11 +1108,10 @@ namespace etl //*************************************************************************** /// partial_sort ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/partial_sort"> //*************************************************************************** template - ETL_CONSTEXPR14 - void partial_sort(TIterator first, TIterator middle, TIterator last) + ETL_CONSTEXPR14 void partial_sort(TIterator first, TIterator middle, TIterator last) { typedef etl::less::value_type> compare; @@ -1195,18 +1121,15 @@ namespace etl //*************************************************************************** /// partial_sort_copy ///\ingroup algorithm - /// + ///< a + ///< href="http://en.cppreference.com/w/cpp/algorithm/partial_sort_copy"> //*************************************************************************** template - ETL_CONSTEXPR14 - TRandomAccessIterator partial_sort_copy(TInputIterator first, - TInputIterator last, - TRandomAccessIterator d_first, - TRandomAccessIterator d_last, - TCompare compare) + ETL_CONSTEXPR14 TRandomAccessIterator partial_sort_copy(TInputIterator first, TInputIterator last, TRandomAccessIterator d_first, + TRandomAccessIterator d_last, TCompare compare) { typedef typename etl::iterator_traits::value_type value_t; - typedef typename etl::iterator_traits::difference_type difference_t; + typedef typename etl::iterator_traits::difference_type difference_t; TRandomAccessIterator result = d_first; @@ -1244,16 +1167,14 @@ namespace etl //*************************************************************************** /// partial_sort_copy ///\ingroup algorithm - /// + ///< a + ///< href="http://en.cppreference.com/w/cpp/algorithm/partial_sort_copy"> //*************************************************************************** template - ETL_CONSTEXPR14 - TRandomAccessIterator partial_sort_copy(TInputIterator first, - TInputIterator last, - TRandomAccessIterator d_first, - TRandomAccessIterator d_last) + ETL_CONSTEXPR14 TRandomAccessIterator partial_sort_copy(TInputIterator first, TInputIterator last, TRandomAccessIterator d_first, + TRandomAccessIterator d_last) { - typedef etl::less::value_type> compare; + typedef etl::less< typename etl::iterator_traits::value_type> compare; return etl::partial_sort_copy(first, last, d_first, d_last, compare()); } @@ -1261,14 +1182,12 @@ namespace etl //*************************************************************************** // Search //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator1 search(TIterator1 first, TIterator1 last, TIterator2 search_first, TIterator2 search_last, TCompare compare) + template + ETL_NODISCARD ETL_CONSTEXPR14 TIterator1 search(TIterator1 first, TIterator1 last, TIterator2 search_first, TIterator2 search_last, TCompare compare) { while (true) { - TIterator1 itr = first; + TIterator1 itr = first; TIterator2 search_itr = search_first; while (true) @@ -1297,10 +1216,8 @@ namespace etl } // Search - template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator1 search(TIterator1 first, TIterator1 last, TIterator2 search_first, TIterator2 search_last) + template + ETL_NODISCARD ETL_CONSTEXPR14 TIterator1 search(TIterator1 first, TIterator1 last, TIterator2 search_first, TIterator2 search_last) { typedef etl::equal_to::value_type> compare; @@ -1315,9 +1232,8 @@ namespace etl //********************************* // For random access iterators template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type - rotate_general(TIterator first, TIterator middle, TIterator last) + ETL_CONSTEXPR14 typename etl::enable_if::value, TIterator>::type + rotate_general(TIterator first, TIterator middle, TIterator last) { if (first == middle) { @@ -1329,35 +1245,35 @@ namespace etl return first; } - typedef typename etl::iterator_traits::value_type value_type; + typedef typename etl::iterator_traits::value_type value_type; typedef typename etl::iterator_traits::difference_type difference_type; - difference_type n = last - first; - difference_type m = middle - first; + difference_type n = last - first; + difference_type m = middle - first; difference_type gcd_nm = (n == 0 || m == 0) ? n + m : etl::gcd(n, m); - TIterator result = first + (last - middle); + TIterator result = first + (last - middle); for (difference_type i = 0; i < gcd_nm; i++) { - value_type temp = ETL_MOVE(*(first + i)); - difference_type j = i; - - while (true) + value_type temp = ETL_MOVE(*(first + i)); + difference_type j = i; + + while (true) { difference_type k = j + m; - + if (k >= n) { k = k - n; } - + if (k == i) { break; } *(first + j) = ETL_MOVE(*(first + k)); - j = k; + j = k; } *(first + j) = ETL_MOVE(temp); @@ -1369,8 +1285,7 @@ namespace etl //********************************* // For bidirectional iterators template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type + ETL_CONSTEXPR14 typename etl::enable_if::value, TIterator>::type rotate_general(TIterator first, TIterator middle, TIterator last) { if (first == middle) @@ -1396,9 +1311,8 @@ namespace etl //********************************* // For forward iterators template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type - rotate_general(TIterator first, TIterator middle, TIterator last) + ETL_CONSTEXPR14 typename etl::enable_if::value, TIterator>::type rotate_general(TIterator first, TIterator middle, + TIterator last) { if (first == middle) { @@ -1410,7 +1324,7 @@ namespace etl return first; } - TIterator next = middle; + TIterator next = middle; TIterator result = first; etl::advance(result, etl::distance(middle, last)); @@ -1435,8 +1349,7 @@ namespace etl //********************************* // Simplified algorithm for rotate left by one template - ETL_CONSTEXPR14 - TIterator rotate_left_by_one(TIterator first, TIterator last) + ETL_CONSTEXPR14 TIterator rotate_left_by_one(TIterator first, TIterator last) { typedef typename etl::iterator_traits::value_type value_type; @@ -1456,13 +1369,12 @@ namespace etl //********************************* // Simplified for algorithm rotate right by one template - ETL_CONSTEXPR14 - TIterator rotate_right_by_one(TIterator first, TIterator last) + ETL_CONSTEXPR14 TIterator rotate_right_by_one(TIterator first, TIterator last) { typedef typename etl::iterator_traits::value_type value_type; // Save the last item. - TIterator previous = etl::prev(last); + TIterator previous = etl::prev(last); value_type temp(ETL_MOVE(*previous)); // Move the rest. @@ -1474,12 +1386,11 @@ namespace etl // The new position of the first item. return result; } - } + } // namespace private_algorithm //********************************* - template - ETL_CONSTEXPR14 - TIterator rotate(TIterator first, TIterator middle, TIterator last) + template + ETL_CONSTEXPR14 TIterator rotate(TIterator first, TIterator middle, TIterator last) { if (first == middle) { @@ -1498,7 +1409,7 @@ namespace etl #if ETL_USING_CPP20 if (etl::next(middle) == last) { - if ETL_IF_CONSTEXPR(etl::is_bidirectional_iterator_concept::value) + if ETL_IF_CONSTEXPR (etl::is_bidirectional_iterator_concept< TIterator>::value) { return private_algorithm::rotate_right_by_one(first, last); } @@ -1513,11 +1424,7 @@ namespace etl //*************************************************************************** // Predicate template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator1 find_end(TIterator1 b, TIterator1 e, - TIterator2 sb, TIterator2 se, - TPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator1 find_end(TIterator1 b, TIterator1 e, TIterator2 sb, TIterator2 se, TPredicate predicate) { if (sb == se) { @@ -1537,7 +1444,7 @@ namespace etl else { result = new_result; - b = result; + b = result; ++b; } } @@ -1546,10 +1453,7 @@ namespace etl // Default template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator1 find_end(TIterator1 b, TIterator1 e, - TIterator2 sb, TIterator2 se) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator1 find_end(TIterator1 b, TIterator1 e, TIterator2 sb, TIterator2 se) { typedef etl::equal_to::value_type> predicate; @@ -1558,15 +1462,11 @@ namespace etl //*************************************************************************** /// Finds the iterator to the smallest element in the range (begin, end).
- /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/min_element"> ///\ingroup algorithm //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator min_element(TIterator begin, - TIterator end, - TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator min_element(TIterator begin, TIterator end, TCompare compare) { TIterator minimum = begin; @@ -1591,13 +1491,10 @@ namespace etl //*************************************************************************** /// min_element ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/min_element"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator min_element(TIterator begin, - TIterator end) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator min_element(TIterator begin, TIterator end) { typedef typename etl::iterator_traits::value_type value_t; @@ -1606,15 +1503,11 @@ namespace etl //*************************************************************************** /// Finds the iterator to the largest element in the range (begin, end).
- /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/max_element"> ///\ingroup algorithm //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator max_element(TIterator begin, - TIterator end, - TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator max_element(TIterator begin, TIterator end, TCompare compare) { TIterator maximum = begin; @@ -1639,13 +1532,10 @@ namespace etl //*************************************************************************** /// max_element ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/max_element"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator max_element(TIterator begin, - TIterator end) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator max_element(TIterator begin, TIterator end) { typedef typename etl::iterator_traits::value_type value_t; @@ -1654,15 +1544,11 @@ namespace etl //*************************************************************************** /// Finds the greatest and the smallest element in the range (begin, end).
- /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/minmax_element"> ///\ingroup algorithm //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - ETL_OR_STD::pair minmax_element(TIterator begin, - TIterator end, - TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 ETL_OR_STD::pair minmax_element(TIterator begin, TIterator end, TCompare compare) { TIterator minimum = begin; TIterator maximum = begin; @@ -1693,13 +1579,10 @@ namespace etl //*************************************************************************** /// minmax_element ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/minmax_element"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - ETL_OR_STD::pair minmax_element(TIterator begin, - TIterator end) + ETL_NODISCARD ETL_CONSTEXPR14 ETL_OR_STD::pair minmax_element(TIterator begin, TIterator end) { typedef typename etl::iterator_traits::value_type value_t; @@ -1709,13 +1592,10 @@ namespace etl //*************************************************************************** /// minmax ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/minmax"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - ETL_OR_STD::pair minmax(const T& a, - const T& b) + ETL_NODISCARD ETL_CONSTEXPR14 ETL_OR_STD::pair minmax(const T& a, const T& b) { return (b < a) ? ETL_OR_STD::pair(b, a) : ETL_OR_STD::pair(a, b); } @@ -1723,14 +1603,10 @@ namespace etl //*************************************************************************** /// minmax ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/minmax"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - ETL_OR_STD::pair minmax(const T& a, - const T& b, - TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 ETL_OR_STD::pair minmax(const T& a, const T& b, TCompare compare) { return compare(b, a) ? ETL_OR_STD::pair(b, a) : ETL_OR_STD::pair(a, b); } @@ -1738,14 +1614,10 @@ namespace etl //*************************************************************************** /// is_sorted_until ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_sorted_until"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator is_sorted_until(TIterator begin, - TIterator end, - TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator is_sorted_until(TIterator begin, TIterator end, TCompare compare) { if (begin != end) { @@ -1768,13 +1640,10 @@ namespace etl //*************************************************************************** /// is_sorted_until ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_sorted_until"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator is_sorted_until(TIterator begin, - TIterator end) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator is_sorted_until(TIterator begin, TIterator end) { typedef etl::less::value_type> compare; @@ -1784,13 +1653,10 @@ namespace etl //*************************************************************************** /// is_sorted ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_sorted"> //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_sorted(TIterator begin, - TIterator end) + template + ETL_NODISCARD ETL_CONSTEXPR14 bool is_sorted(TIterator begin, TIterator end) { return etl::is_sorted_until(begin, end) == end; } @@ -1798,14 +1664,10 @@ namespace etl //*************************************************************************** /// is_sorted ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_sorted"> //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_sorted(TIterator begin, - TIterator end, - TCompare compare) + template + ETL_NODISCARD ETL_CONSTEXPR14 bool is_sorted(TIterator begin, TIterator end, TCompare compare) { return etl::is_sorted_until(begin, end, compare) == end; } @@ -1815,11 +1677,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator is_unique_sorted_until(TIterator begin, - TIterator end, - TCompare compare) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator is_unique_sorted_until(TIterator begin, TIterator end, TCompare compare) { if (begin != end) { @@ -1844,10 +1702,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator is_unique_sorted_until(TIterator begin, - TIterator end) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator is_unique_sorted_until(TIterator begin, TIterator end) { typedef etl::less::value_type> compare; @@ -1858,11 +1713,8 @@ namespace etl /// is_unique_sorted ///\ingroup algorithm //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_unique_sorted(TIterator begin, - TIterator end) + template + ETL_NODISCARD ETL_CONSTEXPR14 bool is_unique_sorted(TIterator begin, TIterator end) { return etl::is_unique_sorted_until(begin, end) == end; } @@ -1871,12 +1723,8 @@ namespace etl /// is_unique_sorted ///\ingroup algorithm //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_unique_sorted(TIterator begin, - TIterator end, - TCompare compare) + template + ETL_NODISCARD ETL_CONSTEXPR14 bool is_unique_sorted(TIterator begin, TIterator end, TCompare compare) { return etl::is_unique_sorted_until(begin, end, compare) == end; } @@ -1884,14 +1732,10 @@ namespace etl //*************************************************************************** /// find_if_not ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/find"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator find_if_not(TIterator begin, - TIterator end, - TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator find_if_not(TIterator begin, TIterator end, TUnaryPredicate predicate) { while (begin != end) { @@ -1909,12 +1753,10 @@ namespace etl //*************************************************************************** /// adjacent_find ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/adjacent_find"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator adjacent_find(TIterator first, TIterator last, TBinaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator adjacent_find(TIterator first, TIterator last, TBinaryPredicate predicate) { if (first != last) { @@ -1939,12 +1781,10 @@ namespace etl //*************************************************************************** /// adjacent_find ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/adjacent_find"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator adjacent_find(TIterator first, TIterator last) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator adjacent_find(TIterator first, TIterator last) { typedef etl::equal_to::value_type> predicate; @@ -1954,14 +1794,10 @@ namespace etl //*************************************************************************** /// is_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_permutation"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_permutation(TIterator1 begin1, - TIterator1 end1, - TIterator2 begin2) + ETL_NODISCARD ETL_CONSTEXPR14 bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2) { if (begin1 != end1) { @@ -1989,15 +1825,10 @@ namespace etl //*************************************************************************** /// is_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_permutation"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_permutation(TIterator1 begin1, - TIterator1 end1, - TIterator2 begin2, - TBinaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2, TBinaryPredicate predicate) { if (begin1 != end1) { @@ -2026,15 +1857,10 @@ namespace etl //*************************************************************************** /// is_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_permutation"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_permutation(TIterator1 begin1, - TIterator1 end1, - TIterator2 begin2, - TIterator2 end2) + ETL_NODISCARD ETL_CONSTEXPR14 bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2, TIterator2 end2) { if (etl::distance(begin1, end1) != etl::distance(begin2, end2)) { @@ -2063,16 +1889,10 @@ namespace etl //*************************************************************************** /// is_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_permutation"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_permutation(TIterator1 begin1, - TIterator1 end1, - TIterator2 begin2, - TIterator2 end2, - TBinaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2, TIterator2 end2, TBinaryPredicate predicate) { if (etl::distance(begin1, end1) != etl::distance(begin2, end2)) { @@ -2102,11 +1922,10 @@ namespace etl //*************************************************************************** /// next_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/next_permutation"> //*************************************************************************** template - ETL_CONSTEXPR14 - bool next_permutation(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 bool next_permutation(TIterator first, TIterator last, TCompare compare) { if (first == last) { @@ -2152,11 +1971,10 @@ namespace etl //*************************************************************************** /// next_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/next_permutation"> //*************************************************************************** template - ETL_CONSTEXPR14 - bool next_permutation(TIterator first, TIterator last) + ETL_CONSTEXPR14 bool next_permutation(TIterator first, TIterator last) { typedef etl::less::value_type> compare; return etl::next_permutation(first, last, compare()); @@ -2165,11 +1983,10 @@ namespace etl //*************************************************************************** /// prev_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/prev_permutation"> //*************************************************************************** template - ETL_CONSTEXPR14 - bool prev_permutation(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 bool prev_permutation(TIterator first, TIterator last, TCompare compare) { if (first == last) { @@ -2215,11 +2032,10 @@ namespace etl //*************************************************************************** /// prev_permutation ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/prev_permutation"> //*************************************************************************** template - ETL_CONSTEXPR14 - bool prev_permutation(TIterator first, TIterator last) + ETL_CONSTEXPR14 bool prev_permutation(TIterator first, TIterator last) { typedef etl::less::value_type> compare; return etl::prev_permutation(first, last, compare()); @@ -2228,14 +2044,10 @@ namespace etl //*************************************************************************** /// is_partitioned ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/is_partitioned"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool is_partitioned(TIterator begin, - TIterator end, - TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool is_partitioned(TIterator begin, TIterator end, TUnaryPredicate predicate) { while (begin != end) { @@ -2262,20 +2074,16 @@ namespace etl //*************************************************************************** /// partition_point - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/partition_point"> ///\ingroup algorithm //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator partition_point(TIterator begin, - TIterator end, - TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator partition_point(TIterator begin, TIterator end, TUnaryPredicate predicate) { typedef typename etl::iterator_traits::difference_type difference_t; // binary search on a partitioned range - for (difference_t length = etl::distance(begin, end); 0 < length; ) + for (difference_t length = etl::distance(begin, end); 0 < length;) { difference_t half = length / 2; TIterator middle = etl::next(begin, half); @@ -2296,16 +2104,12 @@ namespace etl //*************************************************************************** /// Copies the elements from the range (begin, end) to two different ranges /// depending on the value returned by the predicate.
- /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/partition_copy"> ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - ETL_OR_STD::pair partition_copy(TSource begin, - TSource end, - TDestinationTrue destination_true, - TDestinationFalse destination_false, - TUnaryPredicate predicate) + ETL_CONSTEXPR14 ETL_OR_STD::pair partition_copy(TSource begin, TSource end, TDestinationTrue destination_true, + TDestinationFalse destination_false, TUnaryPredicate predicate) { while (begin != end) { @@ -2329,14 +2133,10 @@ namespace etl //*************************************************************************** /// copy_if ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/copy"> //*************************************************************************** template - ETL_CONSTEXPR14 - TOutputIterator copy_if(TIterator begin, - TIterator end, - TOutputIterator out, - TUnaryPredicate predicate) + ETL_CONSTEXPR14 TOutputIterator copy_if(TIterator begin, TIterator end, TOutputIterator out, TUnaryPredicate predicate) { while (begin != end) { @@ -2355,14 +2155,10 @@ namespace etl //*************************************************************************** /// all_of ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/all_any_none_of"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool all_of(TIterator begin, - TIterator end, - TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool all_of(TIterator begin, TIterator end, TUnaryPredicate predicate) { return etl::find_if_not(begin, end, predicate) == end; } @@ -2370,14 +2166,10 @@ namespace etl //*************************************************************************** /// any_of ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/all_any_none_of"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool any_of(TIterator begin, - TIterator end, - TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool any_of(TIterator begin, TIterator end, TUnaryPredicate predicate) { return etl::find_if(begin, end, predicate) != end; } @@ -2385,14 +2177,10 @@ namespace etl //*************************************************************************** /// none_of ///\ingroup algorithm - /// + ///< a href="http://en.cppreference.com/w/cpp/algorithm/all_any_none_of"> //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - bool none_of(TIterator begin, - TIterator end, - TUnaryPredicate predicate) + ETL_NODISCARD ETL_CONSTEXPR14 bool none_of(TIterator begin, TIterator end, TUnaryPredicate predicate) { return etl::find_if(begin, end, predicate) == end; } @@ -2492,15 +2280,14 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - T accumulate(TIterator first, TIterator last, T sum) + ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum) { while (first != last) { sum = static_cast(ETL_MOVE(sum) + static_cast(*first)); ++first; } - + return sum; } @@ -2509,8 +2296,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - T accumulate(TIterator first, TIterator last, T sum, TBinaryOperation operation) + ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum, TBinaryOperation operation) { while (first != last) { @@ -2525,30 +2311,26 @@ namespace etl /// Clamp values. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR - T clamp(const T& value, const T& low, const T& high, TCompare compare) + template + ETL_CONSTEXPR T clamp(const T& value, const T& low, const T& high, TCompare compare) { return compare(value, low) ? low : compare(high, value) ? high : value; } - + template - ETL_CONSTEXPR - T clamp(const T& value, const T& low, const T& high) + ETL_CONSTEXPR T clamp(const T& value, const T& low, const T& high) { return clamp(value, low, high, etl::less()); } - template - ETL_CONSTEXPR - T clamp(const T& value, TCompare compare) + template + ETL_CONSTEXPR T clamp(const T& value, TCompare compare) { return compare(value, Low) ? Low : compare(High, value) ? High : value; } template - ETL_CONSTEXPR - T clamp(const T& value) + ETL_CONSTEXPR T clamp(const T& value) { return clamp(value, etl::less()); } @@ -2558,9 +2340,8 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TIterator remove(TIterator first, TIterator last, const T& value) - { + ETL_CONSTEXPR14 TIterator remove(TIterator first, TIterator last, const T& value) + { first = etl::find(first, last, value); if (first != last) @@ -2575,7 +2356,7 @@ namespace etl } } } - + return first; } @@ -2584,8 +2365,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TIterator remove_if(TIterator first, TIterator last, TUnaryPredicate predicate) + ETL_CONSTEXPR14 TIterator remove_if(TIterator first, TIterator last, TUnaryPredicate predicate) { first = etl::find_if(first, last, predicate); @@ -2602,7 +2382,7 @@ namespace etl } } - return first; + return first; } //*************************************************************************** @@ -2611,8 +2391,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TIterator unique(TIterator first, TIterator last) + ETL_CONSTEXPR14 TIterator unique(TIterator first, TIterator last) { if (first == last) { @@ -2639,8 +2418,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TIterator unique(TIterator first, TIterator last, TBinaryPredicate predicate) + ETL_CONSTEXPR14 TIterator unique(TIterator first, TIterator last, TBinaryPredicate predicate) { if (first == last) { @@ -2666,10 +2444,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TOutputIterator unique_copy(TInputIterator first, - TInputIterator last, - TOutputIterator d_first) + ETL_CONSTEXPR14 TOutputIterator unique_copy(TInputIterator first, TInputIterator last, TOutputIterator d_first) { if (first == last) { @@ -2677,13 +2452,13 @@ namespace etl } typename etl::iterator_traits::value_type prev = *first; - *d_first = prev; + *d_first = prev; while (++first != last) { if (!(prev == *first)) { - prev = *first; + prev = *first; *(++d_first) = prev; } } @@ -2698,11 +2473,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TOutputIterator unique_copy(TInputIterator first, - TInputIterator last, - TOutputIterator d_first, - TBinaryPredicate predicate) + ETL_CONSTEXPR14 TOutputIterator unique_copy(TInputIterator first, TInputIterator last, TOutputIterator d_first, TBinaryPredicate predicate) { if (first == last) { @@ -2710,13 +2481,13 @@ namespace etl } typename etl::iterator_traits::value_type prev = *first; - *d_first = prev; + *d_first = prev; while (++first != last) { if (!predicate(prev, *first)) { - prev = *first; + prev = *first; *(++d_first) = prev; } } @@ -2731,11 +2502,8 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TOutputIterator merge(TInputIterator1 first1, TInputIterator1 last1, - TInputIterator2 first2, TInputIterator2 last2, - TOutputIterator d_first, - TCompare compare) + ETL_CONSTEXPR14 TOutputIterator merge(TInputIterator1 first1, TInputIterator1 last1, TInputIterator2 first2, TInputIterator2 last2, + TOutputIterator d_first, TCompare compare) { while ((first1 != last1) && (first2 != last2)) { @@ -2766,12 +2534,10 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TOutputIterator merge(TInputIterator1 first1, TInputIterator1 last1, - TInputIterator2 first2, TInputIterator2 last2, - TOutputIterator d_first) + ETL_CONSTEXPR14 TOutputIterator merge(TInputIterator1 first1, TInputIterator1 last1, TInputIterator2 first2, TInputIterator2 last2, + TOutputIterator d_first) { - typedef etl::less::value_type> compare; + typedef etl::less< typename etl::iterator_traits::value_type> compare; return etl::merge(first1, last1, first2, last2, d_first, compare()); } @@ -2788,10 +2554,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - void inplace_merge(TBidirectionalIterator first, - TBidirectionalIterator middle, - TBidirectionalIterator last, - TCompare compare) + void inplace_merge(TBidirectionalIterator first, TBidirectionalIterator middle, TBidirectionalIterator last, TCompare compare) { typedef typename etl::iterator_traits::difference_type difference_type; @@ -2800,10 +2563,11 @@ namespace etl while ((len1 != 0) && (len2 != 0)) { - // Find where the first element of the right half belongs in the left half. - // All elements in [first, cut1) are <= *middle, so they are already in place. - TBidirectionalIterator cut1 = etl::upper_bound(first, middle, *middle, compare); - difference_type prefix = etl::distance(first, cut1); + // Find where the first element of the right half belongs in the left + // half. All elements in [first, cut1) are <= *middle, so they are already + // in place. + TBidirectionalIterator cut1 = etl::upper_bound(first, middle, *middle, compare); + difference_type prefix = etl::distance(first, cut1); len1 -= prefix; // If the entire left half is <= *middle, we are done. @@ -2819,7 +2583,7 @@ namespace etl // the right half. All elements in [middle, cut2) are < *first, so they // need to be moved before *first. TBidirectionalIterator cut2 = etl::lower_bound(middle, last, *first, compare); - difference_type run = etl::distance(middle, cut2); + difference_type run = etl::distance(middle, cut2); len2 -= run; // Rotate the block [first, middle, cut2) so that [middle, cut2) moves @@ -2843,15 +2607,13 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - void inplace_merge(TBidirectionalIterator first, - TBidirectionalIterator middle, - TBidirectionalIterator last) + void inplace_merge(TBidirectionalIterator first, TBidirectionalIterator middle, TBidirectionalIterator last) { - typedef etl::less::value_type> compare; + typedef etl::less< typename etl::iterator_traits::value_type> compare; etl::inplace_merge(first, middle, last, compare()); } -} +} // namespace etl //***************************************************************************** // ETL extensions to the STL algorithms. @@ -2869,27 +2631,22 @@ namespace etl ///\param o_end End of the output range. ///\ingroup algorithm //*************************************************************************** - template + template ETL_CONSTEXPR14 - typename etl::enable_if::value && - etl::is_random_iterator::value, TOutputIterator>::type - copy_s(TInputIterator i_begin, - TInputIterator i_end, - TOutputIterator o_begin, - TOutputIterator o_end) + typename etl::enable_if< etl::is_random_iterator::value && etl::is_random_iterator::value, TOutputIterator>::type + copy_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end) { typedef typename iterator_traits::difference_type s_size_type; typedef typename iterator_traits::difference_type d_size_type; #if ETL_USING_CPP11 - typedef typename etl::common_type::type min_size_type; + typedef typename etl::common_type::type min_size_type; #else - typedef typename etl::largest_type::type min_size_type; + typedef typename etl::largest_type::type min_size_type; #endif - s_size_type s_size = etl::distance(i_begin, i_end); - d_size_type d_size = etl::distance(o_begin, o_end); + s_size_type s_size = etl::distance(i_begin, i_end); + d_size_type d_size = etl::distance(o_begin, o_end); min_size_type min_size = etl::min(s_size, d_size); return etl::copy(i_begin, i_begin + min_size, o_begin); @@ -2906,15 +2663,10 @@ namespace etl ///\param o_end End of the output range. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - typename etl::enable_if::value || - !etl::is_random_iterator::value, TOutputIterator>::type - copy_s(TInputIterator i_begin, - TInputIterator i_end, - TOutputIterator o_begin, - TOutputIterator o_end) + template + ETL_CONSTEXPR14 typename etl::enable_if< !etl::is_random_iterator::value || !etl::is_random_iterator::value, + TOutputIterator>::type + copy_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end) { while ((i_begin != i_end) && (o_begin != o_end)) { @@ -2931,14 +2683,8 @@ namespace etl /// A safer form of copy_n where the smallest of the two ranges is used. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator copy_n_s(TInputIterator i_begin, - TSize n, - TOutputIterator o_begin, - TOutputIterator o_end) + template + ETL_CONSTEXPR14 TOutputIterator copy_n_s(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TOutputIterator o_end) { while ((n-- > 0) && (o_begin != o_end)) { @@ -2955,15 +2701,8 @@ namespace etl /// A safer form of copy_n where the smallest of the two ranges is used. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator copy_n_s(TInputIterator i_begin, - TSize1 n1, - TOutputIterator o_begin, - TSize2 n2) + template + ETL_CONSTEXPR14 TOutputIterator copy_n_s(TInputIterator i_begin, TSize1 n1, TOutputIterator o_begin, TSize2 n2) { while ((n1-- > 0) && (n2-- > 0)) { @@ -2977,19 +2716,13 @@ namespace etl //*************************************************************************** /// copy_if - /// A safer form of copy_if where it terminates when the first end iterator is reached. - /// There is currently no STL equivalent. + /// A safer form of copy_if where it terminates when the first end iterator is + /// reached. There is currently no STL equivalent. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator copy_if_s(TInputIterator i_begin, - TInputIterator i_end, - TOutputIterator o_begin, - TOutputIterator o_end, - TUnaryPredicate predicate) + template + ETL_CONSTEXPR14 TOutputIterator copy_if_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end, + TUnaryPredicate predicate) { while ((i_begin != i_end) && (o_begin != o_end)) { @@ -3009,16 +2742,9 @@ namespace etl /// copy_n_if /// Combination of copy_n and copy_if. ///\ingroup algorithm - //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator copy_n_if(TInputIterator i_begin, - TSize n, - TOutputIterator o_begin, - TUnaryPredicate predicate) + //*************************************************************************** + template + ETL_CONSTEXPR14 TOutputIterator copy_n_if(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TUnaryPredicate predicate) { while (n-- > 0) { @@ -3048,19 +2774,15 @@ namespace etl //*************************************************************************** template ETL_CONSTEXPR14 - typename etl::enable_if::value && - etl::is_random_iterator::value, TOutputIterator>::type - move_s(TInputIterator i_begin, - TInputIterator i_end, - TOutputIterator o_begin, - TOutputIterator o_end) + typename etl::enable_if< etl::is_random_iterator::value && etl::is_random_iterator::value, TOutputIterator>::type + move_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end) { - using s_size_type = typename iterator_traits::difference_type; - using d_size_type = typename iterator_traits::difference_type; + using s_size_type = typename iterator_traits::difference_type; + using d_size_type = typename iterator_traits::difference_type; using min_size_type = typename etl::common_type::type; - s_size_type s_size = etl::distance(i_begin, i_end); - d_size_type d_size = etl::distance(o_begin, o_end); + s_size_type s_size = etl::distance(i_begin, i_end); + d_size_type d_size = etl::distance(o_begin, o_end); min_size_type min_size = etl::min(s_size, d_size); return etl::move(i_begin, i_begin + min_size, o_begin); @@ -3078,13 +2800,9 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - typename etl::enable_if::value || - !etl::is_random_iterator::value, TOutputIterator>::type - move_s(TInputIterator i_begin, - TInputIterator i_end, - TOutputIterator o_begin, - TOutputIterator o_end) + ETL_CONSTEXPR14 typename etl::enable_if< !etl::is_random_iterator::value || !etl::is_random_iterator::value, + TOutputIterator>::type + move_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end) { while ((i_begin != i_end) && (o_begin != o_end)) { @@ -3109,10 +2827,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - TOutputIterator move_s(TInputIterator i_begin, - TInputIterator i_end, - TOutputIterator o_begin, - TOutputIterator o_end) + TOutputIterator move_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end) { // Move not supported. Defer to copy. return etl::copy_s(i_begin, i_end, o_begin, o_end); @@ -3122,14 +2837,11 @@ namespace etl //*************************************************************************** /// binary_find ///\ingroup algorithm - /// Does a binary search and returns an iterator to the value or end if not found. + /// Does a binary search and returns an iterator to the value or end if not + /// found. //*************************************************************************** template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator binary_find(TIterator begin, - TIterator end, - const TValue& value) + ETL_NODISCARD ETL_CONSTEXPR14 TIterator binary_find(TIterator begin, TIterator end, const TValue& value) { TIterator it = etl::lower_bound(begin, end, value); @@ -3144,19 +2856,11 @@ namespace etl //*************************************************************************** /// binary_find ///\ingroup algorithm - /// Does a binary search and returns an iterator to the value or end if not found. + /// Does a binary search and returns an iterator to the value or end if not + /// found. //*************************************************************************** - template - ETL_NODISCARD - ETL_CONSTEXPR14 - TIterator binary_find(TIterator begin, - TIterator end, - const TValue& value, - TBinaryPredicate predicate, - TBinaryEquality equality) + template + ETL_NODISCARD ETL_CONSTEXPR14 TIterator binary_find(TIterator begin, TIterator end, const TValue& value, TBinaryPredicate predicate, TBinaryEquality equality) { TIterator it = etl::lower_bound(begin, end, value, predicate); @@ -3172,14 +2876,8 @@ namespace etl /// Like std::for_each but applies a predicate before calling the function. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TUnaryFunction for_each_if(TIterator begin, - const TIterator end, - TUnaryFunction function, - TUnaryPredicate predicate) + template + ETL_CONSTEXPR14 TUnaryFunction for_each_if(TIterator begin, const TIterator end, TUnaryFunction function, TUnaryPredicate predicate) { while (begin != end) { @@ -3198,13 +2896,8 @@ namespace etl /// Like std::for_each but for 'n' iterations. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TIterator for_each_n(TIterator begin, - TSize n, - TUnaryFunction function) + template + ETL_CONSTEXPR14 TIterator for_each_n(TIterator begin, TSize n, TUnaryFunction function) { while (n-- > 0) { @@ -3216,18 +2909,12 @@ namespace etl } //*************************************************************************** - /// Like std::for_each but applies a predicate before calling the function, for 'n' iterations + /// Like std::for_each but applies a predicate before calling the function, + /// for 'n' iterations ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TIterator for_each_n_if(TIterator begin, - TSize n, - TUnaryFunction function, - TUnaryPredicate predicate) + template + ETL_CONSTEXPR14 TIterator for_each_n_if(TIterator begin, TSize n, TUnaryFunction function, TUnaryPredicate predicate) { while (n-- > 0) { @@ -3249,12 +2936,8 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - TOutputIterator transform_s(TInputIterator i_begin, - TInputIterator i_end, - TOutputIterator o_begin, - TOutputIterator o_end, - TUnaryFunction function) + ETL_CONSTEXPR14 TOutputIterator transform_s(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TOutputIterator o_end, + TUnaryFunction function) { while ((i_begin != i_end) && (o_begin != o_end)) { @@ -3272,15 +2955,8 @@ namespace etl /// There is currently no STL equivalent. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - void transform_n(TInputIterator i_begin, - TSize n, - TOutputIterator o_begin, - TUnaryFunction function) + template + ETL_CONSTEXPR14 void transform_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TUnaryFunction function) { TInputIterator i_end(i_begin); etl::advance(i_end, n); @@ -3294,17 +2970,8 @@ namespace etl /// There is currently no STL equivalent. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - void transform_n(TInputIterator1 i_begin1, - TInputIterator2 i_begin2, - TSize n, - TOutputIterator o_begin, - TBinaryFunction function) + template + ETL_CONSTEXPR14 void transform_n(TInputIterator1 i_begin1, TInputIterator2 i_begin2, TSize n, TOutputIterator o_begin, TBinaryFunction function) { TInputIterator1 i_end1(i_begin1); etl::advance(i_end1, n); @@ -3316,16 +2983,9 @@ namespace etl /// Like std::transform but applies a predicate before calling the function. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator transform_if(TInputIterator i_begin, - const TInputIterator i_end, - TOutputIterator o_begin, - TUnaryFunction function, - TUnaryPredicate predicate) + template + ETL_CONSTEXPR14 TOutputIterator transform_if(TInputIterator i_begin, const TInputIterator i_end, TOutputIterator o_begin, TUnaryFunction function, + TUnaryPredicate predicate) { while (i_begin != i_end) { @@ -3345,18 +3005,9 @@ namespace etl /// Like etl::transform_if but inputs from two ranges. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator transform_if(TInputIterator1 i_begin1, - const TInputIterator1 i_end1, - TInputIterator2 i_begin2, - TOutputIterator o_begin, - TBinaryFunction function, - TBinaryPredicate predicate) + template + ETL_CONSTEXPR14 TOutputIterator transform_if(TInputIterator1 i_begin1, const TInputIterator1 i_end1, TInputIterator2 i_begin2, TOutputIterator o_begin, + TBinaryFunction function, TBinaryPredicate predicate) { while (i_begin1 != i_end1) { @@ -3377,17 +3028,9 @@ namespace etl /// Like std::transform_if, for 'n' items. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator transform_n_if(TInputIterator i_begin, - TSize n, - TOutputIterator o_begin, - TUnaryFunction function, - TUnaryPredicate predicate) + template + ETL_CONSTEXPR14 TOutputIterator transform_n_if(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TUnaryFunction function, + TUnaryPredicate predicate) { while (n-- > 0) { @@ -3407,19 +3050,10 @@ namespace etl /// Like etl::transform_if but inputs from two ranges for 'n' items. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - TOutputIterator transform_n_if(TInputIterator1 i_begin1, - TInputIterator2 i_begin2, - TSize n, - TOutputIterator o_begin, - TBinaryFunction function, - TBinaryPredicate predicate) + ETL_CONSTEXPR14 TOutputIterator transform_n_if(TInputIterator1 i_begin1, TInputIterator2 i_begin2, TSize n, TOutputIterator o_begin, + TBinaryFunction function, TBinaryPredicate predicate) { while (n-- > 0) { @@ -3436,21 +3070,15 @@ namespace etl } //*************************************************************************** - /// Transforms the elements from the range (begin, end) to two different ranges - /// depending on the value returned by the predicate.
+ /// Transforms the elements from the range (begin, end) to two different + /// ranges depending on the value returned by the predicate.
///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - ETL_OR_STD::pair partition_transform(TSource begin, - TSource end, - TDestinationTrue destination_true, - TDestinationFalse destination_false, - TUnaryFunctionTrue function_true, - TUnaryFunctionFalse function_false, - TUnaryPredicate predicate) + ETL_CONSTEXPR14 ETL_OR_STD::pair + partition_transform(TSource begin, TSource end, TDestinationTrue destination_true, TDestinationFalse destination_false, + TUnaryFunctionTrue function_true, TUnaryFunctionFalse function_false, TUnaryPredicate predicate) { while (begin != end) { @@ -3476,22 +3104,11 @@ namespace etl /// to two different ranges depending on the value returned by the predicate. ///\ingroup algorithm //*************************************************************************** - template - ETL_CONSTEXPR14 - ETL_OR_STD::pair partition_transform(TSource1 begin1, - TSource1 end1, - TSource2 begin2, - TDestinationTrue destination_true, - TDestinationFalse destination_false, - TBinaryFunctionTrue function_true, - TBinaryFunctionFalse function_false, - TBinaryPredicate predicate) + template + ETL_CONSTEXPR14 ETL_OR_STD::pair + partition_transform(TSource1 begin1, TSource1 end1, TSource2 begin2, TDestinationTrue destination_true, TDestinationFalse destination_false, + TBinaryFunctionTrue function_true, TBinaryFunctionFalse function_false, TBinaryPredicate predicate) { while (begin1 != end1) { @@ -3524,7 +3141,8 @@ namespace etl #else ETL_CONSTEXPR14 #endif - void shell_sort(TIterator first, TIterator last, TCompare compare) + void + shell_sort(TIterator first, TIterator last, TCompare compare) { if (first == last) { @@ -3566,7 +3184,8 @@ namespace etl #else ETL_CONSTEXPR14 #endif - void shell_sort(TIterator first, TIterator last) + void + shell_sort(TIterator first, TIterator last) { etl::shell_sort(first, last, etl::less::value_type>()); } @@ -3577,8 +3196,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - void insertion_sort(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 void insertion_sort(TIterator first, TIterator last, TCompare compare) { for (TIterator itr = first; itr != last; ++itr) { @@ -3591,8 +3209,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - void insertion_sort(TIterator first, TIterator last) + ETL_CONSTEXPR14 void insertion_sort(TIterator first, TIterator last) { etl::insertion_sort(first, last, etl::less::value_type>()); } @@ -3601,9 +3218,7 @@ namespace etl namespace private_algorithm { template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type - get_before_last(TIterator first_, TIterator last_) + ETL_CONSTEXPR14 typename etl::enable_if::value, TIterator>::type get_before_last(TIterator first_, TIterator last_) { TIterator last = first_; TIterator lastplus1 = first_; @@ -3619,9 +3234,8 @@ namespace etl } template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type - get_before_last(TIterator /*first_*/, TIterator last_) + ETL_CONSTEXPR14 typename etl::enable_if::value, TIterator>::type get_before_last(TIterator /*first_*/, + TIterator last_) { TIterator last = last_; --last; @@ -3630,13 +3244,12 @@ namespace etl } template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type - get_before_last(TIterator /*first_*/, TIterator last_) + ETL_CONSTEXPR14 typename etl::enable_if::value, TIterator>::type get_before_last(TIterator /*first_*/, + TIterator last_) { return last_ - 1; } - } + } // namespace private_algorithm //*************************************************************************** /// Sorts the elements using selection sort. @@ -3644,22 +3257,21 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR20 - void selection_sort(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR20 void selection_sort(TIterator first, TIterator last, TCompare compare) { if (first == last) { return; } - TIterator min; + TIterator min; const TIterator ilast = private_algorithm::get_before_last(first, last); const TIterator jlast = last; for (TIterator i = first; i != ilast; ++i) { min = i; - + TIterator j = i; ++j; @@ -3684,8 +3296,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR20 - void selection_sort(TIterator first, TIterator last) + ETL_CONSTEXPR20 void selection_sort(TIterator first, TIterator last) { selection_sort(first, last, etl::less::value_type>()); } @@ -3696,8 +3307,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - void heap_sort(TIterator first, TIterator last, TCompare compare) + ETL_CONSTEXPR14 void heap_sort(TIterator first, TIterator last, TCompare compare) { etl::make_heap(first, last, compare); etl::sort_heap(first, last, compare); @@ -3708,8 +3318,7 @@ namespace etl ///\ingroup algorithm //*************************************************************************** template - ETL_CONSTEXPR14 - void heap_sort(TIterator first, TIterator last) + ETL_CONSTEXPR14 void heap_sort(TIterator first, TIterator last) { etl::make_heap(first, last); etl::sort_heap(first, last); @@ -3877,9 +3486,8 @@ namespace etl /// Does at most etl::distance(first, last) swaps. //*************************************************************************** template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type - partition(TIterator first, TIterator last, TPredicate predicate) + ETL_CONSTEXPR14 typename etl::enable_if::value, TIterator>::type partition(TIterator first, TIterator last, + TPredicate predicate) { first = etl::find_if_not(first, last, predicate); @@ -3907,9 +3515,8 @@ namespace etl /// Does at most (etl::distance(first, last) / 2) swaps. //*************************************************************************** template - ETL_CONSTEXPR14 - typename etl::enable_if::value, TIterator>::type - partition(TIterator first, TIterator last, TPredicate predicate) + ETL_CONSTEXPR14 typename etl::enable_if< etl::is_bidirectional_iterator_concept::value, TIterator>::type + partition(TIterator first, TIterator last, TPredicate predicate) { while (first != last) { @@ -3920,9 +3527,7 @@ namespace etl break; } - last = etl::find_if(etl::reverse_iterator(last), - etl::reverse_iterator(first), - predicate).base(); + last = etl::find_if(etl::reverse_iterator(last), etl::reverse_iterator(first), predicate).base(); if (first == last) { @@ -3947,7 +3552,8 @@ namespace etl #if (ETL_USING_CPP20 && ETL_USING_STL) || (ETL_USING_CPP14 && ETL_NOT_USING_STL && !defined(ETL_IN_UNIT_TEST)) constexpr #endif - TIterator nth_partition(TIterator first, TIterator last, TCompare compare) + TIterator + nth_partition(TIterator first, TIterator last, TCompare compare) { typedef typename etl::iterator_traits::value_type value_type; @@ -3957,7 +3563,8 @@ namespace etl for (TIterator j = first; j < last; ++j) { - if (!compare(pivot_value, *j)) // Hack to get '*j <= pivot_value' in terms of 'pivot_value < *j' + if (!compare(pivot_value, *j)) // Hack to get '*j <= pivot_value' in + // terms of 'pivot_value < *j' { swap(*i, *j); ++i; @@ -3968,7 +3575,7 @@ namespace etl return i; } - } + } // namespace private_algorithm //********************************************************* /// nth_element @@ -3976,10 +3583,10 @@ namespace etl //********************************************************* #if ETL_USING_CPP11 template ::value_type>> -#if (ETL_USING_CPP20 && ETL_USING_STL) || (ETL_USING_CPP14 && ETL_NOT_USING_STL && !defined(ETL_IN_UNIT_TEST)) + #if (ETL_USING_CPP20 && ETL_USING_STL) || (ETL_USING_CPP14 && ETL_NOT_USING_STL && !defined(ETL_IN_UNIT_TEST)) constexpr -#endif - typename etl::enable_if::value, void>::type + #endif + typename etl::enable_if< etl::is_random_access_iterator_concept::value, void>::type nth_element(TIterator first, TIterator nth, TIterator last, TCompare compare = TCompare()) { if (first == last) @@ -4013,8 +3620,8 @@ namespace etl //********************************************************* template - typename etl::enable_if::value, void>::type - nth_element(TIterator first, TIterator nth, TIterator last, TCompare compare) + typename etl::enable_if< etl::is_random_access_iterator_concept::value, void>::type nth_element(TIterator first, TIterator nth, + TIterator last, TCompare compare) { if (first == last) { @@ -4045,8 +3652,8 @@ namespace etl //********************************************************* template - typename etl::enable_if::value, void>::type - nth_element(TIterator first, TIterator nth, TIterator last) + typename etl::enable_if< etl::is_random_access_iterator_concept::value, void>::type nth_element(TIterator first, TIterator nth, + TIterator last) { typedef etl::less::value_type> compare_t; @@ -4056,574 +3663,533 @@ namespace etl #if ETL_USING_CPP17 -namespace ranges { - template - struct in_fun_result + namespace ranges { - ETL_NO_UNIQUE_ADDRESS I in; - ETL_NO_UNIQUE_ADDRESS F fun; - - template - constexpr operator in_fun_result() const& + template + struct in_fun_result { - return {in, fun}; - } + ETL_NO_UNIQUE_ADDRESS I in; + ETL_NO_UNIQUE_ADDRESS F fun; - template - constexpr operator in_fun_result() && - { - return {etl::move(in), etl::move(fun)}; - } - }; - - template - struct in_in_result - { - ETL_NO_UNIQUE_ADDRESS I1 in1; - ETL_NO_UNIQUE_ADDRESS I2 in2; - - template - constexpr operator in_in_result() const& - { - return {in1, in2}; - } - - template - constexpr operator in_in_result() && - { - return {etl::move(in1), etl::move(in2)}; - } - }; - - template - struct in_out_result - { - ETL_NO_UNIQUE_ADDRESS I in; - ETL_NO_UNIQUE_ADDRESS O out; - - template - constexpr operator in_out_result() const& - { - return {in, out}; - } - - template - constexpr operator in_out_result() && - { - return {etl::move(in), etl::move(out)}; - } - }; - - template - struct in_in_out_result - { - ETL_NO_UNIQUE_ADDRESS I1 in1; - ETL_NO_UNIQUE_ADDRESS I2 in2; - ETL_NO_UNIQUE_ADDRESS O out; - - template - constexpr operator in_in_out_result() const& - { - return {in1, in2, out}; - } - - template - constexpr operator in_in_out_result() && - { - return {etl::move(in1), etl::move(in2), etl::move(out)}; - } - }; - - template - struct in_out_out_result - { - ETL_NO_UNIQUE_ADDRESS I in; - ETL_NO_UNIQUE_ADDRESS O1 out1; - ETL_NO_UNIQUE_ADDRESS O2 out2; - - template - constexpr operator in_out_out_result() const& - { - return {in, out1, out2}; - } - - template - constexpr operator in_out_out_result() && - { - return {etl::move(in), etl::move(out1), etl::move(out2)}; - } - }; - - template - struct in_found_result - { - ETL_NO_UNIQUE_ADDRESS I in; - bool found; - - template - constexpr operator in_found_result() const& - { - return {in, found}; - } - - template - constexpr operator in_found_result() && - { - return {etl::move(in), found}; - } - }; - - template - struct out_value_result - { - ETL_NO_UNIQUE_ADDRESS O out; - ETL_NO_UNIQUE_ADDRESS T value; - - template - constexpr operator out_value_result() const& - { - return {out, value}; - } - - template - constexpr operator out_value_result() && - { - return {etl::move(out), etl::move(value)}; - } - }; - - template - using iota_result = out_value_result; - - template - using copy_result = in_out_result; - - template - using copy_n_result = in_out_result; - - template - using copy_if_result = in_out_result; - - template - using copy_backward_result = in_out_result; - - template - using uninitialized_copy_result = in_out_result; - - template - using uninitialized_copy_n_result = in_out_result; - - template - using uninitialized_move_result = in_out_result; - - template - using uninitialized_move_n_result = in_out_result; - - template - using move_result = in_out_result; - - template - using move_backward_result = in_out_result; - - template - using mismatch_result = in_in_result; - - template - using swap_ranges_result = in_in_result; - - template - using unary_transform_result = in_out_result; - - template - using binary_transform_result = in_in_out_result; - - template - using replace_copy_result = in_out_result; - - template - using replace_copy_if_result = in_out_result; - - template - using remove_copy_result = in_out_result; - - template - using unique_copy_result = in_out_result; - - template - using rotate_copy_result = in_out_result; - - template - using partial_sort_copy_result = in_out_result; - - template - using partition_copy_result = in_out_out_result; - - template - using set_union_result = in_in_out_result; - - template - using set_intersection_result = in_in_out_result; - - template - using set_difference_result = in_out_result; - - template - using set_symmetric_difference_result = in_in_out_result; - - template - using merge_result = in_in_out_result; - - template - using next_permutation_result = in_found_result; - - template - using prev_permutation_result = in_found_result; - - template - struct min_max_result - { - T min; - T max; - - template - constexpr operator min_max_result() const& - { - return {min, max}; - } - - template - constexpr operator min_max_result() && - { - return {etl::move(min), etl::move(max)}; - } - }; - - template - using minmax_result = min_max_result; - - template - using minmax_element_result = min_max_result; - - template - using for_each_result = in_fun_result; - - struct for_each_fn - { - template>> - constexpr ranges::for_each_result - operator()(I first, S last, Fun f, Proj proj = {}) const - { - for (; first != last; ++first) + template + constexpr operator in_fun_result() const& { - etl::invoke(f, etl::invoke(proj, *first)); + return {in, fun}; } - return {etl::move(first), etl::move(f)}; - } - template>> - constexpr ranges::for_each_result, Fun> - operator()(R&& r, Fun f, Proj proj = {}) const + template + constexpr operator in_fun_result() && + { + return {etl::move(in), etl::move(fun)}; + } + }; + + template + struct in_in_result { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(f), etl::ref(proj)); - } - }; + ETL_NO_UNIQUE_ADDRESS I1 in1; + ETL_NO_UNIQUE_ADDRESS I2 in2; - inline constexpr for_each_fn for_each; + template + constexpr operator in_in_result() const& + { + return {in1, in2}; + } - template - using for_each_n_result = in_fun_result; + template + constexpr operator in_in_result() && + { + return {etl::move(in1), etl::move(in2)}; + } + }; - struct for_each_n_fn - { - template>> - constexpr for_each_n_result - operator()(I first, etl::iter_difference_t n, Fun fun, Proj proj = Proj{}) const + template + struct in_out_result { + ETL_NO_UNIQUE_ADDRESS I in; + ETL_NO_UNIQUE_ADDRESS O out; + + template + constexpr operator in_out_result() const& + { + return {in, out}; + } + + template + constexpr operator in_out_result() && + { + return {etl::move(in), etl::move(out)}; + } + }; + + template + struct in_in_out_result + { + ETL_NO_UNIQUE_ADDRESS I1 in1; + ETL_NO_UNIQUE_ADDRESS I2 in2; + ETL_NO_UNIQUE_ADDRESS O out; + + template + constexpr operator in_in_out_result() const& + { + return {in1, in2, out}; + } + + template + constexpr operator in_in_out_result() && + { + return {etl::move(in1), etl::move(in2), etl::move(out)}; + } + }; + + template + struct in_out_out_result + { + ETL_NO_UNIQUE_ADDRESS I in; + ETL_NO_UNIQUE_ADDRESS O1 out1; + ETL_NO_UNIQUE_ADDRESS O2 out2; + + template + constexpr operator in_out_out_result() const& + { + return {in, out1, out2}; + } + + template + constexpr operator in_out_out_result() && + { + return {etl::move(in), etl::move(out1), etl::move(out2)}; + } + }; + + template + struct in_found_result + { + ETL_NO_UNIQUE_ADDRESS I in; + bool found; + + template + constexpr operator in_found_result() const& + { + return {in, found}; + } + + template + constexpr operator in_found_result() && + { + return {etl::move(in), found}; + } + }; + + template + struct out_value_result + { + ETL_NO_UNIQUE_ADDRESS O out; + ETL_NO_UNIQUE_ADDRESS T value; + + template + constexpr operator out_value_result() const& + { + return {out, value}; + } + + template + constexpr operator out_value_result() && + { + return {etl::move(out), etl::move(value)}; + } + }; + + template + using iota_result = out_value_result; + + template + using copy_result = in_out_result; + + template + using copy_n_result = in_out_result; + + template + using copy_if_result = in_out_result; + + template + using copy_backward_result = in_out_result; + + template + using uninitialized_copy_result = in_out_result; + + template + using uninitialized_copy_n_result = in_out_result; + + template + using uninitialized_move_result = in_out_result; + + template + using uninitialized_move_n_result = in_out_result; + + template + using move_result = in_out_result; + + template + using move_backward_result = in_out_result; + + template + using mismatch_result = in_in_result; + + template + using swap_ranges_result = in_in_result; + + template + using unary_transform_result = in_out_result; + + template + using binary_transform_result = in_in_out_result; + + template + using replace_copy_result = in_out_result; + + template + using replace_copy_if_result = in_out_result; + + template + using remove_copy_result = in_out_result; + + template + using unique_copy_result = in_out_result; + + template + using rotate_copy_result = in_out_result; + + template + using partial_sort_copy_result = in_out_result; + + template + using partition_copy_result = in_out_out_result; + + template + using set_union_result = in_in_out_result; + + template + using set_intersection_result = in_in_out_result; + + template + using set_difference_result = in_out_result; + + template + using set_symmetric_difference_result = in_in_out_result; + + template + using merge_result = in_in_out_result; + + template + using next_permutation_result = in_found_result; + + template + using prev_permutation_result = in_found_result; + + template + struct min_max_result + { + T min; + T max; + + template + constexpr operator min_max_result() const& + { + return {min, max}; + } + + template + constexpr operator min_max_result() && + { + return {etl::move(min), etl::move(max)}; + } + }; + + template + using minmax_result = min_max_result; + + template + using minmax_element_result = min_max_result; + + template + using for_each_result = in_fun_result; + + struct for_each_fn + { + template >> + constexpr ranges::for_each_result operator()(I first, S last, Fun f, Proj proj = {}) const + { + for (; first != last; ++first) + { + etl::invoke(f, etl::invoke(proj, *first)); + } + return {etl::move(first), etl::move(f)}; + } + + template >> + constexpr ranges::for_each_result, Fun> operator()(R&& r, Fun f, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(f), etl::ref(proj)); + } + }; + + inline constexpr for_each_fn for_each; + + template + using for_each_n_result = in_fun_result; + + struct for_each_n_fn + { + template >> + constexpr for_each_n_result operator()(I first, etl::iter_difference_t n, Fun fun, Proj proj = Proj{}) const + { for (; n-- > 0; ++first) { etl::invoke(fun, etl::invoke(proj, *first)); } return {etl::move(first), etl::move(fun)}; - } - }; - - inline constexpr for_each_n_fn for_each_n {}; - - struct find_fn - { - template>> - constexpr I operator()(I first, S last, const T& value, Proj proj = {}) const - { - for (; first != last; ++first) - { - if (etl::invoke(proj, *first) == value) - { - return first; - } } - return first; - } + }; - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, const T& value, Proj proj = {}) const + inline constexpr for_each_n_fn for_each_n{}; + + struct find_fn { - return (*this)(ranges::begin(r), ranges::end(r), value, etl::ref(proj)); - } - }; - - inline constexpr find_fn find; - - struct find_if_fn - { - template>> - constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const - { - for (; first != last; ++first) + template >> + constexpr I operator()(I first, S last, const T& value, Proj proj = {}) const { - if (etl::invoke(pred, etl::invoke(proj, *first))) + for (; first != last; ++first) { - return first; - } - } - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr find_if_fn find_if; - - struct find_if_not_fn - { - template>> - constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const - { - for (; first != last; ++first) - { - if (!etl::invoke(pred, etl::invoke(proj, *first))) - { - return first; - } - } - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr find_if_not_fn find_if_not; - - struct search_fn - { - template>> - constexpr ranges::subrange - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - for (;; ++first1) - { - I1 it1 = first1; - for (I2 it2 = first2;; ++it1, ++it2) - { - if (it2 == last2) + if (etl::invoke(proj, *first) == value) { - return {first1, it1}; - } - if (it1 == last1) - { - return {it1, it1}; - } - if (!etl::invoke(pred, etl::invoke(proj1, *it1), etl::invoke(proj2, *it2))) - { - break; + return first; } } - } - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr search_fn search {}; - - struct search_n_fn - { - template, - class Pred = ranges::equal_to, - class Proj = etl::identity, typename = etl::enable_if_t>> - constexpr ranges::subrange - operator()(I first, S last, etl::iter_difference_t count, const T& value, Pred pred = {}, - Proj proj = {}) const - { - if (count <= 0) - { - return {first, first}; + return first; } - for (; first != last; ++first) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, const T& value, Proj proj = {}) const { - if (etl::invoke(pred, etl::invoke(proj, *first), value)) + return (*this)(ranges::begin(r), ranges::end(r), value, etl::ref(proj)); + } + }; + + inline constexpr find_fn find; + + struct find_if_fn + { + template >> + constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const + { + for (; first != last; ++first) { - I start = first; - etl::iter_difference_t n = 1; - - for (;;) + if (etl::invoke(pred, etl::invoke(proj, *first))) { - if (n >= count) + return first; + } + } + return first; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Pred pred, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr find_if_fn find_if; + + struct find_if_not_fn + { + template >> + constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const + { + for (; first != last; ++first) + { + if (!etl::invoke(pred, etl::invoke(proj, *first))) + { + return first; + } + } + return first; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Pred pred, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr find_if_not_fn find_if_not; + + struct search_fn + { + template >> + constexpr ranges::subrange operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + for (;; ++first1) + { + I1 it1 = first1; + for (I2 it2 = first2;; ++it1, ++it2) + { + if (it2 == last2) { - return {start, ++first}; + return {first1, it1}; } - ++first; - if (first == last) + if (it1 == last1) { - return {first, first}; + return {it1, it1}; } - if (!etl::invoke(pred, etl::invoke(proj, *first), value)) + if (!etl::invoke(pred, etl::invoke(proj1, *it1), etl::invoke(proj2, *it2))) { break; } - ++n; } } } - return {first, first}; - } - template, - class Pred = ranges::equal_to, - class Proj = etl::identity, typename = etl::enable_if_t>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, ranges::range_difference_t count, const T& value, Pred pred = {}, - Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), - count, value, - etl::move(pred), etl::move(proj)); - } - }; - - inline constexpr search_n_fn search_n {}; - - struct find_end_fn - { - template>> - constexpr ranges::subrange - operator()(I1 first1, S1 last1, - I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - if (first2 == last2) + template >> + constexpr ranges::borrowed_subrange_t operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { - auto last_it = ranges::next(first1, last1); - return {last_it, last_it}; + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); } - auto result = ranges::search(etl::move(first1), last1, first2, last2, pred, proj1, proj2); + }; - if (result.empty()) + inline constexpr search_fn search{}; + + struct search_n_fn + { + template , class Pred = ranges::equal_to, class Proj = etl::identity, + typename = etl::enable_if_t>> + constexpr ranges::subrange operator()(I first, S last, etl::iter_difference_t count, const T& value, Pred pred = {}, Proj proj = {}) const { - return result; + if (count <= 0) + { + return {first, first}; + } + + for (; first != last; ++first) + { + if (etl::invoke(pred, etl::invoke(proj, *first), value)) + { + I start = first; + etl::iter_difference_t n = 1; + + for (;;) + { + if (n >= count) + { + return {start, ++first}; + } + ++first; + if (first == last) + { + return {first, first}; + } + if (!etl::invoke(pred, etl::invoke(proj, *first), value)) + { + break; + } + ++n; + } + } + } + return {first, first}; } - for (;;) + template , class Pred = ranges::equal_to, class Proj = etl::identity, + typename = etl::enable_if_t>> + constexpr ranges::borrowed_subrange_t operator()(R&& r, ranges::range_difference_t count, const T& value, Pred pred = {}, + Proj proj = {}) const { - auto new_result = ranges::search(etl::next(result.begin()), last1, first2, last2, pred, proj1, proj2); - if (new_result.empty()) + return (*this)(ranges::begin(r), ranges::end(r), count, value, etl::move(pred), etl::move(proj)); + } + }; + + inline constexpr search_n_fn search_n{}; + + struct find_end_fn + { + template >> + constexpr ranges::subrange operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + if (first2 == last2) + { + auto last_it = ranges::next(first1, last1); + return {last_it, last_it}; + } + auto result = ranges::search(etl::move(first1), last1, first2, last2, pred, proj1, proj2); + + if (result.empty()) { return result; } - else + + for (;;) { - result = etl::move(new_result); - } - } - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), - etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr find_end_fn find_end {}; - - struct find_first_of_fn - { - template>> - constexpr I1 operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - for (; first1 != last1; ++first1) - { - for (auto i = first2; i != last2; ++i) - { - if (etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *i))) + auto new_result = ranges::search(etl::next(result.begin()), last1, first2, last2, pred, proj1, proj2); + if (new_result.empty()) { - return first1; + return result; + } + else + { + result = etl::move(new_result); } } } - return first1; - } - template>> - constexpr ranges::borrowed_iterator_t - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const + template >> + constexpr ranges::borrowed_subrange_t operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); + } + }; + + inline constexpr find_end_fn find_end{}; + + struct find_first_of_fn { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - }; + template >> + constexpr I1 operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + for (; first1 != last1; ++first1) + { + for (auto i = first2; i != last2; ++i) + { + if (etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *i))) + { + return first1; + } + } + } + return first1; + } - inline constexpr find_first_of_fn find_first_of {}; + template >> + constexpr ranges::borrowed_iterator_t operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); + } + }; - struct adjacent_find_fn - { - template>> - constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const + inline constexpr find_first_of_fn find_first_of{}; + + struct adjacent_find_fn { + template >> + constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const + { if (first == last) { return first; @@ -4637,3458 +4203,3238 @@ namespace ranges { } } return next_it; - } + } - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Pred pred = {}, Proj proj = {}) const - { + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Pred pred = {}, Proj proj = {}) const + { return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; + } + }; - inline constexpr adjacent_find_fn adjacent_find; + inline constexpr adjacent_find_fn adjacent_find; - struct count_fn - { - template, typename = etl::enable_if_t>> - constexpr etl::iter_difference_t - operator()(I first, S last, const T& value, Proj proj = {}) const + struct count_fn { - etl::iter_difference_t counter = 0; - for (; first != last; ++first) + template , + typename = etl::enable_if_t>> + constexpr etl::iter_difference_t operator()(I first, S last, const T& value, Proj proj = {}) const { - if (etl::invoke(proj, *first) == value) + etl::iter_difference_t counter = 0; + for (; first != last; ++first) { - ++counter; - } - } - return counter; - } - - template, Proj>, typename = etl::enable_if_t>> - constexpr ranges::range_difference_t - operator()(R&& r, const T& value, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), value, etl::ref(proj)); - } - }; - - inline constexpr count_fn count; - - struct count_if_fn - { - template>> - constexpr etl::iter_difference_t - operator()(I first, S last, Pred pred, Proj proj = {}) const - { - etl::iter_difference_t counter = 0; - for (; first != last; ++first) - { - if (etl::invoke(pred, etl::invoke(proj, *first))) - { - ++counter; - } - } - return counter; - } - - template>> - constexpr ranges::range_difference_t - operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr count_if_fn count_if; - - struct all_of_fn - { - template>> - constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const - { - return ranges::find_if_not(first, last, etl::ref(pred), etl::ref(proj)) == last; - } - - template>> - constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const - { - return operator()(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr all_of_fn all_of; - - struct any_of_fn - { - template>> - constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const - { - return ranges::find_if(first, last, etl::ref(pred), etl::ref(proj)) != last; - } - - template>> - constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const - { - return operator()(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr any_of_fn any_of; - - struct none_of_fn - { - template>> - constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const - { - return ranges::find_if(first, last, etl::ref(pred), etl::ref(proj)) == last; - } - - template>> - constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const - { - return operator()(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr none_of_fn none_of; - - struct mismatch_fn - { - template>> - constexpr ranges::mismatch_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - for (; first1 != last1 && first2 != last2; ++first1, ++first2) - { - if (!etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - break; - } - } - return {etl::move(first1), etl::move(first2)}; - } - - template>> - constexpr ranges::mismatch_result, ranges::borrowed_iterator_t> - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr mismatch_fn mismatch {}; - - struct equal_fn - { - template>> - constexpr bool - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - for (; first1 != last1 && first2 != last2; ++first1, ++first2) - { - if (!etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - return false; - } - } - return first1 == last1 && first2 == last2; - } - - template>> - constexpr bool - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr equal_fn equal {}; - - struct is_permutation_fn - { - template>> - constexpr bool - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - // Skip common prefix - for (; first1 != last1 && first2 != last2; ++first1, ++first2) - { - if (!etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - break; - } - } - - if (first1 == last1) - { - return first2 == last2; - } - - if (first2 == last2) - { - return false; - } - - // Check remaining elements - for (I1 i = first1; i != last1; ++i) - { - // Check if we already counted this element - bool already_seen = false; - for (I1 j = first1; j != i; ++j) - { - if (etl::invoke(pred, etl::invoke(proj1, *i), etl::invoke(proj1, *j))) + if (etl::invoke(proj, *first) == value) + { + ++counter; + } + } + return counter; + } + + template , Proj>, + typename = etl::enable_if_t>> + constexpr ranges::range_difference_t operator()(R&& r, const T& value, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), value, etl::ref(proj)); + } + }; + + inline constexpr count_fn count; + + struct count_if_fn + { + template >> + constexpr etl::iter_difference_t operator()(I first, S last, Pred pred, Proj proj = {}) const + { + etl::iter_difference_t counter = 0; + for (; first != last; ++first) + { + if (etl::invoke(pred, etl::invoke(proj, *first))) + { + ++counter; + } + } + return counter; + } + + template >> + constexpr ranges::range_difference_t operator()(R&& r, Pred pred, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr count_if_fn count_if; + + struct all_of_fn + { + template >> + constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const + { + return ranges::find_if_not(first, last, etl::ref(pred), etl::ref(proj)) == last; + } + + template >> + constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const + { + return operator()(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr all_of_fn all_of; + + struct any_of_fn + { + template >> + constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const + { + return ranges::find_if(first, last, etl::ref(pred), etl::ref(proj)) != last; + } + + template >> + constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const + { + return operator()(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr any_of_fn any_of; + + struct none_of_fn + { + template >> + constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const + { + return ranges::find_if(first, last, etl::ref(pred), etl::ref(proj)) == last; + } + + template >> + constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const + { + return operator()(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr none_of_fn none_of; + + struct mismatch_fn + { + template >> + constexpr ranges::mismatch_result operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, + Proj2 proj2 = {}) const + { + for (; first1 != last1 && first2 != last2; ++first1, ++first2) + { + if (!etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + break; + } + } + return {etl::move(first1), etl::move(first2)}; + } + + template >> + constexpr ranges::mismatch_result, ranges::borrowed_iterator_t> + operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); + } + }; + + inline constexpr mismatch_fn mismatch{}; + + struct equal_fn + { + template >> + constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + for (; first1 != last1 && first2 != last2; ++first1, ++first2) + { + if (!etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + return false; + } + } + return first1 == last1 && first2 == last2; + } + + template >> + constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); + } + }; + + inline constexpr equal_fn equal{}; + + struct is_permutation_fn + { + template >> + constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + // Skip common prefix + for (; first1 != last1 && first2 != last2; ++first1, ++first2) + { + if (!etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) { - already_seen = true; break; } } - if (already_seen) + if (first1 == last1) { - continue; + return first2 == last2; } - // Count occurrences in range2 - auto count2 = etl::iter_difference_t(0); - for (I2 k = first2; k != last2; ++k) - { - if (etl::invoke(pred, etl::invoke(proj1, *i), etl::invoke(proj2, *k))) - { - ++count2; - } - } - - if (count2 == 0) + if (first2 == last2) { return false; } - // Count occurrences in range1 - auto count1 = etl::iter_difference_t(0); - for (I1 k = i; k != last1; ++k) + // Check remaining elements + for (I1 i = first1; i != last1; ++i) { - if (etl::invoke(pred, etl::invoke(proj1, *i), etl::invoke(proj1, *k))) + // Check if we already counted this element + bool already_seen = false; + for (I1 j = first1; j != i; ++j) { - ++count1; + if (etl::invoke(pred, etl::invoke(proj1, *i), etl::invoke(proj1, *j))) + { + already_seen = true; + break; + } + } + + if (already_seen) + { + continue; + } + + // Count occurrences in range2 + auto count2 = etl::iter_difference_t(0); + for (I2 k = first2; k != last2; ++k) + { + if (etl::invoke(pred, etl::invoke(proj1, *i), etl::invoke(proj2, *k))) + { + ++count2; + } + } + + if (count2 == 0) + { + return false; + } + + // Count occurrences in range1 + auto count1 = etl::iter_difference_t(0); + for (I1 k = i; k != last1; ++k) + { + if (etl::invoke(pred, etl::invoke(proj1, *i), etl::invoke(proj1, *k))) + { + ++count1; + } + } + + if (count1 != count2) + { + return false; } } - if (count1 != count2) + return true; + } + + template >> + constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); + } + }; + + inline constexpr is_permutation_fn is_permutation{}; + + struct starts_with_fn + { + template >> + constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + for (; first2 != last2; ++first1, ++first2) + { + if (first1 == last1 || !etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + return false; + } + } + return true; + } + + template >> + constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); + } + }; + + inline constexpr starts_with_fn starts_with{}; + + struct ends_with_fn + { + template >> + constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + auto n1 = etl::distance(first1, last1); + auto n2 = etl::distance(first2, last2); + + if (n2 > n1) { return false; } + + ranges::advance(first1, n1 - n2); + + return starts_with(first1, last1, first2, last2, etl::move(pred), etl::move(proj1), etl::move(proj2)); } - return true; - } - - template>> - constexpr bool - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr is_permutation_fn is_permutation {}; - - struct starts_with_fn - { - template>> - constexpr bool - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - for (; first2 != last2; ++first1, ++first2) + template >> + constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { - if (first1 == last1 || !etl::invoke(pred, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(pred), etl::move(proj1), etl::move(proj2)); + } + }; + + inline constexpr ends_with_fn ends_with{}; + + struct lexicographical_compare_fn + { + template >> + constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + for (; first1 != last1 && first2 != last2; ++first1, ++first2) { - return false; - } - } - return true; - } - - template>> - constexpr bool - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr starts_with_fn starts_with {}; - - struct ends_with_fn - { - template>> - constexpr bool - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - auto n1 = etl::distance(first1, last1); - auto n2 = etl::distance(first2, last2); - - if (n2 > n1) - { - return false; - } - - ranges::advance(first1, n1 - n2); - - return starts_with(first1, last1, first2, last2, etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - - template>> - constexpr bool - operator()(R1&& r1, R2&& r2, Pred pred = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(pred), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr ends_with_fn ends_with {}; - - struct lexicographical_compare_fn - { - template>> - constexpr bool - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - for (; first1 != last1 && first2 != last2; ++first1, ++first2) - { - if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - return true; - } - - if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) - { - return false; - } - } - return first1 == last1 && first2 != last2; - } - - template>> - constexpr bool - operator()(R1&& r1, R2&& r2, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr lexicographical_compare_fn lexicographical_compare {}; - - template - struct in_value_result - { - ETL_NO_UNIQUE_ADDRESS I in; - ETL_NO_UNIQUE_ADDRESS T value; - - template - constexpr operator in_value_result() const& - { - return {in, value}; - } - - template - constexpr operator in_value_result() && - { - return {etl::move(in), etl::move(value)}; - } - }; - - template - using fold_left_with_iter_result = in_value_result; - - struct fold_left_fn - { - template>> - constexpr auto operator()(I first, S last, T init, F f) const - -> etl::decay_t>> - { - using U = etl::decay_t>>; - U accum = etl::move(init); - for (; first != last; ++first) - { - accum = etl::invoke(f, etl::move(accum), *first); - } - return accum; - } - - template>> - constexpr auto operator()(R&& r, T init, F f) const - -> etl::decay_t>> - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(init), etl::move(f)); - } - }; - - inline constexpr fold_left_fn fold_left {}; - - struct fold_left_with_iter_fn - { - template>> - constexpr auto operator()(I first, S last, T init, F f) const - -> fold_left_with_iter_result>>> - { - using U = etl::decay_t>>; - U accum = etl::move(init); - for (; first != last; ++first) - { - accum = etl::invoke(f, etl::move(accum), *first); - } - return {etl::move(first), etl::move(accum)}; - } - - template>> - constexpr auto operator()(R&& r, T init, F f) const - -> fold_left_with_iter_result, etl::decay_t>>> - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(init), etl::move(f)); - } - }; - - inline constexpr fold_left_with_iter_fn fold_left_with_iter {}; - - struct fold_left_first_fn - { - template>> - constexpr auto operator()(I first, S last, F f) const - -> etl::decay_t, etl::iter_reference_t>> - { - using U = etl::decay_t, etl::iter_reference_t>>; - if (first == last) - { - return U{}; - } - U accum = *first; - ++first; - for (; first != last; ++first) - { - accum = etl::invoke(f, etl::move(accum), *first); - } - return accum; - } - - template>> - constexpr auto operator()(R&& r, F f) const - -> etl::decay_t, etl::ranges::range_reference_t>> - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(f)); - } - }; - - inline constexpr fold_left_first_fn fold_left_first {}; - - struct fold_left_first_with_iter_fn - { - template>> - constexpr auto operator()(I first, S last, F f) const - -> fold_left_with_iter_result, etl::iter_reference_t>>> - { - using U = etl::decay_t, etl::iter_reference_t>>; - if (first == last) - { - return {etl::move(first), U{}}; - } - U accum = *first; - ++first; - for (; first != last; ++first) - { - accum = etl::invoke(f, etl::move(accum), *first); - } - return {etl::move(first), etl::move(accum)}; - } - - template>> - constexpr auto operator()(R&& r, F f) const - -> fold_left_with_iter_result, etl::decay_t, etl::ranges::range_reference_t>>> - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(f)); - } - }; - - inline constexpr fold_left_first_with_iter_fn fold_left_first_with_iter {}; - - struct fold_right_fn - { - template>> - constexpr auto operator()(I first, S last, T init, F f) const - -> etl::decay_t, T>> - { - using U = etl::decay_t, T>>; - U accum = etl::move(init); - I tail = ranges::next(first, last); - while (tail != first) - { - tail = ranges::prev(tail); - accum = etl::invoke(f, *tail, etl::move(accum)); - } - return accum; - } - - template>> - constexpr auto operator()(R&& r, T init, F f) const - -> etl::decay_t, T>> - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(init), etl::move(f)); - } - }; - - inline constexpr fold_right_fn fold_right {}; - - struct fold_right_last_fn - { - template>> - constexpr auto operator()(I first, S last, F f) const - -> etl::decay_t, etl::iter_value_t>> - { - using U = etl::decay_t, etl::iter_value_t>>; - I tail = ranges::next(first, last); - if (tail == first) - { - return U{}; - } - tail = ranges::prev(tail); - U accum = *tail; - while (tail != first) - { - tail = ranges::prev(tail); - accum = etl::invoke(f, *tail, etl::move(accum)); - } - return accum; - } - - template>> - constexpr auto operator()(R&& r, F f) const - -> etl::decay_t, etl::ranges::range_value_t>> - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(f)); - } - }; - - inline constexpr fold_right_last_fn fold_right_last {}; - - struct copy_fn - { - template>> - constexpr ranges::copy_result - operator()(I first, S last, O result) const - { - for (; first != last; ++first, ++result) - { - *result = *first; - } - return {etl::move(first), etl::move(result)}; - } - - template>> - constexpr ranges::copy_result, O> - operator()(R&& r, O result) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); - } - }; - - inline constexpr copy_fn copy {}; - - struct copy_n_fn - { - template - constexpr ranges::copy_n_result - operator()(I first, etl::iter_difference_t n, O result) const - { - for (etl::iter_difference_t i = 0; i < n; ++i, ++first, ++result) - { - *result = *first; - } - return {etl::move(first), etl::move(result)}; - } - }; - - inline constexpr copy_n_fn copy_n {}; - - struct copy_if_fn - { - template>> - constexpr ranges::copy_if_result - operator()(I first, S last, O result, Pred pred, Proj proj = {}) const - { - for (; first != last; ++first) - { - if (etl::invoke(pred, etl::invoke(proj, *first))) - { - *result = *first; - ++result; - } - } - return {etl::move(first), etl::move(result)}; - } - - template>> - constexpr ranges::copy_if_result, O> - operator()(R&& r, O result, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr copy_if_fn copy_if {}; - - struct copy_backward_fn - { - template>> - constexpr ranges::copy_backward_result - operator()(I1 first, S1 last, I2 result) const - { - I1 last_it = ranges::next(first, last); - I1 tail = last_it; - while (first != tail) - { - *(--result) = *(--tail); - } - return {etl::move(last_it), etl::move(result)}; - } - - template>> - constexpr ranges::copy_backward_result, I> - operator()(R&& r, I result) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); - } - }; - - inline constexpr copy_backward_fn copy_backward {}; - - struct move_fn - { - template>> - constexpr ranges::move_result - operator()(I first, S last, O result) const - { - for (; first != last; ++first, ++result) - { - *result = etl::move(*first); - } - return {etl::move(first), etl::move(result)}; - } - - template>> - constexpr ranges::move_result, O> - operator()(R&& r, O result) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); - } - }; - - inline constexpr move_fn move {}; - - struct move_backward_fn - { - template>> - constexpr ranges::move_backward_result - operator()(I1 first, S1 last, I2 result) const - { - I1 last_it = ranges::next(first, last); - I1 tail = last_it; - while (first != tail) - { - *(--result) = etl::move(*(--tail)); - } - return {etl::move(last_it), etl::move(result)}; - } - - template>> - constexpr ranges::move_backward_result, I> - operator()(R&& r, I result) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); - } - }; - - inline constexpr move_backward_fn move_backward {}; - - struct swap_ranges_fn - { - template>> - constexpr ranges::swap_ranges_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2) const - { - for (; first1 != last1 && first2 != last2; ++first1, ++first2) - { - etl::iter_swap(first1, first2); - } - return {etl::move(first1), etl::move(first2)}; - } - - template>> - constexpr ranges::swap_ranges_result, ranges::borrowed_iterator_t> - operator()(R1&& r1, R2&& r2) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2)); - } - }; - - inline constexpr swap_ranges_fn swap_ranges {}; - - struct replace_fn - { - template>> - constexpr I operator()(I first, S last, const T1& old_value, const T2& new_value, Proj proj = {}) const - { - for (; first != last; ++first) - { - if (etl::invoke(proj, *first) == old_value) - { - *first = new_value; - } - } - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, const T1& old_value, const T2& new_value, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), old_value, new_value, etl::ref(proj)); - } - }; - - inline constexpr replace_fn replace {}; - - struct replace_if_fn - { - template>> - constexpr I operator()(I first, S last, Pred pred, const T& new_value, Proj proj = {}) const - { - for (; first != last; ++first) - { - if (etl::invoke(pred, etl::invoke(proj, *first))) - { - *first = new_value; - } - } - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Pred pred, const T& new_value, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), new_value, etl::ref(proj)); - } - }; - - inline constexpr replace_if_fn replace_if {}; - - struct replace_copy_fn - { - template>> - constexpr ranges::replace_copy_result - operator()(I first, S last, O result, const T1& old_value, const T2& new_value, Proj proj = {}) const - { - for (; first != last; ++first, ++result) - { - if (etl::invoke(proj, *first) == old_value) - { - *result = new_value; - } - else - { - *result = *first; - } - } - return {etl::move(first), etl::move(result)}; - } - - template>> - constexpr ranges::replace_copy_result, O> - operator()(R&& r, O result, const T1& old_value, const T2& new_value, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), old_value, new_value, etl::ref(proj)); - } - }; - - inline constexpr replace_copy_fn replace_copy {}; - - struct replace_copy_if_fn - { - template>> - constexpr ranges::replace_copy_if_result - operator()(I first, S last, O result, Pred pred, const T& new_value, Proj proj = {}) const - { - for (; first != last; ++first, ++result) - { - if (etl::invoke(pred, etl::invoke(proj, *first))) - { - *result = new_value; - } - else - { - *result = *first; - } - } - return {etl::move(first), etl::move(result)}; - } - - template>> - constexpr ranges::replace_copy_if_result, O> - operator()(R&& r, O result, Pred pred, const T& new_value, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(pred), new_value, etl::ref(proj)); - } - }; - - inline constexpr replace_copy_if_fn replace_copy_if {}; - - struct remove_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, const T& value, Proj proj = {}) const - { - first = ranges::find(first, last, value, etl::ref(proj)); - - if (first != last) - { - I result = first; - - for (I it = result; ++it != last;) - { - if (!(etl::invoke(proj, *it) == value)) + if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) { - *result = etl::move(*it); - ++result; + return true; + } + + if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + { + return false; } } - - return {result, last}; + return first1 == last1 && first2 != last2; } - return {first, last}; - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, const T& value, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), value, etl::ref(proj)); - } - }; - - inline constexpr remove_fn remove {}; - - struct remove_if_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, Pred pred, Proj proj = {}) const - { - first = ranges::find_if(first, last, etl::ref(pred), etl::ref(proj)); - - if (first != last) + template >> + constexpr bool operator()(R1&& r1, R2&& r2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { - I result = first; + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(comp), etl::move(proj1), etl::move(proj2)); + } + }; - for (I it = result; ++it != last;) - { - if (!etl::invoke(pred, etl::invoke(proj, *it))) - { - *result = etl::move(*it); - ++result; - } - } + inline constexpr lexicographical_compare_fn lexicographical_compare{}; - return {result, last}; + template + struct in_value_result + { + ETL_NO_UNIQUE_ADDRESS I in; + ETL_NO_UNIQUE_ADDRESS T value; + + template + constexpr operator in_value_result() const& + { + return {in, value}; } - return {first, last}; - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr remove_if_fn remove_if {}; - - struct remove_copy_fn - { - template>> - constexpr ranges::remove_copy_result - operator()(I first, S last, O result, const T& value, Proj proj = {}) const - { - for (; first != last; ++first) + template + constexpr operator in_value_result() && { - if (!(etl::invoke(proj, *first) == value)) - { - *result = *first; - ++result; - } + return {etl::move(in), etl::move(value)}; } - return {etl::move(first), etl::move(result)}; - } + }; - template>> - constexpr ranges::remove_copy_result, O> - operator()(R&& r, O result, const T& value, Proj proj = {}) const + template + using fold_left_with_iter_result = in_value_result; + + struct fold_left_fn { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), value, etl::ref(proj)); - } - }; - - inline constexpr remove_copy_fn remove_copy {}; - - struct fill_fn - { - template>> - constexpr I operator()(I first, S last, const T& value) const - { - for (; first != last; ++first) + template >> + constexpr auto operator()(I first, S last, T init, F f) const -> etl::decay_t>> { - *first = value; - } - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, const T& value) const - { - return (*this)(ranges::begin(r), ranges::end(r), value); - } - }; - - inline constexpr fill_fn fill {}; - - struct fill_n_fn - { - template - constexpr I operator()(I first, etl::iter_difference_t n, const T& value) const - { - for (; n-- > 0; ++first) - { - *first = value; - } - return first; - } - }; - - inline constexpr fill_n_fn fill_n {}; - - struct generate_fn - { - template>> - constexpr I operator()(I first, S last, F gen) const - { - for (; first != last; ++first) - { - *first = gen(); - } - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, F gen) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(gen)); - } - }; - - inline constexpr generate_fn generate {}; - - struct generate_n_fn - { - template - constexpr I operator()(I first, etl::iter_difference_t n, F gen) const - { - for (; n-- > 0; ++first) - { - *first = gen(); - } - return first; - } - }; - - inline constexpr generate_n_fn generate_n {}; - - struct iota_fn - { - template>> - constexpr ranges::iota_result - operator()(O first, S last, T value) const - { - while (first != last) - { - *first = value; - ++first; - ++value; - } - return {etl::move(first), etl::move(value)}; - } - - template>> - constexpr ranges::iota_result, T> - operator()(R&& r, T value) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(value)); - } - }; - - inline constexpr iota_fn iota {}; - - struct unique_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, Pred pred = {}, Proj proj = {}) const - { - first = ranges::adjacent_find(first, last, etl::ref(pred), etl::ref(proj)); - - if (first != last) - { - I result = first; - ++first; - - while (++first != last) - { - if (!etl::invoke(pred, etl::invoke(proj, *result), etl::invoke(proj, *first))) - { - *++result = etl::move(*first); - } - } - - return {++result, last}; - } - - return {first, last}; - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, Pred pred = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr unique_fn unique {}; - - struct unique_copy_fn - { - template>> - constexpr ranges::unique_copy_result - operator()(I first, S last, O result, Pred pred = {}, Proj proj = {}) const - { - if (first != last) - { - *result = *first; - ++result; - - auto previous = first; - ++first; - + using U = etl::decay_t>>; + U accum = etl::move(init); for (; first != last; ++first) { - if (!etl::invoke(pred, etl::invoke(proj, *previous), etl::invoke(proj, *first))) + accum = etl::invoke(f, etl::move(accum), *first); + } + return accum; + } + + template >> + constexpr auto operator()(R&& r, T init, F f) const -> etl::decay_t< etl::invoke_result_t>> + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(init), etl::move(f)); + } + }; + + inline constexpr fold_left_fn fold_left{}; + + struct fold_left_with_iter_fn + { + template >> + constexpr auto operator()(I first, S last, T init, + F f) const -> fold_left_with_iter_result< I, etl::decay_t>>> + { + using U = etl::decay_t>>; + U accum = etl::move(init); + for (; first != last; ++first) + { + accum = etl::invoke(f, etl::move(accum), *first); + } + return {etl::move(first), etl::move(accum)}; + } + + template >> + constexpr auto operator()(R&& r, T init, F f) const + -> fold_left_with_iter_result< ranges::borrowed_iterator_t, etl::decay_t< etl::invoke_result_t>>> + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(init), etl::move(f)); + } + }; + + inline constexpr fold_left_with_iter_fn fold_left_with_iter{}; + + struct fold_left_first_fn + { + template >> + constexpr auto operator()(I first, S last, F f) const -> etl::decay_t, etl::iter_reference_t>> + { + using U = etl::decay_t, etl::iter_reference_t>>; + if (first == last) + { + return U{}; + } + U accum = *first; + ++first; + for (; first != last; ++first) + { + accum = etl::invoke(f, etl::move(accum), *first); + } + return accum; + } + + template >> + constexpr auto operator()(R&& r, + F f) const -> etl::decay_t, etl::ranges::range_reference_t>> + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(f)); + } + }; + + inline constexpr fold_left_first_fn fold_left_first{}; + + struct fold_left_first_with_iter_fn + { + template >> + constexpr auto operator()(I first, S last, F f) const + -> fold_left_with_iter_result< I, etl::decay_t, etl::iter_reference_t>>> + { + using U = etl::decay_t, etl::iter_reference_t>>; + if (first == last) + { + return {etl::move(first), U{}}; + } + U accum = *first; + ++first; + for (; first != last; ++first) + { + accum = etl::invoke(f, etl::move(accum), *first); + } + return {etl::move(first), etl::move(accum)}; + } + + template >> + constexpr auto operator()(R&& r, F f) const + -> fold_left_with_iter_result< ranges::borrowed_iterator_t, + etl::decay_t, etl::ranges::range_reference_t>>> + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(f)); + } + }; + + inline constexpr fold_left_first_with_iter_fn fold_left_first_with_iter{}; + + struct fold_right_fn + { + template >> + constexpr auto operator()(I first, S last, T init, F f) const -> etl::decay_t, T>> + { + using U = etl::decay_t, T>>; + U accum = etl::move(init); + I tail = ranges::next(first, last); + while (tail != first) + { + tail = ranges::prev(tail); + accum = etl::invoke(f, *tail, etl::move(accum)); + } + return accum; + } + + template >> + constexpr auto operator()(R&& r, T init, F f) const -> etl::decay_t< etl::invoke_result_t, T>> + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(init), etl::move(f)); + } + }; + + inline constexpr fold_right_fn fold_right{}; + + struct fold_right_last_fn + { + template >> + constexpr auto operator()(I first, S last, F f) const -> etl::decay_t, etl::iter_value_t>> + { + using U = etl::decay_t, etl::iter_value_t>>; + I tail = ranges::next(first, last); + if (tail == first) + { + return U{}; + } + tail = ranges::prev(tail); + U accum = *tail; + while (tail != first) + { + tail = ranges::prev(tail); + accum = etl::invoke(f, *tail, etl::move(accum)); + } + return accum; + } + + template >> + constexpr auto + operator()(R&& r, F f) const -> etl::decay_t, etl::ranges::range_value_t>> + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(f)); + } + }; + + inline constexpr fold_right_last_fn fold_right_last{}; + + struct copy_fn + { + template >> + constexpr ranges::copy_result operator()(I first, S last, O result) const + { + for (; first != last; ++first, ++result) + { + *result = *first; + } + return {etl::move(first), etl::move(result)}; + } + + template >> + constexpr ranges::copy_result, O> operator()(R&& r, O result) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); + } + }; + + inline constexpr copy_fn copy{}; + + struct copy_n_fn + { + template + constexpr ranges::copy_n_result operator()(I first, etl::iter_difference_t n, O result) const + { + for (etl::iter_difference_t i = 0; i < n; ++i, ++first, ++result) + { + *result = *first; + } + return {etl::move(first), etl::move(result)}; + } + }; + + inline constexpr copy_n_fn copy_n{}; + + struct copy_if_fn + { + template >> + constexpr ranges::copy_if_result operator()(I first, S last, O result, Pred pred, Proj proj = {}) const + { + for (; first != last; ++first) + { + if (etl::invoke(pred, etl::invoke(proj, *first))) { *result = *first; ++result; } - previous = first; } + return {etl::move(first), etl::move(result)}; } - return {etl::move(first), etl::move(result)}; - } - - template>> - constexpr ranges::unique_copy_result, O> - operator()(R&& r, O result, Pred pred = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr unique_copy_fn unique_copy {}; - - struct transform_fn - { - // Unary: iterator + sentinel - template>> - constexpr ranges::unary_transform_result - operator()(I first, S last, O result, F op, Proj proj = {}) const - { - for (; first != last; ++first, ++result) + template >> + constexpr ranges::copy_if_result, O> operator()(R&& r, O result, Pred pred, Proj proj = {}) const { - *result = etl::invoke(op, etl::invoke(proj, *first)); + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(pred), etl::ref(proj)); } - return {etl::move(first), etl::move(result)}; - } + }; - // Unary: range - template && !etl::is_range_v>> - constexpr ranges::unary_transform_result, O> - operator()(R&& r, O result, F op, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(op), etl::ref(proj)); - } + inline constexpr copy_if_fn copy_if{}; - // Binary: iterator + sentinel - template>> - constexpr ranges::binary_transform_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, F op, Proj1 proj1 = {}, Proj2 proj2 = {}) const + struct copy_backward_fn { - for (; first1 != last1 && first2 != last2; ++first1, ++first2, ++result) + template >> + constexpr ranges::copy_backward_result operator()(I1 first, S1 last, I2 result) const { - *result = etl::invoke(op, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2)); - } - return {etl::move(first1), etl::move(first2), etl::move(result)}; - } - - // Binary: range - template && etl::is_range_v>> - constexpr ranges::binary_transform_result, ranges::borrowed_iterator_t, O> - operator()(R1&& r1, R2&& r2, O result, F op, Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(result), etl::ref(op), etl::ref(proj1), etl::ref(proj2)); - } - }; - - inline constexpr transform_fn transform {}; - - struct reverse_fn - { - template>> - constexpr I operator()(I first, S last) const - { - I tail = ranges::next(first, last); - I result = tail; - - for (; first != tail && first != --tail; ++first) - { - etl::iter_swap(first, tail); - } - - return result; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r) const - { - return (*this)(ranges::begin(r), ranges::end(r)); - } - }; - - inline constexpr reverse_fn reverse {}; - - template - using reverse_copy_result = in_out_result; - - struct reverse_copy_fn - { - template>> - constexpr ranges::reverse_copy_result - operator()(I first, S last, O result) const - { - I tail = ranges::next(first, last); - I end_it = tail; - - while (tail != first) - { - *result = *--tail; - ++result; - } - - return {etl::move(end_it), etl::move(result)}; - } - - template>> - constexpr ranges::reverse_copy_result, O> - operator()(R&& r, O result) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); - } - }; - - inline constexpr reverse_copy_fn reverse_copy {}; - - template - using rotate_result = ranges::subrange; - - struct rotate_fn - { - template>> - constexpr ranges::rotate_result - operator()(I first, I middle, S last) const - { - if (first == middle) - { - I last_it = ranges::next(first, last); - return {last_it, last_it}; - } - - I last_it = ranges::next(first, last); - - if (middle == last_it) - { - return {first, last_it}; - } - - I new_first = etl::rotate(first, middle, last_it); - return {etl::move(new_first), etl::move(last_it)}; - } - - template>> - constexpr ranges::rotate_result> - operator()(R&& r, ranges::iterator_t middle) const - { - return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r)); - } - }; - - inline constexpr rotate_fn rotate {}; - - struct rotate_copy_fn - { - template>> - constexpr ranges::rotate_copy_result - operator()(I first, I middle, S last, O result) const - { - I last_it = ranges::next(first, last); - O end_out = etl::copy(middle, last_it, result); - end_out = etl::copy(first, middle, end_out); - return {etl::move(last_it), etl::move(end_out)}; - } - - template>> - constexpr ranges::rotate_copy_result, O> - operator()(R&& r, ranges::iterator_t middle, O result) const - { - return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r), etl::move(result)); - } - }; - - inline constexpr rotate_copy_fn rotate_copy {}; - - struct shift_left_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, etl::iter_difference_t n) const - { - I last_it = ranges::next(first, last); - - if (n <= 0) - { - return {first, last_it}; - } - - I mid = first; - if (ranges::advance(mid, n, last_it) != 0) - { - return {first, first}; - } - - I result = ranges::move(mid, last_it, first).out; - return {first, etl::move(result)}; - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, etl::ranges::range_difference_t n) const - { - return (*this)(ranges::begin(r), ranges::end(r), n); - } - }; - - inline constexpr shift_left_fn shift_left {}; - - struct shift_right_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, etl::iter_difference_t n) const - { - I last_it = ranges::next(first, last); - - if (n <= 0) - { - return {first, last_it}; - } - - I trail = last_it; - if (ranges::advance(trail, -n, first) != 0) - { - return {last_it, last_it}; - } - - I new_first = ranges::move_backward(first, trail, last_it).out; - return {etl::move(new_first), etl::move(last_it)}; - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, etl::ranges::range_difference_t n) const - { - return (*this)(ranges::begin(r), ranges::end(r), n); - } - }; - - inline constexpr shift_right_fn shift_right {}; - - struct shuffle_fn - { - template>> - I operator()(I first, S last, Gen&& gen) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "shuffle requires random access iterators"); - - using diff_t = etl::iter_difference_t; - using udiff_t = etl::make_unsigned_t; - using gen_t = etl::remove_reference_t; - using uresult_t = decltype(gen()); - - I last_it = ranges::next(first, last); - diff_t n = last_it - first; - - if (n <= 1) - { - return last_it; - } - - for (diff_t i = n - 1; i > 0; --i) - { - // Generate a uniformly distributed random index in [0, i] - // using rejection sampling to avoid modulo bias. - udiff_t range = static_cast(i); - constexpr uresult_t gmin = gen_t::min(); - constexpr uresult_t gmax = gen_t::max(); - constexpr uresult_t grange = gmax - gmin; - - uresult_t j; - - if ETL_IF_CONSTEXPR (grange == static_cast(-1)) + I1 last_it = ranges::next(first, last); + I1 tail = last_it; + while (first != tail) { - // Generator covers full range of uresult_t, just use modulo with rejection - uresult_t limit = (static_cast(-1) / (static_cast(range) + 1)) * (static_cast(range) + 1); - do + *(--result) = *(--tail); + } + return {etl::move(last_it), etl::move(result)}; + } + + template >> + constexpr ranges::copy_backward_result, I> operator()(R&& r, I result) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); + } + }; + + inline constexpr copy_backward_fn copy_backward{}; + + struct move_fn + { + template >> + constexpr ranges::move_result operator()(I first, S last, O result) const + { + for (; first != last; ++first, ++result) + { + *result = etl::move(*first); + } + return {etl::move(first), etl::move(result)}; + } + + template >> + constexpr ranges::move_result, O> operator()(R&& r, O result) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); + } + }; + + inline constexpr move_fn move{}; + + struct move_backward_fn + { + template >> + constexpr ranges::move_backward_result operator()(I1 first, S1 last, I2 result) const + { + I1 last_it = ranges::next(first, last); + I1 tail = last_it; + while (first != tail) + { + *(--result) = etl::move(*(--tail)); + } + return {etl::move(last_it), etl::move(result)}; + } + + template >> + constexpr ranges::move_backward_result, I> operator()(R&& r, I result) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); + } + }; + + inline constexpr move_backward_fn move_backward{}; + + struct swap_ranges_fn + { + template >> + constexpr ranges::swap_ranges_result operator()(I1 first1, S1 last1, I2 first2, S2 last2) const + { + for (; first1 != last1 && first2 != last2; ++first1, ++first2) + { + etl::iter_swap(first1, first2); + } + return {etl::move(first1), etl::move(first2)}; + } + + template >> + constexpr ranges::swap_ranges_result, ranges::borrowed_iterator_t> operator()(R1&& r1, R2&& r2) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2)); + } + }; + + inline constexpr swap_ranges_fn swap_ranges{}; + + struct replace_fn + { + template >> + constexpr I operator()(I first, S last, const T1& old_value, const T2& new_value, Proj proj = {}) const + { + for (; first != last; ++first) + { + if (etl::invoke(proj, *first) == old_value) { - j = static_cast(gen() - gmin); - } while (j >= limit); - j %= (static_cast(range) + 1); + *first = new_value; + } } - else - { - uresult_t limit = (grange / (static_cast(range) + 1)) * (static_cast(range) + 1); - do - { - j = static_cast(gen() - gmin); - } while (j >= limit); - j %= (static_cast(range) + 1); - } - - etl::iter_swap(first + i, first + static_cast(j)); + return first; } - return last_it; - } + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, const T1& old_value, const T2& new_value, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), old_value, new_value, etl::ref(proj)); + } + }; - template>> - ranges::borrowed_iterator_t - operator()(R&& r, Gen&& gen) const + inline constexpr replace_fn replace{}; + + struct replace_if_fn { - ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "shuffle requires a range with random access iterators"); - - return (*this)(ranges::begin(r), ranges::end(r), static_cast(gen)); - } - }; - - inline constexpr shuffle_fn shuffle {}; - - struct sample_fn - { - template>> - O operator()(I first, S last, O out, etl::iter_difference_t n, Gen&& gen) const - { - using diff_t = etl::iter_difference_t; - using udiff_t = etl::make_unsigned_t; - using gen_t = etl::remove_reference_t; - using uresult_t = decltype(gen()); - - if (n <= 0) + template >> + constexpr I operator()(I first, S last, Pred pred, const T& new_value, Proj proj = {}) const { - return out; - } - - // Compute the size of [first, last). - I first_copy = first; - diff_t pop_size = 0; - for (I it = first_copy; it != last; ++it) - { - ++pop_size; - } - - if (pop_size <= n) - { - // Copy all elements. - for (; first != last; ++first, ++out) + for (; first != last; ++first) { - *out = *first; - } - return out; - } - - // Selection sampling (Algorithm S / Vitter). - // For each element, decide whether to include it. - diff_t remaining = pop_size; - diff_t needed = n; - - for (; first != last && needed > 0; ++first, --remaining) - { - // Generate a uniformly distributed random number in [0, remaining). - udiff_t range = static_cast(remaining - 1); - constexpr uresult_t gmin = gen_t::min(); - constexpr uresult_t gmax = gen_t::max(); - constexpr uresult_t grange = gmax - gmin; - - uresult_t j; - - if ETL_IF_CONSTEXPR (grange == static_cast(-1)) - { - if (range == 0) + if (etl::invoke(pred, etl::invoke(proj, *first))) { - j = 0; + *first = new_value; + } + } + return first; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Pred pred, const T& new_value, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), new_value, etl::ref(proj)); + } + }; + + inline constexpr replace_if_fn replace_if{}; + + struct replace_copy_fn + { + template >> + constexpr ranges::replace_copy_result operator()(I first, S last, O result, const T1& old_value, const T2& new_value, + Proj proj = {}) const + { + for (; first != last; ++first, ++result) + { + if (etl::invoke(proj, *first) == old_value) + { + *result = new_value; } else { - uresult_t limit = (static_cast(-1) / (static_cast(range) + 1)) * (static_cast(range) + 1); - do + *result = *first; + } + } + return {etl::move(first), etl::move(result)}; + } + + template >> + constexpr ranges::replace_copy_result, O> operator()(R&& r, O result, const T1& old_value, const T2& new_value, + Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), old_value, new_value, etl::ref(proj)); + } + }; + + inline constexpr replace_copy_fn replace_copy{}; + + struct replace_copy_if_fn + { + template >> + constexpr ranges::replace_copy_if_result operator()(I first, S last, O result, Pred pred, const T& new_value, Proj proj = {}) const + { + for (; first != last; ++first, ++result) + { + if (etl::invoke(pred, etl::invoke(proj, *first))) + { + *result = new_value; + } + else + { + *result = *first; + } + } + return {etl::move(first), etl::move(result)}; + } + + template >> + constexpr ranges::replace_copy_if_result, O> operator()(R&& r, O result, Pred pred, const T& new_value, + Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(pred), new_value, etl::ref(proj)); + } + }; + + inline constexpr replace_copy_if_fn replace_copy_if{}; + + struct remove_fn + { + template >> + constexpr ranges::subrange operator()(I first, S last, const T& value, Proj proj = {}) const + { + first = ranges::find(first, last, value, etl::ref(proj)); + + if (first != last) + { + I result = first; + + for (I it = result; ++it != last;) + { + if (!(etl::invoke(proj, *it) == value)) { + *result = etl::move(*it); + ++result; + } + } + + return {result, last}; + } + + return {first, last}; + } + + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, const T& value, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), value, etl::ref(proj)); + } + }; + + inline constexpr remove_fn remove{}; + + struct remove_if_fn + { + template >> + constexpr ranges::subrange operator()(I first, S last, Pred pred, Proj proj = {}) const + { + first = ranges::find_if(first, last, etl::ref(pred), etl::ref(proj)); + + if (first != last) + { + I result = first; + + for (I it = result; ++it != last;) + { + if (!etl::invoke(pred, etl::invoke(proj, *it))) + { + *result = etl::move(*it); + ++result; + } + } + + return {result, last}; + } + + return {first, last}; + } + + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, Pred pred, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr remove_if_fn remove_if{}; + + struct remove_copy_fn + { + template >> + constexpr ranges::remove_copy_result operator()(I first, S last, O result, const T& value, Proj proj = {}) const + { + for (; first != last; ++first) + { + if (!(etl::invoke(proj, *first) == value)) + { + *result = *first; + ++result; + } + } + return {etl::move(first), etl::move(result)}; + } + + template >> + constexpr ranges::remove_copy_result, O> operator()(R&& r, O result, const T& value, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), value, etl::ref(proj)); + } + }; + + inline constexpr remove_copy_fn remove_copy{}; + + struct fill_fn + { + template >> + constexpr I operator()(I first, S last, const T& value) const + { + for (; first != last; ++first) + { + *first = value; + } + return first; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, const T& value) const + { + return (*this)(ranges::begin(r), ranges::end(r), value); + } + }; + + inline constexpr fill_fn fill{}; + + struct fill_n_fn + { + template + constexpr I operator()(I first, etl::iter_difference_t n, const T& value) const + { + for (; n-- > 0; ++first) + { + *first = value; + } + return first; + } + }; + + inline constexpr fill_n_fn fill_n{}; + + struct generate_fn + { + template >> + constexpr I operator()(I first, S last, F gen) const + { + for (; first != last; ++first) + { + *first = gen(); + } + return first; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, F gen) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(gen)); + } + }; + + inline constexpr generate_fn generate{}; + + struct generate_n_fn + { + template + constexpr I operator()(I first, etl::iter_difference_t n, F gen) const + { + for (; n-- > 0; ++first) + { + *first = gen(); + } + return first; + } + }; + + inline constexpr generate_n_fn generate_n{}; + + struct iota_fn + { + template >> + constexpr ranges::iota_result operator()(O first, S last, T value) const + { + while (first != last) + { + *first = value; + ++first; + ++value; + } + return {etl::move(first), etl::move(value)}; + } + + template >> + constexpr ranges::iota_result, T> operator()(R&& r, T value) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(value)); + } + }; + + inline constexpr iota_fn iota{}; + + struct unique_fn + { + template >> + constexpr ranges::subrange operator()(I first, S last, Pred pred = {}, Proj proj = {}) const + { + first = ranges::adjacent_find(first, last, etl::ref(pred), etl::ref(proj)); + + if (first != last) + { + I result = first; + ++first; + + while (++first != last) + { + if (!etl::invoke(pred, etl::invoke(proj, *result), etl::invoke(proj, *first))) + { + *++result = etl::move(*first); + } + } + + return {++result, last}; + } + + return {first, last}; + } + + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, Pred pred = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr unique_fn unique{}; + + struct unique_copy_fn + { + template >> + constexpr ranges::unique_copy_result operator()(I first, S last, O result, Pred pred = {}, Proj proj = {}) const + { + if (first != last) + { + *result = *first; + ++result; + + auto previous = first; + ++first; + + for (; first != last; ++first) + { + if (!etl::invoke(pred, etl::invoke(proj, *previous), etl::invoke(proj, *first))) + { + *result = *first; + ++result; + } + previous = first; + } + } + + return {etl::move(first), etl::move(result)}; + } + + template >> + constexpr ranges::unique_copy_result, O> operator()(R&& r, O result, Pred pred = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr unique_copy_fn unique_copy{}; + + struct transform_fn + { + // Unary: iterator + sentinel + template >> + constexpr ranges::unary_transform_result operator()(I first, S last, O result, F op, Proj proj = {}) const + { + for (; first != last; ++first, ++result) + { + *result = etl::invoke(op, etl::invoke(proj, *first)); + } + return {etl::move(first), etl::move(result)}; + } + + // Unary: range + template < class R, class O, class F, class Proj = etl::identity, typename = etl::enable_if_t && !etl::is_range_v>> + constexpr ranges::unary_transform_result, O> operator()(R&& r, O result, F op, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result), etl::ref(op), etl::ref(proj)); + } + + // Binary: iterator + sentinel + template >> + constexpr ranges::binary_transform_result operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, F op, Proj1 proj1 = {}, + Proj2 proj2 = {}) const + { + for (; first1 != last1 && first2 != last2; ++first1, ++first2, ++result) + { + *result = etl::invoke(op, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2)); + } + return {etl::move(first1), etl::move(first2), etl::move(result)}; + } + + // Binary: range + template < class R1, class R2, class O, class F, class Proj1 = etl::identity, class Proj2 = etl::identity, + typename = etl::enable_if_t && etl::is_range_v>> + constexpr ranges::binary_transform_result< ranges::borrowed_iterator_t, ranges::borrowed_iterator_t, O> + operator()(R1&& r1, R2&& r2, O result, F op, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(result), etl::ref(op), etl::ref(proj1), + etl::ref(proj2)); + } + }; + + inline constexpr transform_fn transform{}; + + struct reverse_fn + { + template >> + constexpr I operator()(I first, S last) const + { + I tail = ranges::next(first, last); + I result = tail; + + for (; first != tail && first != --tail; ++first) + { + etl::iter_swap(first, tail); + } + + return result; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r) const + { + return (*this)(ranges::begin(r), ranges::end(r)); + } + }; + + inline constexpr reverse_fn reverse{}; + + template + using reverse_copy_result = in_out_result; + + struct reverse_copy_fn + { + template >> + constexpr ranges::reverse_copy_result operator()(I first, S last, O result) const + { + I tail = ranges::next(first, last); + I end_it = tail; + + while (tail != first) + { + *result = *--tail; + ++result; + } + + return {etl::move(end_it), etl::move(result)}; + } + + template >> + constexpr ranges::reverse_copy_result, O> operator()(R&& r, O result) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(result)); + } + }; + + inline constexpr reverse_copy_fn reverse_copy{}; + + template + using rotate_result = ranges::subrange; + + struct rotate_fn + { + template >> + constexpr ranges::rotate_result operator()(I first, I middle, S last) const + { + if (first == middle) + { + I last_it = ranges::next(first, last); + return {last_it, last_it}; + } + + I last_it = ranges::next(first, last); + + if (middle == last_it) + { + return {first, last_it}; + } + + I new_first = etl::rotate(first, middle, last_it); + return {etl::move(new_first), etl::move(last_it)}; + } + + template >> + constexpr ranges::rotate_result> operator()(R&& r, ranges::iterator_t middle) const + { + return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r)); + } + }; + + inline constexpr rotate_fn rotate{}; + + struct rotate_copy_fn + { + template >> + constexpr ranges::rotate_copy_result operator()(I first, I middle, S last, O result) const + { + I last_it = ranges::next(first, last); + O end_out = etl::copy(middle, last_it, result); + end_out = etl::copy(first, middle, end_out); + return {etl::move(last_it), etl::move(end_out)}; + } + + template >> + constexpr ranges::rotate_copy_result, O> operator()(R&& r, ranges::iterator_t middle, O result) const + { + return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r), etl::move(result)); + } + }; + + inline constexpr rotate_copy_fn rotate_copy{}; + + struct shift_left_fn + { + template >> + constexpr ranges::subrange operator()(I first, S last, etl::iter_difference_t n) const + { + I last_it = ranges::next(first, last); + + if (n <= 0) + { + return {first, last_it}; + } + + I mid = first; + if (ranges::advance(mid, n, last_it) != 0) + { + return {first, first}; + } + + I result = ranges::move(mid, last_it, first).out; + return {first, etl::move(result)}; + } + + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, etl::ranges::range_difference_t n) const + { + return (*this)(ranges::begin(r), ranges::end(r), n); + } + }; + + inline constexpr shift_left_fn shift_left{}; + + struct shift_right_fn + { + template >> + constexpr ranges::subrange operator()(I first, S last, etl::iter_difference_t n) const + { + I last_it = ranges::next(first, last); + + if (n <= 0) + { + return {first, last_it}; + } + + I trail = last_it; + if (ranges::advance(trail, -n, first) != 0) + { + return {last_it, last_it}; + } + + I new_first = ranges::move_backward(first, trail, last_it).out; + return {etl::move(new_first), etl::move(last_it)}; + } + + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, etl::ranges::range_difference_t n) const + { + return (*this)(ranges::begin(r), ranges::end(r), n); + } + }; + + inline constexpr shift_right_fn shift_right{}; + + struct shuffle_fn + { + template >> + I operator()(I first, S last, Gen&& gen) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "shuffle requires random access iterators"); + + using diff_t = etl::iter_difference_t; + using udiff_t = etl::make_unsigned_t; + using gen_t = etl::remove_reference_t; + using uresult_t = decltype(gen()); + + I last_it = ranges::next(first, last); + diff_t n = last_it - first; + + if (n <= 1) + { + return last_it; + } + + for (diff_t i = n - 1; i > 0; --i) + { + // Generate a uniformly distributed random index in [0, i] + // using rejection sampling to avoid modulo bias. + udiff_t range = static_cast(i); + constexpr uresult_t gmin = gen_t::min(); + constexpr uresult_t gmax = gen_t::max(); + constexpr uresult_t grange = gmax - gmin; + + uresult_t j; + + if ETL_IF_CONSTEXPR (grange == static_cast(-1)) + { + // Generator covers full range of uresult_t, just use modulo with + // rejection + uresult_t limit = (static_cast(-1) / (static_cast(range) + 1)) * (static_cast(range) + 1); + do { j = static_cast(gen() - gmin); } while (j >= limit); j %= (static_cast(range) + 1); } - } - else - { - if (range == 0) - { - j = 0; - } else { uresult_t limit = (grange / (static_cast(range) + 1)) * (static_cast(range) + 1); - do - { + do { j = static_cast(gen() - gmin); } while (j >= limit); j %= (static_cast(range) + 1); } + + etl::iter_swap(first + i, first + static_cast(j)); } - if (static_cast(j) < needed) - { - *out = *first; - ++out; - --needed; - } - } - - return out; - } - - template>> - O operator()(R&& r, O out, etl::ranges::range_difference_t n, Gen&& gen) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(out), n, static_cast(gen)); - } - }; - - inline constexpr sample_fn sample {}; - - struct sort_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "sort requires random access iterators"); - - I last_it = ranges::next(first, last); - - if (first == last_it) - { return last_it; } - // Shell sort with projection support - auto n = etl::distance(first, last_it); - - for (auto gap = n / 2; gap > 0; gap /= 2) + template >> + ranges::borrowed_iterator_t operator()(R&& r, Gen&& gen) const { - for (auto i = gap; i < n; ++i) - { - auto temp = etl::move(*(first + i)); - auto j = i; + ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "shuffle requires a range with random access iterators"); - while (j >= gap && etl::invoke(comp, etl::invoke(proj, temp), etl::invoke(proj, *(first + (j - gap))))) + return (*this)(ranges::begin(r), ranges::end(r), static_cast(gen)); + } + }; + + inline constexpr shuffle_fn shuffle{}; + + struct sample_fn + { + template >> + O operator()(I first, S last, O out, etl::iter_difference_t n, Gen&& gen) const + { + using diff_t = etl::iter_difference_t; + using udiff_t = etl::make_unsigned_t; + using gen_t = etl::remove_reference_t; + using uresult_t = decltype(gen()); + + if (n <= 0) + { + return out; + } + + // Compute the size of [first, last). + I first_copy = first; + diff_t pop_size = 0; + for (I it = first_copy; it != last; ++it) + { + ++pop_size; + } + + if (pop_size <= n) + { + // Copy all elements. + for (; first != last; ++first, ++out) { - *(first + j) = etl::move(*(first + (j - gap))); - j -= gap; + *out = *first; } - - *(first + j) = etl::move(temp); + return out; } - } - return last_it; - } + // Selection sampling (Algorithm S / Vitter). + // For each element, decide whether to include it. + diff_t remaining = pop_size; + diff_t needed = n; - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "sort requires a range with random access iterators"); - - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr sort_fn sort {}; - - struct stable_sort_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - I last_it = ranges::next(first, last); - - if (first == last_it) - { - return last_it; - } - - // Insertion sort with projection support (stable) - for (I i = ranges::next(first); i != last_it; ++i) - { - auto temp = etl::move(*i); - I j = i; - - while (j != first) + for (; first != last && needed > 0; ++first, --remaining) { - I prev_j = ranges::prev(j); - if (etl::invoke(comp, etl::invoke(proj, temp), etl::invoke(proj, *prev_j))) + // Generate a uniformly distributed random number in [0, remaining). + udiff_t range = static_cast(remaining - 1); + constexpr uresult_t gmin = gen_t::min(); + constexpr uresult_t gmax = gen_t::max(); + constexpr uresult_t grange = gmax - gmin; + + uresult_t j; + + if ETL_IF_CONSTEXPR (grange == static_cast(-1)) { - *j = etl::move(*prev_j); - j = prev_j; + if (range == 0) + { + j = 0; + } + else + { + uresult_t limit = (static_cast(-1) / (static_cast(range) + 1)) * (static_cast(range) + 1); + do { + j = static_cast(gen() - gmin); + } while (j >= limit); + j %= (static_cast(range) + 1); + } } else + { + if (range == 0) + { + j = 0; + } + else + { + uresult_t limit = (grange / (static_cast(range) + 1)) * (static_cast(range) + 1); + do { + j = static_cast(gen() - gmin); + } while (j >= limit); + j %= (static_cast(range) + 1); + } + } + + if (static_cast(j) < needed) + { + *out = *first; + ++out; + --needed; + } + } + + return out; + } + + template >> + O operator()(R&& r, O out, etl::ranges::range_difference_t n, Gen&& gen) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(out), n, static_cast(gen)); + } + }; + + inline constexpr sample_fn sample{}; + + struct sort_fn + { + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "sort requires random access iterators"); + + I last_it = ranges::next(first, last); + + if (first == last_it) + { + return last_it; + } + + // Shell sort with projection support + auto n = etl::distance(first, last_it); + + for (auto gap = n / 2; gap > 0; gap /= 2) + { + for (auto i = gap; i < n; ++i) + { + auto temp = etl::move(*(first + i)); + auto j = i; + + while (j >= gap && etl::invoke(comp, etl::invoke(proj, temp), etl::invoke(proj, *(first + (j - gap))))) + { + *(first + j) = etl::move(*(first + (j - gap))); + j -= gap; + } + + *(first + j) = etl::move(temp); + } + } + + return last_it; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "sort requires a range with random access iterators"); + + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr sort_fn sort{}; + + struct stable_sort_fn + { + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + I last_it = ranges::next(first, last); + + if (first == last_it) + { + return last_it; + } + + // Insertion sort with projection support (stable) + for (I i = ranges::next(first); i != last_it; ++i) + { + auto temp = etl::move(*i); + I j = i; + + while (j != first) + { + I prev_j = ranges::prev(j); + if (etl::invoke(comp, etl::invoke(proj, temp), etl::invoke(proj, *prev_j))) + { + *j = etl::move(*prev_j); + j = prev_j; + } + else + { + break; + } + } + + *j = etl::move(temp); + } + + return last_it; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr stable_sort_fn stable_sort{}; + + struct partial_sort_fn + { + template >> + constexpr I operator()(I first, I middle, S last, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "partial_sort requires random access iterators"); + + I last_it = ranges::next(first, last); + + if (first == middle || first == last_it) + { + return last_it; + } + + // Build a max-heap on [first, middle) + auto heap_size = etl::distance(first, middle); + + // Heapify: process from the last parent down to 0 + for (auto start = (heap_size - 1) / 2; start >= 0; --start) + { + sift_down(first, start, heap_size, comp, proj); + } + + // For each element in [middle, last_it), if it is smaller than the heap + // root, swap it in and re-heapify + for (I it = middle; it != last_it; ++it) + { + if (etl::invoke(comp, etl::invoke(proj, *it), etl::invoke(proj, *first))) + { + etl::iter_swap(it, first); + sift_down(first, decltype(heap_size){0}, heap_size, comp, proj); + } + } + + // Sort the heap to produce a sorted [first, middle) + // Repeatedly extract the max from the heap + for (auto heap_end = heap_size - 1; heap_end > 0; --heap_end) + { + etl::iter_swap(first, first + heap_end); + sift_down(first, decltype(heap_size){0}, heap_end, comp, proj); + } + + return last_it; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, ranges::iterator_t middle, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "partial_sort requires a range with random access iterators"); + + return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r), etl::move(comp), etl::move(proj)); + } + + private: + + template + static constexpr void sift_down(I first, DiffType index, DiffType heap_size, Comp& comp, Proj& proj) + { + while (true) + { + auto largest = index; + auto left = 2 * index + 1; + auto right = 2 * index + 2; + + if (left < heap_size && etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + left)))) + { + largest = left; + } + + if (right < heap_size && etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + right)))) + { + largest = right; + } + + if (largest == index) + { + break; + } + + etl::iter_swap(first + index, first + largest); + index = largest; + } + } + }; + + inline constexpr partial_sort_fn partial_sort{}; + + struct partial_sort_copy_fn + { + template >> + constexpr ranges::partial_sort_copy_result operator()(I1 first, S1 last, I2 result_first, S2 result_last, Comp comp = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "partial_sort_copy requires the output to be random access iterators"); + + I1 in_last = ranges::next(first, last); + I2 out_last = ranges::next(result_first, result_last); + + I2 r = result_first; + + // Copy elements from the input range into the output range + for (I1 it = first; it != in_last && r != out_last; ++it, ++r) + { + *r = *it; + } + + auto heap_size = etl::distance(result_first, r); + + if (heap_size == 0) + { + return {etl::move(in_last), etl::move(r)}; + } + + // Build a max-heap on [result_first, r) + for (auto start = (heap_size - 1) / 2; start >= 0; --start) + { + sift_down(result_first, start, heap_size, comp, proj2); + } + + // For remaining elements in [first + heap_size, in_last), if smaller + // than the heap root, swap it in and re-heapify + I1 it = first; + etl::advance(it, heap_size); + for (; it != in_last; ++it) + { + if (etl::invoke(comp, etl::invoke(proj1, *it), etl::invoke(proj2, *result_first))) + { + *result_first = *it; + sift_down(result_first, decltype(heap_size){0}, heap_size, comp, proj2); + } + } + + // Sort the heap to produce a sorted output range + for (auto heap_end = heap_size - 1; heap_end > 0; --heap_end) + { + etl::iter_swap(result_first, result_first + heap_end); + sift_down(result_first, decltype(heap_size){0}, heap_end, comp, proj2); + } + + return {etl::move(in_last), etl::move(r)}; + } + + template >> + constexpr ranges::partial_sort_copy_result< ranges::borrowed_iterator_t, ranges::borrowed_iterator_t> + operator()(R1&& r, R2&& result_r, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, + "partial_sort_copy requires the output range to have random access iterators"); + + return (*this)(ranges::begin(r), ranges::end(r), ranges::begin(result_r), ranges::end(result_r), etl::move(comp), etl::move(proj1), + etl::move(proj2)); + } + + private: + + template + static constexpr void sift_down(I first, DiffType index, DiffType heap_size, Comp& comp, Proj& proj) + { + while (true) + { + auto largest = index; + auto left = 2 * index + 1; + auto right = 2 * index + 2; + + if (left < heap_size && etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + left)))) + { + largest = left; + } + + if (right < heap_size && etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + right)))) + { + largest = right; + } + + if (largest == index) + { + break; + } + + etl::iter_swap(first + index, first + largest); + index = largest; + } + } + }; + + inline constexpr partial_sort_copy_fn partial_sort_copy{}; + + struct nth_element_fn + { + template >> + constexpr I operator()(I first, I nth, S last, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "nth_element requires random access iterators"); + + I last_it = ranges::next(first, last); + + if (first == last_it || ranges::next(first) == last_it) + { + return last_it; + } + + I lo = first; + I hi = ranges::prev(last_it); + + while (lo <= hi) + { + I p = nth_partition(lo, hi, comp, proj); + + if (p == nth) + { + return last_it; + } + else if (p > nth) + { + hi = ranges::prev(p); + } + else + { + lo = ranges::next(p); + } + } + + return last_it; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, ranges::iterator_t nth, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "nth_element requires a range with random access iterators"); + + return (*this)(ranges::begin(r), etl::move(nth), ranges::end(r), etl::move(comp), etl::move(proj)); + } + + private: + + template + static constexpr I nth_partition(I first, I last, Comp& comp, Proj& proj) + { + if (first == last) + { + return first; + } + + if (last - first == 1) + { + if (etl::invoke(comp, etl::invoke(proj, *last), etl::invoke(proj, *first))) + { + etl::iter_swap(first, last); + } + return first; + } + + // Median-of-three pivot selection + I mid = first + (last - first) / 2; + + if (etl::invoke(comp, etl::invoke(proj, *mid), etl::invoke(proj, *first))) + { + etl::iter_swap(first, mid); + } + + if (etl::invoke(comp, etl::invoke(proj, *last), etl::invoke(proj, *first))) + { + etl::iter_swap(first, last); + } + + if (etl::invoke(comp, etl::invoke(proj, *mid), etl::invoke(proj, *last))) + { + etl::iter_swap(mid, last); + } + + // Pivot is now at *last + I i = first; + I j = last; + + while (true) + { + while (etl::invoke(comp, etl::invoke(proj, *i), etl::invoke(proj, *last))) + { + ++i; + } + + --j; + + while (i < j && etl::invoke(comp, etl::invoke(proj, *last), etl::invoke(proj, *j))) + { + --j; + } + + if (i >= j) + { + break; + } + + etl::iter_swap(i, j); + ++i; + } + + etl::iter_swap(i, last); + return i; + } + }; + + inline constexpr nth_element_fn nth_element{}; + + struct partition_fn + { + template >> + constexpr ranges::subrange operator()(I first, S last, Pred pred, Proj proj = {}) const + { + first = ranges::find_if_not(first, last, etl::ref(pred), etl::ref(proj)); + + if (first == last) + { + return {first, first}; + } + + for (I i = ranges::next(first); i != last; ++i) + { + if (etl::invoke(pred, etl::invoke(proj, *i))) + { + etl::iter_swap(i, first); + ++first; + } + } + + return {first, ranges::next(first, last)}; + } + + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, Pred pred, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr partition_fn partition{}; + + struct is_partitioned_fn + { + template >> + constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const + { + for (; first != last; ++first) + { + if (!etl::invoke(pred, etl::invoke(proj, *first))) { break; } } - *j = etl::move(temp); + for (; first != last; ++first) + { + if (etl::invoke(pred, etl::invoke(proj, *first))) + { + return false; + } + } + + return true; } - return last_it; - } + template >> + constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); + } + }; - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const + inline constexpr is_partitioned_fn is_partitioned{}; + + struct partition_copy_fn { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; + template >> + constexpr ranges::partition_copy_result operator()(I first, S last, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const + { + for (; first != last; ++first) + { + if (etl::invoke(pred, etl::invoke(proj, *first))) + { + *out_true = *first; + ++out_true; + } + else + { + *out_false = *first; + ++out_false; + } + } - inline constexpr stable_sort_fn stable_sort {}; + return {etl::move(first), etl::move(out_true), etl::move(out_false)}; + } - struct partial_sort_fn - { - template>> - constexpr I operator()(I first, I middle, S last, Comp comp = {}, Proj proj = {}) const + template >> + constexpr ranges::partition_copy_result, O1, O2> operator()(R&& r, O1 out_true, O2 out_false, Pred pred, + Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(out_true), etl::move(out_false), etl::ref(pred), etl::ref(proj)); + } + }; + + inline constexpr partition_copy_fn partition_copy{}; + + struct partition_point_fn { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "partial_sort requires random access iterators"); - - I last_it = ranges::next(first, last); - - if (first == middle || first == last_it) + template >> + constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const { - return last_it; - } - - // Build a max-heap on [first, middle) - auto heap_size = etl::distance(first, middle); - - // Heapify: process from the last parent down to 0 - for (auto start = (heap_size - 1) / 2; start >= 0; --start) - { - sift_down(first, start, heap_size, comp, proj); - } - - // For each element in [middle, last_it), if it is smaller than the heap root, - // swap it in and re-heapify - for (I it = middle; it != last_it; ++it) - { - if (etl::invoke(comp, etl::invoke(proj, *it), etl::invoke(proj, *first))) + for (; first != last; ++first) { - etl::iter_swap(it, first); - sift_down(first, decltype(heap_size){0}, heap_size, comp, proj); - } - } - - // Sort the heap to produce a sorted [first, middle) - // Repeatedly extract the max from the heap - for (auto heap_end = heap_size - 1; heap_end > 0; --heap_end) - { - etl::iter_swap(first, first + heap_end); - sift_down(first, decltype(heap_size){0}, heap_end, comp, proj); - } - - return last_it; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, ranges::iterator_t middle, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "partial_sort requires a range with random access iterators"); - - return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r), etl::move(comp), etl::move(proj)); - } - - private: - - template - static constexpr void sift_down(I first, DiffType index, DiffType heap_size, Comp& comp, Proj& proj) - { - while (true) - { - auto largest = index; - auto left = 2 * index + 1; - auto right = 2 * index + 2; - - if (left < heap_size && - etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + left)))) - { - largest = left; + if (!etl::invoke(pred, etl::invoke(proj, *first))) + { + return first; + } } - if (right < heap_size && - etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + right)))) - { - largest = right; - } - - if (largest == index) - { - break; - } - - etl::iter_swap(first + index, first + largest); - index = largest; - } - } - }; - - inline constexpr partial_sort_fn partial_sort {}; - - struct partial_sort_copy_fn - { - template>> - constexpr ranges::partial_sort_copy_result - operator()(I1 first, S1 last, I2 result_first, S2 result_last, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "partial_sort_copy requires the output to be random access iterators"); - - I1 in_last = ranges::next(first, last); - I2 out_last = ranges::next(result_first, result_last); - - I2 r = result_first; - - // Copy elements from the input range into the output range - for (I1 it = first; it != in_last && r != out_last; ++it, ++r) - { - *r = *it; - } - - auto heap_size = etl::distance(result_first, r); - - if (heap_size == 0) - { - return {etl::move(in_last), etl::move(r)}; - } - - // Build a max-heap on [result_first, r) - for (auto start = (heap_size - 1) / 2; start >= 0; --start) - { - sift_down(result_first, start, heap_size, comp, proj2); - } - - // For remaining elements in [first + heap_size, in_last), if smaller than - // the heap root, swap it in and re-heapify - I1 it = first; - etl::advance(it, heap_size); - for (; it != in_last; ++it) - { - if (etl::invoke(comp, etl::invoke(proj1, *it), etl::invoke(proj2, *result_first))) - { - *result_first = *it; - sift_down(result_first, decltype(heap_size){0}, heap_size, comp, proj2); - } - } - - // Sort the heap to produce a sorted output range - for (auto heap_end = heap_size - 1; heap_end > 0; --heap_end) - { - etl::iter_swap(result_first, result_first + heap_end); - sift_down(result_first, decltype(heap_size){0}, heap_end, comp, proj2); - } - - return {etl::move(in_last), etl::move(r)}; - } - - template>> - constexpr ranges::partial_sort_copy_result, ranges::borrowed_iterator_t> - operator()(R1&& r, R2&& result_r, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "partial_sort_copy requires the output range to have random access iterators"); - - return (*this)(ranges::begin(r), ranges::end(r), ranges::begin(result_r), ranges::end(result_r), etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - - private: - - template - static constexpr void sift_down(I first, DiffType index, DiffType heap_size, Comp& comp, Proj& proj) - { - while (true) - { - auto largest = index; - auto left = 2 * index + 1; - auto right = 2 * index + 2; - - if (left < heap_size && - etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + left)))) - { - largest = left; - } - - if (right < heap_size && - etl::invoke(comp, etl::invoke(proj, *(first + largest)), etl::invoke(proj, *(first + right)))) - { - largest = right; - } - - if (largest == index) - { - break; - } - - etl::iter_swap(first + index, first + largest); - index = largest; - } - } - }; - - inline constexpr partial_sort_copy_fn partial_sort_copy {}; - - struct nth_element_fn - { - template>> - constexpr I operator()(I first, I nth, S last, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "nth_element requires random access iterators"); - - I last_it = ranges::next(first, last); - - if (first == last_it || ranges::next(first) == last_it) - { - return last_it; - } - - I lo = first; - I hi = ranges::prev(last_it); - - while (lo <= hi) - { - I p = nth_partition(lo, hi, comp, proj); - - if (p == nth) - { - return last_it; - } - else if (p > nth) - { - hi = ranges::prev(p); - } - else - { - lo = ranges::next(p); - } - } - - return last_it; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, ranges::iterator_t nth, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator>::value, "nth_element requires a range with random access iterators"); - - return (*this)(ranges::begin(r), etl::move(nth), ranges::end(r), etl::move(comp), etl::move(proj)); - } - - private: - - template - static constexpr I nth_partition(I first, I last, Comp& comp, Proj& proj) - { - if (first == last) - { return first; } - if (last - first == 1) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Pred pred, Proj proj = {}) const { - if (etl::invoke(comp, etl::invoke(proj, *last), etl::invoke(proj, *first))) - { - etl::iter_swap(first, last); - } - return first; + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); } + }; - // Median-of-three pivot selection - I mid = first + (last - first) / 2; + inline constexpr partition_point_fn partition_point{}; - if (etl::invoke(comp, etl::invoke(proj, *mid), etl::invoke(proj, *first))) - { - etl::iter_swap(first, mid); - } - - if (etl::invoke(comp, etl::invoke(proj, *last), etl::invoke(proj, *first))) - { - etl::iter_swap(first, last); - } - - if (etl::invoke(comp, etl::invoke(proj, *mid), etl::invoke(proj, *last))) - { - etl::iter_swap(mid, last); - } - - // Pivot is now at *last - I i = first; - I j = last; - - while (true) - { - while (etl::invoke(comp, etl::invoke(proj, *i), etl::invoke(proj, *last))) - { - ++i; - } - - --j; - - while (i < j && etl::invoke(comp, etl::invoke(proj, *last), etl::invoke(proj, *j))) - { - --j; - } - - if (i >= j) - { - break; - } - - etl::iter_swap(i, j); - ++i; - } - - etl::iter_swap(i, last); - return i; - } - }; - - inline constexpr nth_element_fn nth_element {}; - - struct partition_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, Pred pred, Proj proj = {}) const + struct stable_partition_fn { - first = ranges::find_if_not(first, last, etl::ref(pred), etl::ref(proj)); - - if (first == last) + template >> + constexpr ranges::subrange operator()(I first, S last, Pred pred, Proj proj = {}) const { - return {first, first}; + // Find the first element that does not satisfy the predicate + first = ranges::find_if_not(first, last, etl::ref(pred), etl::ref(proj)); + + if (first == last) + { + return {first, first}; + } + + I last_it = ranges::next(first, last); + + I pp = stable_partition_impl(first, last_it, etl::ref(pred), etl::ref(proj), etl::distance(first, last_it)); + + return {pp, last_it}; } - for (I i = ranges::next(first); i != last; ++i) + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, Pred pred, Proj proj = {}) const { - if (etl::invoke(pred, etl::invoke(proj, *i))) - { - etl::iter_swap(i, first); - ++first; - } + return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); } - return {first, ranges::next(first, last)}; - } + private: - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr partition_fn partition {}; - - struct is_partitioned_fn - { - template>> - constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const - { - for (; first != last; ++first) + template + static constexpr I stable_partition_impl(I first, I last, Pred pred, Proj proj, typename etl::iterator_traits::difference_type len) { - if (!etl::invoke(pred, etl::invoke(proj, *first))) - { - break; - } - } - - for (; first != last; ++first) - { - if (etl::invoke(pred, etl::invoke(proj, *first))) - { - return false; - } - } - - return true; - } - - template>> - constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr is_partitioned_fn is_partitioned {}; - - struct partition_copy_fn - { - template>> - constexpr ranges::partition_copy_result - operator()(I first, S last, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const - { - for (; first != last; ++first) - { - if (etl::invoke(pred, etl::invoke(proj, *first))) - { - *out_true = *first; - ++out_true; - } - else - { - *out_false = *first; - ++out_false; - } - } - - return {etl::move(first), etl::move(out_true), etl::move(out_false)}; - } - - template>> - constexpr ranges::partition_copy_result, O1, O2> - operator()(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(out_true), etl::move(out_false), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr partition_copy_fn partition_copy {}; - - struct partition_point_fn - { - template>> - constexpr I operator()(I first, S last, Pred pred, Proj proj = {}) const - { - for (; first != last; ++first) - { - if (!etl::invoke(pred, etl::invoke(proj, *first))) + if (len == 0) { return first; } + + if (len == 1) + { + return etl::invoke(pred, etl::invoke(proj, *first)) ? ranges::next(first) : first; + } + + I middle = ranges::next(first, len / 2); + + I left_partition = stable_partition_impl(first, middle, etl::ref(pred), etl::ref(proj), len / 2); + I right_partition = stable_partition_impl(middle, last, etl::ref(pred), etl::ref(proj), len - len / 2); + + return etl::rotate(left_partition, middle, right_partition); + } + }; + + inline constexpr stable_partition_fn stable_partition{}; + + struct is_sorted_until_fn + { + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + if (first != last) + { + I next_it = ranges::next(first); + + while (next_it != last) + { + if (etl::invoke(comp, etl::invoke(proj, *next_it), etl::invoke(proj, *first))) + { + return next_it; + } + + first = next_it; + ++next_it; + } + } + + return ranges::next(first, last); } - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - }; - - inline constexpr partition_point_fn partition_point {}; - - struct stable_partition_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, Pred pred, Proj proj = {}) const - { - // Find the first element that does not satisfy the predicate - first = ranges::find_if_not(first, last, etl::ref(pred), etl::ref(proj)); - - if (first == last) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - return {first, first}; + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr is_sorted_until_fn is_sorted_until{}; + + struct is_sorted_fn + { + template >> + constexpr bool operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + return ranges::is_sorted_until(first, last, etl::ref(comp), etl::ref(proj)) == last; } - I last_it = ranges::next(first, last); - - I pp = stable_partition_impl(first, last_it, etl::ref(pred), etl::ref(proj), etl::distance(first, last_it)); - - return {pp, last_it}; - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, Pred pred, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::ref(pred), etl::ref(proj)); - } - - private: - - template - static constexpr I stable_partition_impl(I first, I last, Pred pred, Proj proj, typename etl::iterator_traits::difference_type len) - { - if (len == 0) + template >> + constexpr bool operator()(R&& r, Comp comp = {}, Proj proj = {}) const { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr is_sorted_fn is_sorted{}; + + struct lower_bound_fn + { + template >> + constexpr I operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const + { + auto len = etl::distance(first, last); + + while (len > 0) + { + auto half = len / 2; + I middle = ranges::next(first, half); + + if (etl::invoke(comp, etl::invoke(proj, *middle), value)) + { + first = ranges::next(middle); + len -= half + 1; + } + else + { + len = half; + } + } + return first; } - if (len == 1) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const { - return etl::invoke(pred, etl::invoke(proj, *first)) ? ranges::next(first) : first; + return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr lower_bound_fn lower_bound{}; + + struct upper_bound_fn + { + template >> + constexpr I operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const + { + auto len = etl::distance(first, last); + + while (len > 0) + { + auto half = len / 2; + I middle = ranges::next(first, half); + + if (!etl::invoke(comp, value, etl::invoke(proj, *middle))) + { + first = ranges::next(middle); + len -= half + 1; + } + else + { + len = half; + } + } + + return first; } - I middle = ranges::next(first, len / 2); - - I left_partition = stable_partition_impl(first, middle, etl::ref(pred), etl::ref(proj), len / 2); - I right_partition = stable_partition_impl(middle, last, etl::ref(pred), etl::ref(proj), len - len / 2); - - return etl::rotate(left_partition, middle, right_partition); - } - }; - - inline constexpr stable_partition_fn stable_partition {}; - - struct is_sorted_until_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - if (first != last) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const { - I next_it = ranges::next(first); + return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); + } + }; - while (next_it != last) + inline constexpr upper_bound_fn upper_bound{}; + + struct equal_range_fn + { + template >> + constexpr ranges::subrange operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const + { + return {ranges::lower_bound(first, last, value, etl::ref(comp), etl::ref(proj)), + ranges::upper_bound(first, last, value, etl::ref(comp), etl::ref(proj))}; + } + + template >> + constexpr ranges::borrowed_subrange_t operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr equal_range_fn equal_range{}; + + struct binary_search_fn + { + template >> + ETL_NODISCARD + constexpr bool operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const + { + first = ranges::lower_bound(first, last, value, etl::ref(comp), etl::ref(proj)); + + return (!(first == last) && !(etl::invoke(comp, value, etl::invoke(proj, *first)))); + } + + template >> + ETL_NODISCARD + constexpr bool operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr binary_search_fn binary_search{}; + + struct includes_fn + { + template >> + ETL_NODISCARD + constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + for (; first2 != last2; ++first1) { - if (etl::invoke(comp, etl::invoke(proj, *next_it), etl::invoke(proj, *first))) + if (first1 == last1) { - return next_it; + return false; } - first = next_it; - ++next_it; - } - } + if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + { + return false; + } - return ranges::next(first, last); - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr is_sorted_until_fn is_sorted_until {}; - - struct is_sorted_fn - { - template>> - constexpr bool operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - return ranges::is_sorted_until(first, last, etl::ref(comp), etl::ref(proj)) == last; - } - - template>> - constexpr bool operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr is_sorted_fn is_sorted {}; - - struct lower_bound_fn - { - template>> - constexpr I operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const - { - auto len = etl::distance(first, last); - - while (len > 0) - { - auto half = len / 2; - I middle = ranges::next(first, half); - - if (etl::invoke(comp, etl::invoke(proj, *middle), value)) - { - first = ranges::next(middle); - len -= half + 1; - } - else - { - len = half; - } - } - - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr lower_bound_fn lower_bound {}; - - struct upper_bound_fn - { - template>> - constexpr I operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const - { - auto len = etl::distance(first, last); - - while (len > 0) - { - auto half = len / 2; - I middle = ranges::next(first, half); - - if (!etl::invoke(comp, value, etl::invoke(proj, *middle))) - { - first = ranges::next(middle); - len -= half + 1; - } - else - { - len = half; - } - } - - return first; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr upper_bound_fn upper_bound {}; - - struct equal_range_fn - { - template>> - constexpr ranges::subrange - operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const - { - return {ranges::lower_bound(first, last, value, etl::ref(comp), etl::ref(proj)), - ranges::upper_bound(first, last, value, etl::ref(comp), etl::ref(proj))}; - } - - template>> - constexpr ranges::borrowed_subrange_t - operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr equal_range_fn equal_range {}; - - struct binary_search_fn - { - template>> - ETL_NODISCARD - constexpr bool operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const - { - first = ranges::lower_bound(first, last, value, etl::ref(comp), etl::ref(proj)); - - return (!(first == last) && !(etl::invoke(comp, value, etl::invoke(proj, *first)))); - } - - template>> - ETL_NODISCARD - constexpr bool operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), value, etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr binary_search_fn binary_search {}; - - struct includes_fn - { - template>> - ETL_NODISCARD - constexpr bool - operator()(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - for (; first2 != last2; ++first1) - { - if (first1 == last1) - { - return false; + if (!etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + ++first2; + } } - if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) - { - return false; - } - - if (!etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - ++first2; - } + return true; } - return true; - } + template >> + ETL_NODISCARD + constexpr bool operator()(R1&& r1, R2&& r2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(comp), etl::move(proj1), etl::move(proj2)); + } + }; - template>> - ETL_NODISCARD - constexpr bool - operator()(R1&& r1, R2&& r2, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const + inline constexpr includes_fn includes{}; + + struct merge_fn { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr includes_fn includes {}; - - struct merge_fn - { - template>> - constexpr ranges::merge_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - while (first1 != last1 && first2 != last2) + template >> + constexpr ranges::merge_result operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, + Proj2 proj2 = {}) const { - if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + while (first1 != last1 && first2 != last2) { - *result = *first2; - ++first2; - } - else - { - *result = *first1; - ++first1; - } - ++result; - } - - while (first1 != last1) - { - *result = *first1; - ++first1; - ++result; - } - - while (first2 != last2) - { - *result = *first2; - ++first2; - ++result; - } - - return {etl::move(first1), etl::move(first2), etl::move(result)}; - } - - template>> - constexpr ranges::merge_result, ranges::borrowed_iterator_t, O> - operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(result), etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr merge_fn merge {}; - - struct inplace_merge_fn - { - template>> - constexpr I operator()(I first, I middle, S last, Comp comp = {}, Proj proj = {}) const - { - I last_it = ranges::next(first, last); - - if (first == middle || middle == last_it) - { - return last_it; - } - - inplace_merge_impl(first, middle, last_it, comp, proj, - etl::distance(first, middle), - etl::distance(middle, last_it)); - - return last_it; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, ranges::iterator_t middle, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r), etl::move(comp), etl::move(proj)); - } - - private: - - template - static constexpr void inplace_merge_impl(I first, I middle, I last, Comp& comp, Proj& proj, - typename etl::iterator_traits::difference_type len1, - typename etl::iterator_traits::difference_type len2) - { - if (len1 == 0 || len2 == 0) - { - return; - } - - if (len1 + len2 == 2) - { - if (etl::invoke(comp, etl::invoke(proj, *middle), etl::invoke(proj, *first))) - { - etl::iter_swap(first, middle); - } - return; - } - - I first_cut; - I second_cut; - typename etl::iterator_traits::difference_type new_len1; - typename etl::iterator_traits::difference_type new_len2; - - if (len1 > len2) - { - new_len1 = len1 / 2; - first_cut = ranges::next(first, new_len1); - second_cut = ranges::lower_bound(middle, last, etl::invoke(proj, *first_cut), etl::ref(comp), etl::ref(proj)); - new_len2 = etl::distance(middle, second_cut); - } - else - { - new_len2 = len2 / 2; - second_cut = ranges::next(middle, new_len2); - first_cut = ranges::upper_bound(first, middle, etl::invoke(proj, *second_cut), etl::ref(comp), etl::ref(proj)); - new_len1 = etl::distance(first, first_cut); - } - - I new_middle; - // Due to a non-standard etl::rotate implementation, we need to handle - // the case where one of the cuts is the middle separately to avoid - // returning an iterator outside of [first, last) - // As soon as etl::rotate is fixed to return an iterator in the middle - // of the rotated range, this can be simplified to just calling etl::rotate - if (first_cut == middle) - { - new_middle = second_cut; - } - else if (second_cut == middle) - { - new_middle = first_cut; - } - else - { - new_middle = etl::rotate(first_cut, middle, second_cut); - } - - inplace_merge_impl(first, first_cut, new_middle, comp, proj, - new_len1, new_len2); - inplace_merge_impl(new_middle, second_cut, last, comp, proj, - len1 - new_len1, len2 - new_len2); - } - }; - - inline constexpr inplace_merge_fn inplace_merge {}; - - struct set_union_fn - { - template>> - constexpr ranges::set_union_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - while (first1 != last1 && first2 != last2) - { - if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - *result = *first1; - ++first1; - } - else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) - { - *result = *first2; - ++first2; - } - else - { - *result = *first1; - ++first1; - ++first2; - } - ++result; - } - - while (first1 != last1) - { - *result = *first1; - ++first1; - ++result; - } - - while (first2 != last2) - { - *result = *first2; - ++first2; - ++result; - } - - return {etl::move(first1), etl::move(first2), etl::move(result)}; - } - - template>> - constexpr ranges::set_union_result, ranges::borrowed_iterator_t, O> - operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(result), etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr set_union_fn set_union {}; - - struct set_intersection_fn - { - template>> - constexpr ranges::set_intersection_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - while (first1 != last1 && first2 != last2) - { - if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - ++first1; - } - else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) - { - ++first2; - } - else - { - *result = *first1; - ++first1; - ++first2; + if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + { + *result = *first2; + ++first2; + } + else + { + *result = *first1; + ++first1; + } ++result; } - } - return {etl::move(first1), etl::move(first2), etl::move(result)}; - } - - template>> - constexpr ranges::set_intersection_result, ranges::borrowed_iterator_t, O> - operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(result), etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr set_intersection_fn set_intersection {}; - - struct set_difference_fn - { - template>> - constexpr ranges::set_difference_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - while (first1 != last1 && first2 != last2) - { - if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + while (first1 != last1) { *result = *first1; ++first1; ++result; } - else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) - { - ++first2; - } - else - { - ++first1; - ++first2; - } - } - while (first1 != last1) - { - *result = *first1; - ++first1; - ++result; - } - - return {etl::move(first1), etl::move(result)}; - } - - template>> - constexpr ranges::set_difference_result, O> - operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(result), etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr set_difference_fn set_difference {}; - - struct set_symmetric_difference_fn - { - template>> - constexpr ranges::set_symmetric_difference_result - operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const - { - while (first1 != last1 && first2 != last2) - { - if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) - { - *result = *first1; - ++first1; - ++result; - } - else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + while (first2 != last2) { *result = *first2; ++first2; ++result; } + + return {etl::move(first1), etl::move(first2), etl::move(result)}; + } + + template >> + constexpr ranges::merge_result, ranges::borrowed_iterator_t, O> + operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(result), etl::move(comp), etl::move(proj1), + etl::move(proj2)); + } + }; + + inline constexpr merge_fn merge{}; + + struct inplace_merge_fn + { + template >> + constexpr I operator()(I first, I middle, S last, Comp comp = {}, Proj proj = {}) const + { + I last_it = ranges::next(first, last); + + if (first == middle || middle == last_it) + { + return last_it; + } + + inplace_merge_impl(first, middle, last_it, comp, proj, etl::distance(first, middle), etl::distance(middle, last_it)); + + return last_it; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, ranges::iterator_t middle, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), etl::move(middle), ranges::end(r), etl::move(comp), etl::move(proj)); + } + + private: + + template + static constexpr void inplace_merge_impl(I first, I middle, I last, Comp& comp, Proj& proj, + typename etl::iterator_traits::difference_type len1, + typename etl::iterator_traits::difference_type len2) + { + if (len1 == 0 || len2 == 0) + { + return; + } + + if (len1 + len2 == 2) + { + if (etl::invoke(comp, etl::invoke(proj, *middle), etl::invoke(proj, *first))) + { + etl::iter_swap(first, middle); + } + return; + } + + I first_cut; + I second_cut; + typename etl::iterator_traits::difference_type new_len1; + typename etl::iterator_traits::difference_type new_len2; + + if (len1 > len2) + { + new_len1 = len1 / 2; + first_cut = ranges::next(first, new_len1); + second_cut = ranges::lower_bound(middle, last, etl::invoke(proj, *first_cut), etl::ref(comp), etl::ref(proj)); + new_len2 = etl::distance(middle, second_cut); + } else { + new_len2 = len2 / 2; + second_cut = ranges::next(middle, new_len2); + first_cut = ranges::upper_bound(first, middle, etl::invoke(proj, *second_cut), etl::ref(comp), etl::ref(proj)); + new_len1 = etl::distance(first, first_cut); + } + + I new_middle; + // Due to a non-standard etl::rotate implementation, we need to handle + // the case where one of the cuts is the middle separately to avoid + // returning an iterator outside of [first, last) + // As soon as etl::rotate is fixed to return an iterator in the middle + // of the rotated range, this can be simplified to just calling + // etl::rotate + if (first_cut == middle) + { + new_middle = second_cut; + } + else if (second_cut == middle) + { + new_middle = first_cut; + } + else + { + new_middle = etl::rotate(first_cut, middle, second_cut); + } + + inplace_merge_impl(first, first_cut, new_middle, comp, proj, new_len1, new_len2); + inplace_merge_impl(new_middle, second_cut, last, comp, proj, len1 - new_len1, len2 - new_len2); + } + }; + + inline constexpr inplace_merge_fn inplace_merge{}; + + struct set_union_fn + { + template >> + constexpr ranges::set_union_result operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, + Proj2 proj2 = {}) const + { + while (first1 != last1 && first2 != last2) + { + if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + *result = *first1; + ++first1; + } + else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + { + *result = *first2; + ++first2; + } + else + { + *result = *first1; + ++first1; + ++first2; + } + ++result; + } + + while (first1 != last1) + { + *result = *first1; ++first1; + ++result; + } + + while (first2 != last2) + { + *result = *first2; ++first2; + ++result; } + + return {etl::move(first1), etl::move(first2), etl::move(result)}; } - while (first1 != last1) + template >> + constexpr ranges::set_union_result, ranges::borrowed_iterator_t, O> + operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { - *result = *first1; - ++first1; - ++result; + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(result), etl::move(comp), etl::move(proj1), + etl::move(proj2)); } + }; - while (first2 != last2) - { - *result = *first2; - ++first2; - ++result; - } + inline constexpr set_union_fn set_union{}; - return {etl::move(first1), etl::move(first2), etl::move(result)}; - } - - template>> - constexpr ranges::set_symmetric_difference_result, ranges::borrowed_iterator_t, O> - operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, - Proj1 proj1 = {}, Proj2 proj2 = {}) const + struct set_intersection_fn { - return (*this)(ranges::begin(r1), ranges::end(r1), - ranges::begin(r2), ranges::end(r2), - etl::move(result), etl::move(comp), etl::move(proj1), etl::move(proj2)); - } - }; - - inline constexpr set_symmetric_difference_fn set_symmetric_difference {}; - - struct make_heap_fn - { - private: - - template - static constexpr void sift_down(I first, typename etl::iterator_traits::difference_type index, - typename etl::iterator_traits::difference_type length, - Comp& comp, Proj& proj) - { - while (true) + template >> + constexpr ranges::set_intersection_result operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}) const { - auto child = 2 * index + 1; - - if (child >= length) + while (first1 != last1 && first2 != last2) { - break; + if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + ++first1; + } + else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + { + ++first2; + } + else + { + *result = *first1; + ++first1; + ++first2; + ++result; + } } - if ((child + 1 < length) && - etl::invoke(comp, etl::invoke(proj, *(first + child)), - etl::invoke(proj, *(first + (child + 1))))) - { - ++child; - } - - if (!etl::invoke(comp, etl::invoke(proj, *(first + index)), - etl::invoke(proj, *(first + child)))) - { - break; - } - - etl::iter_swap(first + index, first + child); - index = child; + return {etl::move(first1), etl::move(first2), etl::move(result)}; } - } - public: - - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - I last_it = ranges::next(first, last); - - auto length = etl::distance(first, last_it); - - if (length < 2) + template >> + constexpr ranges::set_intersection_result< ranges::borrowed_iterator_t, ranges::borrowed_iterator_t, O> + operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(result), etl::move(comp), etl::move(proj1), + etl::move(proj2)); + } + }; + + inline constexpr set_intersection_fn set_intersection{}; + + struct set_difference_fn + { + template >> + constexpr ranges::set_difference_result operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, + Proj2 proj2 = {}) const + { + while (first1 != last1 && first2 != last2) + { + if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + *result = *first1; + ++first1; + ++result; + } + else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + { + ++first2; + } + else + { + ++first1; + ++first2; + } + } + + while (first1 != last1) + { + *result = *first1; + ++first1; + ++result; + } + + return {etl::move(first1), etl::move(result)}; + } + + template >> + constexpr ranges::set_difference_result, O> operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(result), etl::move(comp), etl::move(proj1), + etl::move(proj2)); + } + }; + + inline constexpr set_difference_fn set_difference{}; + + struct set_symmetric_difference_fn + { + template >> + constexpr ranges::set_symmetric_difference_result operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + while (first1 != last1 && first2 != last2) + { + if (etl::invoke(comp, etl::invoke(proj1, *first1), etl::invoke(proj2, *first2))) + { + *result = *first1; + ++first1; + ++result; + } + else if (etl::invoke(comp, etl::invoke(proj2, *first2), etl::invoke(proj1, *first1))) + { + *result = *first2; + ++first2; + ++result; + } + else + { + ++first1; + ++first2; + } + } + + while (first1 != last1) + { + *result = *first1; + ++first1; + ++result; + } + + while (first2 != last2) + { + *result = *first2; + ++first2; + ++result; + } + + return {etl::move(first1), etl::move(first2), etl::move(result)}; + } + + template >> + constexpr ranges::set_symmetric_difference_result< ranges::borrowed_iterator_t, ranges::borrowed_iterator_t, O> + operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const + { + return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), etl::move(result), etl::move(comp), etl::move(proj1), + etl::move(proj2)); + } + }; + + inline constexpr set_symmetric_difference_fn set_symmetric_difference{}; + + struct make_heap_fn + { + private: + + template + static constexpr void sift_down(I first, typename etl::iterator_traits::difference_type index, + typename etl::iterator_traits::difference_type length, Comp& comp, Proj& proj) + { + while (true) + { + auto child = 2 * index + 1; + + if (child >= length) + { + break; + } + + if ((child + 1 < length) && etl::invoke(comp, etl::invoke(proj, *(first + child)), etl::invoke(proj, *(first + (child + 1))))) + { + ++child; + } + + if (!etl::invoke(comp, etl::invoke(proj, *(first + index)), etl::invoke(proj, *(first + child)))) + { + break; + } + + etl::iter_swap(first + index, first + child); + index = child; + } + } + + public: + + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + I last_it = ranges::next(first, last); + + auto length = etl::distance(first, last_it); + + if (length < 2) + { + return last_it; + } + + auto parent = (length - 2) / 2; + + while (true) + { + sift_down(first, parent, length, comp, proj); + + if (parent == 0) + { + break; + } + + --parent; + } + return last_it; } - auto parent = (length - 2) / 2; - - while (true) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - sift_down(first, parent, length, comp, proj); + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; - if (parent == 0) + inline constexpr make_heap_fn make_heap{}; + + struct push_heap_fn + { + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "push_heap requires random access iterators"); + + I last_it = ranges::next(first, last); + + auto length = etl::distance(first, last_it); + + if (length < 2) { - break; + return last_it; } - --parent; - } + auto value_index = length - 1; + auto parent = (value_index - 1) / 2; + auto value = etl::move(*(first + value_index)); - return last_it; - } + while ((value_index > 0) && etl::invoke(comp, etl::invoke(proj, *(first + parent)), etl::invoke(proj, value))) + { + *(first + value_index) = etl::move(*(first + parent)); + value_index = parent; + parent = (value_index - 1) / 2; + } - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; + *(first + value_index) = etl::move(value); - inline constexpr make_heap_fn make_heap {}; - - struct push_heap_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "push_heap requires random access iterators"); - - I last_it = ranges::next(first, last); - - auto length = etl::distance(first, last_it); - - if (length < 2) - { return last_it; } - auto value_index = length - 1; - auto parent = (value_index - 1) / 2; - auto value = etl::move(*(first + value_index)); - - while ((value_index > 0) && - etl::invoke(comp, etl::invoke(proj, *(first + parent)), - etl::invoke(proj, value))) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - *(first + value_index) = etl::move(*(first + parent)); - value_index = parent; - parent = (value_index - 1) / 2; + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr push_heap_fn push_heap{}; + + struct pop_heap_fn + { + private: + + template + static constexpr void sift_down(I first, typename etl::iterator_traits::difference_type index, + typename etl::iterator_traits::difference_type length, Comp& comp, Proj& proj) + { + while (true) + { + auto child = 2 * index + 1; + + if (child >= length) + { + break; + } + + if ((child + 1 < length) && etl::invoke(comp, etl::invoke(proj, *(first + child)), etl::invoke(proj, *(first + (child + 1))))) + { + ++child; + } + + if (!etl::invoke(comp, etl::invoke(proj, *(first + index)), etl::invoke(proj, *(first + child)))) + { + break; + } + + etl::iter_swap(first + index, first + child); + index = child; + } } - *(first + value_index) = etl::move(value); + public: - return last_it; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr push_heap_fn push_heap {}; - - struct pop_heap_fn - { - private: - - template - static constexpr void sift_down(I first, typename etl::iterator_traits::difference_type index, - typename etl::iterator_traits::difference_type length, - Comp& comp, Proj& proj) - { - while (true) + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { - auto child = 2 * index + 1; + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "pop_heap requires random access iterators"); - if (child >= length) + I last_it = ranges::next(first, last); + + auto length = etl::distance(first, last_it); + + if (length < 2) { - break; + return last_it; } - if ((child + 1 < length) && - etl::invoke(comp, etl::invoke(proj, *(first + child)), - etl::invoke(proj, *(first + (child + 1))))) - { - ++child; - } + --last_it; - if (!etl::invoke(comp, etl::invoke(proj, *(first + index)), - etl::invoke(proj, *(first + child)))) - { - break; - } + etl::iter_swap(first, last_it); - etl::iter_swap(first + index, first + child); - index = child; + sift_down(first, decltype(length)(0), etl::distance(first, last_it), comp, proj); + + return ranges::next(first, last); } - } - public: - - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "pop_heap requires random access iterators"); - - I last_it = ranges::next(first, last); - - auto length = etl::distance(first, last_it); - - if (length < 2) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr pop_heap_fn pop_heap{}; + + struct is_heap_until_fn + { + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "is_heap_until requires random access iterators"); + + I last_it = ranges::next(first, last); + + auto length = etl::distance(first, last_it); + + decltype(length) parent = 0; + + for (decltype(length) child = 1; child < length; ++child) + { + if (etl::invoke(comp, etl::invoke(proj, *(first + parent)), etl::invoke(proj, *(first + child)))) + { + return first + child; + } + + if ((child & 1) == 0) + { + ++parent; + } + } + return last_it; } - --last_it; - - etl::iter_swap(first, last_it); - - sift_down(first, decltype(length)(0), etl::distance(first, last_it), comp, proj); - - return ranges::next(first, last); - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr pop_heap_fn pop_heap {}; - - struct is_heap_until_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "is_heap_until requires random access iterators"); - - I last_it = ranges::next(first, last); - - auto length = etl::distance(first, last_it); - - decltype(length) parent = 0; - - for (decltype(length) child = 1; child < length; ++child) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - if (etl::invoke(comp, etl::invoke(proj, *(first + parent)), - etl::invoke(proj, *(first + child)))) + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr is_heap_until_fn is_heap_until{}; + + struct is_heap_fn + { + template >> + constexpr bool operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + return ranges::is_heap_until(first, last, etl::ref(comp), etl::ref(proj)) == last; + } + + template >> + constexpr bool operator()(R&& r, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr is_heap_fn is_heap{}; + + struct sort_heap_fn + { + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "sort_heap requires random access iterators"); + + I last_it = ranges::next(first, last); + I current_last = last_it; + + while (first != current_last) { - return first + child; + ranges::pop_heap(first, current_last, comp, proj); + --current_last; } - if ((child & 1) == 0) + return last_it; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr sort_heap_fn sort_heap{}; + + struct min_fn + { + template + constexpr const T& operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const + { + return etl::invoke(comp, etl::invoke(proj, b), etl::invoke(proj, a)) ? b : a; + } + + template + constexpr T operator()(std::initializer_list r, Comp comp = {}, Proj proj = {}) const + { + auto first = r.begin(); + auto last = r.end(); + + auto smallest = first; + while (++first != last) { - ++parent; + if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + { + smallest = first; + } } + return *smallest; } - return last_it; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr is_heap_until_fn is_heap_until {}; - - struct is_heap_fn - { - template>> - constexpr bool operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - return ranges::is_heap_until(first, last, etl::ref(comp), etl::ref(proj)) == last; - } - - template>> - constexpr bool operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr is_heap_fn is_heap {}; - - struct sort_heap_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - ETL_STATIC_ASSERT(etl::is_random_access_iterator::value, "sort_heap requires random access iterators"); - - I last_it = ranges::next(first, last); - I current_last = last_it; - - while (first != current_last) + template >> + constexpr ranges::range_value_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - ranges::pop_heap(first, current_last, comp, proj); - --current_last; - } + auto first = ranges::begin(r); + auto last = ranges::end(r); - return last_it; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr sort_heap_fn sort_heap {}; - - struct min_fn - { - template - constexpr const T& operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const - { - return etl::invoke(comp, etl::invoke(proj, b), etl::invoke(proj, a)) ? b : a; - } - - template - constexpr T operator()(std::initializer_list r, Comp comp = {}, Proj proj = {}) const - { - auto first = r.begin(); - auto last = r.end(); - - auto smallest = first; - while (++first != last) - { - if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + auto smallest = first; + while (++first != last) { - smallest = first; + if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + { + smallest = first; + } } + return *smallest; } - return *smallest; - } + }; - template>> - constexpr ranges::range_value_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const + inline constexpr min_fn min{}; + + struct min_element_fn { - auto first = ranges::begin(r); - auto last = ranges::end(r); - - auto smallest = first; - while (++first != last) + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { - if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + if (first == last) { - smallest = first; + return first; } - } - return *smallest; - } - }; - inline constexpr min_fn min {}; + I smallest = first; + ++first; - struct min_element_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - if (first == last) - { - return first; - } - - I smallest = first; - ++first; - - for (; first != last; ++first) - { - if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + for (; first != last; ++first) { - smallest = first; + if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + { + smallest = first; + } } + + return smallest; } - return smallest; - } - - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr min_element_fn min_element {}; - - struct max_fn - { - template - constexpr const T& operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const - { - return etl::invoke(comp, etl::invoke(proj, a), etl::invoke(proj, b)) ? b : a; - } - - template - constexpr T operator()(std::initializer_list r, Comp comp = {}, Proj proj = {}) const - { - auto first = r.begin(); - auto last = r.end(); - - auto largest = first; - while (++first != last) + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr min_element_fn min_element{}; + + struct max_fn + { + template + constexpr const T& operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const + { + return etl::invoke(comp, etl::invoke(proj, a), etl::invoke(proj, b)) ? b : a; + } + + template + constexpr T operator()(std::initializer_list r, Comp comp = {}, Proj proj = {}) const + { + auto first = r.begin(); + auto last = r.end(); + + auto largest = first; + while (++first != last) { - largest = first; + if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + { + largest = first; + } } + return *largest; } - return *largest; - } - template>> - constexpr ranges::range_value_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - auto first = ranges::begin(r); - auto last = ranges::end(r); - - auto largest = first; - while (++first != last) + template >> + constexpr ranges::range_value_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + auto first = ranges::begin(r); + auto last = ranges::end(r); + + auto largest = first; + while (++first != last) { - largest = first; + if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + { + largest = first; + } } + return *largest; } - return *largest; - } - }; + }; - inline constexpr max_fn max {}; + inline constexpr max_fn max{}; - struct max_element_fn - { - template>> - constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + struct max_element_fn { - if (first == last) + template >> + constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { - return first; - } - - I largest = first; - ++first; - - for (; first != last; ++first) - { - if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + if (first == last) { - largest = first; + return first; } - } - return largest; - } + I largest = first; + ++first; - template>> - constexpr ranges::borrowed_iterator_t - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr max_element_fn max_element {}; - - struct minmax_fn - { - template - constexpr ranges::minmax_result operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const - { - if (etl::invoke(comp, etl::invoke(proj, b), etl::invoke(proj, a))) - { - return {b, a}; - } - return {a, b}; - } - - template - constexpr ranges::minmax_result operator()(std::initializer_list r, Comp comp = {}, Proj proj = {}) const - { - auto first = r.begin(); - auto last = r.end(); - - auto smallest = first; - auto largest = first; - - while (++first != last) - { - if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + for (; first != last; ++first) { - smallest = first; + if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + { + largest = first; + } } - if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) - { - largest = first; - } - } - return {*smallest, *largest}; - } - template>> - constexpr ranges::minmax_result> operator()(R&& r, Comp comp = {}, Proj proj = {}) const + return largest; + } + + template >> + constexpr ranges::borrowed_iterator_t operator()(R&& r, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr max_element_fn max_element{}; + + struct minmax_fn { - auto first = ranges::begin(r); - auto last = ranges::end(r); - - auto smallest = first; - auto largest = first; - - while (++first != last) + template + constexpr ranges::minmax_result operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const { - if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + if (etl::invoke(comp, etl::invoke(proj, b), etl::invoke(proj, a))) { - smallest = first; + return {b, a}; } - if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + return {a, b}; + } + + template + constexpr ranges::minmax_result operator()(std::initializer_list r, Comp comp = {}, Proj proj = {}) const + { + auto first = r.begin(); + auto last = r.end(); + + auto smallest = first; + auto largest = first; + + while (++first != last) { - largest = first; + if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + { + smallest = first; + } + if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + { + largest = first; + } } - } - return {*smallest, *largest}; - } - }; - - inline constexpr minmax_fn minmax {}; - - struct minmax_element_fn - { - template>> - constexpr ranges::minmax_element_result operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - if (first == last) - { - return {first, first}; + return {*smallest, *largest}; } - I smallest = first; - I largest = first; - ++first; - - for (; first != last; ++first) + template >> + constexpr ranges::minmax_result> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + auto first = ranges::begin(r); + auto last = ranges::end(r); + + auto smallest = first; + auto largest = first; + + while (++first != last) { - smallest = first; + if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + { + smallest = first; + } + if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + { + largest = first; + } } - if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + return {*smallest, *largest}; + } + }; + + inline constexpr minmax_fn minmax{}; + + struct minmax_element_fn + { + template >> + constexpr ranges::minmax_element_result operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + if (first == last) { - largest = first; + return {first, first}; } + + I smallest = first; + I largest = first; + ++first; + + for (; first != last; ++first) + { + if (etl::invoke(comp, etl::invoke(proj, *first), etl::invoke(proj, *smallest))) + { + smallest = first; + } + if (etl::invoke(comp, etl::invoke(proj, *largest), etl::invoke(proj, *first))) + { + largest = first; + } + } + + return {smallest, largest}; } - return {smallest, largest}; - } - - template>> - constexpr ranges::minmax_element_result> - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr minmax_element_fn minmax_element {}; - - struct clamp_fn - { - template - constexpr const T& operator()(const T& value, const T& low, const T& high, Comp comp = {}, Proj proj = {}) const - { - auto&& projected_value = etl::invoke(proj, value); - - return etl::invoke(comp, projected_value, etl::invoke(proj, low)) ? low : - etl::invoke(comp, etl::invoke(proj, high), projected_value) ? high : - value; - } - }; - - inline constexpr clamp_fn clamp {}; - - struct next_permutation_fn - { - template>> - constexpr ranges::next_permutation_result - operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - I last_it = ranges::next(first, last); - - // Empty or single-element range: already at last permutation - if (first == last_it) + template >> + constexpr ranges::minmax_element_result> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { - return {etl::move(last_it), false}; + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); } + }; - I i = last_it; - --i; + inline constexpr minmax_element_fn minmax_element{}; - if (i == first) + struct clamp_fn + { + template + constexpr const T& operator()(const T& value, const T& low, const T& high, Comp comp = {}, Proj proj = {}) const { - return {etl::move(last_it), false}; + auto&& projected_value = etl::invoke(proj, value); + + return etl::invoke(comp, projected_value, etl::invoke(proj, low)) ? low + : etl::invoke(comp, etl::invoke(proj, high), projected_value) ? high + : value; } + }; - for (;;) + inline constexpr clamp_fn clamp{}; + + struct next_permutation_fn + { + template >> + constexpr ranges::next_permutation_result operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { - I i1 = i; + I last_it = ranges::next(first, last); + + // Empty or single-element range: already at last permutation + if (first == last_it) + { + return {etl::move(last_it), false}; + } + + I i = last_it; --i; - // Find the rightmost element where projected *i < projected *i1 - if (etl::invoke(comp, etl::invoke(proj, *i), etl::invoke(proj, *i1))) - { - // Find the rightmost element j where projected *j > projected *i - I j = last_it; - while (!etl::invoke(comp, etl::invoke(proj, *i), etl::invoke(proj, *--j))) - { - } - - etl::iter_swap(i, j); - - // Reverse from i1 to last - I left = i1; - I right = last_it; - while (left != right && left != --right) - { - etl::iter_swap(left, right); - ++left; - } - - return {etl::move(last_it), true}; - } - if (i == first) { - // Already at last (ascending) permutation: wrap to first (descending) - I left = first; - I right = last_it; - while (left != right && left != --right) - { - etl::iter_swap(left, right); - ++left; - } - return {etl::move(last_it), false}; } - } - } - template>> - constexpr ranges::next_permutation_result> - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; - - inline constexpr next_permutation_fn next_permutation {}; - - struct prev_permutation_fn - { - template>> - constexpr ranges::prev_permutation_result - operator()(I first, S last, Comp comp = {}, Proj proj = {}) const - { - I last_it = ranges::next(first, last); - - // Empty or single-element range: already at last permutation - if (first == last_it) - { - return {etl::move(last_it), false}; - } - - I i = last_it; - --i; - - if (i == first) - { - return {etl::move(last_it), false}; - } - - for (;;) - { - I i1 = i; - --i; - - // Find the rightmost element where projected *i > projected *i1 - if (etl::invoke(comp, etl::invoke(proj, *i1), etl::invoke(proj, *i))) + for (;;) { - // Find the rightmost element j where projected *j < projected *i - I j = last_it; - while (!etl::invoke(comp, etl::invoke(proj, *--j), etl::invoke(proj, *i))) + I i1 = i; + --i; + + // Find the rightmost element where projected *i < projected *i1 + if (etl::invoke(comp, etl::invoke(proj, *i), etl::invoke(proj, *i1))) { + // Find the rightmost element j where projected *j > projected *i + I j = last_it; + while (!etl::invoke(comp, etl::invoke(proj, *i), etl::invoke(proj, *--j))) + { + } + + etl::iter_swap(i, j); + + // Reverse from i1 to last + I left = i1; + I right = last_it; + while (left != right && left != --right) + { + etl::iter_swap(left, right); + ++left; + } + + return {etl::move(last_it), true}; } - etl::iter_swap(i, j); - - // Reverse from i1 to last - I left = i1; - I right = last_it; - while (left != right && left != --right) + if (i == first) { - etl::iter_swap(left, right); - ++left; - } + // Already at last (ascending) permutation: wrap to first + // (descending) + I left = first; + I right = last_it; + while (left != right && left != --right) + { + etl::iter_swap(left, right); + ++left; + } - return {etl::move(last_it), true}; + return {etl::move(last_it), false}; + } } + } + + template >> + constexpr ranges::next_permutation_result> operator()(R&& r, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; + + inline constexpr next_permutation_fn next_permutation{}; + + struct prev_permutation_fn + { + template >> + constexpr ranges::prev_permutation_result operator()(I first, S last, Comp comp = {}, Proj proj = {}) const + { + I last_it = ranges::next(first, last); + + // Empty or single-element range: already at last permutation + if (first == last_it) + { + return {etl::move(last_it), false}; + } + + I i = last_it; + --i; if (i == first) { - // Already at last (descending) permutation: wrap to first (ascending) - I left = first; - I right = last_it; - while (left != right && left != --right) - { - etl::iter_swap(left, right); - ++left; - } - return {etl::move(last_it), false}; } + + for (;;) + { + I i1 = i; + --i; + + // Find the rightmost element where projected *i > projected *i1 + if (etl::invoke(comp, etl::invoke(proj, *i1), etl::invoke(proj, *i))) + { + // Find the rightmost element j where projected *j < projected *i + I j = last_it; + while (!etl::invoke(comp, etl::invoke(proj, *--j), etl::invoke(proj, *i))) + { + } + + etl::iter_swap(i, j); + + // Reverse from i1 to last + I left = i1; + I right = last_it; + while (left != right && left != --right) + { + etl::iter_swap(left, right); + ++left; + } + + return {etl::move(last_it), true}; + } + + if (i == first) + { + // Already at last (descending) permutation: wrap to first + // (ascending) + I left = first; + I right = last_it; + while (left != right && left != --right) + { + etl::iter_swap(left, right); + ++left; + } + + return {etl::move(last_it), false}; + } + } } - } - template>> - constexpr ranges::prev_permutation_result> - operator()(R&& r, Comp comp = {}, Proj proj = {}) const - { - return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); - } - }; + template >> + constexpr ranges::prev_permutation_result> operator()(R&& r, Comp comp = {}, Proj proj = {}) const + { + return (*this)(ranges::begin(r), ranges::end(r), etl::move(comp), etl::move(proj)); + } + }; - inline constexpr prev_permutation_fn prev_permutation {}; -} + inline constexpr prev_permutation_fn prev_permutation{}; + } // namespace ranges #endif -} +} // namespace etl #include "private/minmax_pop.h" diff --git a/include/etl/alignment.h b/include/etl/alignment.h index d46c582d..023acac2 100644 --- a/include/etl/alignment.h +++ b/include/etl/alignment.h @@ -32,10 +32,10 @@ SOFTWARE. #define ETL_ALIGNMENT_INCLUDED #include "platform.h" -#include "type_traits.h" -#include "static_assert.h" #include "error_handler.h" #include "exception.h" +#include "static_assert.h" +#include "type_traits.h" #include "utility.h" #include @@ -124,28 +124,28 @@ namespace etl // Matching alignment. template - class type_with_alignment_matcher + class type_with_alignment_matcher { public: - + typedef T1 type; }; // Non-matching alignment template - class type_with_alignment_matcher + class type_with_alignment_matcher { public: - - typedef typename type_with_alignment_matcher < Alignment <= etl::alignment_of::value , Alignment, T2, TRest... > ::type type; + + typedef typename type_with_alignment_matcher< Alignment <= etl::alignment_of::value, Alignment, T2, TRest... >::type type; }; // Non-matching alignment, none left. template - class type_with_alignment_matcher + class type_with_alignment_matcher { public: - + typedef char type; }; @@ -153,23 +153,23 @@ namespace etl // Helper. //*************************************************************************** template - class type_with_alignment_helper + class type_with_alignment_helper { public: - + typedef typename type_with_alignment_matcher::value, Alignment, T1, T...>::type type; }; #else //*************************************************************************** // Matcher. //*************************************************************************** - template + template class type_with_alignment_matcher; // Matching alignment. template - class type_with_alignment_matcher + class type_with_alignment_matcher { public: @@ -178,16 +178,17 @@ namespace etl // Non-matching alignment. template - class type_with_alignment_matcher + class type_with_alignment_matcher { public: - typedef typename type_with_alignment_matcher::value, Alignment, T2, T3, T4, T5, T6, T7, T8, void>::type type; + typedef + typename type_with_alignment_matcher< Alignment <= etl::alignment_of::value, Alignment, T2, T3, T4, T5, T6, T7, T8, void>::type type; }; // Non-matching alignment, none left. template - class type_with_alignment_matcher + class type_with_alignment_matcher { public: @@ -197,16 +198,16 @@ namespace etl //*************************************************************************** // Helper. //*************************************************************************** - template + template class type_with_alignment_helper { public: - typedef typename type_with_alignment_matcher::value, Alignment, T1, T2, T3, T4, T5, T6, T7, T8>::type type; + typedef typename type_with_alignment_matcher< Alignment <= etl::alignment_of::value, Alignment, T1, T2, T3, T4, T5, T6, T7, T8>::type type; }; #endif - } + } // namespace private_alignment //*************************************************************************** /// Gets a type that has the same as the specified alignment. @@ -218,12 +219,17 @@ namespace etl public: #if ETL_USING_CPP11 - typedef struct { alignas(Alignment) char dummy; } type; + typedef struct + { + alignas(Alignment) char dummy; + } type; #else #if ETL_NOT_USING_64BIT_TYPES - typedef typename private_alignment::type_with_alignment_helper::type type; + typedef typename private_alignment::type_with_alignment_helper< Alignment, int_least8_t, int_least16_t, int32_t, float, double, void*>::type type; #else - typedef typename private_alignment::type_with_alignment_helper::type type; + typedef + typename private_alignment::type_with_alignment_helper< Alignment, int_least8_t, int_least16_t, int32_t, int64_t, float, double, void*>::type + type; #endif #endif @@ -247,35 +253,35 @@ namespace etl { /// Convert to T reference. template - operator T& () + operator T&() { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); T* t = *this; return *t; } /// Convert to const T reference. template - operator const T& () const + operator const T&() const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); const T* t = *this; return *t; } /// Convert to T pointer. template - operator T* () + operator T*() { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } /// Convert to const T pointer. template - operator const T* () const + operator const T*() const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } @@ -283,7 +289,7 @@ namespace etl template T& get_reference() { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); T* t = *this; return *t; } @@ -292,7 +298,7 @@ namespace etl template const T& get_reference() const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); const T* t = *this; return *t; } @@ -301,7 +307,7 @@ namespace etl template T* get_address() { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } @@ -309,7 +315,7 @@ namespace etl template const T* get_address() const { - ETL_STATIC_ASSERT((etl::is_same:: value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); + ETL_STATIC_ASSERT((etl::is_same::value || ((Alignment % etl::alignment_of::value) == 0)), "Incompatible alignment"); return reinterpret_cast(data); } @@ -318,8 +324,9 @@ namespace etl #else union { - char data[Length]; - typename etl::type_with_alignment::type etl_alignment_type; // A POD type that has the same alignment as Alignment. + char data[Length]; + typename etl::type_with_alignment::type etl_alignment_type; // A POD type that has the same alignment + // as Alignment. }; #endif }; @@ -373,7 +380,8 @@ namespace etl #if ETL_USING_CPP11 //*************************************************************************** - /// Constructs the instance of T forwarding the given \p args to its constructor. + /// Constructs the instance of T forwarding the given \p args to its + /// constructor. //*************************************************************************** template typed_storage(TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) @@ -424,8 +432,8 @@ namespace etl #endif //*************************************************************************** - /// Default destructor which will NOT call the destructor of the object which - /// was created by calling create(). + /// Default destructor which will NOT call the destructor of the object + /// which was created by calling create(). //*************************************************************************** ~typed_storage() ETL_NOEXCEPT { @@ -443,67 +451,72 @@ namespace etl #if ETL_USING_CPP11 //*************************************************************************** - /// Constructs the instance of T forwarding the given \p args to its constructor. - /// \returns the instance of T which has been constructed in the internal byte array. + /// Constructs the instance of T forwarding the given \p args to its + /// constructor. \returns the instance of T which has been constructed in + /// the internal byte array. //*************************************************************************** template reference create(TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (&storage.value) value_type(etl::forward(args)...); - valid = true; + valid = true; return *p; } #else //*************************************************************************** /// Constructs the instance of T with type T1 - /// \returns the instance of T which has been constructed in the internal byte array. + /// \returns the instance of T which has been constructed in the internal + /// byte array. //*************************************************************************** template reference create(const T1& t1) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (&storage.value) value_type(t1); - valid = true; + valid = true; return *p; } //*************************************************************************** /// Constructs the instance of T with types T1, T2 - /// \returns the instance of T which has been constructed in the internal byte array. + /// \returns the instance of T which has been constructed in the internal + /// byte array. //*************************************************************************** template reference create(const T1& t1, const T2& t2) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (&storage.value) value_type(t1, t2); - valid = true; + valid = true; return *p; } //*************************************************************************** /// Constructs the instance of T with types T1, T2, T3 - /// \returns the instance of T which has been constructed in the internal byte array. + /// \returns the instance of T which has been constructed in the internal + /// byte array. //*************************************************************************** template reference create(const T1& t1, const T2& t2, const T3& t3) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (&storage.value) value_type(t1, t2, t3); - valid = true; + valid = true; return *p; } //*************************************************************************** /// Constructs the instance of T with types T1, T2, T3, T4 - /// \returns the instance of T which has been constructed in the internal byte array. + /// \returns the instance of T which has been constructed in the internal + /// byte array. //*************************************************************************** template reference create(const T1& t1, const T2& t2, const T3& t3, const T4& t4) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (&storage.value) value_type(t1, t2, t3, t4); - valid = true; + valid = true; return *p; } #endif @@ -536,7 +549,7 @@ namespace etl const_pointer operator->() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) { ETL_ASSERT(has_value(), ETL_ERROR(etl::typed_storage_error)); - + return &storage.value; } @@ -559,11 +572,13 @@ namespace etl private: typed_storage(etl::typed_storage&) ETL_DELETE; - typed_storage& operator =(etl::typed_storage&) ETL_DELETE; + typed_storage& operator=(etl::typed_storage&) ETL_DELETE; - struct dummy_t {}; + struct dummy_t + { + }; - //******************************* + //******************************* union union_type { ETL_CONSTEXPR union_type() ETL_NOEXCEPT @@ -571,9 +586,7 @@ namespace etl { } - ~union_type() ETL_NOEXCEPT - { - } + ~union_type() ETL_NOEXCEPT {} dummy_t dummy; value_type value; @@ -600,7 +613,7 @@ namespace etl typedef const T& const_reference; typedef T* pointer; typedef const T* const_pointer; - + template friend ETL_CONSTEXPR14 void swap(typed_storage_ext& lhs, typed_storage_ext& rhs) ETL_NOEXCEPT; @@ -608,15 +621,16 @@ namespace etl /// Constructor. //*************************************************************************** typed_storage_ext(void* pbuffer_) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) - : pbuffer(reinterpret_cast(pbuffer_)), - valid(false) + : pbuffer(reinterpret_cast(pbuffer_)) + , valid(false) { ETL_ASSERT(etl::is_aligned(pbuffer_, etl::alignment_of::value), ETL_ERROR(etl::alignment_error)); } #if ETL_USING_CPP11 //*************************************************************************** - /// Constructs the instance of T forwarding the given \p args to its constructor. + /// Constructs the instance of T forwarding the given \p args to its + /// constructor. //*************************************************************************** template typed_storage_ext(void* pbuffer_, TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) @@ -689,8 +703,8 @@ namespace etl #endif //*************************************************************************** - /// Default destructor which will NOT call the destructor of the object which - /// was created by calling create(). + /// Default destructor which will NOT call the destructor of the object + /// which was created by calling create(). //*************************************************************************** ~typed_storage_ext() ETL_NOEXCEPT { @@ -708,67 +722,72 @@ namespace etl #if ETL_USING_CPP11 //*************************************************************************** - /// Constructs the instance of T forwarding the given \p args to its constructor. - /// \returns the instance of T which has been constructed in the external buffer. + /// Constructs the instance of T forwarding the given \p args to its + /// constructor. \returns the instance of T which has been constructed in + /// the external buffer. //*************************************************************************** template reference create(TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (pbuffer) value_type(etl::forward(args)...); - valid = true; + valid = true; return *p; } #else //*************************************************************************** /// Constructs the instance of T with type T1 - /// \returns the instance of T which has been constructed in the external buffer. + /// \returns the instance of T which has been constructed in the external + /// buffer. //*************************************************************************** template reference create(const T1& t1) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (pbuffer) value_type(t1); - valid = true; + valid = true; return *p; } //*************************************************************************** /// Constructs the instance of T with types T1, T2 - /// \returns the instance of T which has been constructed in the external buffer. + /// \returns the instance of T which has been constructed in the external + /// buffer. //*************************************************************************** template reference create(const T1& t1, const T2& t2) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (pbuffer) value_type(t1, t2); - valid = true; + valid = true; return *p; } //*************************************************************************** /// Constructs the instance of T with types T1, T2, T3 - /// \returns the instance of T which has been constructed in the external buffer. + /// \returns the instance of T which has been constructed in the external + /// buffer. //*************************************************************************** template reference create(const T1& t1, const T2& t2, const T3& t3) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (pbuffer) value_type(t1, t2, t3); - valid = true; + valid = true; return *p; } //*************************************************************************** /// Constructs the instance of T with types T1, T2, T3, T4 - /// \returns the instance of T which has been constructed in the external buffer. + /// \returns the instance of T which has been constructed in the external + /// buffer. //*************************************************************************** template reference create(const T1& t1, const T2& t2, const T3& t3, const T4& t4) { ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error)); pointer p = ::new (pbuffer) value_type(t1, t2, t3, t4); - valid = true; + valid = true; return *p; } #endif @@ -824,10 +843,10 @@ namespace etl private: typed_storage_ext(etl::typed_storage_ext&) ETL_DELETE; - typed_storage_ext& operator =(etl::typed_storage_ext&) ETL_DELETE; + typed_storage_ext& operator=(etl::typed_storage_ext&) ETL_DELETE; pointer pbuffer; - bool valid; + bool valid; }; //*************************************************************************** @@ -839,8 +858,8 @@ namespace etl using ETL_OR_STD::swap; swap(lhs.pbuffer, rhs.pbuffer); - swap(lhs.valid, rhs.valid); + swap(lhs.valid, rhs.valid); } -} +} // namespace etl #endif diff --git a/include/etl/array.h b/include/etl/array.h index 87987276..2051807b 100644 --- a/include/etl/array.h +++ b/include/etl/array.h @@ -33,15 +33,15 @@ SOFTWARE. #include "platform.h" #include "algorithm.h" -#include "iterator.h" -#include "functional.h" +#include "error_handler.h" #include "exception.h" -#include "type_traits.h" +#include "functional.h" +#include "initializer_list.h" +#include "iterator.h" +#include "nth_type.h" #include "parameter_type.h" #include "static_assert.h" -#include "error_handler.h" -#include "nth_type.h" -#include "initializer_list.h" +#include "type_traits.h" #include @@ -94,15 +94,15 @@ namespace etl static ETL_CONSTANT size_t SIZE = SIZE_; - typedef T value_type; - typedef size_t size_type; - typedef ptrdiff_t difference_type; - typedef T& reference; - typedef const T& const_reference; - typedef T* pointer; - typedef const T* const_pointer; - typedef T* iterator; - typedef const T* const_iterator; + typedef T value_type; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T& reference; + typedef const T& const_reference; + typedef T* pointer; + typedef const T* const_pointer; + typedef T* iterator; + typedef const T* const_iterator; typedef ETL_OR_STD::reverse_iterator reverse_iterator; typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; @@ -114,9 +114,7 @@ namespace etl /// Returns a reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference at(size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) + ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) { ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range)); @@ -127,9 +125,7 @@ namespace etl /// Returns a const reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const_reference at(size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) + ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS) { ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range)); @@ -141,9 +137,7 @@ namespace etl /// Returns a reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference operator[](size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) + ETL_NODISCARD ETL_CONSTEXPR14 reference operator[](size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) { ETL_ASSERT_CHECK_INDEX_OPERATOR(i < SIZE, ETL_ERROR(array_out_of_range)); @@ -155,8 +149,7 @@ namespace etl /// Returns a const reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) + ETL_NODISCARD ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) { // Throwing from c++11 constexpr requires special syntax #if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR @@ -171,9 +164,7 @@ namespace etl //************************************************************************* /// Returns a reference to the first element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference front() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reference front() ETL_NOEXCEPT { ETL_STATIC_ASSERT(SIZE > 0, "Array is empty."); @@ -183,8 +174,7 @@ namespace etl //************************************************************************* /// Returns a const reference to the first element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reference front() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reference front() const ETL_NOEXCEPT { ETL_STATIC_ASSERT(SIZE > 0, "Array is empty."); @@ -194,9 +184,7 @@ namespace etl //************************************************************************* /// Returns a reference to the last element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference back() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reference back() ETL_NOEXCEPT { ETL_STATIC_ASSERT(SIZE > 0, "Array is empty."); @@ -206,8 +194,7 @@ namespace etl //************************************************************************* /// Returns a const reference to the last element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reference back() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reference back() const ETL_NOEXCEPT { ETL_STATIC_ASSERT(SIZE > 0, "Array is empty."); @@ -217,9 +204,7 @@ namespace etl //************************************************************************* /// Returns a pointer to the first element of the internal buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - pointer data() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 pointer data() ETL_NOEXCEPT { return _buffer; } @@ -227,8 +212,7 @@ namespace etl //************************************************************************* /// Returns a const pointer to the first element of the internal buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT { return _buffer; } @@ -240,9 +224,7 @@ namespace etl //************************************************************************* /// Returns an iterator to the beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - iterator begin() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 iterator begin() ETL_NOEXCEPT { return _buffer; } @@ -250,8 +232,7 @@ namespace etl //************************************************************************* /// Returns a const iterator to the beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT { return _buffer; } @@ -259,8 +240,7 @@ namespace etl //************************************************************************* /// Returns a const iterator to the beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT { return begin(); } @@ -268,9 +248,7 @@ namespace etl //************************************************************************* /// Returns an iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - iterator end() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 iterator end() ETL_NOEXCEPT { return _buffer + SIZE; } @@ -278,8 +256,7 @@ namespace etl //************************************************************************* /// Returns a const iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT { return _buffer + SIZE; } @@ -287,8 +264,7 @@ namespace etl //************************************************************************* // Returns a const iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT { return _buffer + SIZE; } @@ -296,9 +272,7 @@ namespace etl //************************************************************************* // Returns an reverse iterator to the reverse beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reverse_iterator rbegin() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reverse_iterator rbegin() ETL_NOEXCEPT { return reverse_iterator(end()); } @@ -306,8 +280,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the reverse beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT { return const_reverse_iterator(end()); } @@ -315,8 +288,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the reverse beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT { return const_reverse_iterator(end()); } @@ -324,9 +296,7 @@ namespace etl //************************************************************************* /// Returns a reverse iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reverse_iterator rend() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reverse_iterator rend() ETL_NOEXCEPT { return reverse_iterator(begin()); } @@ -334,8 +304,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT { return const_reverse_iterator(begin()); } @@ -343,8 +312,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT { return const_reverse_iterator(begin()); } @@ -356,8 +324,7 @@ namespace etl //************************************************************************* /// Returns true if the array size is zero. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT { return (SIZE == 0); } @@ -365,8 +332,7 @@ namespace etl //************************************************************************* /// Returns the size of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT { return SIZE; } @@ -374,8 +340,7 @@ namespace etl //************************************************************************* /// Returns the maximum possible size of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT { return SIZE; } @@ -409,7 +374,8 @@ namespace etl //************************************************************************* /// Fills the array from the range. - /// If the range is smaller than the array then the unused array elements are left unmodified. + /// If the range is smaller than the array then the unused array elements + /// are left unmodified. ///\param first The iterator to the first item in the range. ///\param last The iterator to one past the final item in the range. ///\return An iterator to the first unassigned array element, or end(). @@ -422,7 +388,8 @@ namespace etl //************************************************************************* /// Fills the array from the range. - /// If the range is smaller than the array then the unused array elements are initialised with the supplied value. + /// If the range is smaller than the array then the unused array elements + /// are initialised with the supplied value. ///\param first The iterator to the first item in the range. ///\param last The iterator to one past the final item in the range. ///\return An iterator to the first array element set to 'value', or end(). @@ -570,7 +537,8 @@ namespace etl //************************************************************************* /// Erases a value from the array. ///\param position The index of the position to erase at. - ///\param value The value to use to overwrite the last element in the array. + ///\param value The value to use to overwrite the last element in the + /// array. //************************************************************************* inline iterator erase_at(size_t position, parameter_t value) { @@ -582,7 +550,8 @@ namespace etl //************************************************************************* /// Erases a value from the array. ///\param position The iterator to the position to erase at. - ///\param value The value to use to overwrite the last element in the array. + ///\param value The value to use to overwrite the last element in the + /// array. //************************************************************************* iterator erase(const_iterator position, parameter_t value) { @@ -600,7 +569,8 @@ namespace etl /// Erases a range of values from the array. ///\param first The first item to erase. ///\param last The one past the last item to erase. - ///\param value The value to use to overwrite the last elements in the array. + ///\param value The value to use to overwrite the last elements in the + /// array. //************************************************************************* iterator erase_range(size_t first, size_t last, parameter_t value) { @@ -613,7 +583,8 @@ namespace etl /// Erases a range of values from the array. ///\param first The first item to erase. ///\param last The one past the last item to erase. - ///\param value The value to use to overwrite the last elements in the array. + ///\param value The value to use to overwrite the last elements in the + /// array. //************************************************************************* iterator erase(const_iterator first, const_iterator last, parameter_t value) { @@ -660,15 +631,15 @@ namespace etl static ETL_CONSTANT size_t SIZE = 0; - typedef T value_type; - typedef size_t size_type; - typedef ptrdiff_t difference_type; - typedef T& reference; - typedef const T& const_reference; - typedef T* pointer; - typedef const T* const_pointer; - typedef T* iterator; - typedef const T* const_iterator; + typedef T value_type; + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T& reference; + typedef const T& const_reference; + typedef T* pointer; + typedef const T* const_pointer; + typedef T* iterator; + typedef const T* const_iterator; typedef ETL_OR_STD::reverse_iterator reverse_iterator; typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; @@ -680,9 +651,7 @@ namespace etl /// Returns a reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference at(size_t) ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reference at(size_t) ETL_NOEXCEPT { return *data(); } @@ -691,9 +660,7 @@ namespace etl /// Returns a const reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const_reference at(size_t) const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 const_reference at(size_t) const ETL_NOEXCEPT { return *data(); } @@ -703,9 +670,7 @@ namespace etl /// Returns a reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference operator[](size_t) ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reference operator[](size_t) ETL_NOEXCEPT { return *data(); } @@ -715,8 +680,7 @@ namespace etl /// Returns a const reference to the value at index 'i'. ///\param i The index of the element to access. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reference operator[](size_t) const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reference operator[](size_t) const ETL_NOEXCEPT { return *data(); } @@ -724,9 +688,7 @@ namespace etl //************************************************************************* /// Returns a reference to the first element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference front() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reference front() ETL_NOEXCEPT { return *data(); } @@ -734,8 +696,7 @@ namespace etl //************************************************************************* /// Returns a const reference to the first element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reference front() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reference front() const ETL_NOEXCEPT { return *data(); } @@ -743,9 +704,7 @@ namespace etl //************************************************************************* /// Returns a reference to the last element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reference back() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reference back() ETL_NOEXCEPT { return *data(); } @@ -753,8 +712,7 @@ namespace etl //************************************************************************* /// Returns a const reference to the last element. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reference back() const + ETL_NODISCARD ETL_CONSTEXPR const_reference back() const { return *data(); } @@ -762,9 +720,7 @@ namespace etl //************************************************************************* /// Returns a pointer to the first element of the internal buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - pointer data() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 pointer data() ETL_NOEXCEPT { return (T*)0; } @@ -772,8 +728,7 @@ namespace etl //************************************************************************* /// Returns a const pointer to the first element of the internal buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT { return (const T*)0; } @@ -785,9 +740,7 @@ namespace etl //************************************************************************* /// Returns an iterator to the beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - iterator begin() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 iterator begin() ETL_NOEXCEPT { return iterator(); } @@ -795,8 +748,7 @@ namespace etl //************************************************************************* /// Returns a const iterator to the beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT { return const_iterator(); } @@ -804,8 +756,7 @@ namespace etl //************************************************************************* /// Returns a const iterator to the beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT { return const_iterator(); } @@ -813,9 +764,7 @@ namespace etl //************************************************************************* /// Returns an iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - iterator end() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 iterator end() ETL_NOEXCEPT { return iterator(); } @@ -823,8 +772,7 @@ namespace etl //************************************************************************* /// Returns a const iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT { return const_iterator(); } @@ -832,8 +780,7 @@ namespace etl //************************************************************************* // Returns a const iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT { return const_iterator(); } @@ -841,9 +788,7 @@ namespace etl //************************************************************************* // Returns an reverse iterator to the reverse beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reverse_iterator rbegin() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reverse_iterator rbegin() ETL_NOEXCEPT { return reverse_iterator(end()); } @@ -851,8 +796,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the reverse beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT { return const_reverse_iterator(end()); } @@ -860,8 +804,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the reverse beginning of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT { return const_reverse_iterator(end()); } @@ -869,9 +812,7 @@ namespace etl //************************************************************************* /// Returns a reverse iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - reverse_iterator rend() ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR14 reverse_iterator rend() ETL_NOEXCEPT { return reverse_iterator(begin()); } @@ -879,8 +820,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT { return const_reverse_iterator(begin()); } @@ -888,8 +828,7 @@ namespace etl //************************************************************************* /// Returns a const reverse iterator to the end of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT { return const_reverse_iterator(begin()); } @@ -901,8 +840,7 @@ namespace etl //************************************************************************* /// Returns true if the array size is zero. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT { return true; } @@ -910,8 +848,7 @@ namespace etl //************************************************************************* /// Returns the size of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT { return 0; } @@ -919,8 +856,7 @@ namespace etl //************************************************************************* /// Returns the maximum possible size of the array. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT + ETL_NODISCARD ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT { return 0; } @@ -947,7 +883,8 @@ namespace etl //************************************************************************* /// Fills the array from the range. - /// If the range is smaller than the array then the unused array elements are left unmodified. + /// If the range is smaller than the array then the unused array elements + /// are left unmodified. ///\param first The iterator to the first item in the range. ///\param last The iterator to one past the final item in the range. ///\return An iterator to the first unassigned array element, or end(). @@ -960,7 +897,8 @@ namespace etl //************************************************************************* /// Fills the array from the range. - /// If the range is smaller than the array then the unused array elements are initialised with the supplied value. + /// If the range is smaller than the array then the unused array elements + /// are initialised with the supplied value. ///\param first The iterator to the first item in the range. ///\param last The iterator to one past the final item in the range. ///\return An iterator to the first array element set to 'value', or end(). @@ -1060,7 +998,8 @@ namespace etl //************************************************************************* /// Erases a value from the array. ///\param position The index of the position to erase at. - ///\param value The value to use to overwrite the last element in the array. + ///\param value The value to use to overwrite the last element in the + /// array. //************************************************************************* inline iterator erase_at(size_t, parameter_t) ETL_NOEXCEPT { @@ -1070,7 +1009,8 @@ namespace etl //************************************************************************* /// Erases a value from the array. ///\param position The iterator to the position to erase at. - ///\param value The value to use to overwrite the last element in the array. + ///\param value The value to use to overwrite the last element in the + /// array. //************************************************************************* iterator erase(const_iterator, parameter_t) ETL_NOEXCEPT { @@ -1081,7 +1021,8 @@ namespace etl /// Erases a range of values from the array. ///\param first The first item to erase. ///\param last The one past the last item to erase. - ///\param value The value to use to overwrite the last elements in the array. + ///\param value The value to use to overwrite the last elements in the + /// array. //************************************************************************* iterator erase_range(size_t, size_t, parameter_t) ETL_NOEXCEPT { @@ -1091,7 +1032,8 @@ namespace etl //************************************************************************* /// Erases a range of values from the array. ///\param position The iterator to the position to erase at. - ///\param value The value to use to overwrite the last elements in the array. + ///\param value The value to use to overwrite the last elements in the + /// array. //************************************************************************* iterator erase(const_iterator, const_iterator, parameter_t) ETL_NOEXCEPT { @@ -1114,7 +1056,7 @@ namespace etl template constexpr auto make_array(TValues&&... values) ETL_NOEXCEPT -> etl::array { - return { etl::forward(values)... }; + return {etl::forward(values)...}; } #endif @@ -1124,7 +1066,7 @@ namespace etl ///\param rhs The second array. //************************************************************************* template - void swap(etl::array &lhs, etl::array &rhs) + void swap(etl::array& lhs, etl::array& rhs) { lhs.swap(rhs); } @@ -1136,7 +1078,7 @@ namespace etl ///\return true if the arrays are equal, otherwise false //************************************************************************* template - ETL_CONSTEXPR14 bool operator ==(const etl::array& lhs, const etl::array& rhs) + ETL_CONSTEXPR14 bool operator==(const etl::array& lhs, const etl::array& rhs) { return etl::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin()); } @@ -1148,7 +1090,7 @@ namespace etl ///\return true if the arrays are not equal, otherwise false //************************************************************************* template - ETL_CONSTEXPR14 bool operator !=(const etl::array& lhs, const etl::array& rhs) + ETL_CONSTEXPR14 bool operator!=(const etl::array& lhs, const etl::array& rhs) { return !(lhs == rhs); } @@ -1157,25 +1099,25 @@ namespace etl /// Less than operator. ///\param lhs The first array. ///\param rhs The second array. - ///\return true if the first array is lexicographically less than the second, otherwise false + ///\return true if the first array is lexicographically less than the + /// second, otherwise false //************************************************************************* template - bool operator <(const etl::array& lhs, const etl::array& rhs) + bool operator<(const etl::array& lhs, const etl::array& rhs) { - return etl::lexicographical_compare(lhs.cbegin(), - lhs.cend(), - rhs.cbegin(), - rhs.cend()); + return etl::lexicographical_compare(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend()); } //************************************************************************* /// Less than or equal operator. ///\param lhs The first array. ///\param rhs The second array. - ///\return true if the first array is lexicographically less than or equal to the second, otherwise false + ///\return true if the first array is lexicographically less than or + /// equal to the second, otherwise + ///< b>false //************************************************************************* template - bool operator <=(const etl::array& lhs, const etl::array& rhs) + bool operator<=(const etl::array& lhs, const etl::array& rhs) { return !(lhs > rhs); } @@ -1184,10 +1126,11 @@ namespace etl /// Greater than operator. ///\param lhs The first array. ///\param rhs The second array. - ///\return true if the first array is lexicographically greater than the second, otherwise false + ///\return true if the first array is lexicographically greater than + /// the second, otherwise false template //************************************************************************* - bool operator >(const etl::array& lhs, const etl::array& rhs) + bool operator>(const etl::array& lhs, const etl::array& rhs) { return (rhs < lhs); } @@ -1196,10 +1139,12 @@ namespace etl /// Greater than or equal operator. ///\param lhs The first array. ///\param rhs The second array. - ///\return true if the first array is lexicographically greater than or equal to the second, otherwise false + ///\return true if the first array is lexicographically greater than or + /// equal to the second, otherwise + ///< b>false //************************************************************************* template - bool operator >=(const etl::array& lhs, const etl::array& rhs) + bool operator>=(const etl::array& lhs, const etl::array& rhs) { return !(lhs < rhs); } @@ -1233,6 +1178,6 @@ namespace etl ETL_STATIC_ASSERT(Index < Size, "Index out of bounds"); return a[Index]; } -} +} // namespace etl #endif diff --git a/include/etl/array_view.h b/include/etl/array_view.h index da38c67d..bb25dac0 100644 --- a/include/etl/array_view.h +++ b/include/etl/array_view.h @@ -32,18 +32,18 @@ SOFTWARE. #define ETL_ARRAY_VIEW_INCLUDED #include "platform.h" -#include "memory.h" +#include "algorithm.h" #include "array.h" -#include "iterator.h" #include "error_handler.h" #include "exception.h" -#include "nullptr.h" #include "hash.h" -#include "algorithm.h" +#include "iterator.h" +#include "memory.h" +#include "nullptr.h" #include "type_traits.h" #if ETL_USING_STL && ETL_USING_CPP11 -#include + #include #endif ///\defgroup array array @@ -114,22 +114,22 @@ namespace etl { public: - typedef T value_type; - typedef size_t size_type; - typedef const T& const_reference; - typedef const T* const_pointer; - typedef const T* const_iterator; + typedef T value_type; + typedef size_t size_type; + typedef const T& const_reference; + typedef const T* const_pointer; + typedef const T* const_iterator; typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; #if defined(ETL_ARRAY_VIEW_IS_MUTABLE) - typedef T* pointer; - typedef T& reference; - typedef T* iterator; + typedef T* pointer; + typedef T& reference; + typedef T* iterator; typedef ETL_OR_STD::reverse_iterator reverse_iterator; #else - typedef const_pointer pointer; - typedef const_reference reference; - typedef const_pointer iterator; + typedef const_pointer pointer; + typedef const_reference reference; + typedef const_pointer iterator; typedef const_reverse_iterator reverse_iterator; #endif @@ -137,8 +137,8 @@ namespace etl /// Default constructor. //************************************************************************* ETL_CONSTEXPR array_view() ETL_NOEXCEPT - : mbegin(ETL_NULLPTR), - mend(ETL_NULLPTR) + : mbegin(ETL_NULLPTR) + , mend(ETL_NULLPTR) { } @@ -146,7 +146,8 @@ namespace etl //************************************************************************* /// Construct from etl::array. //************************************************************************* - template , etl::remove_cv_t>::value, void>::type> + template < typename U, size_t Size, + typename = typename etl::enable_if< etl::is_same, etl::remove_cv_t>::value, void>::type> ETL_CONSTEXPR array_view(etl::array& a) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) @@ -156,7 +157,8 @@ namespace etl //************************************************************************* /// Construct from etl::array. //************************************************************************* - template , etl::remove_cv_t>::value, void>::type> + template < typename U, size_t Size, + typename = typename etl::enable_if< etl::is_same, etl::remove_cv_t>::value, void>::type> ETL_CONSTEXPR array_view(const etl::array& a) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) @@ -167,7 +169,9 @@ namespace etl /// Construct from etl::array. //************************************************************************* template - ETL_CONSTEXPR array_view(etl::array& a, typename etl::enable_if::type, typename etl::remove_cv::type>::value, void>::type* = 0) ETL_NOEXCEPT + ETL_CONSTEXPR array_view( + etl::array& a, + typename etl::enable_if< etl::is_same::type, typename etl::remove_cv::type>::value, void>::type* = 0) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) { @@ -177,7 +181,9 @@ namespace etl /// Construct from etl::array. //************************************************************************* template - ETL_CONSTEXPR array_view(const etl::array& a, typename etl::enable_if::type, typename etl::remove_cv::type>::value, void>::type* = 0) ETL_NOEXCEPT + ETL_CONSTEXPR array_view( + const etl::array& a, + typename etl::enable_if< etl::is_same::type, typename etl::remove_cv::type>::value, void>::type* = 0) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) { @@ -188,7 +194,8 @@ namespace etl //************************************************************************* /// Construct from std::array. //************************************************************************* - template , etl::remove_cv_t>::value, void>::type> + template < typename U, size_t Size, + typename = typename etl::enable_if< etl::is_same, etl::remove_cv_t>::value, void>::type> ETL_CONSTEXPR array_view(std::array& a) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) @@ -198,7 +205,8 @@ namespace etl //************************************************************************* /// Construct from std::array. //************************************************************************* - template , etl::remove_cv_t>::value, void>::type> + template < typename U, size_t Size, + typename = typename etl::enable_if< etl::is_same, etl::remove_cv_t>::value, void>::type> ETL_CONSTEXPR array_view(const std::array& a) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) @@ -211,10 +219,12 @@ namespace etl /// Construct from a container or other type that supports /// data() and size() member functions. //************************************************************************* - template >::value && - !etl::is_array>::value && - etl::is_same, etl::remove_cv_t::value_type>>::value, void>::type> - ETL_CONSTEXPR array_view(TContainer&& a) ETL_NOEXCEPT + template < typename TContainer, + typename = typename etl::enable_if< + !etl::is_pointer>::value && !etl::is_array>::value + && etl::is_same, etl::remove_cv_t::value_type>>::value, + void>::type> + ETL_CONSTEXPR array_view(TContainer&& a) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) { @@ -225,9 +235,12 @@ namespace etl /// data() and size() member functions. //************************************************************************* template - ETL_CONSTEXPR array_view(TContainer& a, typename etl::enable_if::type>::value && - !etl::is_array::value && - etl::is_same::type, typename etl::remove_cv::type::value_type>::type>::value, void>::type* = 0) ETL_NOEXCEPT + ETL_CONSTEXPR array_view(TContainer& a, + typename etl::enable_if< + !etl::is_pointer< typename etl::remove_reference::type>::value && !etl::is_array::value + && etl::is_same< typename etl::remove_cv::type, + typename etl::remove_cv::type::value_type>::type>::value, + void>::type* = 0) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) { @@ -238,9 +251,12 @@ namespace etl /// data() and size() member functions. //************************************************************************* template - ETL_CONSTEXPR array_view(const TContainer& a, typename etl::enable_if::type>::value && - !etl::is_array::value && - etl::is_same::type, typename etl::remove_cv::type::value_type>::type>::value, void>::type* = 0) ETL_NOEXCEPT + ETL_CONSTEXPR array_view(const TContainer& a, + typename etl::enable_if< + !etl::is_pointer< typename etl::remove_reference::type>::value && !etl::is_array::value + && etl::is_same< typename etl::remove_cv::type, + typename etl::remove_cv::type::value_type>::type>::value, + void>::type* = 0) ETL_NOEXCEPT : mbegin(a.data()) , mend(a.data() + a.size()) { @@ -252,29 +268,28 @@ namespace etl //************************************************************************* template ETL_CONSTEXPR array_view(const TIterator begin_, const TIterator end_) ETL_NOEXCEPT - : mbegin(etl::to_address(begin_)), - mend(etl::to_address(begin_) + etl::distance(begin_, end_)) + : mbegin(etl::to_address(begin_)) + , mend(etl::to_address(begin_) + etl::distance(begin_, end_)) { } //************************************************************************* /// Construct from iterator and size //************************************************************************* - template + template ETL_CONSTEXPR array_view(const TIterator begin_, const TSize size_) ETL_NOEXCEPT - : mbegin(etl::to_address(begin_)), - mend(etl::to_address(begin_) + size_) + : mbegin(etl::to_address(begin_)) + , mend(etl::to_address(begin_) + size_) { } //************************************************************************* /// Construct from C array //************************************************************************* - template - ETL_CONSTEXPR array_view(T(&begin_)[Array_Size]) ETL_NOEXCEPT - : mbegin(begin_), - mend(begin_ + Array_Size) + template + ETL_CONSTEXPR array_view(T (&begin_)[Array_Size]) ETL_NOEXCEPT + : mbegin(begin_) + , mend(begin_ + Array_Size) { } @@ -282,14 +297,15 @@ namespace etl /// Copy constructor //************************************************************************* ETL_CONSTEXPR array_view(const array_view& other) ETL_NOEXCEPT - : mbegin(other.mbegin), - mend(other.mend) + : mbegin(other.mbegin) + , mend(other.mend) { } //************************************************************************* /// Returns a reference to the first element. - /// If asserts or exceptions are enabled, throws an etl::array_view_empty if the view is empty. + /// If asserts or exceptions are enabled, throws an etl::array_view_empty if + /// the view is empty. //************************************************************************* reference front() { @@ -299,7 +315,8 @@ namespace etl //************************************************************************* /// Returns a const reference to the first element. - /// If asserts or exceptions are enabled, throws an etl::array_view_empty if the view is empty. + /// If asserts or exceptions are enabled, throws an etl::array_view_empty if + /// the view is empty. //************************************************************************* const_reference front() const { @@ -309,7 +326,8 @@ namespace etl //************************************************************************* /// Returns a reference to the last element. - /// If asserts or exceptions are enabled, throws an etl::array_view_empty if the view is empty. + /// If asserts or exceptions are enabled, throws an etl::array_view_empty if + /// the view is empty. //************************************************************************* reference back() { @@ -319,7 +337,8 @@ namespace etl //************************************************************************* /// Returns a const reference to the last element. - /// If asserts or exceptions are enabled, throws an etl::array_view_empty if the view is empty. + /// If asserts or exceptions are enabled, throws an etl::array_view_empty if + /// the view is empty. //************************************************************************* const_reference back() const { @@ -486,8 +505,7 @@ namespace etl //************************************************************************* /// Assign from iterator and size. //************************************************************************* - template + template void assign(const TIterator begin_, const TSize size_) { mbegin = etl::to_address(begin_); @@ -497,7 +515,8 @@ namespace etl #if defined(ETL_ARRAY_VIEW_IS_MUTABLE) //************************************************************************* /// Returns a reference to the indexed value. - /// If asserts or exceptions are enabled, throws an etl::array_view_bounds if the index is out of bounds. + /// If asserts or exceptions are enabled, throws an etl::array_view_bounds + /// if the index is out of bounds. //************************************************************************* reference operator[](const size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) { @@ -509,7 +528,8 @@ namespace etl //************************************************************************* /// Returns a const reference to the indexed value. - /// If asserts or exceptions are enabled, throws an etl::array_view_bounds if the index is out of bounds. + /// If asserts or exceptions are enabled, throws an etl::array_view_bounds + /// if the index is out of bounds. //************************************************************************* const_reference operator[](const size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) { @@ -556,10 +576,10 @@ namespace etl //************************************************************************* void remove_prefix(const size_type n) ETL_NOEXCEPT { - if (n < size()) - mbegin += n; - else - mbegin = mend; + if (n < size()) + mbegin += n; + else + mbegin = mend; } //************************************************************************* @@ -567,10 +587,10 @@ namespace etl //************************************************************************* void remove_suffix(const size_type n) ETL_NOEXCEPT { - if (n < size()) - mend -= n; - else - mend = mbegin; + if (n < size()) + mend -= n; + else + mend = mbegin; } //************************************************************************* @@ -584,16 +604,15 @@ namespace etl //************************************************************************* /// Equality for array views. //************************************************************************* - friend bool operator == (const array_view& lhs, const array_view& rhs) + friend bool operator==(const array_view& lhs, const array_view& rhs) { - return (lhs.size() == rhs.size()) && - etl::equal(lhs.begin(), lhs.end(), rhs.begin()); + return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin()); } //************************************************************************* /// Inequality for array views. //************************************************************************* - friend bool operator != (const array_view& lhs, const array_view& rhs) + friend bool operator!=(const array_view& lhs, const array_view& rhs) { return !(lhs == rhs); } @@ -601,7 +620,7 @@ namespace etl //************************************************************************* /// Less-than for array views. //************************************************************************* - friend bool operator < (const array_view& lhs, const array_view& rhs) + friend bool operator<(const array_view& lhs, const array_view& rhs) { return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } @@ -609,7 +628,7 @@ namespace etl //************************************************************************* /// Greater-than for array views. //************************************************************************* - friend bool operator > (const array_view& lhs, const array_view& rhs) + friend bool operator>(const array_view& lhs, const array_view& rhs) { return rhs < lhs; } @@ -617,7 +636,7 @@ namespace etl //************************************************************************* /// Less-than-equal for array views. //************************************************************************* - friend bool operator <= (const array_view& lhs, const array_view& rhs) + friend bool operator<=(const array_view& lhs, const array_view& rhs) { return !(lhs > rhs); } @@ -625,7 +644,7 @@ namespace etl //************************************************************************* /// Greater-than-equal for array views. //************************************************************************* - friend bool operator >= (const array_view& lhs, const array_view& rhs) + friend bool operator>=(const array_view& lhs, const array_view& rhs) { return !(lhs < rhs); } @@ -641,18 +660,14 @@ namespace etl //************************************************************************* #if ETL_USING_CPP17 template - array_view(TArray& a) - -> array_view; + array_view(TArray& a) -> array_view; template - array_view(const TIterator begin_, const TIterator end_) - -> array_view>; + array_view(const TIterator begin_, const TIterator end_) -> array_view>; - template - array_view(const TIterator begin_, const TSize size_) - -> array_view>; -#endif + template + array_view(const TIterator begin_, const TSize size_) -> array_view>; +#endif //************************************************************************* /// Hash function. @@ -668,7 +683,7 @@ namespace etl } }; #endif -} +} // namespace etl //************************************************************************* /// Swaps the values. diff --git a/include/etl/array_wrapper.h b/include/etl/array_wrapper.h index c56ca50a..2c434a05 100644 --- a/include/etl/array_wrapper.h +++ b/include/etl/array_wrapper.h @@ -32,12 +32,12 @@ SOFTWARE. #define ETL_ARRAY_WRAPPER_INCLUDED #include "platform.h" -#include "iterator.h" +#include "algorithm.h" #include "error_handler.h" #include "exception.h" #include "hash.h" +#include "iterator.h" #include "parameter_type.h" -#include "algorithm.h" ///\defgroup array array /// A wrapper for arrays @@ -75,19 +75,19 @@ namespace etl //*************************************************************************** /// Array wrapper. //*************************************************************************** - template + template class array_wrapper { public: - typedef T value_type; - typedef size_t size_type; - typedef T& reference; - typedef const T& const_reference; - typedef T* pointer; - typedef const T* const_pointer; - typedef T* iterator; - typedef const T* const_iterator; + typedef T value_type; + typedef size_t size_type; + typedef T& reference; + typedef const T& const_reference; + typedef T* pointer; + typedef const T* const_pointer; + typedef T* iterator; + typedef const T* const_iterator; typedef ETL_OR_STD::reverse_iterator reverse_iterator; typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; @@ -268,7 +268,8 @@ namespace etl //************************************************************************* /// Returns a reference to the indexed value. - /// If asserts or exceptions are enabled, throws an etl::array_wrapper_bounds if the index is out of bounds. + /// If asserts or exceptions are enabled, throws an + /// etl::array_wrapper_bounds if the index is out of bounds. //************************************************************************* reference operator[](size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) { @@ -278,14 +279,15 @@ namespace etl //************************************************************************* /// Returns a const reference to the indexed value. - /// If asserts or exceptions are enabled, throws an etl::array_wrapper_bounds if the index is out of bounds. + /// If asserts or exceptions are enabled, throws an + /// etl::array_wrapper_bounds if the index is out of bounds. //************************************************************************* ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) { - // Throwing from c++11 constexpr requires special syntax -#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR - return i < SIZE ? ARRAY_[i] : throw(ETL_ERROR(etl::array_wrapper_bounds)); -#else + // Throwing from c++11 constexpr requires special syntax +#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR + return i < SIZE ? ARRAY_[i] : throw(ETL_ERROR(etl::array_wrapper_bounds)); +#else ETL_ASSERT_CHECK_INDEX_OPERATOR(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds)); return ARRAY_[i]; #endif @@ -320,9 +322,8 @@ namespace etl //************************************************************************* /// Swaps the contents of arrays. //************************************************************************* - template - typename etl::enable_if::value, void>::type - swap(etl::array_wrapper& other) + template + typename etl::enable_if::value, void>::type swap(etl::array_wrapper& other) { using ETL_OR_STD::swap; // Allow ADL @@ -336,9 +337,8 @@ namespace etl //************************************************************************* /// Equality for array wrappers. //************************************************************************* - template - bool operator == (const etl::array_wrapper& lhs, - const etl::array_wrapper& rhs) + template + bool operator==(const etl::array_wrapper& lhs, const etl::array_wrapper& rhs) { return (SIZEL == SIZER) && etl::equal(lhs.begin(), lhs.end(), rhs.begin()); } @@ -346,9 +346,8 @@ namespace etl //************************************************************************* /// Inequality for array wrapper. //************************************************************************* - template - bool operator != (const etl::array_wrapper& lhs, - const etl::array_wrapper& rhs) + template + bool operator!=(const etl::array_wrapper& lhs, const etl::array_wrapper& rhs) { return !(lhs == rhs); } @@ -356,9 +355,8 @@ namespace etl //************************************************************************* /// Less-than for array wrapper. //************************************************************************* - template - bool operator < (const etl::array_wrapper& lhs, - const etl::array_wrapper& rhs) + template + bool operator<(const etl::array_wrapper& lhs, const etl::array_wrapper& rhs) { return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } @@ -366,9 +364,8 @@ namespace etl //************************************************************************* /// Greater-than for array wrapper. //************************************************************************* - template - bool operator > (const etl::array_wrapper& lhs, - const etl::array_wrapper& rhs) + template + bool operator>(const etl::array_wrapper& lhs, const etl::array_wrapper& rhs) { return rhs < lhs; } @@ -376,9 +373,8 @@ namespace etl //************************************************************************* /// Less-than-equal for array wrapper. //************************************************************************* - template - bool operator <= (const etl::array_wrapper& lhs, - const etl::array_wrapper& rhs) + template + bool operator<=(const etl::array_wrapper& lhs, const etl::array_wrapper& rhs) { return !(lhs > rhs); } @@ -386,9 +382,8 @@ namespace etl //************************************************************************* /// Greater-than-equal for array wrapper. //************************************************************************* - template - bool operator >= (const etl::array_wrapper& lhs, - const etl::array_wrapper& rhs) + template + bool operator>=(const etl::array_wrapper& lhs, const etl::array_wrapper& rhs) { return !(lhs < rhs); } @@ -397,7 +392,7 @@ namespace etl /// Hash function. //************************************************************************* #if ETL_USING_8BIT_TYPES - template + template struct hash > { size_t operator()(const etl::array_wrapper& aw) const @@ -409,19 +404,17 @@ namespace etl } }; #endif -} +} // namespace etl //************************************************************************* /// Swap. //************************************************************************* -template -void swap(etl::array_wrapper& lhs, - etl::array_wrapper& rhs) +template +void swap(etl::array_wrapper& lhs, etl::array_wrapper& rhs) { lhs.swap(rhs); } -#define ETL_ARRAY_WRAPPER(arraytype, arrayobject) etl::array_wrapper +#define ETL_ARRAY_WRAPPER(arraytype, arrayobject) etl::array_wrapper #endif - diff --git a/include/etl/atomic.h b/include/etl/atomic.h index 9d18ee5a..b3a556a5 100644 --- a/include/etl/atomic.h +++ b/include/etl/atomic.h @@ -32,7 +32,7 @@ SOFTWARE. #include "platform.h" #if ETL_HAS_ATOMIC - #if (ETL_USING_CPP11 && (ETL_USING_STL || defined(ETL_IN_UNIT_TEST))) + #if (ETL_USING_CPP11 && (ETL_USING_STL || defined(ETL_IN_UNIT_TEST))) #include "atomic/atomic_std.h" #elif defined(ETL_COMPILER_ARM5) #include "atomic/atomic_arm.h" diff --git a/include/etl/atomic/atomic_gcc_sync.h b/include/etl/atomic/atomic_gcc_sync.h index cc7a0ea1..087489aa 100644 --- a/include/etl/atomic/atomic_gcc_sync.h +++ b/include/etl/atomic/atomic_gcc_sync.h @@ -30,11 +30,11 @@ SOFTWARE. #define ETL_ATOMIC_GCC_SYNC_INCLUDED #include "../platform.h" -#include "../type_traits.h" -#include "../static_assert.h" -#include "../nullptr.h" #include "../char_traits.h" #include "../mutex.h" +#include "../nullptr.h" +#include "../static_assert.h" +#include "../type_traits.h" #include #include @@ -75,12 +75,20 @@ namespace etl { #if defined(ETL_USE_ATOMIC_BUILTINS) -#define ETL_BUILTIN_LOCK do { while (__atomic_test_and_set(&flag, etl::memory_order_seq_cst)) {} } while (0) -#define ETL_BUILTIN_UNLOCK do { __atomic_clear(&flag, etl::memory_order_seq_cst); } while (0) + #define ETL_BUILTIN_LOCK \ + do { \ + while (__atomic_test_and_set(&flag, etl::memory_order_seq_cst)) \ + { \ + } \ + } while (0) + #define ETL_BUILTIN_UNLOCK \ + do { \ + __atomic_clear(&flag, etl::memory_order_seq_cst); \ + } while (0) //*************************************************************************** - // Atomic type for pre C++11 GCC compilers that support the builtin '__atomic' functions. - // Only integral and pointer types are supported. + // Atomic type for pre C++11 GCC compilers that support the builtin '__atomic' + // functions. Only integral and pointer types are supported. //*************************************************************************** typedef enum memory_order @@ -121,14 +129,14 @@ namespace etl } // Assignment - T operator =(T v) + T operator=(T v) { store(v); return v; } - T operator =(T v) volatile + T operator=(T v) volatile { store(v); @@ -136,106 +144,106 @@ namespace etl } // Pre-increment - T operator ++() + T operator++() { return __atomic_add_fetch(&value, 1, etl::memory_order_seq_cst); } - T operator ++() volatile + T operator++() volatile { return __atomic_add_fetch(&value, 1, etl::memory_order_seq_cst); } // Post-increment - T operator ++(int) + T operator++(int) { return __atomic_fetch_add(&value, 1, etl::memory_order_seq_cst); } - T operator ++(int) volatile + T operator++(int) volatile { return __atomic_fetch_add(&value, 1, etl::memory_order_seq_cst); } // Pre-decrement - T operator --() + T operator--() { return __atomic_sub_fetch(&value, 1, etl::memory_order_seq_cst); } - T operator --() volatile + T operator--() volatile { return __atomic_sub_fetch(&value, 1, etl::memory_order_seq_cst); } // Post-decrement - T operator --(int) + T operator--(int) { return __atomic_fetch_sub(&value, 1, etl::memory_order_seq_cst); } - T operator --(int) volatile + T operator--(int) volatile { return __atomic_fetch_sub(&value, 1, etl::memory_order_seq_cst); } // Add - T operator +=(T v) + T operator+=(T v) { return __atomic_fetch_add(&value, v, etl::memory_order_seq_cst); } - T operator +=(T v) volatile + T operator+=(T v) volatile { return __atomic_fetch_add(&value, v, etl::memory_order_seq_cst); } // Subtract - T operator -=(T v) + T operator-=(T v) { return __atomic_fetch_sub(&value, v, etl::memory_order_seq_cst); } - T operator -=(T v) volatile + T operator-=(T v) volatile { return __atomic_fetch_sub(&value, v, etl::memory_order_seq_cst); } // And - T operator &=(T v) + T operator&=(T v) { return __atomic_fetch_and(&value, v, etl::memory_order_seq_cst); } - T operator &=(T v) volatile + T operator&=(T v) volatile { return __atomic_fetch_and(&value, v, etl::memory_order_seq_cst); } // Or - T operator |=(T v) + T operator|=(T v) { return __atomic_fetch_or(&value, v, etl::memory_order_seq_cst); } - T operator |=(T v) volatile + T operator|=(T v) volatile { return __atomic_fetch_or(&value, v, etl::memory_order_seq_cst); } // Exclusive or - T operator ^=(T v) + T operator^=(T v) { return __atomic_fetch_xor(&value, v, etl::memory_order_seq_cst); } - T operator ^=(T v) volatile + T operator^=(T v) volatile { return __atomic_fetch_xor(&value, v, etl::memory_order_seq_cst); } // Conversion operator - operator T () const + operator T() const { return __atomic_fetch_add(&value, 0, etl::memory_order_seq_cst); } @@ -388,8 +396,8 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable T value; }; @@ -413,14 +421,14 @@ namespace etl } // Assignment - T* operator =(T* v) + T* operator=(T* v) { store(v); return v; } - T* operator =(T* v) volatile + T* operator=(T* v) volatile { store(v); @@ -428,73 +436,77 @@ namespace etl } // Pre-increment - T* operator ++() + T* operator++() { return reinterpret_cast(__atomic_add_fetch(&value, sizeof(T), etl::memory_order_seq_cst)); } - T* operator ++() volatile + T* operator++() volatile { return reinterpret_cast(__atomic_add_fetch(&value, sizeof(T), etl::memory_order_seq_cst)); } // Post-increment - T* operator ++(int) + T* operator++(int) { return reinterpret_cast(__atomic_fetch_add(&value, sizeof(T), etl::memory_order_seq_cst)); } - T* operator ++(int) volatile + T* operator++(int) volatile { return reinterpret_cast(__atomic_fetch_add(&value, sizeof(T), etl::memory_order_seq_cst)); } // Pre-decrement - T* operator --() + T* operator--() { return reinterpret_cast(__atomic_sub_fetch(&value, sizeof(T), etl::memory_order_seq_cst)); } - T* operator --() volatile + T* operator--() volatile { return reinterpret_cast(__atomic_sub_fetch(&value, sizeof(T), etl::memory_order_seq_cst)); } // Post-decrement - T* operator --(int) + T* operator--(int) { return reinterpret_cast(__atomic_fetch_sub(&value, sizeof(T), etl::memory_order_seq_cst)); } - T* operator --(int) volatile + T* operator--(int) volatile { return reinterpret_cast(__atomic_fetch_sub(&value, sizeof(T), etl::memory_order_seq_cst)); } // Add - T* operator +=(ptrdiff_t v) + T* operator+=(ptrdiff_t v) { - return reinterpret_cast(__atomic_fetch_add(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); + return reinterpret_cast( + __atomic_fetch_add(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); } - T* operator +=(ptrdiff_t v) volatile + T* operator+=(ptrdiff_t v) volatile { - return reinterpret_cast(__atomic_fetch_add(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); + return reinterpret_cast( + __atomic_fetch_add(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); } // Subtract - T* operator -=(ptrdiff_t v) + T* operator-=(ptrdiff_t v) { - return reinterpret_cast(__atomic_fetch_sub(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); + return reinterpret_cast( + __atomic_fetch_sub(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); } - T* operator -=(ptrdiff_t v) volatile + T* operator-=(ptrdiff_t v) volatile { - return reinterpret_cast(__atomic_fetch_sub(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); + return reinterpret_cast( + __atomic_fetch_sub(&value, static_cast(v * static_cast(sizeof(T))), etl::memory_order_seq_cst)); } // Conversion operator - operator T* () const + operator T*() const { return reinterpret_cast(__atomic_fetch_add(&value, 0, etl::memory_order_seq_cst)); } @@ -630,8 +642,8 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable uintptr_t value; }; @@ -655,14 +667,14 @@ namespace etl } // Assignment - bool operator =(bool v) + bool operator=(bool v) { store(v); return v; } - bool operator =(bool v) volatile + bool operator=(bool v) volatile { store(v); @@ -670,7 +682,7 @@ namespace etl } // Conversion operator - operator bool () const + operator bool() const { return static_cast(__atomic_fetch_add(&value, 0, etl::memory_order_seq_cst)); } @@ -728,7 +740,7 @@ namespace etl bool compare_exchange_weak(bool& expected, bool desired, etl::memory_order order = etl::memory_order_seq_cst) { char expected_v = char(expected); - char desired_v = char(desired); + char desired_v = char(desired); return __atomic_compare_exchange_n(&value, &expected_v, desired_v, true, order, order); } @@ -736,7 +748,7 @@ namespace etl bool compare_exchange_weak(bool& expected, bool desired, etl::memory_order order = etl::memory_order_seq_cst) volatile { char expected_v = char(expected); - char desired_v = char(desired); + char desired_v = char(desired); return __atomic_compare_exchange_n(&value, &expected_v, desired_v, true, order, order); } @@ -744,7 +756,7 @@ namespace etl bool compare_exchange_weak(bool& expected, bool desired, etl::memory_order success, etl::memory_order failure) { char expected_v = char(expected); - char desired_v = char(desired); + char desired_v = char(desired); return __atomic_compare_exchange_n(&value, &expected_v, desired_v, true, success, failure); } @@ -792,8 +804,8 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable char value; }; @@ -827,7 +839,7 @@ namespace etl } // Assignment - T operator =(T v) + T operator=(T v) { store(v); @@ -835,7 +847,7 @@ namespace etl } // Conversion operator - operator T () const + operator T() const { ETL_BUILTIN_LOCK; T result = value; @@ -876,7 +888,7 @@ namespace etl (void)order; ETL_BUILTIN_LOCK; T result = value; - value = v; + value = v; ETL_BUILTIN_UNLOCK; return result; @@ -891,7 +903,7 @@ namespace etl ETL_BUILTIN_LOCK; if (memcmp(&value, &expected, sizeof(T)) == 0) { - value = desired; + value = desired; result = true; } else @@ -926,26 +938,34 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable char flag; - mutable T value; + mutable T value; }; -#undef ETL_BUILTIN_LOCK -#undef ETL_BUILTIN_UNLOCK + #undef ETL_BUILTIN_LOCK + #undef ETL_BUILTIN_UNLOCK #endif #if defined(ETL_USE_SYNC_BUILTINS) -#define ETL_BUILTIN_LOCK do { while (__sync_lock_test_and_set(&flag, 1U)) {} } while(0) -#define ETL_BUILTIN_UNLOCK do { __sync_lock_release(&flag); } while(0) + #define ETL_BUILTIN_LOCK \ + do { \ + while (__sync_lock_test_and_set(&flag, 1U)) \ + { \ + } \ + } while (0) + #define ETL_BUILTIN_UNLOCK \ + do { \ + __sync_lock_release(&flag); \ + } while (0) //*************************************************************************** - // Atomic type for pre C++11 GCC compilers that support the builtin '__sync' functions. - // Only integral and pointer types are supported. + // Atomic type for pre C++11 GCC compilers that support the builtin '__sync' + // functions. Only integral and pointer types are supported. //*************************************************************************** typedef enum memory_order @@ -988,14 +1008,14 @@ namespace etl } // Assignment - T operator =(T v) + T operator=(T v) { store(v); return v; } - T operator =(T v) volatile + T operator=(T v) volatile { store(v); @@ -1003,106 +1023,106 @@ namespace etl } // Pre-increment - T operator ++() + T operator++() { return __sync_add_and_fetch(&value, 1); } - T operator ++() volatile + T operator++() volatile { return __sync_add_and_fetch(&value, 1); } // Post-increment - T operator ++(int) + T operator++(int) { return __sync_fetch_and_add(&value, 1); } - T operator ++(int) volatile + T operator++(int) volatile { return __sync_fetch_and_add(&value, 1); } // Pre-decrement - T operator --() + T operator--() { return __sync_sub_and_fetch(&value, 1); } - T operator --() volatile + T operator--() volatile { return __sync_sub_and_fetch(&value, 1); } // Post-decrement - T operator --(int) + T operator--(int) { return __sync_fetch_and_sub(&value, 1); } - T operator --(int) volatile + T operator--(int) volatile { return __sync_fetch_and_sub(&value, 1); } // Add - T operator +=(T v) + T operator+=(T v) { return __sync_fetch_and_add(&value, v); } - T operator +=(T v) volatile + T operator+=(T v) volatile { return __sync_fetch_and_add(&value, v); } // Subtract - T operator -=(T v) + T operator-=(T v) { return __sync_fetch_and_sub(&value, v); } - T operator -=(T v) volatile + T operator-=(T v) volatile { return __sync_fetch_and_sub(&value, v); } // And - T operator &=(T v) + T operator&=(T v) { return __sync_fetch_and_and(&value, v); } - T operator &=(T v) volatile + T operator&=(T v) volatile { return __sync_fetch_and_and(&value, v); } // Or - T operator |=(T v) + T operator|=(T v) { return __sync_fetch_and_or(&value, v); } - T operator |=(T v) volatile + T operator|=(T v) volatile { return __sync_fetch_and_or(&value, v); } // Exclusive or - T operator ^=(T v) + T operator^=(T v) { return __sync_fetch_and_xor(&value, v); } - T operator ^=(T v) volatile + T operator^=(T v) volatile { return __sync_fetch_and_xor(&value, v); } // Conversion operator - operator T () const + operator T() const { return __sync_fetch_and_add(&value, 0); } @@ -1367,8 +1387,8 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable volatile T value; }; @@ -1392,14 +1412,14 @@ namespace etl } // Assignment - T* operator =(T* v) + T* operator=(T* v) { store(v); return v; } - T* operator =(T* v) volatile + T* operator=(T* v) volatile { store(v); @@ -1407,78 +1427,78 @@ namespace etl } // Pre-increment - T* operator ++() + T* operator++() { return reinterpret_cast(__sync_add_and_fetch(&value, sizeof(T))); } - T* operator ++() volatile + T* operator++() volatile { return reinterpret_cast(__sync_add_and_fetch(&value, sizeof(T))); } // Post-increment - T* operator ++(int) + T* operator++(int) { return reinterpret_cast(__sync_fetch_and_add(&value, sizeof(T))); } - T* operator ++(int) volatile + T* operator++(int) volatile { return reinterpret_cast(__sync_fetch_and_add(&value, sizeof(T))); } // Pre-decrement - T* operator --() + T* operator--() { return reinterpret_cast(__sync_sub_and_fetch(&value, sizeof(T))); } - T* operator --() volatile + T* operator--() volatile { return reinterpret_cast(__sync_sub_and_fetch(&value, sizeof(T))); } // Post-decrement - T* operator --(int) + T* operator--(int) { return reinterpret_cast(__sync_fetch_and_sub(&value, sizeof(T))); } - T* operator --(int) volatile + T* operator--(int) volatile { return reinterpret_cast(__sync_fetch_and_sub(&value, sizeof(T))); } // Add - T* operator +=(ptrdiff_t v) + T* operator+=(ptrdiff_t v) { return reinterpret_cast(__sync_fetch_and_add(&value, v * sizeof(T))); } - T* operator +=(ptrdiff_t v) volatile + T* operator+=(ptrdiff_t v) volatile { return reinterpret_cast(__sync_fetch_and_add(&value, v * sizeof(T))); } // Subtract - T* operator -=(ptrdiff_t v) + T* operator-=(ptrdiff_t v) { return reinterpret_cast(__sync_fetch_and_sub(&value, v * sizeof(T))); } - T* operator -=(ptrdiff_t v) volatile + T* operator-=(ptrdiff_t v) volatile { return reinterpret_cast(__sync_fetch_and_sub(&value, v * sizeof(T))); } // Conversion operator - operator T* () const + operator T*() const { return reinterpret_cast(__sync_fetch_and_add(&value, 0)); } - operator T* () volatile const + operator T*() volatile const { return reinterpret_cast(__sync_fetch_and_add(&value, 0)); } @@ -1677,8 +1697,8 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable uintptr_t value; }; @@ -1702,14 +1722,14 @@ namespace etl } // Assignment - bool operator =(bool v) + bool operator=(bool v) { store(v); return v; } - bool operator =(bool v) volatile + bool operator=(bool v) volatile { store(v); @@ -1899,8 +1919,8 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable char value; }; @@ -1934,7 +1954,7 @@ namespace etl } // Assignment - T operator =(T v) + T operator=(T v) { store(v); @@ -1942,7 +1962,7 @@ namespace etl } // Conversion operator - operator T () const + operator T() const { ETL_BUILTIN_LOCK; T result = value; @@ -1980,7 +2000,7 @@ namespace etl { ETL_BUILTIN_LOCK; T result = value; - value = v; + value = v; ETL_BUILTIN_UNLOCK; return result; @@ -1994,7 +2014,7 @@ namespace etl ETL_BUILTIN_LOCK; if (memcmp(&value, &expected, sizeof(T)) == 0) { - value = desired; + value = desired; result = true; } else @@ -2024,79 +2044,78 @@ namespace etl private: - atomic& operator =(const atomic&) ETL_DELETE; - atomic& operator =(const atomic&) volatile ETL_DELETE; + atomic& operator=(const atomic&) ETL_DELETE; + atomic& operator=(const atomic&) volatile ETL_DELETE; mutable char flag; - mutable T value; + mutable T value; }; -#undef ETL_SYNC_BUILTIN_LOCK -#undef ETL_SYNC_BUILTIN_UNLOCK + #undef ETL_SYNC_BUILTIN_LOCK + #undef ETL_SYNC_BUILTIN_UNLOCK #endif - typedef etl::atomic atomic_bool; - typedef etl::atomic atomic_char; - typedef etl::atomic atomic_schar; - typedef etl::atomic atomic_uchar; - typedef etl::atomic atomic_short; - typedef etl::atomic atomic_ushort; - typedef etl::atomic atomic_int; - typedef etl::atomic atomic_uint; - typedef etl::atomic atomic_long; - typedef etl::atomic atomic_ulong; - typedef etl::atomic atomic_llong; - typedef etl::atomic atomic_ullong; - typedef etl::atomic atomic_wchar_t; + typedef etl::atomic atomic_bool; + typedef etl::atomic atomic_char; + typedef etl::atomic atomic_schar; + typedef etl::atomic atomic_uchar; + typedef etl::atomic atomic_short; + typedef etl::atomic atomic_ushort; + typedef etl::atomic atomic_int; + typedef etl::atomic atomic_uint; + typedef etl::atomic atomic_long; + typedef etl::atomic atomic_ulong; + typedef etl::atomic atomic_llong; + typedef etl::atomic atomic_ullong; + typedef etl::atomic atomic_wchar_t; #if ETL_HAS_NATIVE_CHAR8_T - typedef etl::atomic atomic_char8_t; + typedef etl::atomic atomic_char8_t; #endif #if ETL_HAS_NATIVE_CHAR16_T - typedef etl::atomic atomic_char16_t; + typedef etl::atomic atomic_char16_t; #endif #if ETL_HAS_NATIVE_CHAR32_T - typedef etl::atomic atomic_char32_t; + typedef etl::atomic atomic_char32_t; #endif #if ETL_USING_8BIT_TYPES - typedef etl::atomic atomic_uint8_t; - typedef etl::atomic atomic_int8_t; + typedef etl::atomic atomic_uint8_t; + typedef etl::atomic atomic_int8_t; #endif - typedef etl::atomic atomic_uint16_t; - typedef etl::atomic atomic_int16_t; - typedef etl::atomic atomic_uint32_t; - typedef etl::atomic atomic_int32_t; + typedef etl::atomic atomic_uint16_t; + typedef etl::atomic atomic_int16_t; + typedef etl::atomic atomic_uint32_t; + typedef etl::atomic atomic_int32_t; #if ETL_USING_64BIT_TYPES - typedef etl::atomic atomic_uint64_t; - typedef etl::atomic atomic_int64_t; + typedef etl::atomic atomic_uint64_t; + typedef etl::atomic atomic_int64_t; #endif - typedef etl::atomic atomic_int_least8_t; - typedef etl::atomic atomic_uint_least8_t; - typedef etl::atomic atomic_int_least16_t; - typedef etl::atomic atomic_uint_least16_t; - typedef etl::atomic atomic_int_least32_t; - typedef etl::atomic atomic_uint_least32_t; + typedef etl::atomic atomic_int_least8_t; + typedef etl::atomic atomic_uint_least8_t; + typedef etl::atomic atomic_int_least16_t; + typedef etl::atomic atomic_uint_least16_t; + typedef etl::atomic atomic_int_least32_t; + typedef etl::atomic atomic_uint_least32_t; #if ETL_USING_64BIT_TYPES - typedef etl::atomic atomic_int_least64_t; - typedef etl::atomic atomic_uint_least64_t; + typedef etl::atomic atomic_int_least64_t; + typedef etl::atomic atomic_uint_least64_t; #endif - typedef etl::atomic atomic_int_fast8_t; - typedef etl::atomic atomic_uint_fast8_t; - typedef etl::atomic atomic_int_fast16_t; - typedef etl::atomic atomic_uint_fast16_t; - typedef etl::atomic atomic_int_fast32_t; - typedef etl::atomic atomic_uint_fast32_t; + typedef etl::atomic atomic_int_fast8_t; + typedef etl::atomic atomic_uint_fast8_t; + typedef etl::atomic atomic_int_fast16_t; + typedef etl::atomic atomic_uint_fast16_t; + typedef etl::atomic atomic_int_fast32_t; + typedef etl::atomic atomic_uint_fast32_t; #if ETL_USING_64BIT_TYPES - typedef etl::atomic atomic_int_fast64_t; - typedef etl::atomic atomic_uint_fast64_t; + typedef etl::atomic atomic_int_fast64_t; + typedef etl::atomic atomic_uint_fast64_t; #endif - typedef etl::atomic atomic_intptr_t; - typedef etl::atomic atomic_uintptr_t; - typedef etl::atomic atomic_size_t; - typedef etl::atomic atomic_ptrdiff_t; - typedef etl::atomic atomic_intmax_t; - typedef etl::atomic atomic_uintmax_t; -} + typedef etl::atomic atomic_intptr_t; + typedef etl::atomic atomic_uintptr_t; + typedef etl::atomic atomic_size_t; + typedef etl::atomic atomic_ptrdiff_t; + typedef etl::atomic atomic_intmax_t; + typedef etl::atomic atomic_uintmax_t; +} // namespace etl #endif - diff --git a/include/etl/atomic/atomic_std.h b/include/etl/atomic/atomic_std.h index bea19d50..ee765a70 100644 --- a/include/etl/atomic/atomic_std.h +++ b/include/etl/atomic/atomic_std.h @@ -30,8 +30,8 @@ SOFTWARE. #define ETL_ATOMIC_STD_INCLUDED #include "../platform.h" -#include "../nullptr.h" #include "../char_traits.h" +#include "../nullptr.h" #include #include @@ -55,39 +55,39 @@ namespace etl static ETL_CONSTANT etl::memory_order memory_order_acq_rel = std::memory_order_acq_rel; static ETL_CONSTANT etl::memory_order memory_order_seq_cst = std::memory_order_seq_cst; - using atomic_bool = std::atomic; - using atomic_char = std::atomic; - using atomic_schar = std::atomic; - using atomic_uchar = std::atomic; - using atomic_short = std::atomic; - using atomic_ushort = std::atomic; - using atomic_int = std::atomic; - using atomic_uint = std::atomic; - using atomic_long = std::atomic; - using atomic_ulong = std::atomic; - using atomic_llong = std::atomic; - using atomic_ullong = std::atomic; - using atomic_wchar_t = std::atomic; + using atomic_bool = std::atomic; + using atomic_char = std::atomic; + using atomic_schar = std::atomic; + using atomic_uchar = std::atomic; + using atomic_short = std::atomic; + using atomic_ushort = std::atomic; + using atomic_int = std::atomic; + using atomic_uint = std::atomic; + using atomic_long = std::atomic; + using atomic_ulong = std::atomic; + using atomic_llong = std::atomic; + using atomic_ullong = std::atomic; + using atomic_wchar_t = std::atomic; #if ETL_HAS_NATIVE_CHAR8_T using atomic_char8_t = std::atomic; #endif #if ETL_HAS_NATIVE_CHAR16_T - using atomic_char16_t = std::atomic; + using atomic_char16_t = std::atomic; #endif #if ETL_HAS_NATIVE_CHAR32_T - using atomic_char32_t = std::atomic; + using atomic_char32_t = std::atomic; #endif #if ETL_USING_8BIT_TYPES - using atomic_uint8_t = std::atomic; - using atomic_int8_t = std::atomic; + using atomic_uint8_t = std::atomic; + using atomic_int8_t = std::atomic; #endif - using atomic_uint16_t = std::atomic; - using atomic_int16_t = std::atomic; - using atomic_uint32_t = std::atomic; - using atomic_int32_t = std::atomic; + using atomic_uint16_t = std::atomic; + using atomic_int16_t = std::atomic; + using atomic_uint32_t = std::atomic; + using atomic_int32_t = std::atomic; #if ETL_USING_64BIT_TYPES - using atomic_uint64_t = std::atomic; - using atomic_int64_t = std::atomic; + using atomic_uint64_t = std::atomic; + using atomic_int64_t = std::atomic; #endif using atomic_int_least8_t = std::atomic; using atomic_uint_least8_t = std::atomic; @@ -99,22 +99,22 @@ namespace etl using atomic_int_least64_t = std::atomic; using atomic_uint_least64_t = std::atomic; #endif - using atomic_int_fast8_t = std::atomic; - using atomic_uint_fast8_t = std::atomic; - using atomic_int_fast16_t = std::atomic; - using atomic_uint_fast16_t = std::atomic; - using atomic_int_fast32_t = std::atomic; - using atomic_uint_fast32_t = std::atomic; + using atomic_int_fast8_t = std::atomic; + using atomic_uint_fast8_t = std::atomic; + using atomic_int_fast16_t = std::atomic; + using atomic_uint_fast16_t = std::atomic; + using atomic_int_fast32_t = std::atomic; + using atomic_uint_fast32_t = std::atomic; #if ETL_USING_64BIT_TYPES - using atomic_int_fast64_t = std::atomic; - using atomic_uint_fast64_t = std::atomic; + using atomic_int_fast64_t = std::atomic; + using atomic_uint_fast64_t = std::atomic; #endif - using atomic_intptr_t = std::atomic; - using atomic_uintptr_t = std::atomic; - using atomic_size_t = std::atomic; - using atomic_ptrdiff_t = std::atomic; - using atomic_intmax_t = std::atomic; - using atomic_uintmax_t = std::atomic; -} + using atomic_intptr_t = std::atomic; + using atomic_uintptr_t = std::atomic; + using atomic_size_t = std::atomic; + using atomic_ptrdiff_t = std::atomic; + using atomic_intmax_t = std::atomic; + using atomic_uintmax_t = std::atomic; +} // namespace etl #endif diff --git a/include/etl/base64.h b/include/etl/base64.h index 4034248d..4ce98b3c 100644 --- a/include/etl/base64.h +++ b/include/etl/base64.h @@ -27,27 +27,29 @@ SOFTWARE. #define ETL_BASE64_INCLUDED #include "platform.h" -#include "static_assert.h" -#include "exception.h" -#include "error_handler.h" -#include "type_traits.h" #include "enum_type.h" +#include "error_handler.h" +#include "exception.h" #include "integral_limits.h" +#include "static_assert.h" +#include "type_traits.h" #include /************************************************************************************************************************************************************************** -* See https://en.wikipedia.org/wiki/Base64 -* -* Encoding Encoding characters Separate encoding of lines Decoding non-encoding characters -* 62nd 63rd Pad Separators Length Checksum -* RFC 1421 : Base64 for Privacy - Enhanced Mail(deprecated) + / = mandatory CR + LF 64, or lower for the last line No No -* RFC 2045 : Base64 transfer encoding for MIME + / = mandatory CR + LF At most 76 No Discarded -* RFC 2152 : Base64 for UTF - 7 + / No No No -* RFC 3501 : Base64 encoding for IMAP mailbox names + , No No No -* RFC 4648 : base64(standard)[a] + / = optional No No -* RFC 4648 : base64url(URL - and filename - safe standard) - _ = optional No No -**************************************************************************************************************************************************************************/ + * See https://en.wikipedia.org/wiki/Base64 + * + * Encoding Encoding characters + *Separate encoding of lines Decoding non-encoding characters 62nd 63rd Pad + *Separators Length Checksum RFC 1421 : Base64 for + *Privacy - Enhanced Mail(deprecated) + / = mandatory CR + LF 64, + *or lower for the last line No No RFC 2045 : Base64 transfer encoding for MIME + *+ / = mandatory CR + LF At most 76 No Discarded RFC 2152 : Base64 + *for UTF - 7 + / No No No RFC + *3501 : Base64 encoding for IMAP mailbox names + , No + *No No RFC 4648 : base64(standard)[a] + / = optional No No RFC 4648 : + *base64url(URL - and filename - safe standard) - _ = optional No No + **************************************************************************************************************************************************************************/ namespace etl { @@ -114,8 +116,8 @@ namespace etl { enum enum_type { - //RFC_1421, // Not implemented - //RFC_2045, // Not implemented + // RFC_1421, // Not implemented + // RFC_2045, // Not implemented RFC_2152, RFC_3501, RFC_4648, @@ -125,8 +127,8 @@ namespace etl }; ETL_DECLARE_ENUM_TYPE(Encoding, int) - //ETL_ENUM_TYPE(RFC_1421, "RFC_1421") // Not implemented - //ETL_ENUM_TYPE(RFC_2045, "RFC_2045") // Not implemented + // ETL_ENUM_TYPE(RFC_1421, "RFC_1421") // Not implemented + // ETL_ENUM_TYPE(RFC_2045, "RFC_2045") // Not implemented ETL_ENUM_TYPE(RFC_2152, "RFC_2152") ETL_ENUM_TYPE(RFC_3501, "RFC_3501") ETL_ENUM_TYPE(RFC_4648, "RFC_4648") @@ -166,16 +168,14 @@ namespace etl enum { - Invalid_Data = etl::integral_limits::max, + Invalid_Data = etl::integral_limits::max, Min_Encode_Buffer_Size = 4, Min_Decode_Buffer_Size = 3 }; protected: - ETL_CONSTEXPR14 - base64(const char* encoder_table_, - bool use_padding_) + ETL_CONSTEXPR14 base64(const char* encoder_table_, bool use_padding_) : encoder_table(encoder_table_) , use_padding(use_padding_) { @@ -184,9 +184,7 @@ namespace etl //************************************************************************* // Character set for RFC-1421, RFC-2045, RFC-2152 and RFC-4648 //************************************************************************* - static - ETL_CONSTEXPR14 - const char* character_set_1() + static ETL_CONSTEXPR14 const char* character_set_1() { return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; } @@ -194,9 +192,7 @@ namespace etl //************************************************************************* // Character set for RFC-4648-URL //************************************************************************* - static - ETL_CONSTEXPR14 - const char* character_set_2() + static ETL_CONSTEXPR14 const char* character_set_2() { return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; } @@ -204,9 +200,7 @@ namespace etl //************************************************************************* // Character set for RFC-3501-URL //************************************************************************* - static - ETL_CONSTEXPR14 - const char* character_set_3() + static ETL_CONSTEXPR14 const char* character_set_3() { return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; } @@ -214,5 +208,5 @@ namespace etl const char* encoder_table; const bool use_padding; }; -} +} // namespace etl #endif diff --git a/include/etl/base64_decoder.h b/include/etl/base64_decoder.h index 7f542616..8e55d9dc 100644 --- a/include/etl/base64_decoder.h +++ b/include/etl/base64_decoder.h @@ -1,5 +1,5 @@ //************************************************************************* -///Decode from Base64 from and to pointer/length +/// Decode from Base64 from and to pointer/length //*************************************************************************///\file /****************************************************************************** @@ -29,16 +29,16 @@ SOFTWARE. #define ETL_BASE64_DECODER_INCLUDED #include "platform.h" -#include "static_assert.h" -#include "error_handler.h" -#include "type_traits.h" -#include "binary.h" #include "algorithm.h" +#include "binary.h" +#include "delegate.h" +#include "enum_type.h" +#include "error_handler.h" #include "integral_limits.h" #include "iterator.h" -#include "enum_type.h" -#include "delegate.h" #include "span.h" +#include "static_assert.h" +#include "type_traits.h" #include "base64.h" @@ -48,11 +48,12 @@ SOFTWARE. #include #endif -#define ETL_IS_8_BIT_INTEGRAL(Type) (etl::is_integral::type>::value && \ - (etl::integral_limits::type>::bits == 8U)) +#define ETL_IS_8_BIT_INTEGRAL(Type) \ + (etl::is_integral::type>::value && (etl::integral_limits::type>::bits == 8U)) -#define ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(Type) (etl::is_integral::type>::value_type>::value && \ - (etl::integral_limits::type>::value_type>::bits == 8U)) +#define ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(Type) \ + (etl::is_integral< typename etl::iterator_traits< typename etl::remove_cv::type>::value_type>::value \ + && (etl::integral_limits< typename etl::iterator_traits< typename etl::remove_cv::type>::value_type>::bits == 8U)) namespace etl { @@ -70,8 +71,7 @@ namespace etl /// Decode to Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool decode(T value) + ETL_CONSTEXPR14 bool decode(T value) { ETL_STATIC_ASSERT(ETL_IS_8_BIT_INTEGRAL(T), "Input type must be an 8 bit integral"); @@ -101,8 +101,7 @@ namespace etl /// Decode from Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool decode(TInputIterator input_begin, TInputIterator input_end) + ETL_CONSTEXPR14 bool decode(TInputIterator input_begin, TInputIterator input_end) { ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral"); @@ -121,8 +120,7 @@ namespace etl /// Decode from Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool decode(TInputIterator input_begin, size_t input_length) + ETL_CONSTEXPR14 bool decode(TInputIterator input_begin, size_t input_length) { ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral"); @@ -141,8 +139,7 @@ namespace etl /// Decode from Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool decode_final(TInputIterator input_begin, TInputIterator input_end) + ETL_CONSTEXPR14 bool decode_final(TInputIterator input_begin, TInputIterator input_end) { return decode(input_begin, input_end) && flush(); } @@ -151,8 +148,7 @@ namespace etl /// Decode from Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool decode_final(TInputIterator input_begin, size_t input_length) + ETL_CONSTEXPR14 bool decode_final(TInputIterator input_begin, size_t input_length) { return decode(input_begin, input_length) && flush(); } @@ -160,8 +156,7 @@ namespace etl //************************************************************************* /// Flush any remaining data to the output. //************************************************************************* - ETL_CONSTEXPR14 - bool flush() + ETL_CONSTEXPR14 bool flush() { // Encode any remaining input data. bool success = decode_block(); @@ -193,32 +188,28 @@ namespace etl //************************************************************************* /// Reset the encoder. //************************************************************************* - ETL_CONSTEXPR14 - void restart() + ETL_CONSTEXPR14 void restart() { reset_input_buffer(); reset_output_buffer(); - overflow_detected = false; + overflow_detected = false; invalid_data_detected = false; - padding_received = false; + padding_received = false; } //************************************************************************* /// Returns the beginning of the output buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const unsigned char* begin() const + ETL_NODISCARD ETL_CONSTEXPR14 const unsigned char* begin() const { return p_output_buffer; } //************************************************************************* - /// This only returns a useful value if a callback has not been set or called. + /// This only returns a useful value if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const unsigned char* end() const + ETL_NODISCARD ETL_CONSTEXPR14 const unsigned char* end() const { return p_output_buffer + output_buffer_length; } @@ -226,30 +217,26 @@ namespace etl //************************************************************************* /// Returns the beginning of the output buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const unsigned char* cbegin() const + ETL_NODISCARD ETL_CONSTEXPR14 const unsigned char* cbegin() const { return p_output_buffer; } //************************************************************************* - /// This only returns a useful value if a callback has not been set or called. + /// This only returns a useful value if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const unsigned char* cend() const + ETL_NODISCARD ETL_CONSTEXPR14 const unsigned char* cend() const { return p_output_buffer + output_buffer_length; } //************************************************************************* /// Returns the size of the output buffer. - /// This only returns a useful value if a callback has not been set or called. + /// This only returns a useful value if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - size_t size() const + ETL_NODISCARD ETL_CONSTEXPR14 size_t size() const { return output_buffer_length; } @@ -257,20 +244,17 @@ namespace etl //************************************************************************* /// Returns the maximum size of the output buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - size_t buffer_size() const + ETL_NODISCARD ETL_CONSTEXPR14 size_t buffer_size() const { return output_buffer_max_size; } //************************************************************************* /// Get a span of the output data. - /// This only returns a useful span if a callback has not been set or called. + /// This only returns a useful span if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - span_type span() const + ETL_NODISCARD ETL_CONSTEXPR14 span_type span() const { return span_type(begin(), end()); } @@ -278,9 +262,7 @@ namespace etl //************************************************************************* /// Returns true if the output buffer has overflowed //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - bool overflow() const + ETL_NODISCARD ETL_CONSTEXPR14 bool overflow() const { return overflow_detected; } @@ -288,9 +270,7 @@ namespace etl //************************************************************************* /// Returns true if an invalid character was detected. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - bool invalid_data() const + ETL_NODISCARD ETL_CONSTEXPR14 bool invalid_data() const { return invalid_data_detected; } @@ -298,9 +278,7 @@ namespace etl //************************************************************************* /// Returns true if an error was detected. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - bool error() const + ETL_NODISCARD ETL_CONSTEXPR14 bool error() const { return overflow() || invalid_data(); } @@ -310,12 +288,8 @@ namespace etl //************************************************************************* /// Constructor //************************************************************************* - ETL_CONSTEXPR14 - ibase64_decoder(const char* encoder_table_, - bool use_padding_, - unsigned char* p_output_buffer_, - size_t ouput_buffer_max_size_, - callback_type callback_) + ETL_CONSTEXPR14 ibase64_decoder(const char* encoder_table_, bool use_padding_, unsigned char* p_output_buffer_, size_t ouput_buffer_max_size_, + callback_type callback_) : base64(encoder_table_, use_padding_) , input_buffer() , input_buffer_length(0) @@ -333,9 +307,7 @@ namespace etl /// Calculates the minimum buffer size required to decode from Base64 //************************************************************************* ETL_NODISCARD - static - ETL_CONSTEXPR14 - size_t decoded_size_from_valid_input_length(size_t input_length) + static ETL_CONSTEXPR14 size_t decoded_size_from_valid_input_length(size_t input_length) { return input_length - (input_length / 4U); } @@ -343,14 +315,13 @@ namespace etl private: //************************************************************************* - // Translates a sextet into an index + // Translates a sextet into an index //************************************************************************* template - ETL_CONSTEXPR14 - uint32_t get_index_from_sextet(T sextet) + ETL_CONSTEXPR14 uint32_t get_index_from_sextet(T sextet) { const char* encoder_table_end = encoder_table + 64; - const char* p_sextet = etl::find(encoder_table, encoder_table_end, static_cast(sextet)); + const char* p_sextet = etl::find(encoder_table, encoder_table_end, static_cast(sextet)); if (p_sextet != encoder_table_end) { @@ -369,8 +340,7 @@ namespace etl template ETL_NODISCARD static - ETL_CONSTEXPR14 - T padding() + ETL_CONSTEXPR14 T padding() { return static_cast('='); } @@ -378,51 +348,50 @@ namespace etl //************************************************************************* /// Decode one block of data. //************************************************************************* - ETL_CONSTEXPR14 - bool decode_block() + ETL_CONSTEXPR14 bool decode_block() { switch (input_buffer_length) { // Only triggered on call to flush(). case 2: - { - uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 6); - sextets = sextets | (get_index_from_sextet(input_buffer[1])); - push_to_output_buffer((sextets >> 4) & 0xFF); - break; - } + { + uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 6); + sextets = sextets | (get_index_from_sextet(input_buffer[1])); + push_to_output_buffer((sextets >> 4) & 0xFF); + break; + } // Only triggered on call to flush(). case 3: - { - uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 12); - sextets = sextets | (get_index_from_sextet(input_buffer[1]) << 6); - sextets = sextets | (get_index_from_sextet(input_buffer[2])); - push_to_output_buffer((sextets >> 10) & 0xFF); - push_to_output_buffer((sextets >> 2) & 0xFF); - break; - } + { + uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 12); + sextets = sextets | (get_index_from_sextet(input_buffer[1]) << 6); + sextets = sextets | (get_index_from_sextet(input_buffer[2])); + push_to_output_buffer((sextets >> 10) & 0xFF); + push_to_output_buffer((sextets >> 2) & 0xFF); + break; + } // Only triggered on call to decode(). case 4: - { - // Read in four sextets - uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 18); - sextets = sextets | (get_index_from_sextet(input_buffer[1]) << 12); - sextets = sextets | (get_index_from_sextet(input_buffer[2]) << 6); - sextets = sextets | (get_index_from_sextet(input_buffer[3])); + { + // Read in four sextets + uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 18); + sextets = sextets | (get_index_from_sextet(input_buffer[1]) << 12); + sextets = sextets | (get_index_from_sextet(input_buffer[2]) << 6); + sextets = sextets | (get_index_from_sextet(input_buffer[3])); - // Write out three octets - push_to_output_buffer((sextets >> 16) & 0xFF); - push_to_output_buffer((sextets >> 8) & 0xFF); - push_to_output_buffer((sextets >> 0) & 0xFF); - break; - } + // Write out three octets + push_to_output_buffer((sextets >> 16) & 0xFF); + push_to_output_buffer((sextets >> 8) & 0xFF); + push_to_output_buffer((sextets >> 0) & 0xFF); + break; + } default: - { - break; - } + { + break; + } } ETL_ASSERT(!invalid_data_detected, ETL_ERROR(etl::base64_invalid_data)); @@ -434,8 +403,7 @@ namespace etl //************************************************************************* // Push to the output buffer. //************************************************************************* - ETL_CONSTEXPR14 - void push_to_output_buffer(unsigned char c) + ETL_CONSTEXPR14 void push_to_output_buffer(unsigned char c) { if (output_buffer_length < output_buffer_max_size) { @@ -448,28 +416,25 @@ namespace etl } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - bool output_buffer_is_full() const + ETL_CONSTEXPR14 bool output_buffer_is_full() const { return output_buffer_length == output_buffer_max_size; } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - bool output_buffer_is_empty() const + ETL_CONSTEXPR14 bool output_buffer_is_empty() const { return output_buffer_length == 0; } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - void reset_output_buffer() + ETL_CONSTEXPR14 void reset_output_buffer() { output_buffer_length = 0; } @@ -478,8 +443,7 @@ namespace etl // Push to the input buffer. //************************************************************************* template - ETL_CONSTEXPR14 - void push_to_input_buffer(T value) + ETL_CONSTEXPR14 void push_to_input_buffer(T value) { if (value == padding()) { @@ -500,19 +464,17 @@ namespace etl } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - bool input_buffer_is_full() const + ETL_CONSTEXPR14 bool input_buffer_is_full() const { return input_buffer_length == 4U; } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - void reset_input_buffer() + ETL_CONSTEXPR14 void reset_input_buffer() { input_buffer_length = 0; } @@ -544,13 +506,8 @@ namespace etl //************************************************************************* /// Base64 RFC-2152 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc2152_decoder() - : ibase64_decoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc2152_decoder() + : ibase64_decoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -558,13 +515,8 @@ namespace etl //************************************************************************* /// Base64 RFC-2152 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc2152_decoder(callback_type callback_) - : ibase64_decoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc2152_decoder(callback_type callback_) + : ibase64_decoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -574,8 +526,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_decoder::decoded_size_from_valid_input_length(input_length); } @@ -599,13 +550,8 @@ namespace etl //************************************************************************* /// Base64 RFC-3501 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc3501_decoder() - : ibase64_decoder(etl::base64::character_set_3(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc3501_decoder() + : ibase64_decoder(etl::base64::character_set_3(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -613,13 +559,8 @@ namespace etl //************************************************************************* /// Base64 RFC-3501 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc3501_decoder(callback_type callback_) - : ibase64_decoder(etl::base64::character_set_3(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc3501_decoder(callback_type callback_) + : ibase64_decoder(etl::base64::character_set_3(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -629,8 +570,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_decoder::decoded_size_from_valid_input_length(input_length); } @@ -654,13 +594,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_decoder() - : ibase64_decoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_decoder() + : ibase64_decoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -668,13 +603,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_decoder(callback_type callback_) - : ibase64_decoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_decoder(callback_type callback_) + : ibase64_decoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -683,9 +613,8 @@ namespace etl /// Calculate the required output encode buffer size. //************************************************************************* ETL_NODISCARD - static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + static + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_decoder::decoded_size_from_valid_input_length(input_length); } @@ -709,13 +638,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_padding_decoder() - : ibase64_decoder(etl::base64::character_set_1(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_padding_decoder() + : ibase64_decoder(etl::base64::character_set_1(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -723,13 +647,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_padding_decoder(callback_type callback_) - : ibase64_decoder(etl::base64::character_set_1(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_padding_decoder(callback_type callback_) + : ibase64_decoder(etl::base64::character_set_1(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -739,8 +658,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_decoder::decoded_size_from_valid_input_length(input_length); } @@ -764,13 +682,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_decoder() - : ibase64_decoder(etl::base64::character_set_2(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_url_decoder() + : ibase64_decoder(etl::base64::character_set_2(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -778,13 +691,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_decoder(callback_type callback_) - : ibase64_decoder(etl::base64::character_set_2(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_url_decoder(callback_type callback_) + : ibase64_decoder(etl::base64::character_set_2(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -794,8 +702,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_decoder::decoded_size_from_valid_input_length(input_length); } @@ -819,13 +726,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_padding_decoder() - : ibase64_decoder(etl::base64::character_set_2(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_url_padding_decoder() + : ibase64_decoder(etl::base64::character_set_2(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -833,13 +735,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_padding_decoder(callback_type callback_) - : ibase64_decoder(etl::base64::character_set_2(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_url_padding_decoder(callback_type callback_) + : ibase64_decoder(etl::base64::character_set_2(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -849,8 +746,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_decoder::decoded_size_from_valid_input_length(input_length); } @@ -860,7 +756,7 @@ namespace etl /// The internal output buffer. unsigned char output_buffer[Buffer_Size]; }; -} +} // namespace etl #undef ETL_IS_TYPE_8_BIT_INTEGRAL #undef ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL diff --git a/include/etl/base64_encoder.h b/include/etl/base64_encoder.h index ac9646ec..75ccd83a 100644 --- a/include/etl/base64_encoder.h +++ b/include/etl/base64_encoder.h @@ -1,5 +1,5 @@ //************************************************************************* -///Decode from Base64 from and to pointer/length +/// Decode from Base64 from and to pointer/length //*************************************************************************///\file /****************************************************************************** @@ -29,16 +29,16 @@ SOFTWARE. #define ETL_BASE64_ENCODER_INCLUDED #include "platform.h" -#include "static_assert.h" -#include "error_handler.h" -#include "type_traits.h" -#include "binary.h" #include "algorithm.h" +#include "binary.h" +#include "delegate.h" +#include "enum_type.h" +#include "error_handler.h" #include "integral_limits.h" #include "iterator.h" -#include "enum_type.h" -#include "delegate.h" #include "span.h" +#include "static_assert.h" +#include "type_traits.h" #include "base64.h" @@ -48,11 +48,12 @@ SOFTWARE. #include #endif -#define ETL_IS_8_BIT_INTEGRAL(Type) (etl::is_integral::type>::value && \ - (etl::integral_limits::type>::bits == 8U)) +#define ETL_IS_8_BIT_INTEGRAL(Type) \ + (etl::is_integral::type>::value && (etl::integral_limits::type>::bits == 8U)) -#define ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(Type) (etl::is_integral::type>::value_type>::value && \ - (etl::integral_limits::type>::value_type>::bits == 8U)) +#define ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(Type) \ + (etl::is_integral< typename etl::iterator_traits< typename etl::remove_cv::type>::value_type>::value \ + && (etl::integral_limits< typename etl::iterator_traits< typename etl::remove_cv::type>::value_type>::bits == 8U)) namespace etl { @@ -70,8 +71,7 @@ namespace etl /// Encode to Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool encode(T value) + ETL_CONSTEXPR14 bool encode(T value) { ETL_STATIC_ASSERT(ETL_IS_8_BIT_INTEGRAL(T), "Input type must be an 8 bit integral"); @@ -99,8 +99,7 @@ namespace etl /// Encode to Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool encode(TInputIterator input_begin, size_t input_length) + ETL_CONSTEXPR14 bool encode(TInputIterator input_begin, size_t input_length) { ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral"); @@ -119,8 +118,7 @@ namespace etl /// Encode to Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool encode(TInputIterator input_begin, TInputIterator input_end) + ETL_CONSTEXPR14 bool encode(TInputIterator input_begin, TInputIterator input_end) { ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral"); @@ -139,8 +137,7 @@ namespace etl /// Encode to Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool encode_final(TInputIterator input_begin, size_t input_length) + ETL_CONSTEXPR14 bool encode_final(TInputIterator input_begin, size_t input_length) { return encode(input_begin, input_length) && flush(); } @@ -149,8 +146,7 @@ namespace etl /// Encode to Base64 //************************************************************************* template - ETL_CONSTEXPR14 - bool encode_final(TInputIterator input_begin, TInputIterator input_end) + ETL_CONSTEXPR14 bool encode_final(TInputIterator input_begin, TInputIterator input_end) { return encode(input_begin, input_end) && flush(); } @@ -158,8 +154,7 @@ namespace etl //************************************************************************* /// Flush any remaining data to the output. //************************************************************************* - ETL_CONSTEXPR14 - bool flush() + ETL_CONSTEXPR14 bool flush() { // Encode any remaining input data. bool success = encode_block(); @@ -189,8 +184,7 @@ namespace etl //************************************************************************* /// Reset the encoder. //************************************************************************* - ETL_CONSTEXPR14 - void restart() + ETL_CONSTEXPR14 void restart() { reset_input_buffer(); reset_output_buffer(); @@ -199,19 +193,16 @@ namespace etl //************************************************************************* /// Returns the beginning of the output buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const char* begin() const + ETL_NODISCARD ETL_CONSTEXPR14 const char* begin() const { return p_output_buffer; } //************************************************************************* - /// This only returns a useful value if a callback has not been set or called. + /// This only returns a useful value if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const char* end() const + ETL_NODISCARD ETL_CONSTEXPR14 const char* end() const { return p_output_buffer + output_buffer_length; } @@ -219,30 +210,26 @@ namespace etl //************************************************************************* /// Returns the beginning of the output buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const char* cbegin() const + ETL_NODISCARD ETL_CONSTEXPR14 const char* cbegin() const { return p_output_buffer; } //************************************************************************* - /// This only returns a useful value if a callback has not been set or called. + /// This only returns a useful value if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - const char* cend() const + ETL_NODISCARD ETL_CONSTEXPR14 const char* cend() const { return p_output_buffer + output_buffer_length; } //************************************************************************* /// Returns the size of the output buffer. - /// This only returns a useful value if a callback has not been set or called. + /// This only returns a useful value if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - size_t size() const + ETL_NODISCARD ETL_CONSTEXPR14 size_t size() const { return output_buffer_length; } @@ -250,20 +237,17 @@ namespace etl //************************************************************************* /// Returns the maximum size of the output buffer. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - size_t max_size() const + ETL_NODISCARD ETL_CONSTEXPR14 size_t max_size() const { return output_buffer_max_size; } //************************************************************************* /// Get a span of the output data. - /// This only returns a useful span if a callback has not been set or called. + /// This only returns a useful span if a callback has not been set or + /// called. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - span_type span() const + ETL_NODISCARD ETL_CONSTEXPR14 span_type span() const { return span_type(begin(), end()); } @@ -271,9 +255,7 @@ namespace etl //************************************************************************* /// Returns true if the output buffer has overflowed //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - bool overflow() const + ETL_NODISCARD ETL_CONSTEXPR14 bool overflow() const { return overflowed; } @@ -281,9 +263,7 @@ namespace etl //************************************************************************* /// Returns true if an error was detected. //************************************************************************* - ETL_NODISCARD - ETL_CONSTEXPR14 - bool error() const + ETL_NODISCARD ETL_CONSTEXPR14 bool error() const { return overflow(); } @@ -293,12 +273,8 @@ namespace etl //************************************************************************* /// Constructor //************************************************************************* - ETL_CONSTEXPR14 - ibase64_encoder(const char* encoder_table_, - bool use_padding_, - char* p_output_buffer_, - size_t ouput_buffer_max_size_, - callback_type callback_) + ETL_CONSTEXPR14 ibase64_encoder(const char* encoder_table_, bool use_padding_, char* p_output_buffer_, size_t ouput_buffer_max_size_, + callback_type callback_) : base64(encoder_table_, use_padding_) , input_buffer() , input_buffer_length(0) @@ -313,64 +289,65 @@ namespace etl //************************************************************************* /// Encode one block of data. //************************************************************************* - ETL_CONSTEXPR14 - bool encode_block() + ETL_CONSTEXPR14 bool encode_block() { switch (input_buffer_length) { // Only triggered on call to flush(). case 1: - { - uint32_t octets = input_buffer[0]; - octets = octets << 4; // Adjust one octet (8 bits) for two sextets worth of data (12 bits) - - // Write out two sextets + optional padding. - push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]); - push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]); - - if (use_padding) { - push_to_output_buffer('='); - push_to_output_buffer('='); - } - break; - } + uint32_t octets = input_buffer[0]; + octets = octets << 4; // Adjust one octet (8 bits) for two sextets + // worth of data (12 bits) + + // Write out two sextets + optional padding. + push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]); + push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]); + + if (use_padding) + { + push_to_output_buffer('='); + push_to_output_buffer('='); + } + break; + } // Only triggered on call to flush(). case 2: - { - uint32_t octets = (static_cast(input_buffer[0]) << 8) | input_buffer[1]; - octets <<= 2; // Adjust two octets (16 bits) for three sextets worth of data (18 bits) - - // Write out three sextets + optional padding. - push_to_output_buffer(encoder_table[(octets >> 12) & 0x3F]); - push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]); - push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]); - - if (use_padding) { - push_to_output_buffer('='); + uint32_t octets = (static_cast(input_buffer[0]) << 8) | input_buffer[1]; + octets <<= 2; // Adjust two octets (16 bits) for three sextets worth + // of data (18 bits) + + // Write out three sextets + optional padding. + push_to_output_buffer(encoder_table[(octets >> 12) & 0x3F]); + push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]); + push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]); + + if (use_padding) + { + push_to_output_buffer('='); + } + break; } - break; - } // Only triggered on call to encode(). case 3: - { - uint32_t octets = (static_cast(input_buffer[0]) << 16) | (static_cast(input_buffer[1]) << 8) | input_buffer[2]; + { + uint32_t octets = (static_cast(input_buffer[0]) << 16) | (static_cast(input_buffer[1]) << 8) | input_buffer[2]; - // Write out four sextets - push_to_output_buffer(encoder_table[(octets >> 18) & 0x3F]); - push_to_output_buffer(encoder_table[(octets >> 12) & 0x3F]); - push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]); - push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]); - break; - } + // Write out four sextets + push_to_output_buffer(encoder_table[(octets >> 18) & 0x3F]); + push_to_output_buffer(encoder_table[(octets >> 12) & 0x3F]); + push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]); + push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]); + break; + } default: - { - break; - } + { + break; + } } ETL_ASSERT(!overflowed, ETL_ERROR(etl::base64_overflow)); @@ -383,8 +360,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t encoded_size(size_t input_length, bool use_padding) + ETL_CONSTEXPR14 size_t encoded_size(size_t input_length, bool use_padding) { size_t required_output_length = 0; @@ -420,8 +396,7 @@ namespace etl //************************************************************************* // Push to the output buffer. //************************************************************************* - ETL_CONSTEXPR14 - void push_to_output_buffer(char c) + ETL_CONSTEXPR14 void push_to_output_buffer(char c) { if (output_buffer_length < output_buffer_max_size) { @@ -434,28 +409,25 @@ namespace etl } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - bool output_buffer_is_full() const + ETL_CONSTEXPR14 bool output_buffer_is_full() const { return output_buffer_length == output_buffer_max_size; } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - bool output_buffer_is_empty() const + ETL_CONSTEXPR14 bool output_buffer_is_empty() const { return output_buffer_length == 0; } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - void reset_output_buffer() + ETL_CONSTEXPR14 void reset_output_buffer() { output_buffer_length = 0; } @@ -464,26 +436,23 @@ namespace etl // Push to the input buffer. //************************************************************************* template - ETL_CONSTEXPR14 - void push_to_input_buffer(T value) + ETL_CONSTEXPR14 void push_to_input_buffer(T value) { input_buffer[input_buffer_length++] = static_cast(value); } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - bool input_buffer_is_full() const + ETL_CONSTEXPR14 bool input_buffer_is_full() const { return input_buffer_length == 3U; } //************************************************************************* - // + // //************************************************************************* - ETL_CONSTEXPR14 - void reset_input_buffer() + ETL_CONSTEXPR14 void reset_input_buffer() { input_buffer_length = 0; } @@ -496,7 +465,7 @@ namespace etl const size_t output_buffer_max_size; callback_type callback; - + bool overflowed; }; @@ -508,19 +477,15 @@ namespace etl { public: - ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); - ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), + "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); //************************************************************************* /// Base64 RFC-2152 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc2152_encoder() - : ibase64_encoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc2152_encoder() + : ibase64_encoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -528,13 +493,8 @@ namespace etl //************************************************************************* /// Base64 RFC-2152 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc2152_encoder(callback_type callback_) - : ibase64_encoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc2152_encoder(callback_type callback_) + : ibase64_encoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -544,8 +504,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding); } @@ -564,19 +523,15 @@ namespace etl { public: - ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); - ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), + "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); //************************************************************************* /// Base64 RFC-3501 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc3501_encoder() - : ibase64_encoder(etl::base64::character_set_3(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc3501_encoder() + : ibase64_encoder(etl::base64::character_set_3(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -584,13 +539,8 @@ namespace etl //************************************************************************* /// Base64 RFC-3501 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc3501_encoder(callback_type callback_) - : ibase64_encoder(etl::base64::character_set_3(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc3501_encoder(callback_type callback_) + : ibase64_encoder(etl::base64::character_set_3(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -600,8 +550,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding); } @@ -620,19 +569,15 @@ namespace etl { public: - ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); - ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), + "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); //************************************************************************* /// Base64 RFC-4648 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_encoder() - : ibase64_encoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_encoder() + : ibase64_encoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -640,13 +585,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648 constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_encoder(callback_type callback_) - : ibase64_encoder(etl::base64::character_set_1(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_encoder(callback_type callback_) + : ibase64_encoder(etl::base64::character_set_1(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -656,8 +596,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding); } @@ -676,19 +615,15 @@ namespace etl { public: - ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); - ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), + "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_padding_encoder() - : ibase64_encoder(etl::base64::character_set_1(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_padding_encoder() + : ibase64_encoder(etl::base64::character_set_1(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -696,13 +631,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-Padding constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_padding_encoder(callback_type callback_) - : ibase64_encoder(etl::base64::character_set_1(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_padding_encoder(callback_type callback_) + : ibase64_encoder(etl::base64::character_set_1(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -712,8 +642,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::Use_Padding); } @@ -732,19 +661,15 @@ namespace etl { public: - ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); - ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), + "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); //************************************************************************* /// Base64 RFC-4648-URL constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_encoder() - : ibase64_encoder(etl::base64::character_set_2(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_url_encoder() + : ibase64_encoder(etl::base64::character_set_2(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -752,13 +677,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-URL constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_encoder(callback_type callback_) - : ibase64_encoder(etl::base64::character_set_2(), - etl::base64::Padding::No_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_url_encoder(callback_type callback_) + : ibase64_encoder(etl::base64::character_set_2(), etl::base64::Padding::No_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -768,8 +688,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding); } @@ -788,19 +707,15 @@ namespace etl { public: - ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); - ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size"); + ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), + "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size"); //************************************************************************* /// Base64 RFC-4648-URL constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_padding_encoder() - : ibase64_encoder(etl::base64::character_set_2(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_type()) + ETL_CONSTEXPR14 base64_rfc4648_url_padding_encoder() + : ibase64_encoder(etl::base64::character_set_2(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_type()) , output_buffer() { } @@ -808,13 +723,8 @@ namespace etl //************************************************************************* /// Base64 RFC-4648-URL constructor. //************************************************************************* - ETL_CONSTEXPR14 - base64_rfc4648_url_padding_encoder(callback_type callback_) - : ibase64_encoder(etl::base64::character_set_2(), - etl::base64::Padding::Use_Padding, - output_buffer, - Buffer_Size, - callback_) + ETL_CONSTEXPR14 base64_rfc4648_url_padding_encoder(callback_type callback_) + : ibase64_encoder(etl::base64::character_set_2(), etl::base64::Padding::Use_Padding, output_buffer, Buffer_Size, callback_) , output_buffer() { } @@ -824,8 +734,7 @@ namespace etl //************************************************************************* ETL_NODISCARD static - ETL_CONSTEXPR14 - size_t safe_output_buffer_size(size_t input_length) + ETL_CONSTEXPR14 size_t safe_output_buffer_size(size_t input_length) { return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::Use_Padding); } @@ -835,7 +744,7 @@ namespace etl /// The internal output buffer. char output_buffer[Buffer_Size]; }; -} +} // namespace etl #undef ETL_IS_TYPE_8_BIT_INTEGRAL #undef ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h index c374e35f..f80d0ec6 100644 --- a/include/etl/basic_format_spec.h +++ b/include/etl/basic_format_spec.h @@ -34,8 +34,8 @@ SOFTWARE. ///\ingroup string #include "platform.h" -#include "type_traits.h" #include "static_assert.h" +#include "type_traits.h" #include "utility.h" namespace etl @@ -131,7 +131,7 @@ namespace etl struct right_spec { }; - } + } // namespace private_basic_format_spec //*************************************************************************** // Stream formatting manipulators. @@ -222,14 +222,8 @@ namespace etl //*************************************************************************** /// Constructor. //*************************************************************************** - ETL_CONSTEXPR basic_format_spec(uint_least8_t base__, - uint_least8_t width__, - uint_least8_t precision__, - bool upper_case__, - bool left_justified__, - bool boolalpha__, - bool show_base__, - typename TString::value_type fill__) ETL_NOEXCEPT + ETL_CONSTEXPR basic_format_spec(uint_least8_t base__, uint_least8_t width__, uint_least8_t precision__, bool upper_case__, bool left_justified__, + bool boolalpha__, bool show_base__, typename TString::value_type fill__) ETL_NOEXCEPT : base_(base__) , width_(width__) , precision_(precision__) @@ -400,7 +394,7 @@ namespace etl //*************************************************************************** /// Gets the width. //*************************************************************************** - ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT + ETL_CONSTEXPR uint32_t get_width() const ETL_NOEXCEPT { return width_; } @@ -570,36 +564,32 @@ namespace etl //*************************************************************************** /// Equality operator. //*************************************************************************** - ETL_CONSTEXPR friend bool operator ==(const basic_format_spec& lhs, const basic_format_spec& rhs) + ETL_CONSTEXPR friend bool operator==(const basic_format_spec& lhs, const basic_format_spec& rhs) { - return (lhs.base_ == rhs.base_) && - (lhs.width_ == rhs.width_) && - (lhs.precision_ == rhs.precision_) && - (lhs.upper_case_ == rhs.upper_case_) && - (lhs.left_justified_ == rhs.left_justified_) && - (lhs.boolalpha_ == rhs.boolalpha_) && - (lhs.show_base_ == rhs.show_base_) && - (lhs.fill_ == rhs.fill_); + return (lhs.base_ == rhs.base_) && (lhs.width_ == rhs.width_) && (lhs.precision_ == rhs.precision_) && (lhs.upper_case_ == rhs.upper_case_) + && (lhs.left_justified_ == rhs.left_justified_) && (lhs.boolalpha_ == rhs.boolalpha_) && (lhs.show_base_ == rhs.show_base_) + && (lhs.fill_ == rhs.fill_); } //*************************************************************************** /// Inequality operator. //*************************************************************************** - ETL_CONSTEXPR friend bool operator !=(const basic_format_spec& lhs, const basic_format_spec& rhs) + ETL_CONSTEXPR friend bool operator!=(const basic_format_spec& lhs, const basic_format_spec& rhs) { return !(lhs == rhs); } private: - uint_least8_t base_; - uint_least8_t width_; - uint_least8_t precision_; - bool upper_case_; - bool left_justified_; - bool boolalpha_; - bool show_base_; + + uint_least8_t base_; + uint_least8_t width_; + uint_least8_t precision_; + bool upper_case_; + bool left_justified_; + bool boolalpha_; + bool show_base_; typename TString::value_type fill_; }; -} +} // namespace etl #endif diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 90c655c3..1a38cc99 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -33,18 +33,18 @@ SOFTWARE. #include "platform.h" #include "algorithm.h" -#include "iterator.h" -#include "functional.h" #include "alignment.h" #include "array.h" -#include "type_traits.h" -#include "error_handler.h" -#include "integral_limits.h" -#include "exception.h" -#include "memory.h" #include "binary.h" +#include "error_handler.h" +#include "exception.h" #include "flags.h" +#include "functional.h" +#include "integral_limits.h" +#include "iterator.h" +#include "memory.h" #include "string_utilities.h" +#include "type_traits.h" #include #include @@ -71,7 +71,7 @@ SOFTWARE. //***************************************************************************** // Forward declaration of string_view -namespace etl +namespace etl { template class basic_string_view; @@ -165,7 +165,7 @@ namespace etl static ETL_CONSTANT uint_least8_t IS_TRUNCATED = etl::bit<0>::value; static ETL_CONSTANT uint_least8_t CLEAR_AFTER_USE = etl::bit<1>::value; - + static ETL_CONSTANT size_type npos = etl::integral_limits::max; }; @@ -177,7 +177,7 @@ namespace etl template ETL_CONSTANT typename string_base_statics::size_type string_base_statics::npos; - } + } // namespace private_basic_string //*************************************************************************** class string_base : public private_basic_string::string_base_statics<> @@ -329,12 +329,10 @@ namespace etl //************************************************************************* /// Destructor. //************************************************************************* - ~string_base() - { - } + ~string_base() {} - size_type current_size; ///< The current number of elements in the string. - const size_type CAPACITY; ///< The maximum number of elements in the string. + size_type current_size; ///< The current number of elements in the string. + const size_type CAPACITY; ///< The maximum number of elements in the string. #if ETL_HAS_STRING_TRUNCATION_CHECKS || ETL_HAS_STRING_CLEAR_AFTER_USE etl::flags flags; @@ -343,7 +341,8 @@ namespace etl //*************************************************************************** /// The base class for specifically sized strings. - /// Can be used as a reference type for all strings containing a specific type. + /// Can be used as a reference type for all strings containing a specific + /// type. ///\ingroup string //*************************************************************************** template @@ -353,13 +352,13 @@ namespace etl typedef ibasic_string interface_type; - typedef T value_type; - typedef T& reference; - typedef const T& const_reference; - typedef T* pointer; - typedef const T* const_pointer; - typedef T* iterator; - typedef const T* const_iterator; + typedef T value_type; + typedef T& reference; + typedef const T& const_reference; + typedef T* pointer; + typedef const T* const_pointer; + typedef T* iterator; + typedef const T* const_iterator; typedef ETL_OR_STD::reverse_iterator reverse_iterator; typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; @@ -486,7 +485,8 @@ namespace etl //********************************************************************* /// Resizes the string. ///\param new_size The new size. - ///\param value The value to fill new elements with. Default = default constructed value. + ///\param value The value to fill new elements with. Default = default + /// constructed value. //********************************************************************* void resize(size_type new_size, T value) { @@ -495,9 +495,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif } @@ -509,7 +509,7 @@ namespace etl etl::fill(p_buffer + current_size, p_buffer + new_size, value); } - current_size = new_size; + current_size = new_size; p_buffer[new_size] = 0; cleanup(); } @@ -525,7 +525,7 @@ namespace etl ETL_ASSERT_FAIL(ETL_ERROR(string_out_of_bounds)); } - current_size = operation(p_buffer, new_size); + current_size = operation(p_buffer, new_size); p_buffer[current_size] = '\0'; cleanup(); } @@ -539,7 +539,7 @@ namespace etl { new_size = etl::min(new_size, CAPACITY); - current_size = new_size; + current_size = new_size; p_buffer[new_size] = 0; } @@ -558,7 +558,7 @@ namespace etl ///\param i The index. ///\return A reference to the value at index 'i' //********************************************************************* - reference operator [](size_type i) + reference operator[](size_type i) { ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(string_out_of_bounds)); return p_buffer[i]; @@ -569,7 +569,7 @@ namespace etl ///\param i The index. ///\return A const reference to the value at index 'i' //********************************************************************* - const_reference operator [](size_type i) const + const_reference operator[](size_type i) const { ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(string_out_of_bounds)); return p_buffer[i]; @@ -577,7 +577,8 @@ namespace etl //********************************************************************* /// Returns a reference to the value at index 'i' - /// If asserts or exceptions are enabled, emits an etl::string_out_of_bounds if the index is out of range. + /// If asserts or exceptions are enabled, emits an etl::string_out_of_bounds + /// if the index is out of range. ///\param i The index. ///\return A reference to the value at index 'i' //********************************************************************* @@ -589,7 +590,8 @@ namespace etl //********************************************************************* /// Returns a const reference to the value at index 'i' - /// If asserts or exceptions are enabled, emits an etl::string_out_of_bounds if the index is out of range. + /// If asserts or exceptions are enabled, emits an etl::string_out_of_bounds + /// if the index is out of range. ///\param i The index. ///\return A const reference to the value at index 'i' //********************************************************************* @@ -715,8 +717,9 @@ namespace etl //********************************************************************* /// Assigns values to the string. - /// If asserts or exceptions are enabled, emits string_iterator if the iterators are reversed. - /// Truncates if the string does not have enough free space. + /// If asserts or exceptions are enabled, emits string_iterator if the + /// iterators are reversed. Truncates if the string does not have enough + /// free space. ///\param first The iterator to the first element. ///\param last The iterator to the last element + 1. //********************************************************************* @@ -747,7 +750,7 @@ namespace etl append_impl(begin(), str, str + n, false, false); } - //********************************************************************* + //********************************************************************* /// Assigns values to the string from a view. //********************************************************************* template @@ -769,15 +772,15 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(n > CAPACITY); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT(is_truncated() == false, ETL_ERROR(string_truncation)); -#endif + #endif #endif n = etl::min(n, CAPACITY); etl::fill_n(begin(), n, c); - current_size = n; + current_size = n; p_buffer[current_size] = 0; } @@ -807,9 +810,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif } } @@ -917,9 +920,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(n > free_space); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT(is_truncated() == false, ETL_ERROR(string_truncation)); -#endif + #endif #endif n = etl::min(n, size_t(free_space)); @@ -966,16 +969,16 @@ namespace etl if (position != end()) { // Insert in the middle. - etl::mem_move(insert_position, end() - 1, insert_position + 1); + etl::mem_move(insert_position, end() - 1, insert_position + 1); *insert_position = value; } #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif } @@ -1002,8 +1005,8 @@ namespace etl } // Quick hack, as iterators are pointers. - iterator insert_position = to_iterator(position); - const size_type start = static_cast(etl::distance(cbegin(), position)); + iterator insert_position = to_iterator(position); + const size_type start = static_cast(etl::distance(cbegin(), position)); // No effect. if (start >= CAPACITY) @@ -1011,9 +1014,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif return to_iterator(position); } @@ -1026,9 +1029,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif } @@ -1038,11 +1041,11 @@ namespace etl else { // Lets do some shifting. - const size_type shift_amount = n; - const size_type to_position = start + shift_amount; + const size_type shift_amount = n; + const size_type to_position = start + shift_amount; const size_type remaining_characters = current_size - start; const size_type max_shift_characters = CAPACITY - start - shift_amount; - const size_type characters_to_shift = etl::min(max_shift_characters, remaining_characters); + const size_type characters_to_shift = etl::min(max_shift_characters, remaining_characters); // Will the string truncate? if ((start + shift_amount + remaining_characters) > CAPACITY) @@ -1052,9 +1055,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif } else @@ -1073,7 +1076,8 @@ namespace etl //********************************************************************* /// Inserts a range of values to the string. - /// If asserts or exceptions are enabled, emits string_full if the string does not have enough free space. + /// If asserts or exceptions are enabled, emits string_full if the string + /// does not have enough free space. ///\param position The position to insert before. ///\param first The first element to add. ///\param last The last + 1 element to add. @@ -1083,7 +1087,7 @@ namespace etl { ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT_CHECK_EXTRA(first <= last, ETL_ERROR(string_iterator)); - + iterator position_ = to_iterator(position); if (first == last) @@ -1092,7 +1096,7 @@ namespace etl } const size_type start = static_cast(etl::distance(begin(), position_)); - const size_type n = static_cast(etl::distance(first, last)); + const size_type n = static_cast(etl::distance(first, last)); // No effect. if (start >= CAPACITY) @@ -1100,9 +1104,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif return position_; } @@ -1115,9 +1119,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif } @@ -1128,11 +1132,11 @@ namespace etl else { // Lets do some shifting. - const size_type shift_amount = n; - const size_type to_position = start + shift_amount; + const size_type shift_amount = n; + const size_type to_position = start + shift_amount; const size_type remaining_characters = current_size - start; const size_type max_shift_characters = CAPACITY - start - shift_amount; - const size_type characters_to_shift = etl::min(max_shift_characters, remaining_characters); + const size_type characters_to_shift = etl::min(max_shift_characters, remaining_characters); // Will the string truncate? if ((start + shift_amount + remaining_characters) > CAPACITY) @@ -1142,9 +1146,9 @@ namespace etl #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif #endif } else @@ -1153,7 +1157,8 @@ namespace etl } etl::mem_move(position_, position_ + characters_to_shift, begin() + to_position); - //etl::copy_backward(position_, position_ + characters_to_shift, begin() + to_position + characters_to_shift); + // etl::copy_backward(position_, position_ + characters_to_shift, + // begin() + to_position + characters_to_shift); position_ = copy_characters(first, static_cast(etl::distance(first, last)), position_); } @@ -1165,7 +1170,8 @@ namespace etl //********************************************************************* /// Inserts a view to the string. - /// If asserts or exceptions are enabled, emits string_full if the string does not have enough free space. + /// If asserts or exceptions are enabled, emits string_full if the string + /// does not have enough free space. ///\param position The position to insert before. ///\param view The view element to add. //********************************************************************* @@ -1191,9 +1197,9 @@ namespace etl { set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif } #endif @@ -1216,7 +1222,8 @@ namespace etl } //********************************************************************* - /// Inserts a string at the specified position from subposition for sublength. + /// Inserts a string at the specified position from subposition for + /// sublength. ///\param position The position to insert before. ///\param str The string to insert. ///\param subposition The subposition to start from. @@ -1224,7 +1231,7 @@ namespace etl //********************************************************************* etl::ibasic_string& insert(size_type position, const etl::ibasic_string& str, size_type subposition, size_type sublength) { - ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); + ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); if ((sublength == npos) || (subposition + sublength > str.size())) @@ -1239,9 +1246,9 @@ namespace etl { set_truncated(true); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT_FAIL(ETL_ERROR(string_truncation)); -#endif + #endif } #endif @@ -1258,7 +1265,7 @@ namespace etl template etl::ibasic_string& insert(size_type position, const etl::basic_string_view& view, size_type subposition, size_type sublength) { - ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); + ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT(subposition <= view.size(), ETL_ERROR(string_out_of_bounds)); if ((sublength == npos) || (subposition + sublength > view.size())) @@ -1285,7 +1292,8 @@ namespace etl } //********************************************************************* - /// Inserts a string at the specified position from pointer for n characters. + /// Inserts a string at the specified position from pointer for n + /// characters. ///\param position The position to insert before. ///\param s The string to insert. ///\param n The number of characters to insert. @@ -1333,7 +1341,8 @@ namespace etl //********************************************************************* /// Erases an element. ///\param i_element Iterator to the element. - ///\return An iterator pointing to the element that followed the erased element. + ///\return An iterator pointing to the element that followed the erased + /// element. //********************************************************************* iterator erase(iterator i_element) { @@ -1348,12 +1357,13 @@ namespace etl //********************************************************************* /// Erases an element. ///\param i_element Iterator to the element. - ///\return An iterator pointing to the element that followed the erased element. + ///\return An iterator pointing to the element that followed the erased + /// element. //********************************************************************* iterator erase(const_iterator i_element) { ETL_ASSERT_CHECK_EXTRA(cbegin() <= i_element && i_element < cend(), ETL_ERROR(string_out_of_bounds)); - + iterator i_element_(to_iterator(i_element)); etl::mem_move(i_element + 1, end(), i_element_); @@ -1364,11 +1374,12 @@ namespace etl //********************************************************************* /// Erases a range of elements. - /// The range includes all the elements between first and last, including the - /// element pointed by first, but not the one pointed by last. + /// The range includes all the elements between first and last, including + /// the element pointed by first, but not the one pointed by last. ///\param first Iterator to the first element. ///\param last Iterator to the last element. - ///\return An iterator pointing to the element that followed the erased element. + ///\return An iterator pointing to the element that followed the erased + /// element. //********************************************************************* iterator erase(const_iterator first, const_iterator last) { @@ -1572,7 +1583,7 @@ namespace etl //********************************************************************* /// Checks that the string is within this string //********************************************************************* - bool contains(const etl::ibasic_string& str) const + bool contains(const etl::ibasic_string& str) const { return find(str) != npos; } @@ -1581,7 +1592,7 @@ namespace etl /// Checks that the view is within this string //********************************************************************* template - bool contains(const etl::basic_string_view& view) const + bool contains(const etl::basic_string_view& view) const { return find(view) != npos; } @@ -1589,7 +1600,7 @@ namespace etl //********************************************************************* /// Checks that text is within this string //********************************************************************* - bool contains(const_pointer s) const + bool contains(const_pointer s) const { return find(s) != npos; } @@ -1597,7 +1608,7 @@ namespace etl //********************************************************************* /// Checks that character is within this string //********************************************************************* - bool contains(value_type c) const + bool contains(value_type c) const { return find(c) != npos; } @@ -1605,7 +1616,7 @@ namespace etl //********************************************************************* /// Checks that the string is the start of this string //********************************************************************* - bool starts_with(const etl::ibasic_string& str) const + bool starts_with(const etl::ibasic_string& str) const { return compare(0, str.size(), str) == 0; } @@ -1614,7 +1625,7 @@ namespace etl /// Checks that the view is the start of this string //********************************************************************* template - bool starts_with(const etl::basic_string_view& view) const + bool starts_with(const etl::basic_string_view& view) const { return compare(0, view.size(), view) == 0; } @@ -1622,7 +1633,7 @@ namespace etl //********************************************************************* /// Checks that the string is the start of this string //********************************************************************* - bool starts_with(const_pointer s) const + bool starts_with(const_pointer s) const { size_t len = etl::strlen(s); @@ -1632,7 +1643,7 @@ namespace etl //********************************************************************* /// Checks that the character is the start of this string //********************************************************************* - bool starts_with(value_type c) const + bool starts_with(value_type c) const { return !empty() && (front() == c); } @@ -1640,9 +1651,9 @@ namespace etl //********************************************************************* /// Checks that the string is the end of this string //********************************************************************* - bool ends_with(const etl::ibasic_string& str) const + bool ends_with(const etl::ibasic_string& str) const { - if (str.size() > size()) + if (str.size() > size()) { return false; } @@ -1654,9 +1665,9 @@ namespace etl /// Checks that the view is the end of this string //********************************************************************* template - bool ends_with(const etl::basic_string_view& view) const + bool ends_with(const etl::basic_string_view& view) const { - if (view.size() > size()) + if (view.size() > size()) { return false; } @@ -1667,11 +1678,11 @@ namespace etl //********************************************************************* /// Checks that the string is the end of this string //********************************************************************* - bool ends_with(const_pointer s) const + bool ends_with(const_pointer s) const { size_t len = etl::strlen(s); - if (len > size()) + if (len > size()) { return false; } @@ -1682,7 +1693,7 @@ namespace etl //********************************************************************* /// Checks that the character is the end of this string //********************************************************************* - bool ends_with(value_type c) const + bool ends_with(value_type c) const { return !empty() && (back() == c); } @@ -1744,7 +1755,8 @@ namespace etl } //********************************************************************* - /// Replace characters from 'position' of 'length' with 'str' from 'subposition' of 'sublength'. + /// Replace characters from 'position' of 'length' with 'str' from + /// 'subposition' of 'sublength'. //********************************************************************* ibasic_string& replace(size_type position, size_type length_, const ibasic_string& str, size_type subposition, size_type sublength) { @@ -1754,15 +1766,17 @@ namespace etl // Limit the lengths. length_ = etl::min(length_, size() - position); sublength = etl::min(sublength, str.size() - subposition); - + return replace_impl(begin() + position, begin() + position + length_, str.begin() + subposition, sublength, str.is_truncated()); } //********************************************************************* - /// Replace characters from 'position' of 'length' with 'view' from 'subposition' of 'sublength'. + /// Replace characters from 'position' of 'length' with 'view' from + /// 'subposition' of 'sublength'. //********************************************************************* template - ibasic_string& replace(size_type position, size_type length_, const etl::basic_string_view& view, size_type subposition, size_type sublength) + ibasic_string& replace(size_type position, size_type length_, const etl::basic_string_view& view, size_type subposition, + size_type sublength) { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT(subposition <= view.size(), ETL_ERROR(string_out_of_bounds)); @@ -1805,8 +1819,8 @@ namespace etl /// Replace characters from 'first' to 'last' with pointed to string. //********************************************************************* template - typename etl::enable_if::value, ibasic_string>::type& - replace(const_iterator first, const_iterator last, TIterator s) + typename etl::enable_if::value, ibasic_string>::type& replace(const_iterator first, const_iterator last, + TIterator s) { return replace_impl(first, last, s, etl::strlen(s), false); } @@ -1821,7 +1835,8 @@ namespace etl } //********************************************************************* - /// Replace characters from 'position' of 'length' with 'n' characters from pointed to string. + /// Replace characters from 'position' of 'length' with 'n' characters from + /// pointed to string. //********************************************************************* ibasic_string& replace(size_type position, size_type length_, const_pointer s, size_type n) { @@ -1871,7 +1886,8 @@ namespace etl } //********************************************************************* - /// Replace characters from 'first' of 'last' with characters from 'first_replace' to 'last_replace'. + /// Replace characters from 'first' of 'last' with characters from + /// 'first_replace' to 'last_replace'. //********************************************************************* template ibasic_string& replace(const_iterator first, const_iterator last, TIterator first_replace, TIterator last_replace) @@ -1894,10 +1910,7 @@ namespace etl //************************************************************************* int compare(const ibasic_string& str) const { - return compare(p_buffer, - p_buffer + size(), - str.p_buffer, - str.p_buffer + str.size()); + return compare(p_buffer, p_buffer + size(), str.p_buffer, str.p_buffer + str.size()); } //************************************************************************* @@ -1906,10 +1919,7 @@ namespace etl template int compare(const etl::basic_string_view& view) const { - return compare(p_buffer, - p_buffer + size(), - view.data(), - view.data() + view.size()); + return compare(p_buffer, p_buffer + size(), view.data(), view.data() + view.size()); } //************************************************************************* @@ -1922,10 +1932,7 @@ namespace etl // Limit the length. length_ = etl::min(length_, size() - position); - return compare(p_buffer + position, - p_buffer + position + length_, - str.p_buffer, - str.p_buffer + str.size()); + return compare(p_buffer + position, p_buffer + position + length_, str.p_buffer, str.p_buffer + str.size()); } //************************************************************************* @@ -1934,10 +1941,7 @@ namespace etl template int compare(size_type position, size_type length_, const etl::basic_string_view& view) const { - return compare(p_buffer + position, - p_buffer + position + length_, - view.data(), - view.data() + view.size()); + return compare(p_buffer + position, p_buffer + position + length_, view.data(), view.data() + view.size()); } //************************************************************************* @@ -1949,20 +1953,19 @@ namespace etl ETL_ASSERT(subposition <= str.size(), ETL_ERROR(string_out_of_bounds)); // Limit the lengths. - length_ = etl::min(length_, size() - position); + length_ = etl::min(length_, size() - position); sublength = etl::min(sublength, str.size() - subposition); - return compare(p_buffer + position, - p_buffer + position + length_, - str.p_buffer + subposition, - str.p_buffer + subposition + sublength); + return compare(p_buffer + position, p_buffer + position + length_, str.p_buffer + subposition, str.p_buffer + subposition + sublength); } //************************************************************************* - /// Compare position / length with etl::basic_string_view. / subposition / sublength. + /// Compare position / length with etl::basic_string_view. / subposition / + /// sublength. //************************************************************************* template - int compare(size_type position, size_type length_, const etl::basic_string_view& view, size_type subposition, size_type sublength) const + int compare(size_type position, size_type length_, const etl::basic_string_view& view, size_type subposition, + size_type sublength) const { ETL_ASSERT(position <= size(), ETL_ERROR(string_out_of_bounds)); ETL_ASSERT(subposition <= view.size(), ETL_ERROR(string_out_of_bounds)); @@ -1971,10 +1974,7 @@ namespace etl length_ = etl::min(length_, size() - position); sublength = etl::min(sublength, view.size() - subposition); - return compare(p_buffer + position, - p_buffer + position + length_, - view.data() + subposition, - view.data() + subposition + sublength); + return compare(p_buffer + position, p_buffer + position + length_, view.data() + subposition, view.data() + subposition + sublength); } //************************************************************************* @@ -1982,10 +1982,7 @@ namespace etl //************************************************************************* int compare(const value_type* s) const { - return compare(p_buffer, - p_buffer + size(), - s, - s + etl::strlen(s)); + return compare(p_buffer, p_buffer + size(), s, s + etl::strlen(s)); } //************************************************************************* @@ -1993,10 +1990,7 @@ namespace etl //************************************************************************* int compare(size_type position, size_type length_, const_pointer s) const { - return compare(p_buffer + position, - p_buffer + position + length_, - s, - s + etl::strlen(s)); + return compare(p_buffer + position, p_buffer + position + length_, s, s + etl::strlen(s)); } //************************************************************************* @@ -2004,10 +1998,7 @@ namespace etl //************************************************************************* int compare(size_type position, size_type length_, const_pointer s, size_type n) const { - return compare(p_buffer + position, - p_buffer + position + length_, - s, - s + n); + return compare(p_buffer + position, p_buffer + position + length_, s, s + n); } //********************************************************************* @@ -2369,7 +2360,7 @@ namespace etl //************************************************************************* /// Assignment operator. //************************************************************************* - ibasic_string& operator = (const ibasic_string& rhs) + ibasic_string& operator=(const ibasic_string& rhs) { if (&rhs != this) { @@ -2382,7 +2373,7 @@ namespace etl //************************************************************************* /// Assignment operator. //************************************************************************* - ibasic_string& operator = (const_pointer rhs) + ibasic_string& operator=(const_pointer rhs) { assign(rhs); @@ -2393,7 +2384,7 @@ namespace etl /// Assignment operator. //************************************************************************* template - ibasic_string& operator = (const etl::basic_string_view& view) + ibasic_string& operator=(const etl::basic_string_view& view) { assign(view); @@ -2403,7 +2394,7 @@ namespace etl //************************************************************************* /// += operator. //************************************************************************* - ibasic_string& operator += (const ibasic_string& rhs) + ibasic_string& operator+=(const ibasic_string& rhs) { append(rhs); @@ -2414,7 +2405,7 @@ namespace etl /// += operator. //************************************************************************* template - ibasic_string& operator += (const etl::basic_string_view& rhs) + ibasic_string& operator+=(const etl::basic_string_view& rhs) { append(rhs); @@ -2424,7 +2415,7 @@ namespace etl //************************************************************************* /// += operator. //************************************************************************* - ibasic_string& operator += (const_pointer rhs) + ibasic_string& operator+=(const_pointer rhs) { append(rhs); @@ -2434,7 +2425,7 @@ namespace etl //************************************************************************* /// += operator. //************************************************************************* - ibasic_string& operator += (T rhs) + ibasic_string& operator+=(T rhs) { append(size_type(1), rhs); @@ -2471,7 +2462,7 @@ namespace etl #endif p_buffer[CAPACITY] = T(0); // Ensure a terminating null. - current_size = etl::strlen(p_buffer); + current_size = etl::strlen(p_buffer); } protected: @@ -2480,8 +2471,8 @@ namespace etl /// Constructor. //********************************************************************* ibasic_string(T* p_buffer_, size_type MAX_SIZE_) - : string_base(MAX_SIZE_), - p_buffer(p_buffer_) + : string_base(MAX_SIZE_) + , p_buffer(p_buffer_) { } @@ -2491,7 +2482,7 @@ namespace etl void initialise() { current_size = 0U; - p_buffer[0] = 0; + p_buffer[0] = 0; #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated(false); #endif @@ -2532,9 +2523,7 @@ namespace etl // If source pointer is inside our current buffer we take the safe // (legacy) path to preserve correct semantics for overlapping self use. - const bool source_overlaps = (s != ETL_NULLPTR) && - (s >= p_buffer) && - (s < p_buffer + current_size); + const bool source_overlaps = (s != ETL_NULLPTR) && (s >= p_buffer) && (s < p_buffer + current_size); if (source_overlaps) { @@ -2553,9 +2542,10 @@ namespace etl // Calculate the remove parameters. const size_type remove_index = size_type(first_ - p_buffer); - const size_type remove_length = size_type(last_ - first_); - const size_type free_space = CAPACITY - remove_index; // Free space is the space from the remove index to the end of the buffer. - + const size_type remove_length = size_type(last_ - first_); + const size_type free_space = CAPACITY - remove_index; // Free space is the space from the remove + // index to the end of the buffer. + size_type insert_length = (s == ETL_NULLPTR) ? 0U : length; // Limit the insert length to the available free space. @@ -2568,12 +2558,13 @@ namespace etl size_type tail_index = remove_index + remove_length; size_type tail_length = current_size - tail_index; size_type tail_space = free_space - insert_length; - + #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated((insert_length != length) || (tail_space < tail_length) || is_truncated() || other_truncated); #endif - // The some or all of tail may be erased if the space remaining for it is smaller than the tail length. + // The some or all of tail may be erased if the space remaining for it is + // smaller than the tail length. if (tail_space < tail_length) { tail_length = tail_space; @@ -2604,7 +2595,7 @@ namespace etl etl::mem_move(&p_buffer[tail_index], tail_length, &p_buffer[remove_index + insert_length]); } - current_size = remove_index + insert_length + tail_length; + current_size = remove_index + insert_length + tail_length; p_buffer[current_size] = value_type(0); cleanup(); @@ -2615,8 +2606,7 @@ namespace etl //************************************************************************* /// Compare helper function //************************************************************************* - static int compare(const_pointer first1, const_pointer last1, - const_pointer first2, const_pointer last2) + static int compare(const_pointer first1, const_pointer last1, const_pointer first2, const_pointer last2) { typedef typename etl::make_unsigned::type type; @@ -2649,7 +2639,7 @@ namespace etl // First string is shorter. return -1; } - + if (length1 > length2) { // First string is longer. @@ -2687,12 +2677,15 @@ namespace etl /// Destructor. //************************************************************************* #if defined(ETL_POLYMORPHIC_STRINGS) || defined(ETL_POLYMORPHIC_CONTAINERS) || defined(ETL_ISTRING_REPAIR_ENABLE) + public: + virtual #else + protected: #endif - ~ibasic_string() + ~ibasic_string() { #if ETL_HAS_STRING_CLEAR_AFTER_USE if (is_secure()) @@ -2728,7 +2721,9 @@ namespace etl //********************************************************************* template static - typename etl::enable_if::type>::value && sizeof(typename etl::remove_pointer::type>::type) == sizeof(value_type), iterator>::type + typename etl::enable_if< etl::is_pointer::type>::value + && sizeof(typename etl::remove_pointer< typename etl::remove_cvref::type>::type) == sizeof(value_type), + iterator>::type copy_characters(TIterator1 from, size_t n, iterator to) { etl::mem_move(from, n, to); @@ -2742,12 +2737,14 @@ namespace etl //********************************************************************* template static - typename etl::enable_if::type>::value && sizeof(typename etl::remove_pointer::type>::type) == sizeof(value_type)), iterator>::type + typename etl::enable_if< !(etl::is_pointer::type>::value + && sizeof(typename etl::remove_pointer< typename etl::remove_cvref::type>::type) == sizeof(value_type)), + iterator>::type copy_characters(TIterator1 from, size_t n, iterator to) { size_t count = 0; - while (count != n) + while (count != n) { *to++ = static_cast(*from++); ++count; @@ -2757,26 +2754,27 @@ namespace etl } //********************************************************************* - /// Common implementation for 'assign' and 'append' for non-pointer iterators. + /// Common implementation for 'assign' and 'append' for non-pointer + /// iterators. //********************************************************************* template - typename etl::enable_if::type>::value>::type - append_impl(iterator position, TIterator first, TIterator last, bool truncated, bool secure) + typename etl::enable_if< !etl::is_pointer< typename etl::remove_reference::type>::value>::type + append_impl(iterator position, TIterator first, TIterator last, bool truncated, bool secure) { difference_type start = etl::distance(p_buffer, position); difference_type count = etl::distance(first, last); difference_type free_space = etl::distance(position, p_buffer + CAPACITY); -#if ETL_IS_DEBUG_BUILD +#if ETL_IS_DEBUG_BUILD ETL_ASSERT(count >= 0, ETL_ERROR(string_iterator)); #endif - + #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated((count > free_space) || this->is_truncated() || truncated); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT(is_truncated() == false, ETL_ERROR(string_truncation)); -#endif + #endif #else (void)truncated; #endif @@ -2793,7 +2791,7 @@ namespace etl // Limit the actual distance to the capacity. count = etl::min(count, free_space); copy_characters(first, size_t(count), position); - current_size = size_t(start + count); + current_size = size_t(start + count); p_buffer[current_size] = 0; cleanup(); @@ -2828,14 +2826,14 @@ namespace etl etl::mem_move(src, count, position); - current_size = start + count; + current_size = start + count; p_buffer[current_size] = 0; #if ETL_HAS_STRING_TRUNCATION_CHECKS set_truncated((length > free_space) || truncated); -#if ETL_HAS_ERROR_ON_STRING_TRUNCATION + #if ETL_HAS_ERROR_ON_STRING_TRUNCATION ETL_ASSERT(is_truncated() == false, ETL_ERROR(string_truncation)); -#endif + #endif #else (void)truncated; #endif @@ -2856,8 +2854,8 @@ namespace etl /// Common implementation for 'assign' and 'append' for pointer iterators. //********************************************************************* template - typename etl::enable_if::type>::value>::type - append_impl(iterator position, TIterator first, TIterator last, bool truncated, bool secure) + typename etl::enable_if< etl::is_pointer< typename etl::remove_reference::type>::value>::type + append_impl(iterator position, TIterator first, TIterator last, bool truncated, bool secure) { append_impl(position, first, size_t(etl::distance(first, last)), truncated || is_truncated(), secure); } @@ -2919,9 +2917,7 @@ namespace etl /// get_string_length, optimised for sizeof(U) == sizeof(char). //********************************************************************* template - static - typename etl::enable_if::type - get_string_length(const U* str) + static typename etl::enable_if::type get_string_length(const U* str) { return ::strlen(reinterpret_cast(str)); } @@ -2931,9 +2927,7 @@ namespace etl /// get_string_length, optimised for sizeof(U) == sizeof(wchar_t). //********************************************************************* template - static - typename etl::enable_if::type - get_string_length(const U* str) + static typename etl::enable_if::type get_string_length(const U* str) { return ::wcslen(reinterpret_cast(str)); } @@ -2945,9 +2939,9 @@ namespace etl template static #if ETL_USING_LIBC_WCHAR_H - typename etl::enable_if<(sizeof(U) != sizeof(char)) && (sizeof(U) != sizeof(wchar_t)), size_t>::type + typename etl::enable_if<(sizeof(U) != sizeof(char)) && (sizeof(U) != sizeof(wchar_t)), size_t>::type #else - typename etl::enable_if<(sizeof(U) != sizeof(char)), size_t>::type + typename etl::enable_if<(sizeof(U) != sizeof(char)), size_t>::type #endif get_string_length(const U* str) { @@ -2975,7 +2969,7 @@ namespace etl ///\ingroup string //*************************************************************************** template - bool operator ==(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) + bool operator==(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) { return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin()); } @@ -2988,7 +2982,7 @@ namespace etl ///\ingroup string //*************************************************************************** template - bool operator ==(const etl::ibasic_string& lhs, const T* rhs) + bool operator==(const etl::ibasic_string& lhs, const T* rhs) { return (lhs.size() == etl::strlen(rhs)) && etl::equal(lhs.begin(), lhs.end(), rhs); } @@ -3001,7 +2995,7 @@ namespace etl ///\ingroup string //*************************************************************************** template - bool operator ==(const T* lhs, const etl::ibasic_string& rhs) + bool operator==(const T* lhs, const etl::ibasic_string& rhs) { return (rhs.size() == etl::strlen(lhs)) && etl::equal(rhs.begin(), rhs.end(), lhs); } @@ -3014,7 +3008,7 @@ namespace etl ///\ingroup string //*************************************************************************** template - bool operator !=(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) + bool operator!=(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) { return !(lhs == rhs); } @@ -3027,7 +3021,7 @@ namespace etl ///\ingroup string //*************************************************************************** template - bool operator !=(const etl::ibasic_string& lhs, const T* rhs) + bool operator!=(const etl::ibasic_string& lhs, const T* rhs) { return !(lhs == rhs); } @@ -3040,7 +3034,7 @@ namespace etl ///\ingroup string //*************************************************************************** template - bool operator !=(const T* lhs, const etl::ibasic_string& rhs) + bool operator!=(const T* lhs, const etl::ibasic_string& rhs) { return !(lhs == rhs); } @@ -3049,11 +3043,11 @@ namespace etl /// Less than operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically less than the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically less than the + /// second, otherwise false \ingroup string //*************************************************************************** template - bool operator <(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) + bool operator<(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) { return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } @@ -3062,11 +3056,11 @@ namespace etl /// Less than operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically less than the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically less than the + /// second, otherwise false \ingroup string //*************************************************************************** template - bool operator <(const etl::ibasic_string& lhs, const T* rhs) + bool operator<(const etl::ibasic_string& lhs, const T* rhs) { return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs, rhs + etl::strlen(rhs)); } @@ -3075,25 +3069,24 @@ namespace etl /// Less than operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically less than the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically less than the + /// second, otherwise false \ingroup string //*************************************************************************** template - bool operator <(const T* lhs, const etl::ibasic_string& rhs) + bool operator<(const T* lhs, const etl::ibasic_string& rhs) { return etl::lexicographical_compare(lhs, lhs + etl::strlen(lhs), rhs.begin(), rhs.end()); } - //*************************************************************************** /// Greater than operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically greater than the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically greater than + /// the second, otherwise false \ingroup string //*************************************************************************** template - bool operator >(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) + bool operator>(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) { return (rhs < lhs); } @@ -3102,11 +3095,11 @@ namespace etl /// Greater than operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically greater than the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically greater than + /// the second, otherwise false \ingroup string //*************************************************************************** template - bool operator >(const etl::ibasic_string& lhs, const T* rhs) + bool operator>(const etl::ibasic_string& lhs, const T* rhs) { return (rhs < lhs); } @@ -3115,25 +3108,25 @@ namespace etl /// Greater than operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically greater than the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically greater than + /// the second, otherwise false \ingroup string //*************************************************************************** template - bool operator >(const T* lhs, const etl::ibasic_string& rhs) + bool operator>(const T* lhs, const etl::ibasic_string& rhs) { return (rhs < lhs); } - //*************************************************************************** /// Less than or equal operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically less than or equal to the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically less than or + /// equal to the second, otherwise + ///< b>false \ingroup string //*************************************************************************** template - bool operator <=(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) + bool operator<=(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) { return !(lhs > rhs); } @@ -3142,11 +3135,12 @@ namespace etl /// Less than or equal operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically less than or equal to the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically less than or + /// equal to the second, otherwise + ///< b>false \ingroup string //*************************************************************************** template - bool operator <=(const etl::ibasic_string& lhs, const T* rhs) + bool operator<=(const etl::ibasic_string& lhs, const T* rhs) { return !(lhs > rhs); } @@ -3155,25 +3149,26 @@ namespace etl /// Less than or equal operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically less than or equal to the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically less than or + /// equal to the second, otherwise + ///< b>false \ingroup string //*************************************************************************** template - bool operator <=(const T* lhs, const etl::ibasic_string& rhs) + bool operator<=(const T* lhs, const etl::ibasic_string& rhs) { return !(lhs > rhs); } - //*************************************************************************** /// Greater than or equal operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically greater than or equal to the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically greater than + /// or equal to the second, otherwise + ///< b>false \ingroup string //*************************************************************************** template - bool operator >=(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) + bool operator>=(const etl::ibasic_string& lhs, const etl::ibasic_string& rhs) { return !(lhs < rhs); } @@ -3182,11 +3177,12 @@ namespace etl /// Greater than or equal operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically greater than or equal to the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically greater than + /// or equal to the second, otherwise + ///< b>false \ingroup string //*************************************************************************** template - bool operator >=(const etl::ibasic_string& lhs, const T* rhs) + bool operator>=(const etl::ibasic_string& lhs, const T* rhs) { return !(lhs < rhs); } @@ -3195,11 +3191,12 @@ namespace etl /// Greater than or equal operator. ///\param lhs Reference to the first string. ///\param rhs Reference to the second string. - ///\return true if the first string is lexicographically greater than or equal to the second, otherwise false - ///\ingroup string + ///\return true if the first string is lexicographically greater than + /// or equal to the second, otherwise + ///< b>false \ingroup string //*************************************************************************** template - bool operator >=(const T* lhs, const etl::ibasic_string& rhs) + bool operator>=(const T* lhs, const etl::ibasic_string& rhs) { return !(lhs < rhs); } @@ -3213,14 +3210,13 @@ namespace etl //*************************************************************************** #if ETL_USING_STD_OSTREAM template - std::basic_ostream > &operator<<(std::basic_ostream > &os, - const etl::ibasic_string& str) + std::basic_ostream >& operator<<(std::basic_ostream >& os, const etl::ibasic_string& str) { os.write(str.data(), static_cast(str.size())); return os; } #endif -} +} // namespace etl #undef ETL_USING_WCHAR_T_H diff --git a/include/etl/basic_string_stream.h b/include/etl/basic_string_stream.h index 2aa1f28d..b8e1038b 100644 --- a/include/etl/basic_string_stream.h +++ b/include/etl/basic_string_stream.h @@ -35,9 +35,9 @@ SOFTWARE. #include "platform.h" #include "to_string.h" -#include "to_u8string.h" #include "to_u16string.h" #include "to_u32string.h" +#include "to_u8string.h" #include "to_wstring.h" namespace etl @@ -125,16 +125,17 @@ namespace etl //********************************* /// TFormat //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, const TFormat& fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, const TFormat& fmt) { ss.format = fmt; return ss; } //********************************* - /// etl::base_spec from etl::setbase, etl::bin, etl::oct, etl::dec & etl::hex stream manipulators + /// etl::base_spec from etl::setbase, etl::bin, etl::oct, etl::dec & + /// etl::hex stream manipulators //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::base_spec fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::base_spec fmt) { ss.format.base(fmt.base); return ss; @@ -143,7 +144,7 @@ namespace etl //********************************* /// etl::width_spec from etl::setw stream manipulator //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::width_spec fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::width_spec fmt) { ss.format.width(fmt.width); return ss; @@ -153,7 +154,7 @@ namespace etl /// etl::fill_spec from etl::setfill stream manipulator //********************************* template - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::fill_spec fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::fill_spec fmt) { ss.format.fill(fmt.fill); return ss; @@ -162,34 +163,37 @@ namespace etl //********************************* /// etl::precision_spec from etl::setprecision stream manipulator //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::precision_spec fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::precision_spec fmt) { ss.format.precision(fmt.precision); return ss; } //********************************* - /// etl::boolalpha_spec from etl::boolalpha & etl::noboolalpha stream manipulators + /// etl::boolalpha_spec from etl::boolalpha & etl::noboolalpha stream + /// manipulators //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::boolalpha_spec fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::boolalpha_spec fmt) { ss.format.boolalpha(fmt.boolalpha); return ss; } //********************************* - /// etl::uppercase_spec from etl::uppercase & etl::nouppercase stream manipulators + /// etl::uppercase_spec from etl::uppercase & etl::nouppercase stream + /// manipulators //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::uppercase_spec fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::uppercase_spec fmt) { ss.format.upper_case(fmt.upper_case); return ss; } //********************************* - /// etl::showbase_spec from etl::showbase & etl::noshowbase stream manipulators + /// etl::showbase_spec from etl::showbase & etl::noshowbase stream + /// manipulators //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::showbase_spec fmt) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::showbase_spec fmt) { ss.format.show_base(fmt.show_base); return ss; @@ -198,7 +202,7 @@ namespace etl //********************************* /// etl::left_spec from etl::left stream manipulator //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::left_spec /*fmt*/) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::left_spec /*fmt*/) { ss.format.left(); return ss; @@ -207,7 +211,7 @@ namespace etl //********************************* /// etl::right_spec from etl::left stream manipulator //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::right_spec /*fmt*/) + friend basic_string_stream& operator<<(basic_string_stream& ss, etl::private_basic_format_spec::right_spec /*fmt*/) { ss.format.right(); return ss; @@ -216,7 +220,7 @@ namespace etl //********************************* /// From a string view //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, TStringView view) + friend basic_string_stream& operator<<(basic_string_stream& ss, TStringView view) { etl::to_string(view, ss.text, ss.format, true); return ss; @@ -225,7 +229,7 @@ namespace etl //********************************* /// From a character pointer to a string //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, pointer p) + friend basic_string_stream& operator<<(basic_string_stream& ss, pointer p) { TStringView view(p); ss << view; @@ -235,7 +239,7 @@ namespace etl //********************************* /// From a const character pointer to a string //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, const_pointer p) + friend basic_string_stream& operator<<(basic_string_stream& ss, const_pointer p) { TStringView view(p); ss << view; @@ -245,7 +249,7 @@ namespace etl //********************************* /// From a string interface //********************************* - friend basic_string_stream& operator <<(basic_string_stream& ss, const TIString& t) + friend basic_string_stream& operator<<(basic_string_stream& ss, const TIString& t) { etl::to_string(t, ss.text, ss.format, true); return ss; @@ -255,7 +259,7 @@ namespace etl /// From a string //********************************* template