www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - using memset withing a pure function

reply "D_Learner" <johnsjdsd gmail.com> writes:
When writting a pure fucntion involving C non pure functions like 
  memcpy() and   memset() , what could be the way around? Should I 
re-write these and make them pure?


The code am converting is as below :-

int ag_cmatch(const string   pattern, const string text, 
int[char] bmBc ,  int[size] bmGs ) pure {   //Start of AG search 
algorithm		
		int i,j,k , s, shift;
		int[size] skip;
		int[size]  suff;
                 int m = to!int(pattern.length);
                 int n = to!int(text.length);

		int position = -1;

		/* Preprocessing   */

		memset(&skip[0], 0, m* int.sizeof); // Initialise skip table
		
   	 			
		/* searching   */
		j = 0;
		while(j<=n-m){
			i=m-1;
			while( i >= 0){  // Start inner while
				k = skip[i];
				s = suff[i];
				
				if( k > 0)
				if( k > s){
					//writeln(" Int Size 3 ", int.sizeof);
					if(i+1 == s)
						i = (-1);
					else {
						i -=s;
						//writeln("Innner while loop .....Found............... ");
						break;
					}
				}
				else{
					i-=k;
					if( k < s ){
						//	writeln("Innner while loop .....Found<<<<<<  1  
............... ");
//getchar(); break; } } else { if ( pattern[i] == text[i+j]){ --i; //writeln("Innner while loop .....Found<<<<<< 2 >>>>>> "); } else break; } }// End inner while if ( i< 0){ //writeln(" AG Pattern found at :", j); position = j;//Added for display / reporting //getchar(); // Manually added to cause a pattern found stop(Not in documentation) skip[m-1] = m; shift = bmGs[0]; break; } else{ skip[ m-1 ] = m -1 -i; shift = bmGs[i] > (bmBc[text[i+j]] - m + 1 + i) ? bmGs[i] : (bmBc[text[i+j]] - m + 1 + i ) ; } j+=shift; // writeln(" j Before memcpy== : ", j); memcpy(&skip[0], &skip[0]+shift, (m-shift)*(int.sizeof)); memset(&skip[0]+(m-shift),0, shift*(int.sizeof)); }//End outer while return position ; }//End AG search
Aug 14 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Saturday, 15 August 2015 at 01:09:15 UTC, D_Learner wrote:
 When writting a pure fucntion involving C non pure functions 
 like
  memcpy() and   memset()
Those functions are pure already, and marked so in the newest dmd (and I think older ones too, though I haven't confirmed. Your code compiled out of the box for me. BTW D also has some syntax sugar for them: arr[start .. end] = 0; // memset those bounds to 0 arr[start .. end] = arr2[start .. end]; // memcpy
Aug 14 2015
next sibling parent reply "D_Learner" <johnsjdsd gmail.com> writes:
On Saturday, 15 August 2015 at 01:13:02 UTC, Adam D. Ruppe wrote:
 On Saturday, 15 August 2015 at 01:09:15 UTC, D_Learner wrote:
 When writting a pure fucntion involving C non pure functions 
 like
  memcpy() and   memset()
Those functions are pure already, and marked so in the newest dmd (and I think older ones too, though I haven't confirmed. Your code compiled out of the box for me. BTW D also has some syntax sugar for them: arr[start .. end] = 0; // memset those bounds to 0 arr[start .. end] = arr2[start .. end]; // memcpy
Well, the actual error I got is : Error: memset cannot be interpreted at compile time, because it has no available source code . I seems to suggest I miss the actual code.
Aug 14 2015
next sibling parent "anonymous" <anonymous 6543.21> writes:
On Saturday, 15 August 2015 at 02:21:46 UTC, D_Learner wrote:
 On Saturday, 15 August 2015 at 01:13:02 UTC, Adam D. Ruppe 
 wrote:
 On Saturday, 15 August 2015 at 01:09:15 UTC, D_Learner wrote:
 When writting a pure fucntion involving C non pure functions 
 like
  memcpy() and   memset()
Those functions are pure already, and marked so in the newest dmd (and I think older ones too, though I haven't confirmed. Your code compiled out of the box for me. BTW D also has some syntax sugar for them: arr[start .. end] = 0; // memset those bounds to 0 arr[start .. end] = arr2[start .. end]; // memcpy
Well, the actual error I got is : Error: memset cannot be interpreted at compile time, because it has no available source code . I seems to suggest I miss the actual code.
this is another thing. Here you reach the CTFE limits.
Aug 14 2015
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Saturday, 15 August 2015 at 02:21:46 UTC, D_Learner wrote:
 Well, the actual error I got is :  Error: memset cannot be 
 interpreted at compile time, because it has no available source 
 code . I seems to suggest I miss the actual code.
yeah, ctfe is different than pure functions. That's where the compiler interprets the code inside the compiler. The slice syntax might work there though. Try changing them to that.
Aug 14 2015
prev sibling parent "anonymous" <anonymous example.com> writes:
On Saturday, 15 August 2015 at 02:21:46 UTC, D_Learner wrote:
 Well, the actual error I got is :  Error: memset cannot be 
 interpreted at compile time, because it has no available source 
 code . I seems to suggest I miss the actual code.
I guess I gave you a wrong impression of how pure relates to CTFE over here: <http://forum.dlang.org/post/hdzhkxqgcpsuovciflxj forum.dlang.org>. Sorry 'bout that. There, your function wasn't pure which made CTFE impossible. But being pure is not enough for a function to be CTFEable. The source code needs to be available, too. And then there are things that are ok in pure functions but simply don't work in CTFE.
Aug 14 2015
prev sibling parent reply "D_Learner" <johnsjdsd gmail.com> writes:
On Saturday, 15 August 2015 at 01:13:02 UTC, Adam D. Ruppe wrote:
 On Saturday, 15 August 2015 at 01:09:15 UTC, D_Learner wrote:
 When writting a pure fucntion involving C non pure functions 
 like
  memcpy() and   memset()
Those functions are pure already, and marked so in the newest dmd (and I think older ones too, though I haven't confirmed. Your code compiled out of the box for me. BTW D also has some syntax sugar for them: arr[start .. end] = 0; // memset those bounds to 0 arr[start .. end] = arr2[start .. end]; // memcpy
You could be surprised am still trying to get my head around this. Considering the code :- memcpy(&skip[0], &skip[0]+shift, (m-shift)*(int.sizeof)); memset(&skip[0]+(m-shift),0, shift*(int.sizeof)) I was thinking conversion would be :- skip[0 .. size-1] = skip[shift .. size-1 ]; //For the memcpy(); skip[0 .. size-1] = 0; //For memset() But this doesn't seem to work for me as dmd(v2.066.1) gives the "error slice [8..7] exceeds array bounds [0..8]" .Sure am missing something.
Aug 15 2015
parent reply "anonymous" <anonymous example.com> writes:
On Saturday, 15 August 2015 at 18:04:30 UTC, D_Learner wrote:
     memcpy(&skip[0], &skip[0]+shift, (m-shift)*(int.sizeof));
     memset(&skip[0]+(m-shift),0, shift*(int.sizeof))

 I was thinking conversion would be :-

     skip[0 .. size-1] = skip[shift .. size-1   ];  //For the 
 memcpy();
Those two slices have different lengths (when shift != 0). They must have equal lengths, and they must not overlap.
     skip[0 .. size-1] = 0;    //For memset()

 But this doesn't seem to work for me as dmd(v2.066.1) gives the 
 "error slice [8..7] exceeds array bounds [0..8]"  .Sure am 
 missing something.
You can just do mechanical translations. memcpy(dst, src, n * T.sizeof); becomes dst[0 .. n] = src[0 .. n]; Then simplify. Original: memcpy(&skip[0], &skip[0]+shift, (m-shift)*(int.sizeof)); Identify the pieces: dst = &skip[0] src = &skip[0]+shift n = m-shift Substitute: (&skip[0])[0 .. m-shift] = (&skip[0]+shift)[0 .. m-shift]; Simplify: skip[0 .. m-shift] = skip[shift .. $][0 .. m-shift]; skip[0 .. m-shift] = skip[shift .. shift + m-shift]; skip[0 .. m-shift] = skip[shift .. m]; I sure hope I didn't mess anyting up. memset is very similar. memset(dst, val, n * T.sizeof); becomes dst[0 .. n] = val; Then simplify.
Aug 15 2015
next sibling parent reply "Temtaime" <temtaime gmail.com> writes:
There's a problem with « dst[0 .. n] = val; ».
It should be « dst[0 .. n][] = val; »
Aug 15 2015
parent "anonymous" <anonymous example.com> writes:
On Saturday, 15 August 2015 at 19:50:56 UTC, Temtaime wrote:
 There's a problem with « dst[0 .. n] = val; ».
 It should be « dst[0 .. n][] = val; »
No, you don't need the `[]`.
Aug 15 2015
prev sibling parent "D_Learner" <johnsjdsd gmail.com> writes:
On Saturday, 15 August 2015 at 18:49:15 UTC, anonymous wrote:
 On Saturday, 15 August 2015 at 18:04:30 UTC, D_Learner wrote:
 [...]
Those two slices have different lengths (when shift != 0). They must have equal lengths, and they must not overlap. [...]
Am now sorted. Thanks, your workout simplifies everything
Aug 15 2015
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/14/2015 06:09 PM, D_Learner wrote:

 When writting a pure fucntion involving C non pure functions
If you want to live dangerously, you can use assumePure, which is found in one of the unittest blocks of std.traits: import std.traits; auto assumePure(T)(T t) if (isFunctionPointer!T || isDelegate!T) { enum attrs = functionAttributes!T | FunctionAttribute.pure_; return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; } int i = 0; void foo() { ++i; // foo accesses mutable module-level data } void bar() pure { auto pureFoo = assumePure(&foo); pureFoo(); // <-- pure function is calling impure function } void main() { assert(i == 0); bar(); assert(i == 1); // mutation through a pure function } Ali
Aug 14 2015