forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic pointer array and implement strList using it
A resizable array that can be also used as a stack is a useful data structure that is currently missing in ctags and it's re-implemented at various places unnecessarily (searching for 'realloc' in ctags sources reveals several places where pointer array could be used). The implementation just moves all the non-string-specific functions from strList into the new ptrArray and uses ptrArray internally. The only functional changes are: - instead of creating 0-sized array at the beginning, the new implementation creates an array of size 8 - the new implementation always doubles the size of the array on reallocation; previously the array was increased by 10 elements only - the simplest functions were rewritten as macros to avoid function call overhead
- Loading branch information
Showing
7 changed files
with
228 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright (c) 1999-2002, Darren Hiebert | ||
* | ||
* This source code is released for free distribution under the terms of the | ||
* GNU General Public License version 2 or (at your option) any later version. | ||
* | ||
* This module contains functions managing resizable pointer arrays. | ||
*/ | ||
|
||
/* | ||
* INCLUDE FILES | ||
*/ | ||
#include "general.h" /* must always come first */ | ||
|
||
#include <string.h> | ||
|
||
#include "debug.h" | ||
#include "ptrarray.h" | ||
|
||
/* | ||
* DATA DECLARATIONS | ||
*/ | ||
|
||
struct sPtrArray { | ||
unsigned int max; | ||
unsigned int count; | ||
void **array; | ||
ptrArrayDeleteFunc deleteFunc; | ||
}; | ||
|
||
/* | ||
* FUNCTION DEFINITIONS | ||
*/ | ||
|
||
extern ptrArray *ptrArrayNew (ptrArrayDeleteFunc deleteFunc) | ||
{ | ||
ptrArray* const result = xMalloc (1, ptrArray); | ||
result->max = 8; | ||
result->count = 0; | ||
result->array = xMalloc (result->max, void*); | ||
result->deleteFunc = deleteFunc; | ||
return result; | ||
} | ||
|
||
extern void ptrArrayAdd (ptrArray *const current, void *ptr) | ||
{ | ||
Assert (current != NULL); | ||
if (current->count == current->max) | ||
{ | ||
current->max *= 2; | ||
current->array = xRealloc (current->array, current->max, void*); | ||
} | ||
current->array [current->count++] = ptr; | ||
} | ||
|
||
extern void ptrArrayRemoveLast (ptrArray *const current) | ||
{ | ||
Assert (current != NULL); | ||
Assert (current->count > 0); | ||
--current->count; | ||
} | ||
|
||
/* Combine array `from' into `current', deleting `from' */ | ||
extern void ptrArrayCombine (ptrArray *const current, ptrArray *const from) | ||
{ | ||
unsigned int i; | ||
Assert (current != NULL); | ||
Assert (from != NULL); | ||
for (i = 0 ; i < from->count ; ++i) | ||
ptrArrayAdd (current, from->array [i]); | ||
from->count = 0; | ||
ptrArrayDelete (from); | ||
} | ||
|
||
extern unsigned int ptrArrayCount (const ptrArray *const current) | ||
{ | ||
Assert (current != NULL); | ||
return current->count; | ||
} | ||
|
||
extern void* ptrArrayItem (const ptrArray *const current, const unsigned int indx) | ||
{ | ||
Assert (current != NULL); | ||
return current->array [indx]; | ||
} | ||
|
||
extern void* ptrArrayLast (const ptrArray *const current) | ||
{ | ||
Assert (current != NULL); | ||
Assert (current->count > 0); | ||
return current->array [current->count - 1]; | ||
} | ||
|
||
extern void ptrArrayClear (ptrArray *const current) | ||
{ | ||
Assert (current != NULL); | ||
if (current->deleteFunc) | ||
{ | ||
unsigned int i; | ||
for (i = 0 ; i < current->count ; ++i) | ||
current->deleteFunc (current->array [i]); | ||
} | ||
current->count = 0; | ||
} | ||
|
||
extern void ptrArrayDelete (ptrArray *const current) | ||
{ | ||
if (current != NULL) | ||
{ | ||
ptrArrayClear (current); | ||
eFree (current->array); | ||
eFree (current); | ||
} | ||
} | ||
|
||
extern bool ptrArrayHasTest (const ptrArray *const current, | ||
bool (*test)(const void *ptr, void *userData), | ||
void *userData) | ||
{ | ||
bool result = false; | ||
unsigned int i; | ||
Assert (current != NULL); | ||
for (i = 0 ; ! result && i < current->count ; ++i) | ||
result = (*test)(current->array [i], userData); | ||
return result; | ||
} | ||
|
||
extern void ptrArrayReverse (const ptrArray *const current) | ||
{ | ||
unsigned int i, j; | ||
void *tmp; | ||
|
||
Assert (current != NULL); | ||
for (i = 0, j = current->count - 1 ; i < (current->count / 2); ++i, --j) | ||
{ | ||
tmp = current->array[i]; | ||
current->array[i] = current->array[j]; | ||
current->array[j] = tmp; | ||
} | ||
} | ||
|
||
extern void ptrArrayDeleteItem (ptrArray* const current, unsigned int indx) | ||
{ | ||
void *ptr = current->array[indx]; | ||
|
||
if (current->deleteFunc) | ||
current->deleteFunc (ptr); | ||
|
||
memmove (current->array + indx, current->array + indx + 1, | ||
(current->count - indx) * sizeof (*current->array)); | ||
--current->count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 1999-2002, Darren Hiebert | ||
* | ||
* This source code is released for free distribution under the terms of the | ||
* GNU General Public License version 2 or (at your option) any later version. | ||
* | ||
* Defines external interface to resizable pointer arrays. | ||
*/ | ||
#ifndef CTAGS_MAIN_PTRARRAY_H | ||
#define CTAGS_MAIN_PTRARRAY_H | ||
|
||
/* | ||
* INCLUDE FILES | ||
*/ | ||
#include "general.h" /* must always come first */ | ||
|
||
/* | ||
* DATA DECLARATIONS | ||
*/ | ||
|
||
typedef void (*ptrArrayDeleteFunc) (void *data); | ||
|
||
struct sPtrArray; | ||
typedef struct sPtrArray ptrArray; | ||
|
||
/* | ||
* FUNCTION PROTOTYPES | ||
*/ | ||
|
||
extern ptrArray *ptrArrayNew (ptrArrayDeleteFunc deleteFunc); | ||
extern void ptrArrayAdd (ptrArray *const current, void *ptr); | ||
extern void ptrArrayRemoveLast (ptrArray *const current); | ||
extern void ptrArrayCombine (ptrArray *const current, ptrArray *const from); | ||
extern void ptrArrayClear (ptrArray *const current); | ||
extern unsigned int ptrArrayCount (const ptrArray *const current); | ||
extern void* ptrArrayItem (const ptrArray *const current, const unsigned int indx); | ||
extern void* ptrArrayLast (const ptrArray *const current); | ||
extern void ptrArrayDelete (ptrArray *const current); | ||
extern bool ptrArrayHasInsensitive (const ptrArray *const current, const void *const ptr); | ||
extern bool ptrArrayHasTest (const ptrArray *const current, | ||
bool (*test)(const void *ptr, void *userData), | ||
void *userData); | ||
extern void ptrArrayReverse (const ptrArray *const current); | ||
extern void ptrArrayDeleteItem (ptrArray* const current, unsigned int indx); | ||
|
||
#endif /* CTAGS_MAIN_PTRARRAY_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.