Skip to content

Commit

Permalink
Som devel 20 (#38)
Browse files Browse the repository at this point in the history
* workflow

* constness / static swap

* removed StaticArray add

* removed StaticArray add

* re-defined StaticArray add

* pre commit

* tests -> static assertion; more constness
  • Loading branch information
sorgom authored Jan 29, 2024
1 parent f0116ae commit aeb4fe2
Show file tree
Hide file tree
Showing 31 changed files with 361 additions and 387 deletions.
14 changes: 7 additions & 7 deletions CLOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C++ 15 100 7 919
C/C++ Header 23 239 189 810
C++ 16 102 18 916
C/C++ Header 24 238 191 814
-------------------------------------------------------------------------------
SUM: 38 339 196 1729
SUM: 40 340 209 1730
-------------------------------------------------------------------------------
```
**testenv**
Expand All @@ -26,9 +26,9 @@ SUM: 34 267 193 1242
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C++ 15 212 216 1830
C++ 14 198 206 1797
-------------------------------------------------------------------------------
SUM: 15 212 216 1830
SUM: 14 198 206 1797
-------------------------------------------------------------------------------
```
**systemtests**
Expand All @@ -46,8 +46,8 @@ SUM: 2 33 25 107
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C/C++ Header 17 108 111 339
C/C++ Header 17 108 111 354
-------------------------------------------------------------------------------
SUM: 17 108 111 339
SUM: 17 108 111 354
-------------------------------------------------------------------------------
```
7 changes: 3 additions & 4 deletions application/components/BAS/I_Array.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ============================================================
// defintion of interface I_Array to apply:
// defintion of interface I_Array:
// enables:
// - bubble sort
// - b-tree search
// - uniqueness check / duplicates count
Expand Down Expand Up @@ -27,10 +28,8 @@ class I_Array

// object access by position
virtual const T& at(size_t pos) const = 0;
virtual const T& at(const PosRes& res) const = 0;

// object pointer access by position
virtual const T* ptr(size_t pos) const = 0;

// definition object a is greater than object b
inline virtual bool isGreater(const T& a, const T& b) const
{
Expand Down
19 changes: 11 additions & 8 deletions application/components/BAS/NtpArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
// name, type, position
struct Ntp
{
ElementName name;
INT32 type;
size_t pos;
Ntp() = default;
Ntp(
const ElementName name;
const INT32 type;
const size_t pos;
inline Ntp(
const ElementName& name,
INT32 type = 0,
size_t pos = 0
);
):
name(name),
type(type),
pos(pos)
{}
NOCOPY(Ntp)
NODEF(Ntp)
};

// ============================================================
Expand All @@ -39,9 +43,8 @@ class NtpArray :

inline auto add(const ElementName& name, INT32 type, size_t pos)
{
return StaticArray<Ntp, CAP>::newC(name, type, pos);
return StaticArray<Ntp, CAP>::add(name, type, pos);
}

NOCOPY(NtpArray)
};

Expand Down
12 changes: 8 additions & 4 deletions application/components/BAS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ basic integral types

**I_Array.h**
```
defintion of interface I_Array to apply:
defintion of interface I_Array:
enables:
- bubble sort
- b-tree search
- uniqueness check / duplicates count
Expand Down Expand Up @@ -45,9 +46,12 @@ enables to store references as objects
StaticIndex
provides search for unsorted StaticArray.
Derived classes have to provide
the isGreater method for objects of their type
See interface I_Array
```

**SwapBytes.h**
```
class SwapBytes enables static byte swapping
(no memory reallocation)
```

**coding.h**
Expand Down
58 changes: 21 additions & 37 deletions application/components/BAS/StaticArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <BAS/I_Array.h>
#include <BAS/Mem.h>
#include <BAS/SwapBytes.h>
#include <algorithm>
#include <new>

Expand All @@ -26,7 +27,9 @@ template <class ... Ts>
constexpr auto max_sizeof = std::max({sizeof(Ts)...});

template <class C, size_t CAP, class ... SCS>
class StaticArray : public I_Array<C, CAP>
class StaticArray :
public I_Array<C, CAP>,
private SwapBytes
{
public:
inline StaticArray():
Expand All @@ -38,7 +41,7 @@ class StaticArray : public I_Array<C, CAP>
mSize = 0;
}

inline size_t size() const
inline size_t size() const final
{
return mSize;
}
Expand All @@ -48,53 +51,44 @@ class StaticArray : public I_Array<C, CAP>
return mSize < CAP;
}

inline size_t spaceLeft() const
{
return mSize >= CAP ? 0 : CAP - mSize;
}

inline bool has(size_t pos) const
{
return pos < mSize;
}

template <class T, typename ... ARGS>
size_t newT(ARGS ... args)
size_t addT(const ARGS& ... args)
{
static_assert(sizeof(T) <= DIM);
new (mData[mSize]) T(args...);
return mSize++;
}

template <typename ... ARGS>
size_t newC(ARGS ... args)
size_t add(const ARGS& ... args)
{
new (mData[mSize]) C(args...);
return mSize++;
}

template <class T>
size_t addT(const T& obj)
{
static_assert(sizeof(T) <= DIM);
Mem::cpy(mData[mSize], obj);
return mSize++;
}

size_t add(const C& obj)
{
Mem::cpy(mData[mSize], obj);
return mSize++;
}

inline C& at(size_t pos)
{
return *reinterpret_cast<C*>(mData[pos]);
}

inline const C& at(size_t pos) const
inline const C& at(size_t pos) const final
{
return *reinterpret_cast<const C*>(mData[pos]);
}

inline const C* ptr(size_t pos) const
inline const C& at(const PosRes& res) const final
{
return reinterpret_cast<const C*>(mData[pos]);
return at(res.pos);
}

inline const C* data() const
Expand All @@ -105,15 +99,16 @@ class StaticArray : public I_Array<C, CAP>
NOCOPY(StaticArray)

protected:
inline void swap(size_t posA, size_t posB)
inline void swap(size_t posA, size_t posB) final
{
std::swap(mData[posA], mData[posB]);
swapBytes(mData[posA], mData[posB], mSwap, DIM);
}

private:
constexpr static size_t DIM = max_sizeof<C, SCS...>;
using Segment = BYTE[DIM];
Segment mData[CAP];
Segment mSwap;
size_t mSize;
};

Expand All @@ -128,9 +123,6 @@ class CRef
inline CRef(const T& obj):
mPtr(&obj)
{}
inline CRef(const T* ptr):
mPtr(ptr)
{}
inline const T& ref() const
{
return *mPtr;
Expand All @@ -145,9 +137,6 @@ class CRef
// ============================================================
// StaticIndex
// provides search for unsorted StaticArray.
// Derived classes have to provide
// the isGreater method for objects of their type
// See interface I_Array
// ============================================================

template <class T, size_t CAP>
Expand All @@ -170,7 +159,7 @@ class StaticIndex : private StaticArray<CRef<T>, CAP>
BaseT::reset();
for (size_t p = 0; p < mSrc.size(); ++p)
{
BaseT::newC(mSrc.ptr(p));
BaseT::add(mSrc.at(p));
}
BaseT::sort();
return BaseT::dupCnt() == 0;
Expand All @@ -181,19 +170,14 @@ class StaticIndex : private StaticArray<CRef<T>, CAP>
return isGreater(a.ref(), b.ref());
}

inline auto find(const T& obj) const
inline PosRes find(const T& obj) const
{
return BaseT::find(CRef<T>(obj));
}

inline const T& get(size_t pos) const
{
return BaseT::at(pos).ref();
}

inline const T& get(const PosRes& res) const
{
return get(res.pos);
return BaseT::at(res).ref();
}

NOCOPY(StaticIndex)
Expand Down
20 changes: 20 additions & 0 deletions application/components/BAS/SwapBytes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ============================================================
// class SwapBytes enables static byte swapping
// (no memory reallocation)
// ============================================================
// created by Manfred Sorgo
#pragma once
#ifndef SWAPBYTES_H
#define SWAPBYTES_H

#include <BAS/coding.h>

class SwapBytes
{
protected:
SwapBytes() = default;
static void swapBytes(PTR pA, PTR pB, PTR pS, size_t size);
NOCOPY(SwapBytes)
};

#endif // H_
22 changes: 11 additions & 11 deletions application/components/BAS/src/NtpArray.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <BAS/NtpArray.h>
// #include <BAS/NtpArray.h>

Ntp::Ntp(
const ElementName& name,
const INT32 type,
const size_t pos
):
type(type),
pos(pos)
{
Mem::cpy(this->name, name);
}
// Ntp::Ntp(
// const ElementName& name,
// const INT32 type,
// const size_t pos
// ):
// type(type),
// pos(pos)
// {
// Mem::cpy(this->name, name);
// }
10 changes: 10 additions & 0 deletions application/components/BAS/src/SwapBytes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <BAS/SwapBytes.h>

#include <cstring>

void SwapBytes::swapBytes(const PTR pA, const PTR pB, const PTR pS, const size_t size)
{
std::memcpy(pS, pB, size);
std::memcpy(pB, pA, size);
std::memcpy(pA, pS, size);
}
16 changes: 8 additions & 8 deletions application/components/LCR/src/LCR_Provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ void LCR_Provider::load(const ProjLCR* const data, const UINT32 num)
for (UINT32 n = 0; ok and (n < num); ++n)
{
const ProjLCR& proj = data[n];
const size_t id = disp.assign(proj.name, SUBSYS_LCR, n);
if (id == Dispatcher::invalidPos)
{
ok = false;
}
else
const PosRes res = disp.assign(proj.name, SUBSYS_LCR, n);
if (res.valid)
{
switch (proj.type)
{
case LCR_TYPE_LCR:
mLCRs.newT<LCR>(id);
mLCRs.addT<LCR>(res.pos);
break;
case LCR_TYPE_LCR_UBK:
mLCRs.newT<LCR_UBK>(id);
mLCRs.addT<LCR_UBK>(res.pos);
break;
default:
ok = false;
break;
}
}
else
{
ok = false;
}
}
}
if (ok)
Expand Down
Loading

0 comments on commit aeb4fe2

Please sign in to comment.