KiCad PCB EDA Suite
hash_eda.h File Reference
#include <cstdlib>
#include <functional>

Go to the source code of this file.

Enumerations

enum  HASH_FLAGS {
  HASH_POS = 0x01, REL_COORD = 0x02, HASH_ROT = 0x04, HASH_LAYER = 0x08,
  HASH_NET = 0x10, HASH_REF = 0x20, HASH_VALUE = 0x40, HASH_ALL = 0xff
}
 Enables/disables properties that will be used for calculating the hash. More...
 

Functions

std::size_t hash_fp_item (const EDA_ITEM *aItem, int aFlags=HASH_FLAGS::HASH_ALL)
 Calculate hash of an EDA_ITEM. More...
 
static void hash_combine (std::size_t &seed)
 This is a dummy function to take the final case of hash_combine below. More...
 
template<typename T , typename ... Types>
static void hash_combine (std::size_t &seed, const T &val, const Types &... args)
 Combine multiple hashes utilizing previous hash result. More...
 
template<typename... Types>
static std::size_t hash_val (const Types &... args)
 

Enumeration Type Documentation

◆ HASH_FLAGS

enum HASH_FLAGS

Enables/disables properties that will be used for calculating the hash.

The properties might be combined using the bitwise 'or' operator.

Enumerator
HASH_POS 

use coordinates relative to the parent object

REL_COORD 
HASH_ROT 
HASH_LAYER 
HASH_NET 
HASH_REF 
HASH_VALUE 
HASH_ALL 

Definition at line 41 of file hash_eda.h.

42 {
43  HASH_POS = 0x01,
44 
46  REL_COORD = 0x02,
47  HASH_ROT = 0x04,
48  HASH_LAYER = 0x08,
49  HASH_NET = 0x10,
50  HASH_REF = 0x20,
51  HASH_VALUE = 0x40,
52  HASH_ALL = 0xff
53 };
use coordinates relative to the parent object
Definition: hash_eda.h:43

Function Documentation

◆ hash_combine() [1/2]

static void hash_combine ( std::size_t &  seed)
inlinestatic

This is a dummy function to take the final case of hash_combine below.

Parameters
seed

Definition at line 67 of file hash_eda.h.

67 {}

Referenced by KIID::Hash(), hash_combine(), hash_fp_item(), hash_val(), and SCH_SHEET_PATH::Rehash().

◆ hash_combine() [2/2]

template<typename T , typename ... Types>
static void hash_combine ( std::size_t &  seed,
const T &  val,
const Types &...  args 
)
inlinestatic

Combine multiple hashes utilizing previous hash result.

Template Parameters
TA hashable type
Parameters
seedA seed value input and output for the result.
valA hashable object of type T

Definition at line 77 of file hash_eda.h.

78 {
79  seed ^= std::hash<T>()( val ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
80  hash_combine( seed, args... );
81 }
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash_eda.h:67

References hash_combine().

◆ hash_fp_item()

std::size_t hash_fp_item ( const EDA_ITEM aItem,
int  aFlags = HASH_FLAGS::HASH_ALL 
)

Calculate hash of an EDA_ITEM.

Parameters
aItemis the item for which the hash will be computed.
Returns
Hash value.

Definition at line 49 of file hash_eda.cpp.

50 {
51  size_t ret = 0;
52 
53  switch( aItem->Type() )
54  {
55  case PCB_FOOTPRINT_T:
56  {
57  const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
58 
59  ret = hash_board_item( footprint, aFlags );
60 
61  if( aFlags & HASH_POS )
62  hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
63 
64  if( aFlags & HASH_ROT )
65  hash_combine( ret, footprint->GetOrientation() );
66 
67  for( BOARD_ITEM* item : footprint->GraphicalItems() )
68  hash_combine( ret, hash_fp_item( item, aFlags ) );
69 
70  for( PAD* pad : footprint->Pads() )
71  hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
72  }
73  break;
74 
75  case PCB_PAD_T:
76  {
77  const PAD* pad = static_cast<const PAD*>( aItem );
78 
79  ret = hash<int>{}( pad->GetShape() << 16 );
80  hash_combine( ret, pad->GetDrillShape() << 18 );
81  hash_combine( ret, pad->GetSize().x << 8 );
82  hash_combine( ret, pad->GetSize().y << 9 );
83  hash_combine( ret, pad->GetOffset().x << 6 );
84  hash_combine( ret, pad->GetOffset().y << 7 );
85  hash_combine( ret, pad->GetDelta().x << 4 );
86  hash_combine( ret, pad->GetDelta().y << 5 );
87 
88  hash_combine( ret, hash_board_item( pad, aFlags ) );
89 
90  if( aFlags & HASH_POS )
91  {
92  if( aFlags & REL_COORD )
93  hash_combine( ret, pad->GetPos0().x, pad->GetPos0().y );
94  else
95  hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
96  }
97 
98  if( aFlags & HASH_ROT )
99  hash_combine( ret, pad->GetOrientation() );
100 
101  if( aFlags & HASH_NET )
102  hash_combine( ret, pad->GetNetCode() );
103  }
104  break;
105 
106  case PCB_FP_TEXT_T:
107  {
108  const FP_TEXT* text = static_cast<const FP_TEXT*>( aItem );
109 
110  if( !( aFlags & HASH_REF ) && text->GetType() == FP_TEXT::TEXT_is_REFERENCE )
111  break;
112 
113  if( !( aFlags & HASH_VALUE ) && text->GetType() == FP_TEXT::TEXT_is_VALUE )
114  break;
115 
116  ret = hash_board_item( text, aFlags );
117  hash_combine( ret, text->GetText().ToStdString() );
118  hash_combine( ret, text->IsItalic() );
119  hash_combine( ret, text->IsBold() );
120  hash_combine( ret, text->IsMirrored() );
121  hash_combine( ret, text->GetTextWidth() );
122  hash_combine( ret, text->GetTextHeight() );
123  hash_combine( ret, text->GetHorizJustify() );
124  hash_combine( ret, text->GetVertJustify() );
125 
126  if( aFlags & HASH_POS )
127  {
128  if( aFlags & REL_COORD )
129  hash_combine( ret, text->GetPos0().x, text->GetPos0().y );
130  else
131  hash_combine( ret, text->GetPosition().x, text->GetPosition().y );
132  }
133 
134  if( aFlags & HASH_ROT )
135  hash_combine( ret, text->GetTextAngle() );
136  }
137  break;
138 
139  case PCB_FP_SHAPE_T:
140  {
141  const FP_SHAPE* segment = static_cast<const FP_SHAPE*>( aItem );
142  ret = hash_board_item( segment, aFlags );
143  hash_combine( ret, segment->GetShape() );
144  hash_combine( ret, segment->GetWidth() );
145  hash_combine( ret, segment->IsFilled() );
146  hash_combine( ret, segment->GetRadius() );
147 
148  if( aFlags & HASH_POS )
149  {
150  if( aFlags & REL_COORD )
151  {
152  hash_combine( ret, segment->GetStart0().x );
153  hash_combine( ret, segment->GetStart0().y );
154  hash_combine( ret, segment->GetEnd0().x );
155  hash_combine( ret, segment->GetEnd0().y );
156  }
157  else
158  {
159  hash_combine( ret, segment->GetStart().x );
160  hash_combine( ret, segment->GetStart().y );
161  hash_combine( ret, segment->GetEnd().x );
162  hash_combine( ret, segment->GetEnd().y );
163  }
164  }
165 
166  if( aFlags & HASH_ROT )
167  hash_combine( ret, segment->GetAngle() );
168  }
169  break;
170 
171  default:
172  wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
173  }
174 
175  return ret;
176 }
bool IsBold() const
Definition: eda_text.h:190
bool IsFilled() const
Definition: pcb_shape.h:96
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:206
bool IsMirrored() const
Definition: eda_text.h:196
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:156
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
use coordinates relative to the parent object
Definition: hash_eda.h:43
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
Definition: pcb_shape.h:201
int GetWidth() const
Definition: pcb_shape.h:118
double GetOrientation() const
Definition: footprint.h:186
double GetTextAngle() const
Definition: eda_text.h:181
const wxPoint & GetStart0() const
Definition: fp_shape.h:112
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
class PAD, a pad in a footprint
Definition: typeinfo.h:89
PAD_SHAPE_T GetShape() const
Definition: pad.h:169
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash_eda.h:67
PADS & Pads()
Definition: footprint.h:164
int GetTextHeight() const
Definition: eda_text.h:251
bool IsItalic() const
Definition: eda_text.h:187
const wxPoint & GetOffset() const
Definition: pad.h:249
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:205
double GetOrientation() const
Return the rotation angle of the pad in a variety of units (the basic call returns tenths of degrees)...
Definition: pad.h:341
TEXT_TYPE GetType() const
Definition: fp_text.h:141
const wxSize & GetSize() const
Definition: pad.h:232
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:145
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition: hash_eda.cpp:49
DRAWINGS & GraphicalItems()
Definition: footprint.h:167
static size_t hash_board_item(const BOARD_ITEM *aItem, int aFlags)
Definition: hash_eda.cpp:38
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
const wxSize & GetDelta() const
Definition: pad.h:239
const wxPoint & GetPos0() const
Definition: fp_text.h:166
const wxPoint & GetPos0() const
Definition: pad.h:226
int GetTextWidth() const
Definition: eda_text.h:248
wxPoint GetPosition() const override
Definition: pad.h:177
PAD_DRILL_SHAPE_T GetDrillShape() const
Definition: pad.h:346
double GetAngle() const
Definition: pcb_shape.h:127
const wxPoint & GetEnd0() const
Definition: fp_shape.h:115
wxPoint GetPosition() const override
Definition: footprint.h:182
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:130
Definition: pad.h:60
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162
virtual wxPoint GetPosition() const override
Definition: fp_text.h:88

References PCB_SHAPE::GetAngle(), PAD::GetDelta(), PAD::GetDrillShape(), PCB_SHAPE::GetEnd(), FP_SHAPE::GetEnd0(), EDA_TEXT::GetHorizJustify(), BOARD_CONNECTED_ITEM::GetNetCode(), PAD::GetOffset(), FOOTPRINT::GetOrientation(), PAD::GetOrientation(), FP_TEXT::GetPos0(), PAD::GetPos0(), FP_TEXT::GetPosition(), PAD::GetPosition(), FOOTPRINT::GetPosition(), PCB_SHAPE::GetRadius(), PCB_SHAPE::GetShape(), PAD::GetShape(), PAD::GetSize(), PCB_SHAPE::GetStart(), FP_SHAPE::GetStart0(), EDA_TEXT::GetText(), EDA_TEXT::GetTextAngle(), EDA_TEXT::GetTextHeight(), EDA_TEXT::GetTextWidth(), FP_TEXT::GetType(), EDA_TEXT::GetVertJustify(), PCB_SHAPE::GetWidth(), FOOTPRINT::GraphicalItems(), hash_board_item(), hash_combine(), hash_fp_item(), HASH_NET, HASH_POS, HASH_REF, HASH_ROT, HASH_VALUE, EDA_TEXT::IsBold(), PCB_SHAPE::IsFilled(), EDA_TEXT::IsItalic(), EDA_TEXT::IsMirrored(), FOOTPRINT::Pads(), PCB_FOOTPRINT_T, PCB_FP_SHAPE_T, PCB_FP_TEXT_T, PCB_PAD_T, REL_COORD, FP_TEXT::TEXT_is_REFERENCE, FP_TEXT::TEXT_is_VALUE, and EDA_ITEM::Type().

Referenced by hash_fp_item(), and hashFootprint().

◆ hash_val()

template<typename... Types>
static std::size_t hash_val ( const Types &...  args)
inlinestatic

Definition at line 84 of file hash_eda.h.

85 {
86  std::size_t seed = 0xa82de1c0;
87  hash_combine( seed, args... );
88  return seed;
89 }
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash_eda.h:67

References hash_combine().