Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sparse matrices - vfp8
Message
From
04/10/2011 03:07:41
 
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01525329
Message ID:
01525514
Views:
41
Yes, there are a number of possibilties

You have to find the balance between ease of use and performance

Personally, I would prefer functions a la SetItem(value, x, y, z, ..) and getItem(@value, x, y, z, ...) to avoid the access and assign methods
I suppose the access and assign won't allow more than 2 dimensions
For a couple of dimensions ( say up to 8 or so), SetItem() and GetItem() may be the fastest
If we start dreaming and go beyond 24 dimensions - we'll need an array - SetItem(value, @dimensions) with the overhead of setting the elements of dimensions before each call

Using a cursor - I am in favour of one compound index since I think a locate will be slower. But locate has the advantage you do not need to build the index expression

As to the number of dimensions I cannot imagine when/where we would need 200


All in all - I would end up using the SafeArray api
- need to build the index array - but is just string concatenation
- no need for cursors
- start and end index possible per dimension - you can have zero based arrays

- it gets a bit more challenging when you want to store strings
_

>You can fake a direct access and then implement a cursor like Al suggested, I use a PK so I can convert the subscripts (any number of dimensions up to 253) to a record (I might want to create an index on the value to have the "array" sorted) , so you end up with something like this
>
>(very crude, just spent 15 minutes on it as a proof of concept :))
>
>
>clear
>loMyClass=Createobject('myArray')
>loMyClass.setDimensions(4, 'C(16)') && 4 dimensions and C(16) will be the data type for the value
>
>loMyClass.Data['1,1,1,1'] = '[001,001,001,001]'
>loMyClass.Data['4,2,1,3'] = '[004,002,001,003]'
>
>? loMyClass.Data['1,1,1,1']
>? loMyClass.Data['4,2,1,3']
>
>loMyClass.Data['4,2,1,3'] = '[EMPTY]'
>? loMyClass.Data['4,2,1,3']
>
>
>define class myArray as session
>
>	DIMENSION Data(1)
>
>	procedure Data_assign(vNewVal, m.nIndex1)
>		LOCAL lnItem
>		lnItem			= this.DimensionsToItem(m.nIndex1)
>		IF INDEXSEEK(PK, .t., 'cData', 'PK')
>			replace Value WITH m.vNewVal IN cData
>		ELSE
>			this.newRow(m.nIndex1, m.vNewVal)
>		ENDIF		
>	endfunc
>
>	procedure Data_Access(m.nIndex1)
>		LOCAL lnItem
>		lnItem			= this.DimensionsToItem(m.nIndex1)
>		RETURN IIF(INDEXSEEK(PK, .t., 'cData', 'PK'), cData.Value, null)
>	endfunc
>
>	PROCEDURE newRow(tcDimensions as String, txValue as Variant) as VOID
>	
>		LOCAL laDimensions(1), lnDimensions, lnDimension
>		
>		lnDimensions		= ALINES(laDimensions, tcDimensions, 1, ',')
>		lcCommand			= 'INSERT INTO cData ('
>		FOR lnDimension = 1 TO lnDimensions
>			lcCommand			= lcCommand + IIF(lnDimension = 1, '', ', ') + 'C' + TRANSFORM(lnDimension, '@L 999')
>		NEXT lnDimension
>		lcCommand			= lcCommand +  + ', value) values ('
>		FOR lnDimension = 1 TO lnDimensions
>			lcCommand			= lcCommand + IIF(lnDimension = 1, '', ', ') + TRANSFORM(laDimensions[lnDimension])
>		NEXT lnDimension
>		lcCommand			= lcCommand + ', ' + TRANSFORM(txValue) + ')'
>		EXECSCRIPT(lcCommand)
>		
>		RETURN null
>	ENDPROC
>	
>	PROCEDURE setDimensions(tnDimensions as Integer, tcValueType as String) as VOID
>		LOCAL lcCommand, lnDimension, lnDimension
>
>		lcCommand		= 'CREATE CURSOR cData (PK I AutoInc'
>		FOR lnDimension = 1 TO tnDimensions
>			lcCommand		= lcCommand + ', C' + TRANSFORM(lnDimension, '@L 999') + ' I'
>		NEXT lnDimension
>		lcCommand		= lcCommand + ', Value ' + tcValueType + ')'
>		EXECSCRIPT(lcCommand)
>		INDEX on PK TAG PK
>		* Index on every column to speed up access?
>		RETURN NULL
>	ENDFUNC
>
>	FUNCTION DimensionsToItem(tcDimensions as String) as Integer
>	
>		LOCAL laDimensions(1), lnDimensions, lnDimension
>		
>		lnDimensions		= ALINES(laDimensions, tcDimensions, 1, ',')
>		lcCommand			= 'LOCATE FOR '
>		FOR lnDimension = 1 TO lnDimensions
>			lcCommand			= lcCommand + IIF(lnDimension = 1, '', ' and ') + 'C' + TRANSFORM(lnDimension, '@L 999') + '=' + TRANSFORM(laDimensions[lnDimension])
>		NEXT lnDimension
>		EXECSCRIPT(lcCommand)
>		RETURN IIF(FOUND(), cData.PK, - 1)
>	ENDFUNC
>enddefine
>
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform