Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sparse matrices - vfp8
Message
 
To
02/10/2011 06:10:20
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:
01525482
Views:
54
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
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform