Leptonica  1.54
Файл src/dnabasic.c
#include <string.h>
#include <math.h>
#include "allheaders.h"

Функции

static l_int32 l_dnaExtendArray (L_DNA *da)
static l_int32 l_dnaaExtendArray (L_DNAA *daa)
L_DNAl_dnaCreate (l_int32 n)
L_DNAl_dnaCreateFromIArray (l_int32 *iarray, l_int32 size)
L_DNAl_dnaCreateFromDArray (l_float64 *darray, l_int32 size, l_int32 copyflag)
L_DNAl_dnaMakeSequence (l_float64 startval, l_float64 increment, l_int32 size)
void l_dnaDestroy (L_DNA **pda)
L_DNAl_dnaCopy (L_DNA *da)
L_DNAl_dnaClone (L_DNA *da)
l_int32 l_dnaEmpty (L_DNA *da)
l_int32 l_dnaAddNumber (L_DNA *da, l_float64 val)
l_int32 l_dnaInsertNumber (L_DNA *da, l_int32 index, l_float64 val)
l_int32 l_dnaRemoveNumber (L_DNA *da, l_int32 index)
l_int32 l_dnaReplaceNumber (L_DNA *da, l_int32 index, l_float64 val)
l_int32 l_dnaGetCount (L_DNA *da)
l_int32 l_dnaSetCount (L_DNA *da, l_int32 newcount)
l_int32 l_dnaGetDValue (L_DNA *da, l_int32 index, l_float64 *pval)
l_int32 l_dnaGetIValue (L_DNA *da, l_int32 index, l_int32 *pival)
l_int32 l_dnaSetValue (L_DNA *da, l_int32 index, l_float64 val)
l_int32 l_dnaShiftValue (L_DNA *da, l_int32 index, l_float64 diff)
l_int32l_dnaGetIArray (L_DNA *da)
l_float64l_dnaGetDArray (L_DNA *da, l_int32 copyflag)
l_int32 l_dnaGetRefcount (L_DNA *da)
l_int32 l_dnaChangeRefcount (L_DNA *da, l_int32 delta)
l_int32 l_dnaGetParameters (L_DNA *da, l_float64 *pstartx, l_float64 *pdelx)
l_int32 l_dnaSetParameters (L_DNA *da, l_float64 startx, l_float64 delx)
l_int32 l_dnaCopyParameters (L_DNA *dad, L_DNA *das)
L_DNAl_dnaRead (const char *filename)
L_DNAl_dnaReadStream (FILE *fp)
l_int32 l_dnaWrite (const char *filename, L_DNA *da)
l_int32 l_dnaWriteStream (FILE *fp, L_DNA *da)
L_DNAAl_dnaaCreate (l_int32 n)
L_DNAAl_dnaaCreateFull (l_int32 nptr, l_int32 n)
l_int32 l_dnaaTruncate (L_DNAA *daa)
void l_dnaaDestroy (L_DNAA **pdaa)
l_int32 l_dnaaAddDna (L_DNAA *daa, L_DNA *da, l_int32 copyflag)
l_int32 l_dnaaGetCount (L_DNAA *daa)
l_int32 l_dnaaGetDnaCount (L_DNAA *daa, l_int32 index)
l_int32 l_dnaaGetNumberCount (L_DNAA *daa)
L_DNAl_dnaaGetDna (L_DNAA *daa, l_int32 index, l_int32 accessflag)
l_int32 l_dnaaReplaceDna (L_DNAA *daa, l_int32 index, L_DNA *da)
l_int32 l_dnaaGetValue (L_DNAA *daa, l_int32 i, l_int32 j, l_float64 *pval)
l_int32 l_dnaaAddNumber (L_DNAA *daa, l_int32 index, l_float64 val)
L_DNAAl_dnaaRead (const char *filename)
L_DNAAl_dnaaReadStream (FILE *fp)
l_int32 l_dnaaWrite (const char *filename, L_DNAA *daa)
l_int32 l_dnaaWriteStream (FILE *fp, L_DNAA *daa)
L_DNAHASHl_dnaHashCreate (l_int32 nbuckets, l_int32 initsize)
void l_dnaHashDestroy (L_DNAHASH **pdahash)
l_int32 l_dnaHashGetCount (L_DNAHASH *dahash)
l_int32 l_dnaHashGetTotalCount (L_DNAHASH *dahash)
L_DNAl_dnaHashGetDna (L_DNAHASH *dahash, l_uint64 key, l_int32 copyflag)
l_int32 l_dnaHashAdd (L_DNAHASH *dahash, l_uint64 key, l_float64 value)
L_DNAHASHl_dnaHashCreateFromDna (L_DNA *da)
l_int32 l_dnaRemoveDupsByHash (L_DNA *das, L_DNA **pdad, L_DNAHASH **pdahash)
l_int32 l_dnaMakeHistoByHash (L_DNA *das, L_DNAHASH **pdahash, L_DNA **pdav, L_DNA **pdac)
L_DNAl_dnaIntersectionByHash (L_DNA *da1, L_DNA *da2)
l_int32 l_dnaFindValByHash (L_DNA *da, L_DNAHASH *dahash, l_float64 val, l_int32 *pindex)
L_DNAl_dnaMakeDelta (L_DNA *das)
NUMAl_dnaConvertToNuma (L_DNA *da)
L_DNAnumaConvertToDna (NUMA *na)
l_int32 l_dnaJoin (L_DNA *dad, L_DNA *das, l_int32 istart, l_int32 iend)

