mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-07 22:40:53 +03:00
Update put and ensureCapacity routines
This commit is contained in:
@@ -18,7 +18,10 @@
|
|||||||
#define _NO_CRT_STDIO_INLINE
|
#define _NO_CRT_STDIO_INLINE
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <limits.h>
|
||||||
#include "HWNDMap.h"
|
#include "HWNDMap.h"
|
||||||
|
#include "AllocRoutines.h"
|
||||||
|
|
||||||
#define DEFAULT_CAPACITY 20
|
#define DEFAULT_CAPACITY 20
|
||||||
#define INCREASE_CAPACITY 10
|
#define INCREASE_CAPACITY 10
|
||||||
@@ -58,7 +61,7 @@ LPVOID HWNDMap::get( HWND key ) {
|
|||||||
return (index >= 0) ? table[index].value : NULL;
|
return (index >= 0) ? table[index].value : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HWNDMap::put( HWND key, LPVOID value ) {
|
bool HWNDMap::put( HWND key, LPVOID value ) {
|
||||||
LOCK lock( &criticalSection );
|
LOCK lock( &criticalSection );
|
||||||
|
|
||||||
int index = binarySearch( key );
|
int index = binarySearch( key );
|
||||||
@@ -66,9 +69,11 @@ void HWNDMap::put( HWND key, LPVOID value ) {
|
|||||||
if( index >= 0 ) {
|
if( index >= 0 ) {
|
||||||
// key already in map --> replace
|
// key already in map --> replace
|
||||||
table[index].value = value;
|
table[index].value = value;
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// insert new key
|
// insert new key
|
||||||
ensureCapacity( size + 1 );
|
if(size == INT_MAX || !ensureCapacity( size + 1 ))
|
||||||
|
return false;
|
||||||
|
|
||||||
// make roor for new entry
|
// make roor for new entry
|
||||||
index = -(index + 1);
|
index = -(index + 1);
|
||||||
@@ -79,6 +84,7 @@ void HWNDMap::put( HWND key, LPVOID value ) {
|
|||||||
// insert entry
|
// insert entry
|
||||||
table[index].key = key;
|
table[index].key = key;
|
||||||
table[index].value = value;
|
table[index].value = value;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// dump( "put" );
|
// dump( "put" );
|
||||||
@@ -121,23 +127,38 @@ int HWNDMap::binarySearch( HWND key ) {
|
|||||||
return -(low + 1);
|
return -(low + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HWNDMap::ensureCapacity( int minCapacity ) {
|
static constexpr size_t MaxEntryArrayLength = (SIZE_MAX >> 1) / sizeof(Entry);
|
||||||
|
static_assert(MaxEntryArrayLength > 0, "MaxEntryArrayLength > 0 must be true");
|
||||||
|
|
||||||
|
static constexpr int MaxEntryArrayIntCapacity =
|
||||||
|
(MaxEntryArrayLength <= INT_MAX) ? static_cast<int>(MaxEntryArrayLength) : INT_MAX;
|
||||||
|
static_assert(MaxEntryArrayIntCapacity > 0, "MaxEntryArrayIntCapacity > 0 must be true");
|
||||||
|
|
||||||
|
bool HWNDMap::ensureCapacity( int minCapacity ) {
|
||||||
if(minCapacity <= capacity)
|
if(minCapacity <= capacity)
|
||||||
return;
|
return true;
|
||||||
|
if(minCapacity > MaxEntryArrayIntCapacity)
|
||||||
|
return false;
|
||||||
|
|
||||||
// allocate new table
|
// allocate new table
|
||||||
int newCapacity = minCapacity + INCREASE_CAPACITY;
|
unsigned newCapacity = static_cast<unsigned>(minCapacity) + INCREASE_CAPACITY;
|
||||||
Entry* newTable = new Entry[newCapacity];
|
if(newCapacity > MaxEntryArrayIntCapacity)
|
||||||
|
newCapacity = MaxEntryArrayIntCapacity;
|
||||||
|
|
||||||
|
Entry* newTable = new (FlatLafNoThrow) Entry[newCapacity];
|
||||||
|
if(newTable == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
// copy old table to new table
|
// copy old table to new table
|
||||||
for( int i = 0; i < capacity; i++ )
|
for( int i = 0; i < capacity; i++ )
|
||||||
newTable[i] = table[i];
|
newTable[i] = table[i];
|
||||||
|
|
||||||
// delete old table
|
// delete old table
|
||||||
delete table;
|
FlatLafWin32ProcessHeapFree(table);
|
||||||
|
|
||||||
table = newTable;
|
table = newTable;
|
||||||
capacity = newCapacity;
|
capacity = static_cast<int>(newCapacity);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user