Переменные

static const l_int32 INITIAL_PTR_ARRAYSIZE = 50

Функции

l_int32 l_dnaaAddDna ( L_DNAA daa,
L_DNA da,
l_int32  copyflag 
)

l_dnaaAddDna()

Input: daa da (to be added) copyflag (L_INSERT, L_COPY, L_CLONE) Return: 0 if OK, 1 on error

l_int32 l_dnaaAddNumber ( L_DNAA daa,
l_int32  index,
l_float64  val 
)

l_dnaaAddNumber()

Input: daa index (of l_dna within l_dnaa) val (number to be added; stored as a double) Return: 0 if OK, 1 on error

Notes: (1) Adds to an existing l_dna only.

l_dnaaCreate()

Input: size of l_dna ptr array to be alloc'd (0 for default) Return: daa, or null on error

L_DNAA* l_dnaaCreateFull ( l_int32  nptr,
l_int32  n 
)

l_dnaaCreateFull()

Input: nptr: size of dna ptr array to be alloc'd n: size of individual dna arrays to be alloc'd (0 for default) Return: daa, or null on error

Notes: (1) This allocates a dnaa and fills the array with allocated dnas. In use, after calling this function, use l_dnaaAddNumber(dnaa, index, val); to add val to the index-th dna in dnaa.

l_int32 l_dnaAddNumber ( L_DNA da,
l_float64  val 
)

l_dnaAddNumber()

Input: da val (float or int to be added; stored as a float) Return: 0 if OK, 1 on error

void l_dnaaDestroy ( L_DNAA **  pdaa)

l_dnaaDestroy()

Input: &dnaa <to be="" nulled="" if="" it="" exists>=""> Return: void

static l_int32 l_dnaaExtendArray ( L_DNAA daa) [static]

l_dnaaExtendArray()

Input: daa Return: 0 if OK, 1 on error

l_dnaaGetCount()

Input: daa Return: count (number of l_dna), or 0 if no l_dna or on error

L_DNA* l_dnaaGetDna ( L_DNAA daa,
l_int32  index,
l_int32  accessflag 
)

l_dnaaGetDna()

Input: daa index (to the index-th l_dna) accessflag (L_COPY or L_CLONE) Return: l_dna, or null on error

l_int32 l_dnaaGetDnaCount ( L_DNAA daa,
l_int32  index 
)

l_dnaaGetDnaCount()

Input: daa index (of l_dna in daa) Return: count of numbers in the referenced l_dna, or 0 on error.

l_dnaaGetNumberCount()

Input: daa Return: count (total number of numbers in the l_dnaa), or 0 if no numbers or on error

l_int32 l_dnaaGetValue ( L_DNAA daa,
l_int32  i,
l_int32  j,
l_float64 pval 
)

l_dnaaGetValue()

Input: daa i (index of l_dna within l_dnaa) j (index into l_dna) val (<return> double value) Return: 0 if OK, 1 on error

L_DNAA* l_dnaaRead ( const char *  filename)

l_dnaaRead()

Input: filename Return: daa, or null on error

L_DNAA* l_dnaaReadStream ( FILE *  fp)

l_dnaaReadStream()

Input: stream Return: daa, or null on error

l_int32 l_dnaaReplaceDna ( L_DNAA daa,
l_int32  index,
L_DNA da 
)

l_dnaaReplaceDna()

Input: daa index (to the index-th l_dna) l_dna (insert and replace any existing one) Return: 0 if OK, 1 on error

Notes: (1) Any existing l_dna is destroyed, and the input one is inserted in its place. (2) If the index is invalid, return 1 (error)

l_dnaaTruncate()

Input: daa Return: 0 if OK, 1 on error

Notes: (1) This identifies the largest index containing a dna that has any numbers within it, destroys all dna beyond that index, and resets the count.

l_int32 l_dnaaWrite ( const char *  filename,
L_DNAA daa 
)

l_dnaaWrite()

Input: filename, daa Return: 0 if OK, 1 on error

l_int32 l_dnaaWriteStream ( FILE *  fp,
L_DNAA daa 
)

l_dnaaWriteStream()

Input: stream, daa Return: 0 if OK, 1 on error

l_int32 l_dnaChangeRefcount ( L_DNA da,
l_int32  delta 
)

l_dnaChangeRefCount()

Input: da delta (change to be applied) Return: 0 if OK, 1 on error

L_DNA* l_dnaClone ( L_DNA da)

l_dnaClone()

Input: da Return: ptr to same l_dna, or null on error

l_dnaConvertToNuma()

Input: da Return: na, or null on error

L_DNA* l_dnaCopy ( L_DNA da)

l_dnaCopy()

Input: da Return: copy of l_dna, or null on error

Notes: (1) This removes unused ptrs above da->n.

l_int32 l_dnaCopyParameters ( L_DNA dad,
L_DNA das 
)

l_dnaCopyParameters()

Input: dad (destination DNuma) das (source DNuma) Return: 0 if OK, 1 on error

l_dnaCreate()

Input: size of number array to be alloc'd (0 for default) Return: da, or null on error

L_DNA* l_dnaCreateFromDArray ( l_float64 darray,
l_int32  size,
l_int32  copyflag 
)

l_dnaCreateFromDArray()

Input: da (float) size (of the array) copyflag (L_INSERT or L_COPY) Return: da, or null on error

Notes: (1) With L_INSERT, ownership of the input array is transferred to the returned l_dna, and all elements are considered to be valid.

L_DNA* l_dnaCreateFromIArray ( l_int32 iarray,
l_int32  size 
)

l_dnaCreateFromIArray()

Input: iarray (integer) size (of the array) Return: da, or null on error

Notes: (1) We can't insert this int array into the l_dna, because a l_dna takes a double array. So this just copies the data from the input array into the l_dna. The input array continues to be owned by the caller.

void l_dnaDestroy ( L_DNA **  pda)

l_dnaDestroy()

Input: &da (<to be="" nulled="" if="" it="" exists>="">) Return: void

Notes: (1) Decrements the ref count and, if 0, destroys the l_dna. (2) Always nulls the input ptr.

l_dnaEmpty()

Input: da Return: 0 if OK; 1 on error

Notes: (1) This does not change the allocation of the array. It just clears the number of stored numbers, so that the array appears to be empty.

static l_int32 l_dnaExtendArray ( L_DNA da) [static]

l_dnaExtendArray()

Input: da Return: 0 if OK, 1 on error

l_int32 l_dnaFindValByHash ( L_DNA da,
L_DNAHASH dahash,
l_float64  val,
l_int32 pindex 
)

l_dnaFindValByHash()

Input: da dahash (containing indices into ) val (searching for this number in ) &index (<return> index into da if found; -1 otherwise) Return: 0 if OK; 1 on error

Notes: (1) Algo: hash into a key; hash the key to get the dna in (that holds indices into ); traverse the dna of indices looking for in .

l_dnaGetCount()

Input: da Return: count, or 0 if no numbers or on error

l_float64* l_dnaGetDArray ( L_DNA da,
l_int32  copyflag 
)

l_dnaGetDArray()

Input: da copyflag (L_NOCOPY or L_COPY) Return: either the bare internal array or a copy of it, or null on error

Notes: (1) If copyflag == L_COPY, it makes a copy which the caller is responsible for freeing. Otherwise, it operates directly on the bare array of the l_dna. (2) Very important: for L_NOCOPY, any writes to the array will be in the l_dna. Do not write beyond the size of the count field, because it will not be accessible from the l_dna! If necessary, be sure to set the count field to a larger number (such as the alloc size) BEFORE calling this function. Creating with l_dnaMakeConstant() is another way to insure full initialization.

l_int32 l_dnaGetDValue ( L_DNA da,
l_int32  index,
l_float64 pval 
)

l_dnaGetDValue()

Input: da index (into l_dna) &val (<return> double value; 0.0 on error) Return: 0 if OK; 1 on error

Notes: (1) Caller may need to check the function return value to decide if a 0.0 in the returned ival is valid.

l_dnaGetIArray()

Input: da Return: a copy of the bare internal array, integerized by rounding, or null on error Notes: (1) A copy of the array is made, because we need to generate an integer array from the bare double array. The caller is responsible for freeing the array. (2) The array size is determined by the number of stored numbers, not by the size of the allocated array in the l_dna. (3) This function is provided to simplify calculations using the bare internal array, rather than continually calling accessors on the l_dna. It is typically used on an array of size 256.

l_int32 l_dnaGetIValue ( L_DNA da,
l_int32  index,
l_int32 pival 
)

l_dnaGetIValue()

Input: da index (into l_dna) &ival (<return> integer value; 0 on error) Return: 0 if OK; 1 on error

Notes: (1) Caller may need to check the function return value to decide if a 0 in the returned ival is valid.

l_int32 l_dnaGetParameters ( L_DNA da,
l_float64 pstartx,
l_float64 pdelx 
)

l_dnaGetParameters()

Input: da &startx (<optional return>=""> startx) &delx (<optional return>=""> delx) Return: 0 if OK, 1 on error

l_dnaGetRefCount()

Input: da Return: refcount, or UNDEF on error

l_int32 l_dnaHashAdd ( L_DNAHASH dahash,
l_uint64  key,
l_float64  value 
)

l_dnaHashAdd()

Input: dahash key (key to be hashed into a bucket number) value (float value to be appended to the specific dna) Return: 0 if OK; 1 on error

L_DNAHASH* l_dnaHashCreate ( l_int32  nbuckets,
l_int32  initsize 
)

l_dnaHashCreate()

Input: nbuckets (the number of buckets in the hash table, which should be prime.) initsize (initial size of each allocated numa; 0 for default) Return: ptr to new dnahash, or null on error

Note: actual dna are created only as required by l_dnaHashAdd()

l_dnaHashCreateFromDna()

Input: da Return: dahash if OK; 1 on error

Notes: (1) The values stored in the are indices into ; has no use without .

void l_dnaHashDestroy ( L_DNAHASH **  pdahash)

l_dnaHashDestroy()

Input: &dahash (<to be nulled, if it exists>) Return: void

l_dnaHashGetCount()

Input: dahash Return: nbuckets (allocated, or 0 on error)

L_DNA* l_dnaHashGetDna ( L_DNAHASH dahash,
l_uint64  key,
l_int32  copyflag 
)

l_dnaHashGetDna()

Input: dahash key (key to be hashed into a bucket number) copyflag (L_NOCOPY, L_COPY, L_CLONE) Return: ptr to numa

l_dnaHashGetTotalCount()

Input: dahash Return: n (number of numbers in all dna, or 0 on error)

l_int32 l_dnaInsertNumber ( L_DNA da,
l_int32  index,
l_float64  val 
)

l_dnaInsertNumber()

Input: da index (location in da to insert new value) val (float64 or integer to be added) Return: 0 if OK, 1 on error

Notes: (1) This shifts da[i] --> da[i + 1] for all i >= index, and then inserts val as da[index]. (2) It should not be used repeatedly on large arrays, because the function is O(n).

L_DNA* l_dnaIntersectionByHash ( L_DNA da1,
L_DNA da2 
)

l_dnaIntersectionByHash()

Input: da1, da2 Return: dad (intersection of the number arrays), or null on error

Notes: (1) This uses the same method for building the intersection set as ptaIntersectionByHash() and sarrayIntersectionByHash().

l_int32 l_dnaJoin ( L_DNA dad,
L_DNA das,
l_int32  istart,
l_int32  iend 
)

l_dnaJoin()

Input: dad (dest dna; add to this one) das (<optional> source dna; add from this one) istart (starting index in das) iend (ending index in das; use -1 to cat all) Return: 0 if OK, 1 on error

Notes: (1) istart < 0 is taken to mean 'read from the start' (istart = 0) (2) iend < 0 means 'read to the end' (3) if das == NULL, this is a no-op

L_DNA* l_dnaMakeDelta ( L_DNA das)

l_dnaMakeDelta()

Input: das (input l_dna) Return: dad (of difference values val[i+1] - val[i]), or null on error

l_int32 l_dnaMakeHistoByHash ( L_DNA das,
L_DNAHASH **  pdahash,
L_DNA **  pdav,
L_DNA **  pdac 
)

l_dnaMakeHistoByHash()

Input: das &dahash (<return> hash map: val --> index) &dav (<return> array of values: index --> val) &dac (<return> histo array of counts: index --> count) Return: 0 if OK; 1 on error

Notes: (1) Generates and returns a dna of occurrences (histogram), an aligned dna of values, and an associated hashmap. The hashmap takes and a value, and points into the histogram in . (2) The dna of values, , is aligned with the histogram , and is needed for fast lookup. It is a hash set, because the values are unique. (3) Lookup is simple: l_dnaFindValByHash(dav, dahash, val, &index); if (index >= 0) l_dnaGetIValue(dac, index, &icount); else icount = 0;

L_DNA* l_dnaMakeSequence ( l_float64  startval,
l_float64  increment,
l_int32  size 
)

l_dnaMakeSequence()

Input: startval increment size (of sequence) Return: l_dna of sequence of evenly spaced values, or null on error

L_DNA* l_dnaRead ( const char *  filename)

l_dnaRead()

Input: filename Return: da, or null on error

L_DNA* l_dnaReadStream ( FILE *  fp)

l_dnaReadStream()

Input: stream Return: da, or null on error

Notes: (1) fscanf takes lf to read a double; fprintf takes f to write it.

l_int32 l_dnaRemoveDupsByHash ( L_DNA das,
L_DNA **  pdad,
L_DNAHASH **  pdahash 
)

l_dnaRemoveDupsByHash()

Input: das &dad (<return> hash set) &dahash (<optional return>=""> dnahash used for lookup) Return: 0 if OK; 1 on error

Notes: (1) Generates a dna with unique values. (2) The dnahash is built up with dad to assure uniqueness. It can be used to find if an element is in the set: l_dnaFindValByHash(dad, dahash, val, &index)

l_int32 l_dnaRemoveNumber ( L_DNA da,
l_int32  index 
)

l_dnaRemoveNumber()

Input: da index (element to be removed) Return: 0 if OK, 1 on error

Notes: (1) This shifts da[i] --> da[i - 1] for all i > index. (2) It should not be used repeatedly on large arrays, because the function is O(n).

l_int32 l_dnaReplaceNumber ( L_DNA da,
l_int32  index,
l_float64  val 
)

l_dnaReplaceNumber()

Input: da index (element to be replaced) val (new value to replace old one) Return: 0 if OK, 1 on error

l_int32 l_dnaSetCount ( L_DNA da,
l_int32  newcount 
)

l_dnaSetCount()

Input: da newcount Return: 0 if OK, 1 on error

Notes: (1) If newcount <= da->nalloc, this resets da->n. Using newcount = 0 is equivalent to l_dnaEmpty(). (2) If newcount > da->nalloc, this causes a realloc to a size da->nalloc = newcount. (3) All the previously unused values in da are set to 0.0.

l_int32 l_dnaSetParameters ( L_DNA da,
l_float64  startx,
l_float64  delx 
)

l_dnaSetParameters()

Input: da startx (x value corresponding to da[0]) delx (difference in x values for the situation where the elements of da correspond to the evaulation of a function at equal intervals of size ) Return: 0 if OK, 1 on error

l_int32 l_dnaSetValue ( L_DNA da,
l_int32  index,
l_float64  val 
)

l_dnaSetValue()

Input: da index (to element to be set) val (to set element) Return: 0 if OK; 1 on error

l_int32 l_dnaShiftValue ( L_DNA da,
l_int32  index,
l_float64  diff 
)

l_dnaShiftValue()

Input: da index (to element to change relative to the current value) diff (increment if diff > 0 or decrement if diff < 0) Return: 0 if OK; 1 on error

l_int32 l_dnaWrite ( const char *  filename,
L_DNA da 
)

l_dnaWrite()

Input: filename, da Return: 0 if OK, 1 on error

l_int32 l_dnaWriteStream ( FILE *  fp,
L_DNA da 
)

l_dnaWriteStream()

Input: stream, da Return: 0 if OK, 1 on error

numaConvertToDna

Input: na Return: da, or null on error


Переменные

const l_int32 INITIAL_PTR_ARRAYSIZE = 50 [static]